-
Notifications
You must be signed in to change notification settings - Fork 6
107 lines (93 loc) · 3.59 KB
/
on-pull-request.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
107
name: Test successful build
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout sources 🔰
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Create local branch based on 'origin/main' 🚧
run: |
git fetch origin main
git checkout -b main origin/main
git checkout -
- name: Setup Node.js 🧮
uses: actions/setup-node@v4
with:
node-version: 20
- name: Cache Node.js modules 💾
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-node-
${{ runner.OS }}-
- name: Install dependencies ⏬
run: npm ci
- name: Run typecheck, lint and tests 🧪
run: npm run check
- name: Build artifacts 🏗️
run: npm run build
- name: Run tests related to current changes ✅
run: |
npx jest --config=jest.config.js --coverage --coverageReporters json-summary --changedSince=main --coverageDirectory ./coverage/changed
- name: Check for test results ❓
if: ${{ github.actor != 'dependabot[bot]' }}
id: check-results
run: |
if [ -f ./coverage/changed/coverage-summary.json ]; then
COVERAGE_CONTENT=$(cat ./coverage/changed/coverage-summary.json)
if echo "$COVERAGE_CONTENT" | jq '.total.lines.pct' | grep -q 'Unknown'; then
echo "no_tests_found=true" >> $GITHUB_ENV
else
echo "no_tests_found=false" >> $GITHUB_ENV
fi
else
echo "no_tests_found=true" >> $GITHUB_ENV
fi
- name: Construct jest coverage comment input 💬
if: ${{ github.actor != 'dependabot[bot]' }}
id: construct-input
env:
MULTI_LINES_TEXT: |
All, ./coverage/all/coverage-summary.json
only changed, ./coverage/changed/coverage-summary.json
run: |
if [ "${{ env.no_tests_found }}" == "true" ]; then
echo "MULTIPLE_FILES=All, ./coverage/all/coverage-summary.json" >> $GITHUB_ENV
else
echo "MULTIPLE_FILES<<EOF" >> $GITHUB_ENV
echo "$MULTI_LINES_TEXT" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
cat $GITHUB_ENV
fi
- name: Jest Coverage Comment 💬
if: ${{ github.actor != 'dependabot[bot]' }}
uses: MishaKav/jest-coverage-comment@main
with:
hide-comment: false
create-new-comment: false
hide-summary: false
multiple-files: |
${{ env.MULTIPLE_FILES }}
- name: Get Coverage Comment Id ➡️
if: ${{ env.no_tests_found == 'true' && github.actor != 'dependabot[bot]' }}
id: get-comment-id
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
COMMENTS_JSON=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}/comments")
COVERAGE_COMMENT_ID=$(echo "${COMMENTS_JSON}" | jq -r 'map(select(.user.login == "github-actions[bot]")) | sort_by(.created_at) | .[0].id')
echo "COVERAGE_COMMENT_ID=${COVERAGE_COMMENT_ID}" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Post To No Tests Found Comment ⚠️
if: ${{ env.no_tests_found == 'true' && github.actor != 'dependabot[bot]' }}
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ env.COVERAGE_COMMENT_ID }}
body: |
❌ No tests were found related to the files changed