Skip to content

feat: add GitHub status check script #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ jobs:
- name: Update code coverage badge 🥇
run: node bin/update-badge

- name: Set commit status using REST
# https://developer.github.com/v3/repos/statuses/
run: |
curl --request POST \
--url https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.sha }} \
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
--header 'content-type: application/json' \
--data '{
"state": "success",
"description": "REST commit status",
"context": "a test"
}'

- name: Set code coverage commit status 📫
run: node bin/set-gh-status
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Semantic Release 🚀
uses: cycjimmy/semantic-release-action@v2
env:
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ update-badge --set 78%

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

## set-gh-status

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:

```yaml
- name: Set code coverage commit status 📫
run: node bin/set-gh-status
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

Which should show a commit status message like:

![Commit status check](images/commit-status.png)

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.

## Debug

To see verbose log messages, run with `DEBUG=check-code-coverage` environment variable
Expand Down
81 changes: 81 additions & 0 deletions bin/set-gh-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env node
// @ts-check

const got = require('got')
const debug = require('debug')('check-code-coverage')
const {readCoverage, toPercent} = require('..')

const arg = require('arg')

const args = arg({
'--from': String // input json-summary filename, by default "coverage/coverage-summary.json"
})
debug('args: %o', args)

async function setGitHubCommitStatus(options, envOptions) {
const pct = toPercent(readCoverage(options.filename))
debug('setting commit coverage: %d', pct)
debug('with options %o', {
repository: envOptions.repository,
sha: envOptions.sha
})

// REST call to GitHub API
// https://developer.github.com/v3/repos/statuses/
// https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#example-calling-the-rest-api
// a typical request would be like:
// curl --request POST \
// --url https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.sha }} \
// --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
// --header 'content-type: application/json' \
// --data '{
// "state": "success",
// "description": "REST commit status",
// "context": "a test"
// }'
const url = `https://api.github.com/repos/${envOptions.repository}/statuses/${envOptions.sha}`
// @ts-ignore
const res = await got.post(url, {
headers: {
authorization: `Bearer ${envOptions.token}`
},
json: {
context: 'code-coverage',
state: 'success',
description: `${pct}% of statements`
}
})
console.log('response status: %d %s', res.statusCode, res.statusMessage)
}

function checkEnvVariables(env) {
if (!env.GITHUB_TOKEN) {
console.error('Cannot find environment variable GITHUB_TOKEN')
process.exit(1)
}

if (!env.GITHUB_REPOSITORY) {
console.error('Cannot find environment variable GITHUB_REPOSITORY')
process.exit(1)
}

if (!env.GITHUB_SHA) {
console.error('Cannot find environment variable GITHUB_SHA')
process.exit(1)
}
}

checkEnvVariables(process.env)

const options = {
filename: args['--file']
}
const envOptions = {
token: process.env.GITHUB_TOKEN,
repository: process.env.GITHUB_REPOSITORY,
sha: process.env.GITHUB_SHA
}
setGitHubCommitStatus(options, envOptions).catch(e => {
console.error(e)
process.exit(1)
})
16 changes: 2 additions & 14 deletions bin/update-badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('path')
const fs = require('fs')
const os = require('os')
const arg = require('arg')
const {readCoverage, toPercent} = require('..')

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

function readCoverage(filename) {
if (!filename) {
filename = path.join(process.cwd(), 'coverage', 'coverage-summary.json')
}
debug('reading coverage summary from: %s', filename)
const coverage = require(filename)
return coverage.total.statements.pct
}

function updateBadge(args) {
let pct = 0
if (args['--set']) {
Expand All @@ -48,11 +40,7 @@ function updateBadge(args) {
} else {
pct = readCoverage(args['--from'])
}
if (pct < 0) {
pct = 0
} else if (pct > 100) {
pct = 100
}
pct = toPercent(pct)
debug('clamped coverage: %d', pct)

const readmeFilename = path.join(process.cwd(), 'README.md')
Expand Down
Binary file added images/commit-status.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading