Skip to content

Commit 0f602ec

Browse files
authored
Merge pull request #1709 from fedspendingtransparency/innov-march-gh-actions
Innovation Day - Github Action Coverage Comment
2 parents d147fd4 + 9d10254 commit 0f602ec

File tree

5 files changed

+348
-61
lines changed

5 files changed

+348
-61
lines changed

.github/actions/action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: 'Test Coverage Comment'
2+
description: 'This action comments test coverage on the pr'
3+
runs:
4+
using: 'node20'
5+
main: 'index.js'

.github/actions/index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* istanbul ignore file */
2+
const core = require('@actions/core');
3+
const github = require('@actions/github');
4+
5+
(async () => {
6+
try {
7+
const summaryData = require('../../coverage/coverage-summary.json');
8+
const coverage = summaryData.total.lines.pct;
9+
const icon = coverage < 90 ? '❌' : `✅`;
10+
const token = core.getInput('GITHUB_TOKEN');
11+
const context = github.context;
12+
const pr_number = context.payload.pull_request.number;
13+
const oktokit = github.getOctokit(token);
14+
const pr_id = context.payload.pull_request.id;
15+
const pr_comments_response = await oktokit.rest.issues.listComments({
16+
...context.repo,
17+
issue_number: pr_number,
18+
id: pr_id,
19+
});
20+
const pr_comments = pr_comments_response.data;
21+
const coverage_comment = pr_comments?.find(comment => comment.user.login === 'github-actions[bot]');
22+
if (pr_comments.length > 0 && coverage_comment) {
23+
oktokit.rest.issues.updateComment({
24+
...context.repo,
25+
issue_number: pr_number,
26+
comment_id: coverage_comment.id,
27+
body: `Total Line Coverage: ${coverage}% ${icon}`,
28+
});
29+
} else {
30+
oktokit.rest.issues.createComment({
31+
...context.repo,
32+
issue_number: pr_number,
33+
body: `Total Line Coverage: ${coverage}% ${icon}`,
34+
});
35+
}
36+
} catch (error) {
37+
// Handle errors and indicate failure
38+
core.setFailed(error.message);
39+
}
40+
})();

.github/workflows/main.yml

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
name: Run unit and e2e tests
33

4+
concurrency:
5+
group: ${{ github.workflow }}-${{ github.ref }}
6+
cancel-in-progress: true
7+
48
on:
59
pull_request:
610
types: [opened, synchronize]
@@ -23,7 +27,12 @@ jobs:
2327
- name: run tests with coverage
2428
env:
2529
NODE_OPTIONS: "--max_old_space_size=8192"
26-
run: npm run test:coverage
30+
run: npm run test:report
31+
- name: Test Coverage Comment
32+
uses: ./.github/actions
33+
with:
34+
GITHUB_TOKEN: ${{ github.token }}
35+
if: always()
2736
run-branch-unit-tests-and-coverage:
2837
timeout-minutes: 10
2938
runs-on: ubuntu-22.04
@@ -49,27 +58,27 @@ jobs:
4958
env:
5059
NODE_OPTIONS: "--max_old_space_size=8192"
5160
run: npm run test:branch
52-
run-cypress-tests:
53-
timeout-minutes: 15
54-
runs-on: ubuntu-22.04
55-
steps:
56-
- name: Checkout
57-
uses: actions/checkout@v3
58-
- name: Setup Node.js environment
59-
uses: actions/setup-node@v3.8.1
60-
with:
61-
node-version: 20
62-
- name: Cypress install
63-
env:
64-
NODE_OPTIONS: "--max_old_space_size=8192"
65-
run: npm install --legacy-peer-deps
66-
- name: Cypress run
67-
uses: cypress-io/github-action@v5
68-
env:
69-
NODE_OPTIONS: "--max_old_space_size=8192"
70-
GITHUB_TOKEN: ${{ github.token }}
71-
with:
72-
install: false
73-
build: npm run build
74-
start: npm run serve
75-
broswer: chrome
61+
# run-cypress-tests:
62+
# timeout-minutes: 15
63+
# runs-on: ubuntu-22.04
64+
# steps:
65+
# - name: Checkout
66+
# uses: actions/checkout@v3
67+
# - name: Setup Node.js environment
68+
# uses: actions/setup-node@v3.8.1
69+
# with:
70+
# node-version: 20
71+
# - name: Cypress install
72+
# env:
73+
# NODE_OPTIONS: "--max_old_space_size=8192"
74+
# run: npm install --legacy-peer-deps
75+
# - name: Cypress run
76+
# uses: cypress-io/github-action@v5
77+
# env:
78+
# NODE_OPTIONS: "--max_old_space_size=8192"
79+
# GITHUB_TOKEN: ${{ github.token }}
80+
# with:
81+
# install: false
82+
# build: npm run build
83+
# start: npm run serve
84+
# broswer: chrome

0 commit comments

Comments
 (0)