Skip to content
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

feat: add set status CLI #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
.DS_Store
npm-debug.log
test.md
3 changes: 2 additions & 1 deletion __snapshots__/commit-message-install-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down
3 changes: 2 additions & 1 deletion __snapshots__/get-install-json-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ exports['getInstallJson sets status object 1'] = {
"status": {
"owner": "foo",
"repo": "bar",
"sha": "2d8687c143165218c6b52a76018b76cf99137e48"
"sha": "2d8687c143165218c6b52a76018b76cf99137e48",
"context": "testing"
}
}
100 changes: 100 additions & 0 deletions bin/set-status.js
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 10 additions & 2 deletions src/commit-message-install-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions src/get-install-json-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
})
})
3 changes: 2 additions & 1 deletion src/get-install-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down