Skip to content

Anirud/lc GitHub actions #13

Anirud/lc GitHub actions

Anirud/lc GitHub actions #13

name: Lint and License Check
on:
push:
branches:
- main
pull_request:
branches:
- "main"
- "staging"
types:
- opened
- reopened
- synchronize
- assigned
- review_requested
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install dependencies
working-directory: app/frontend
run: npm install
- name: Run ESLint and Capture Output
working-directory: app/frontend
id: run_eslint
run: |
set +e
output=$(npm run lint --silent 2>&1)
exit_code=$?
echo "$output"
clean_output=$(echo "$output" | sed 's/\x1b\[[0-9;]*m//g') # Remove ANSI escape codes
echo "CLEAN_OUTPUT<<EOF" >> $GITHUB_ENV
echo "$clean_output" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
# Extract only the errors (ignore warnings)
lint_errors=$(echo "$clean_output" | grep 'error')
# Check if there are any lint errors
if [ -n "$lint_errors" ]; then
echo "Errors found."
echo "LINT_ERRORS<<EOF" >> $GITHUB_ENV
echo "$lint_errors" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "HAS_ERRORS=true" >> $GITHUB_ENV
else
echo "No relevant errors found."
echo "HAS_ERRORS=false" >> $GITHUB_ENV
fi
exit 0 # Always exit with 0 to avoid failing the step
- name: Comment on PR with ESLint Errors
if: env.HAS_ERRORS == 'true'
uses: actions/github-script@v7
with:
script: |
const lintErrors = process.env.LINT_ERRORS;
const issueNumber = context.payload.pull_request.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
if (lintErrors && issueNumber) {
const commentBody = `## Frontend Project: ESLint Errors\n\`\`\`\n${lintErrors}\n\`\`\`\nPlease review and resolve the ESLint errors.`;
await github.rest.issues.createComment({
issue_number: issueNumber,
owner: owner,
repo: repo,
body: commentBody
});
} else {
console.log("No relevant errors to report.");
}
- name: Fail if Errors Exist
if: env.HAS_ERRORS == 'true'
run: exit 1 # Fail the workflow if errors exist