Skip to content

Commit fe1fb21

Browse files
committed
feat: add outputs
re actions#96
1 parent 28fa18d commit fe1fb21

File tree

4 files changed

+9890
-41
lines changed

4 files changed

+9890
-41
lines changed

action.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ inputs:
3939
description: Arguments for the git tag command (the tag name always needs to be the first word not preceded by a hyphen)
4040
required: false
4141

42+
outputs:
43+
committed:
44+
description: Whether the action has created a commit.
45+
pushed:
46+
description: Whether the action has pushed to the remote.
47+
tagged:
48+
description: Whether the action has created a tag.
49+
4250
runs:
4351
using: node12
4452
main: lib/index.js

lib/index.js

Lines changed: 9813 additions & 1 deletion
Large diffs are not rendered by default.

src/main.ts

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
import {
22
info,
33
setFailed,
4-
getInput as getInputCore,
54
warning,
65
debug,
76
startGroup,
8-
endGroup,
9-
error
7+
endGroup
108
} from '@actions/core'
119
import axios from 'axios'
1210
import path from 'path'
1311
import simpleGit, { Response } from 'simple-git'
14-
15-
type Input =
16-
| 'add'
17-
| 'author_name'
18-
| 'author_email'
19-
| 'branch'
20-
| 'cwd'
21-
| 'message'
22-
| 'pull_strategy'
23-
| 'push'
24-
| 'remove'
25-
| 'signoff'
26-
| 'tag'
12+
import { getInput, Input, log, outputs, parseBool, setOutput } from './util'
2713

2814
const baseDir = path.join(process.cwd(), getInput('cwd') || '')
2915
const git = simpleGit({ baseDir })
@@ -92,12 +78,18 @@ console.log(`Running in ${baseDir}`)
9278
}
9379
: {})
9480
},
95-
log
81+
(err, data?) => {
82+
if (data) setOutput('committed', 'true')
83+
return log(err, data)
84+
}
9685
)
9786

9887
if (getInput('tag')) {
9988
info('> Tagging commit...')
100-
await git.tag(getInput('tag').split(' '), log)
89+
await git.tag(getInput('tag').split(' '), (err, data?) => {
90+
if (data) setOutput('tagged', 'true')
91+
return log(err, data)
92+
})
10193
} else info('> No tag info provided.')
10294

10395
if (getInput('push')) {
@@ -106,7 +98,10 @@ console.log(`Running in ${baseDir}`)
10698
'origin',
10799
getInput('branch'),
108100
{ '--set-upstream': null },
109-
log
101+
(err, data?) => {
102+
if (data) setOutput('pushed', 'true')
103+
return log(err, data)
104+
}
110105
)
111106

112107
if (getInput('tag')) {
@@ -139,10 +134,13 @@ console.log(`Running in ${baseDir}`)
139134
endGroup()
140135
info('> Working tree clean. Nothing to commit.')
141136
}
142-
})().catch((e) => {
143-
endGroup()
144-
setFailed(e)
145-
})
137+
})()
138+
.then(logOutputs)
139+
.catch((e) => {
140+
endGroup()
141+
logOutputs()
142+
setFailed(e)
143+
})
146144

147145
async function checkInputs() {
148146
function setInput(input: Input, value: string | undefined) {
@@ -286,23 +284,6 @@ async function checkInputs() {
286284
}
287285
// #endregion
288286
}
289-
290-
function getInput(name: Input) {
291-
return getInputCore(name)
292-
}
293-
294-
function parseBool(value: any) {
295-
try {
296-
const parsed = JSON.parse(value)
297-
if (typeof parsed == 'boolean') return parsed
298-
} catch {}
299-
}
300-
301-
function log(err: any | Error, data?: any) {
302-
if (data) console.log(data)
303-
if (err) error(err)
304-
}
305-
306287
function add({
307288
logWarning = true,
308289
ignoreErrors = false
@@ -342,3 +323,9 @@ function remove({
342323
else throw e
343324
})
344325
}
326+
327+
function logOutputs() {
328+
startGroup('Outputs:')
329+
info(JSON.stringify(outputs))
330+
endGroup()
331+
}

src/util.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import core from '@actions/core'
2+
3+
export type Input =
4+
| 'add'
5+
| 'author_name'
6+
| 'author_email'
7+
| 'branch'
8+
| 'cwd'
9+
| 'message'
10+
| 'pull_strategy'
11+
| 'push'
12+
| 'remove'
13+
| 'signoff'
14+
| 'tag'
15+
16+
export const outputs = {
17+
committed: 'false',
18+
pushed: 'false',
19+
tagged: 'false'
20+
}
21+
export type Output = keyof typeof outputs
22+
23+
export function getInput(name: Input) {
24+
return core.getInput(name)
25+
}
26+
27+
export function log(err: any | Error, data?: any) {
28+
if (data) console.log(data)
29+
if (err) core.error(err)
30+
}
31+
32+
export function parseBool(value: any) {
33+
try {
34+
const parsed = JSON.parse(value)
35+
if (typeof parsed == 'boolean') return parsed
36+
} catch {}
37+
}
38+
39+
export function setOutput(name: Output, value: 'true' | 'false') {
40+
core.setOutput(name, value)
41+
}
42+
for (const key in outputs) setOutput(key as Output, outputs[key])

0 commit comments

Comments
 (0)