Skip to content

Commit

Permalink
Update empty-string-warning.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng authored Mar 25, 2024
1 parent f74aac9 commit 683a2cf
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions .github/workflows/empty-string-warning.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
permissions:
issues: write
pull-requests: write
contents: write

jobs:
check-empty-strings:
Expand All @@ -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
Expand All @@ -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 }}

0 comments on commit 683a2cf

Please sign in to comment.