Skip to content

Commit 467535f

Browse files
authored
feat: add commit status script (#27)
* feat: add commit status script * pass description * default label
1 parent 77f2f46 commit 467535f

File tree

6 files changed

+352
-81
lines changed

6 files changed

+352
-81
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ steps:
101101
- run: DEBUG=cypress:cli $(npm bin)/run-if $(npm bin)/cypress verify
102102
```
103103

104+
### Set GitHub commit status
105+
106+
Very useful to notify other projects asynchronously via GitHub commit states.
107+
108+
```text
109+
$(npm bin)/set-status --label "context label" --state success --description "short message"
110+
```
111+
112+
State can be "error", "pending", "failure" or "success". `--label` is optional, if not set, then the platform and the package name will be used.
113+
104114
## API
105115

106116
### getJsonFromGit

bin/set-commit-status.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env node
2+
3+
'use strict'
4+
5+
const debug = require('debug')('commit-message-install')
6+
const os = require('os')
7+
const path = require('path')
8+
9+
const allArgs = process.argv.slice(2)
10+
const args = require('minimist')(allArgs, {
11+
alias: {
12+
file: 'f',
13+
sha: 'commit',
14+
description: 'd'
15+
},
16+
string: ['file', 'sha', 'state', 'label', 'description']
17+
})
18+
19+
const api = require('..')
20+
const getMessage = api.getMessage
21+
const getJsonBlock = api.getJsonBlock
22+
const setCommitStatus = api.setCommitStatus
23+
24+
function onError (e) {
25+
console.error(e)
26+
process.exit(1)
27+
}
28+
29+
let start
30+
if (args.file) {
31+
console.log('loading message from file', args.file)
32+
const fs = require('fs')
33+
const message = fs.readFileSync(args.file, 'utf8')
34+
start = Promise.resolve(message)
35+
} else {
36+
start = getMessage(args.sha)
37+
}
38+
start
39+
.then(getJsonBlock)
40+
.then(json => {
41+
if (!json) {
42+
debug('could not find JSON block in the Git message')
43+
return
44+
}
45+
46+
const status = json.status
47+
if (!status) {
48+
debug('could not find status object in the block %o', json)
49+
return
50+
}
51+
52+
if (!args.label) {
53+
const name = require(path.join(process.cwd(), 'package.json')).name
54+
args.label = `${os.platform()}-${os.arch()} ${name}`
55+
}
56+
57+
return setCommitStatus(args.label, args.state, args.description, status)
58+
})
59+
.catch(onError)

0 commit comments

Comments
 (0)