|
| 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 | +}) |
0 commit comments