diff --git a/.gitignore b/.gitignore index 625212a..79a1db1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ .DS_Store npm-debug.log +test.md diff --git a/__snapshots__/commit-message-install-spec.js b/__snapshots__/commit-message-install-spec.js index 76840e2..c4da922 100644 --- a/__snapshots__/commit-message-install-spec.js +++ b/__snapshots__/commit-message-install-spec.js @@ -53,7 +53,8 @@ exports['commit-message-install getJsonFromGit extracts formed json correctly 1' "status": { "owner": "foo", "repo": "bar", - "sha": "2d8687c143165218c6b52a76018b76cf99137e48" + "sha": "2d8687c143165218c6b52a76018b76cf99137e48", + "context": "testing" } } diff --git a/__snapshots__/get-install-json-spec.js b/__snapshots__/get-install-json-spec.js index 5f3af2d..47c0e68 100644 --- a/__snapshots__/get-install-json-spec.js +++ b/__snapshots__/get-install-json-spec.js @@ -41,6 +41,7 @@ exports['getInstallJson sets status object 1'] = { "status": { "owner": "foo", "repo": "bar", - "sha": "2d8687c143165218c6b52a76018b76cf99137e48" + "sha": "2d8687c143165218c6b52a76018b76cf99137e48", + "context": "testing" } } diff --git a/bin/set-status.js b/bin/set-status.js new file mode 100755 index 0000000..96dc128 --- /dev/null +++ b/bin/set-status.js @@ -0,0 +1,100 @@ +#!/usr/bin/env node + +'use strict' + +// sets given commit status based on the JSON block in the commit message + +const chalk = require('chalk') +const os = require('os') +const debug = require('debug')('commit-message-install') +const { GitHub } = require('gh-app-set-commit-status') +const is = require('check-more-types') + +const hasStatusFields = is.schema({ + owner: is.unemptyString, + repo: is.unemptyString, + sha: is.commitId, + context: is.unemptyString +}) +const isValidCommitState = is.oneOf(Object.keys(GitHub.StatusState)) + +const allArgs = process.argv.slice(2) +const args = require('minimist')(allArgs, { + alias: { + file: 'f', + state: 's', + description: 'd', + context: 'c' + }, + string: ['file', 'state', 'description', 'context', 'url'] +}) + +const api = require('..') +const getMessage = api.getMessage +const getJsonBlock = api.getJsonBlock + +function onError (e) { + console.error(e) + process.exit(1) +} + +let start +if (args.file) { + console.log('loading message from file', args.file) + const fs = require('fs') + const message = fs.readFileSync(args.file, 'utf8') + start = Promise.resolve(message) +} else { + start = getMessage() +} +start + .then(getJsonBlock) + .then(json => { + if (!json) { + console.log('there is no JSON block, nothing to set') + return + } + if (!json.status) { + console.log('JSON commit object does not have status block') + return + } + if (!hasStatusFields(json.status)) { + console.log('missing all required fields in the status object') + console.log(json.status) + return + } + const state = args.state || GitHub.StatusState.pending + if (!isValidCommitState(state)) { + console.error('invalid commit state "%s"', state) + console.error('only valid states are', Object.keys(GitHub.StatusState)) + process.exit(1) + } + + console.log('got json block from the git commit message') + console.log(JSON.stringify(json, null, 2)) + + const osPlatform = os.platform() + if (!api.isPlatformAllowed(json.platform, osPlatform)) { + console.log('Required platform: %s', chalk.green(json.platform)) + console.log('Current platform: %s', chalk.red(osPlatform)) + console.log('skipping commit status ⏩') + return + } + console.log('Platform %s is allowed', chalk.green(osPlatform)) + + const params = GitHub.getFromEnvironment() + const gh = GitHub.createGithubAppClient(params) + + const options = { + owner: json.status.owner, + repo: json.status.repo, + sha: json.status.sha, + state: args.state, + description: args.description, + context: args.context || json.status.context, + targetUrl: args.url + } + debug('setting commit status %o', options) + return GitHub.setCommitStatus(options, gh) + }) + .catch(onError) diff --git a/src/commit-message-install-spec.js b/src/commit-message-install-spec.js index 9e9d657..59aca57 100644 --- a/src/commit-message-install-spec.js +++ b/src/commit-message-install-spec.js @@ -68,9 +68,17 @@ describe('commit-message-install', () => { const status = { owner: 'foo', repo: 'bar', - sha: '2d8687c143165218c6b52a76018b76cf99137e48' + sha: '2d8687c143165218c6b52a76018b76cf99137e48', + context: 'testing' } - const info = getInstallJson(['debug', 'chalk'], {}, 'linux', null, null, status) + const info = getInstallJson( + ['debug', 'chalk'], + {}, + 'linux', + null, + null, + status + ) const json = toMarkdownJsonBlock(info) const message = `some text\n\n` + json diff --git a/src/get-install-json-spec.js b/src/get-install-json-spec.js index fc78ed7..254da6c 100644 --- a/src/get-install-json-spec.js +++ b/src/get-install-json-spec.js @@ -36,8 +36,11 @@ describe('getInstallJson', () => { const status = { owner: 'foo', repo: 'bar', - sha: '2d8687c143165218c6b52a76018b76cf99137e48' + sha: '2d8687c143165218c6b52a76018b76cf99137e48', + context: 'testing' } - snapshot(getInstallJson(['debug', 'chalk'], {}, 'linux', null, null, status)) + snapshot( + getInstallJson(['debug', 'chalk'], {}, 'linux', null, null, status) + ) }) }) diff --git a/src/get-install-json.js b/src/get-install-json.js index 50a43bd..689b699 100644 --- a/src/get-install-json.js +++ b/src/get-install-json.js @@ -7,7 +7,8 @@ const isNpmInstall = require('./utils').isNpmInstall const isStatus = is.schema({ owner: is.unemptyString, repo: is.unemptyString, - sha: is.commitId + sha: is.commitId, + context: is.unemptyString }) // forms JSON object that can be parsed later