Skip to content

Commit 033e352

Browse files
authored
feat: add GitHub status check script (#13)
1 parent 2251448 commit 033e352

File tree

8 files changed

+935
-25
lines changed

8 files changed

+935
-25
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ jobs:
2222
- name: Update code coverage badge 🥇
2323
run: node bin/update-badge
2424

25+
- name: Set commit status using REST
26+
# https://developer.github.com/v3/repos/statuses/
27+
run: |
28+
curl --request POST \
29+
--url https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.sha }} \
30+
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
31+
--header 'content-type: application/json' \
32+
--data '{
33+
"state": "success",
34+
"description": "REST commit status",
35+
"context": "a test"
36+
}'
37+
38+
- name: Set code coverage commit status 📫
39+
run: node bin/set-gh-status
40+
env:
41+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42+
2543
- name: Semantic Release 🚀
2644
uses: cycjimmy/semantic-release-action@v2
2745
env:

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ update-badge --set 78%
8181

8282
Related project: [dependency-version-badge](https://github.com/bahmutov/dependency-version-badge)
8383

84+
## set-gh-status
85+
86+
If you run your tests on [GitHub Actions](https://glebbahmutov.com/blog/trying-github-actions/), there is an easy way to add commit status with code coverage percentage. From your CI workflow use command:
87+
88+
```yaml
89+
- name: Set code coverage commit status 📫
90+
run: node bin/set-gh-status
91+
env:
92+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
93+
```
94+
95+
Which should show a commit status message like:
96+
97+
![Commit status check](images/commit-status.png)
98+
99+
This script reads the code coverage summary from `coverage/coverage-summary.json` by default (you can specific a different file name using `--from` option) and posts the commit status, always passing for now.
100+
84101
## Debug
85102

86103
To see verbose log messages, run with `DEBUG=check-code-coverage` environment variable

bin/set-gh-status.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env node
2+
// @ts-check
3+
4+
const got = require('got')
5+
const debug = require('debug')('check-code-coverage')
6+
const {readCoverage, toPercent} = require('..')
7+
8+
const arg = require('arg')
9+
10+
const args = arg({
11+
'--from': String // input json-summary filename, by default "coverage/coverage-summary.json"
12+
})
13+
debug('args: %o', args)
14+
15+
async function setGitHubCommitStatus(options, envOptions) {
16+
const pct = toPercent(readCoverage(options.filename))
17+
debug('setting commit coverage: %d', pct)
18+
debug('with options %o', {
19+
repository: envOptions.repository,
20+
sha: envOptions.sha
21+
})
22+
23+
// REST call to GitHub API
24+
// https://developer.github.com/v3/repos/statuses/
25+
// https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#example-calling-the-rest-api
26+
// a typical request would be like:
27+
// curl --request POST \
28+
// --url https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.sha }} \
29+
// --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
30+
// --header 'content-type: application/json' \
31+
// --data '{
32+
// "state": "success",
33+
// "description": "REST commit status",
34+
// "context": "a test"
35+
// }'
36+
const url = `https://api.github.com/repos/${envOptions.repository}/statuses/${envOptions.sha}`
37+
// @ts-ignore
38+
const res = await got.post(url, {
39+
headers: {
40+
authorization: `Bearer ${envOptions.token}`
41+
},
42+
json: {
43+
context: 'code-coverage',
44+
state: 'success',
45+
description: `${pct}% of statements`
46+
}
47+
})
48+
console.log('response status: %d %s', res.statusCode, res.statusMessage)
49+
}
50+
51+
function checkEnvVariables(env) {
52+
if (!env.GITHUB_TOKEN) {
53+
console.error('Cannot find environment variable GITHUB_TOKEN')
54+
process.exit(1)
55+
}
56+
57+
if (!env.GITHUB_REPOSITORY) {
58+
console.error('Cannot find environment variable GITHUB_REPOSITORY')
59+
process.exit(1)
60+
}
61+
62+
if (!env.GITHUB_SHA) {
63+
console.error('Cannot find environment variable GITHUB_SHA')
64+
process.exit(1)
65+
}
66+
}
67+
68+
checkEnvVariables(process.env)
69+
70+
const options = {
71+
filename: args['--file']
72+
}
73+
const envOptions = {
74+
token: process.env.GITHUB_TOKEN,
75+
repository: process.env.GITHUB_REPOSITORY,
76+
sha: process.env.GITHUB_SHA
77+
}
78+
setGitHubCommitStatus(options, envOptions).catch(e => {
79+
console.error(e)
80+
process.exit(1)
81+
})

bin/update-badge.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const path = require('path')
66
const fs = require('fs')
77
const os = require('os')
88
const arg = require('arg')
9+
const {readCoverage, toPercent} = require('..')
910

1011
const args = arg({
1112
'--from': String, // input json-summary filename, by default "coverage/coverage-summary.json"
@@ -30,15 +31,6 @@ function getColor(coveredPercent) {
3031
return 'brightgreen'
3132
}
3233

33-
function readCoverage(filename) {
34-
if (!filename) {
35-
filename = path.join(process.cwd(), 'coverage', 'coverage-summary.json')
36-
}
37-
debug('reading coverage summary from: %s', filename)
38-
const coverage = require(filename)
39-
return coverage.total.statements.pct
40-
}
41-
4234
function updateBadge(args) {
4335
let pct = 0
4436
if (args['--set']) {
@@ -48,11 +40,7 @@ function updateBadge(args) {
4840
} else {
4941
pct = readCoverage(args['--from'])
5042
}
51-
if (pct < 0) {
52-
pct = 0
53-
} else if (pct > 100) {
54-
pct = 100
55-
}
43+
pct = toPercent(pct)
5644
debug('clamped coverage: %d', pct)
5745

5846
const readmeFilename = path.join(process.cwd(), 'README.md')

images/commit-status.png

168 KB
Loading

0 commit comments

Comments
 (0)