From 683a2cfd19bb0a66f92d813e02e6ae73f5728c94 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Mon, 25 Mar 2024 16:14:46 +0000 Subject: [PATCH] Update empty-string-warning.yml --- .github/workflows/empty-string-warning.yml | 61 +++++++++++++++------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/.github/workflows/empty-string-warning.yml b/.github/workflows/empty-string-warning.yml index 11a7df4..94259f3 100644 --- a/.github/workflows/empty-string-warning.yml +++ b/.github/workflows/empty-string-warning.yml @@ -9,6 +9,7 @@ on: permissions: issues: write pull-requests: write + contents: write jobs: check-empty-strings: @@ -17,13 +18,27 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 + with: + fetch-depth: 2 + + - name: List files in the pull + id: list_files + run: | + files=$(git diff --name-only HEAD~ HEAD | grep -E "\.(ts|tsx)$") + files="${files//'%'/'%25'}" + files="${files//$'\n'/'%0A'}" + files="${files//$'\r'/'%0D'}" + echo "::set-output name=files::$files" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Find empty strings in TypeScript files + if: steps.list_files.outputs.files != '' id: find_empty_strings run: | - # Find empty strings and mark them explicitly - output=$(grep -Rn --include=\*.ts "''\|\"\"" --exclude-dir={node_modules,dist,out,tests} . || true) - + files="${{ steps.list_files.outputs.files }}" + output=$(echo "$files" | xargs grep -Hn --include=\*.{ts,tsx} "''\|\"\"" --exclude-dir={node_modules,dist,out} || true) + if [ -z "$output" ]; then echo "::set-output name=results::No empty strings found." exit 0 @@ -48,25 +63,33 @@ jobs: uses: actions/github-script@v7 with: script: | - const findings = `${{ steps.find_empty_strings.outputs.results }}`.split('\n'); - for (const finding of findings) { - // ./app/api/chat-with-functions/route.ts:10: apiKey: process.env.API_KEY || '', + const findingsRaw = `${{ steps.find_empty_strings.outputs.results }}`; + const findings = findingsRaw.split('\n'); + const body = "Empty string detected!"; + const owner = context.repo.owner; + const repo = context.repo.repo; + const commit_sha = context.payload.pull_request.head.sha; - const path = finding.split(':')[0].replace('./', ''); - const line = finding.split(':')[1].split(':')[0]; - - const body = "Empty string detected!"; - const prNumber = context.payload.pull_request.number; - const owner = context.repo.owner; - const repo = context.repo.repo; + for (const finding of findings) { + const parts = finding.split(':'); + const path = parts[0]; + const line = parseInt(parts[1], 10); - await github.rest.pulls.createReviewComment({ + const payload = { owner, repo, - pull_number: prNumber, - body, - commit_id: context.payload.pull_request.head.sha, + commit_sha, path, - line: parseInt(line, 10), - }); + body, + position: line + } + + try { + await github.rest.repos.createCommitComment(payload); + console.log(`Comment posted to ${path} at position ${line}`); + } catch (error) { + console.error(`Error posting comment to ${path}:${line}`, error); + } } + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}