Skip to content

Commit d26ed45

Browse files
committed
Use shared actions
1 parent 1ef8d34 commit d26ed45

File tree

7 files changed

+250
-55
lines changed

7 files changed

+250
-55
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: 'Configure Node.js'
2+
description: 'Install Node.js and install Node.js modules or restore cache'
3+
4+
inputs:
5+
node-version:
6+
description: 'NodeJS Version'
7+
default: '18'
8+
lookup-only:
9+
description: 'If true, only checks if cache entry exists and skips download. Does not change save cache behavior'
10+
default: 'false'
11+
12+
runs:
13+
using: 'composite'
14+
steps:
15+
- uses: actions/setup-node@v4
16+
with:
17+
node-version: ${{ inputs.node-version }}
18+
19+
- name: Restore Node Modules from Cache
20+
id: cache-node-modules
21+
uses: actions/cache@v4
22+
with:
23+
path: |
24+
node_modules
25+
packages/**/node_modules
26+
!node_modules/.cache
27+
key: node-modules-${{ inputs.node-version }}-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('package.json', 'package-lock.json', '**/package-lock.json') }}
28+
lookup-only: ${{ inputs.lookup-only }}
29+
30+
- name: Install dependencies
31+
if: steps.cache-node-modules.outputs.cache-hit != 'true'
32+
shell: bash
33+
run: npm ci
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: 'Parse Coverage and Post Comment'
2+
description: 'Parses a coverage report and posts a comment on a PR'
3+
inputs:
4+
lcov-file:
5+
description: 'Path to the lcov.info file'
6+
required: true
7+
title:
8+
description: 'Title of the comment'
9+
default: 'Code Coverage Report'
10+
11+
runs:
12+
using: 'composite'
13+
steps:
14+
- name: Parse Coverage
15+
shell: bash
16+
if: github.event_name == 'pull_request'
17+
id: parse
18+
run: |
19+
./.github/actions/coverage-report/scripts/parse-coverage.js ${{ inputs.lcov-file }} > coverage-summary.txt
20+
echo "coverage-summary<<EOF" >> $GITHUB_OUTPUT
21+
cat coverage-summary.txt >> $GITHUB_OUTPUT
22+
echo "EOF" >> $GITHUB_OUTPUT
23+
24+
- name: Find Coverage Comment
25+
if: github.event_name == 'pull_request'
26+
uses: peter-evans/find-comment@v3
27+
id: fc
28+
with:
29+
issue-number: ${{ github.event.pull_request.number }}
30+
comment-author: 'github-actions[bot]'
31+
body-includes: '### 📊 ${{ inputs.title }}'
32+
33+
- name: Post Coverage Comment
34+
uses: peter-evans/create-or-update-comment@v4
35+
with:
36+
comment-id: ${{ steps.fc.outputs.comment-id }}
37+
edit-mode: replace
38+
issue-number: ${{ github.event.pull_request.number }}
39+
body: |
40+
### 📊 ${{ inputs.title }}
41+
${{ steps.parse.outputs.coverage-summary }}
42+
43+
- name: Upload Coverage Report
44+
if: github.event_name == 'pull_request'
45+
uses: actions/upload-artifact@v4
46+
with:
47+
name: coverage-report
48+
path: coverage/
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/usr/bin/env node
2+
const lcovParse = require("lcov-parse");
3+
const fs = require("fs");
4+
5+
const lcovPath = process.argv[2];
6+
const needsImprovementBelow = parseFloat(
7+
process.argv.length >= 4 ? process.argv[3] : "90"
8+
);
9+
const poorBelow = parseFloat(process.argv[4] >= 5 ? process.argv[4] : "50");
10+
11+
if (!lcovPath || isNaN(needsImprovementBelow) || isNaN(poorBelow)) {
12+
console.error(
13+
"Please provide the path to the lcov.info file and the 'needs-improvement-below' and 'poor-below' percentages as command-line arguments."
14+
);
15+
process.exit(1);
16+
}
17+
18+
if (!fs.existsSync(lcovPath)) {
19+
console.error(
20+
`The file ${lcovPath} does not exist. Please provide the path to the lcov.info file.`
21+
);
22+
process.exit(1);
23+
}
24+
25+
const outputFormat = "markdown";
26+
27+
if (outputFormat === "markdown") {
28+
console.log(
29+
"| File | Lines | Lines Hit / Found | Uncovered Lines | Branches |"
30+
);
31+
console.log("| --- | --- | --- | --- | --- |");
32+
}
33+
34+
function shortenPath(path, maxLength) {
35+
if (path.length <= maxLength) {
36+
return path;
37+
}
38+
39+
const start = path.substring(0, maxLength / 2 - 2); // -2 for the '..' in the middle
40+
const end = path.substring(path.length - maxLength / 2, path.length);
41+
42+
return `${start}..${end}`;
43+
}
44+
45+
function getEmoji(lineCoverage, needsImprovementBelow, poorBelow) {
46+
if (lineCoverage >= needsImprovementBelow) {
47+
return "✅"; // white-check emoji
48+
} else if (
49+
lineCoverage < needsImprovementBelow &&
50+
lineCoverage >= poorBelow
51+
) {
52+
return "🟡"; // yellow-ball emoji
53+
} else {
54+
return "❌"; // red-x emoji
55+
}
56+
}
57+
58+
lcovParse(lcovPath, function (err, data) {
59+
if (err) {
60+
console.error(err);
61+
} else {
62+
let totalLinesHit = 0;
63+
let totalLinesFound = 0;
64+
let totalBranchesHit = 0;
65+
let totalBranchesFound = 0;
66+
67+
data.forEach((file) => {
68+
totalLinesHit += file.lines.hit;
69+
totalLinesFound += file.lines.found;
70+
totalBranchesHit += file.branches.hit;
71+
totalBranchesFound += file.branches.found;
72+
const relativePath = shortenPath(
73+
file.file.replace(process.cwd(), ""),
74+
50
75+
);
76+
const lineCoverage = ((file.lines.hit / file.lines.found) * 100).toFixed(
77+
1
78+
);
79+
const branchCoverage = (
80+
(file.branches.hit / file.branches.found) *
81+
100
82+
).toFixed(1);
83+
let emoji = getEmoji(lineCoverage, needsImprovementBelow, poorBelow);
84+
if (outputFormat === "markdown") {
85+
console.log(
86+
`| ${relativePath} | ${emoji}&nbsp;${lineCoverage}% | ${
87+
file.lines.hit
88+
}&nbsp;/&nbsp;${file.lines.found} | ${
89+
file.lines.found - file.lines.hit
90+
} | ${file.branches.found === 0 ? "-" : `${branchCoverage}%`} |`
91+
);
92+
} else {
93+
console.log(
94+
`${emoji} File: ${relativePath}, Line Coverage: ${lineCoverage}%, Branch Coverage: ${branchCoverage}%`
95+
);
96+
}
97+
});
98+
99+
const overallLineCoverage = (
100+
(totalLinesHit / totalLinesFound) *
101+
100
102+
).toFixed(1);
103+
const overallBranchCoverage = (
104+
(totalBranchesHit / totalBranchesFound) *
105+
100
106+
).toFixed(1);
107+
const totalUncoveredLines = totalLinesFound - totalLinesHit;
108+
const overallEmoji = getEmoji(
109+
overallLineCoverage,
110+
needsImprovementBelow,
111+
poorBelow
112+
);
113+
114+
if (outputFormat === "markdown") {
115+
console.log(
116+
`| Overall | ${overallEmoji}&nbsp;${overallLineCoverage}% | ${totalLinesHit}&nbsp;/&nbsp;${totalLinesFound} | ${totalUncoveredLines} | ${
117+
totalBranchesFound === 0 ? "-" : `${overallBranchCoverage}%`
118+
} |`
119+
);
120+
} else {
121+
console.log(
122+
`Overall Line Coverage: ${overallLineCoverage}%, Overall Branch Coverage: ${overallBranchCoverage}%`
123+
);
124+
}
125+
}
126+
});

.github/workflows/ci.yml

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,33 @@ on:
99
- main
1010

1111
jobs:
12-
build:
12+
check-access:
1313
runs-on: ubuntu-latest
14+
outputs:
15+
has-token-access: ${{ steps.check.outputs.has-token-access }}
1416
steps:
15-
- name: Checkout code
16-
uses: actions/checkout@v3
17+
- id: check
18+
run: |
19+
echo "has-token-access=$(if [[ '${{ github.event.pull_request.head.repo.fork }}' != 'true' && '${{ github.actor }}' != 'dependabot[bot]' ]]; then echo 'true'; else echo 'false'; fi)" >> $GITHUB_OUTPUT
1720
18-
- name: Use Node.js 16
19-
uses: actions/setup-node@v3
21+
install-deps:
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: actions/checkout@v4
25+
- uses: ./.github/actions/configure-nodejs
2026
with:
21-
node-version: 16
27+
lookup-only: 'true' # We only want to lookup from the cache - if a hit, this job does nothing
2228

23-
- name: Cache Node Modules
24-
id: cache-node-modules
25-
uses: actions/cache@v3
26-
with:
27-
path: |
28-
node_modules
29-
!node_modules/.cache
30-
key: node-modules-${{ hashFiles('package.json', 'package-lock.json') }}
31-
32-
- name: Install Modules
33-
if: steps.cache-node-modules.outputs.cache-hit != 'true'
34-
run: npm ci
29+
build:
30+
needs:
31+
- install-deps
32+
- check-access
33+
runs-on: ubuntu-latest
34+
steps:
35+
- name: Checkout code
36+
uses: actions/checkout@v4
37+
38+
- uses: ./.github/actions/configure-nodejs
3539

3640
- name: Build
3741
run: npm run build

.github/workflows/docs.yml

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,21 @@ on:
77
workflow_dispatch:
88

99
jobs:
10-
build:
10+
install-deps:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- name: Checkout code
14-
uses: actions/checkout@v3
15-
16-
- name: Use Node.js 16
17-
uses: actions/setup-node@v3
13+
- uses: actions/checkout@v4
14+
- uses: ./.github/actions/configure-nodejs
1815
with:
19-
node-version: 16
16+
lookup-only: 'true' # We only want to lookup from the cache - if a hit, this job does nothing
2017

21-
- name: Cache Node Modules
22-
id: cache-node-modules
23-
uses: actions/cache@v3
24-
with:
25-
path: |
26-
node_modules
27-
!node_modules/.cache
28-
key: node-modules-${{ hashFiles('package.json', 'package-lock.json') }}
18+
build:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v4
2923

30-
- name: Install Modules
31-
if: steps.cache-node-modules.outputs.cache-hit != 'true'
32-
run: npm ci
24+
- uses: ./.github/actions/configure-nodejs
3325

3426
- name: Build Docs
3527
run: npm run build:docs

.github/workflows/publish.yml

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,21 @@ on:
55
types: [published]
66

77
jobs:
8+
install-deps:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: ./.github/actions/configure-nodejs
13+
with:
14+
lookup-only: 'true' # We only want to lookup from the cache - if a hit, this job does nothing
15+
816
build:
917
runs-on: ubuntu-latest
1018
steps:
1119
- name: Checkout code
12-
uses: actions/checkout@v3
20+
uses: actions/checkout@v4
1321

14-
- name: Use Node.js 16
15-
uses: actions/setup-node@v3
16-
with:
17-
node-version: 16
18-
19-
- name: Cache Node Modules
20-
id: cache-node-modules
21-
uses: actions/cache@v3
22-
with:
23-
path: |
24-
node_modules
25-
!node_modules/.cache
26-
key: node-modules-${{ hashFiles('package.json', 'package-lock.json') }}
27-
28-
- name: Install Modules
29-
if: steps.cache-node-modules.outputs.cache-hit != 'true'
30-
run: npm ci
22+
- uses: ./.github/actions/configure-nodejs
3123

3224
- name: Use the Release Tag Version
3325
run: |

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v16.17.1
1+
v18

0 commit comments

Comments
 (0)