-
Notifications
You must be signed in to change notification settings - Fork 7
83 lines (72 loc) · 2.71 KB
/
lint-and-unit-tests.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
name: Lint and Unit Tests
on:
pull_request:
branches:
- dev
- main
jobs:
lint-and-unit-tests:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm i
- name: Check for linting errors
run: npm run lint > lintOutput.txt || true
- name: Run tests
id: test
run: npm run testCoverage || echo "TESTS_FAILED=true" >> $GITHUB_ENV
- name: Determine comment based on linting and test results
id: comment
if: ${{ github.event_name == 'pull_request' }}
run: |
LINT_ERRORS=""
if [ -f lintOutput.txt ]; then
LINT_ERRORS=$(grep -E '[1-9][0-9]*\serror' lintOutput.txt || true)
fi
if [ "$TESTS_FAILED" = "true" ] || [ -n "$LINT_ERRORS" ]; then
{
echo 'PR_COMMENT<<EOF'
echo "Issue with linting or unit tests detected."
if [ -n "$LINT_ERRORS" ]; then
echo "Linting failed with the following errors:"
echo "\`\`\`"
cat lintOutput.txt
echo "\`\`\`"
echo "For more information on our linting policies, please see our [Linting-Guide](../tree/dev/Linting-Guide.md)."
fi
if [ "$TESTS_FAILED" = "true" ]; then
echo "Unit tests failed."
fi
echo EOF
} >> "$GITHUB_OUTPUT"
else
echo "PR_COMMENT=Linting and unit tests succeeded." >> "$GITHUB_OUTPUT"
fi
- name: Fail if linting errors or unit tests failed
if: steps.comment.outputs.PR_COMMENT != 'Linting and unit tests succeeded.'
run: exit 1
- name: Comment on PR
if: always()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_COMMENT: ${{ steps.comment.outputs.PR_COMMENT }}
run: |
if [[ "${{ github.event_name }}" == 'pull_request' && "$PR_COMMENT" != 'Linting and unit tests succeeded.' ]]; then
JSON_PAYLOAD=$(jq -n --arg body "$PR_COMMENT" '{"body": $body}')
echo "Generated JSON payload:"
echo "$JSON_PAYLOAD"
curl -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"
fi