Skip to content

Commit

Permalink
borrow from tt buda demos workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudTT committed Oct 21, 2024
1 parent fe08882 commit d7b3bff
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions .github/workflows/frontend-lint-license-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
token: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Node.js
uses: actions/setup-node@v3
Expand All @@ -34,25 +34,40 @@ jobs:
working-directory: app/frontend
run: npm install

# Run ESLint with auto-fix
- name: Run ESLint with Auto-fix
working-directory: app/frontend
run: npm run lint

- name: Check for Changes and Commit Fixes
working-directory: app/frontend
# Check if GPG key exists
- name: Check if GPG key exists
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "GitHub Actions Bot"
if [[ `git status --porcelain` ]]; then
echo "Changes detected after ESLint auto-fix. Committing changes."
git add .
git commit -m "chore: auto-fix ESLint issues"
git push origin ${{ github.ref_name }} # Push changes back to the branch
if [ -n "${{ secrets.GPG_PRIVATE_KEY }}" ]; then
echo "GPG_KEY_EXISTS=true" >> $GITHUB_ENV
else
echo "No changes detected."
echo "GPG_KEY_EXISTS=false" >> $GITHUB_ENV
fi
# Import GPG key if it exists
- name: Import GPG key if it exists
if: env.GPG_KEY_EXISTS == 'true'
uses: crazy-max/ghaction-import-gpg@v6
with:
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.GPG_PASSPHRASE }}
git_user_signingkey: true
git_commit_gpgsign: true

# Auto-commit changes using stefanzweifel/git-auto-commit-action
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_user_name: CleanBot
commit_user_email: your-email@example.com
commit_message: '*** AUTOMATED COMMIT | Applied Code Formatting and Cleanup ✨***'
commit_options: ${{ env.GPG_KEY_EXISTS == 'true' && '-S' || '' }}
branch: ${{ github.head_ref }}

# Run ESLint and Capture Output
- name: Run ESLint and Capture Output
working-directory: app/frontend
id: run_eslint
Expand All @@ -66,43 +81,37 @@ jobs:
echo "$clean_output" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
# Process output to group errors under file paths with separators
grouped_errors=""
current_file=""
errors_for_file=""
has_errors=false
missing_lc_headers=false # Flag for missing LC headers
lc_flagged_files="" # To store flagged files for license headers
missing_lc_headers=false
lc_flagged_files=""
while IFS= read -r line; do
if echo "$line" | grep -q '^\s*\/'; then
# Detected a file path line, add to grouped_errors if it contains errors
if [ "$has_errors" = true ]; then
grouped_errors+="$current_file\n$errors_for_file\n------------------------------------------------------------------------------------------------------------------------------------------\n"
errors_for_file="" # Reset error collection for next file
has_errors=false # Reset error flag for the next file
errors_for_file=""
has_errors=false
fi
current_file=$(echo "$line" | sed 's/\n//g') # Clean any \n characters from the file path
current_file=$(echo "$line" | sed 's/\n//g')
elif echo "$line" | grep -q 'error'; then
# Detected an error line, associate with the current file
errors_for_file+=" $line\n"
has_errors=true # Mark that this file has errors
has_errors=true
# Check if the error is related to missing LC headers
if echo "$line" | grep -q 'missing header'; then
missing_lc_headers=true
errors_for_file+="!Flagged: LC header missing\n"
lc_flagged_files+="${current_file}\n" # Add this file to LC header flagged list
lc_flagged_files+="${current_file}\n"
fi
fi
done <<< "$clean_output"
# Add any remaining errors after the loop
if [ "$has_errors" = true ]; then
grouped_errors+="$current_file\n$errors_for_file\n"
fi
# Store grouped errors in environment variable
if [ -n "$grouped_errors" ]; then
echo "Errors found."
echo "GROUPED_ERRORS<<EOF" >> $GITHUB_ENV
Expand All @@ -114,20 +123,19 @@ jobs:
echo "HAS_ERRORS=false" >> $GITHUB_ENV
fi
# Store flagged LC header files in environment variable
if [ "$missing_lc_headers" = true ]; then
echo "LC headers missing in one or more files."
echo "MISSING_LC_HEADERS=true" >> $GITHUB_ENV
# Trim excess \n and store cleaned file paths in env variable
echo "LC_FLAGGED_FILES<<EOF" >> $GITHUB_ENV
echo -e "$(echo -e "$lc_flagged_files" | sed '/^\s*$/d')" >> $GITHUB_ENV # Remove empty lines and ensure clean file paths
echo -e "$(echo -e "$lc_flagged_files" | sed '/^\s*$/d')" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
else
echo "MISSING_LC_HEADERS=false" >> $GITHUB_ENV
fi
exit 0 # Always exit with 0 to avoid failing the step
exit 0
# Comment on PR with ESLint Errors and LC Headers
- name: Comment on PR with ESLint Errors and LC Headers
if: env.HAS_ERRORS == 'true'
uses: actions/github-script@v7
Expand All @@ -138,22 +146,17 @@ jobs:
const owner = context.repo.owner;
const repo = context.repo.repo;
// Initialize the comment body
let commentBody = "";
// Check for missing license headers and prioritize them in the comment
if (process.env.MISSING_LC_HEADERS === 'true') {
const flaggedFiles = process.env.LC_FLAGGED_FILES.trim();
commentBody += `## 🚨 SPDX-License Header Errors\n\nThe following files are missing the required license headers:\n\n\`\`\`\n${flaggedFiles}\n\`\`\`\nPlease ensure each of these files includes a valid SPDX license identifier. This is essential for maintaining licensing compliance. Your prompt attention and cooperation in updating these files are greatly appreciated. Thank you!\n\n---\n`;
commentBody += `## 🚨 SPDX-License Header Errors\n\nThe following files are missing the required license headers:\n\n\`\`\`\n${flaggedFiles}\n\`\`\`\nPlease ensure each of these files includes a valid SPDX license identifier. This is essential for maintaining licensing compliance. Thank you!`;
}
// Add ESLint errors after license header issues
if (lintErrors) {
commentBody += `## Frontend Project: ESLint Errors\n\`\`\`\n${lintErrors}\n\`\`\`\nPlease review and resolve the ESLint errors.`;
}
// Post the comment if errors are present
if (commentBody.trim() && issueNumber) {
await github.rest.issues.createComment({
issue_number: issueNumber,
Expand All @@ -163,7 +166,6 @@ jobs:
});
} else {
console.log("No relevant errors to report.");
}
- name: Fail the Workflow if LC Headers Are Missing
if: env.MISSING_LC_HEADERS == 'true'
Expand Down

0 comments on commit d7b3bff

Please sign in to comment.