-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/no-empty-strings' into development
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
} |