Skip to content

Commit

Permalink
Merge branch 'feat/no-empty-strings' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Oct 8, 2024
2 parents 1d45505 + 0b97b0e commit 712aa53
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions .github/workflows/no-empty-strings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Check for Empty Strings

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
check-empty-strings:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Get GitHub App token
uses: tibdex/[email protected]
id: get_installation_token
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.APP_PRIVATE_KEY }}

- name: Find empty strings
id: find-empty-strings
run: |
# Get the base branch (usually main or master)
BASE_BRANCH=$(gh pr view ${{ github.event.pull_request.number }} --json baseRefName --jq .baseRefName)
# Get the list of changed files in the PR
CHANGED_FILES=$(gh pr diff ${{ github.event.pull_request.number }} --name-only)
# Initialize empty string for results
EMPTY_STRINGS=""
# Loop through changed files
for file in $CHANGED_FILES; do
if [[ $file =~ \.(ts|tsx)$ ]]; then
# Get the diff for this file
DIFF=$(gh pr diff ${{ github.event.pull_request.number }} --color never -- $file)
# Find added lines with empty strings
ADDED_EMPTY=$(echo "$DIFF" | grep -n "^+" | grep -E "(''{2}|\"\"{2})" | sed "s/^+//g" | sed "s/^/${file}:/")
# Append results
if [ ! -z "$ADDED_EMPTY" ]; then
EMPTY_STRINGS+="$ADDED_EMPTY"$'\n'
fi
fi
done
# Output results
echo "EMPTY_STRINGS<<EOF" >> $GITHUB_OUTPUT
echo "$EMPTY_STRINGS" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ steps.get_installation_token.outputs.token }}

- name: Comment on PR
uses: actions/github-script@v6
if: steps.find-empty-strings.outputs.EMPTY_STRINGS
with:
github-token: ${{ steps.get_installation_token.outputs.token }}
script: |
const emptyStrings = process.env.EMPTY_STRINGS.trim().split('\n');
if (emptyStrings.length > 0) {
const body = `### Empty Strings Found\n\nPlease review the following lines containing empty strings:\n\n${emptyStrings.map(line => `- \`${line}\``).join('\n')}`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.name,
body: body
});
}

0 comments on commit 712aa53

Please sign in to comment.