-
Notifications
You must be signed in to change notification settings - Fork 2
106 lines (91 loc) · 3.57 KB
/
lint-and-license-check.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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 # Adjust path according to your repo structure
run: npm install
- name: Run ESLint and Filter Errors
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 and format file names with 'missing header' and other ESLint errors
header_errors=$(echo "$clean_output" | grep 'missing header')
lint_errors=$(echo "$clean_output" | grep 'error' | grep -v 'missing header')
# Format the output to highlight file names
format_output() {
echo "$1" | while read -r line; do
file=$(echo "$line" | awk '{print $1}')
error_msg=$(echo "$line" | cut -d' ' -f3-)
# Generate markdown link to the file
file_link="[**$file**](https://github.com/${GITHUB_REPOSITORY}/blob/${GITHUB_SHA}/${file})"
echo "$file_link $error_msg"
done
}
formatted_header_errors=$(format_output "$header_errors")
formatted_lint_errors=$(format_output "$lint_errors")
# Combine formatted outputs
combined_errors="$formatted_header_errors\n$formatted_lint_errors"
# Check if there are any header or lint errors
if [ -n "$combined_errors" ]; then
echo "Errors found."
echo "COMBINED_ERRORS<<EOF" >> $GITHUB_ENV
echo -e "$combined_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 and Header Errors
if: env.HAS_ERRORS == 'true'
uses: actions/github-script@v7
with:
script: |
const lintOutput = process.env.COMBINED_ERRORS;
const issueNumber = context.issue.number || context.payload.pull_request.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
if (lintOutput && issueNumber) {
const commentBody = `## Frontend Project: ESLint and License Header Issues\n\`\`\`\n${lintOutput}\n\`\`\`\nPlease review and resolve the ESLint errors and missing license headers.`;
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