Skip to content

Commit

Permalink
feat: add outputs
Browse files Browse the repository at this point in the history
re #96
  • Loading branch information
EndBug committed Dec 5, 2020
1 parent 28fa18d commit fe1fb21
Show file tree
Hide file tree
Showing 4 changed files with 9,890 additions and 41 deletions.
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ inputs:
description: Arguments for the git tag command (the tag name always needs to be the first word not preceded by a hyphen)
required: false

outputs:
committed:
description: Whether the action has created a commit.
pushed:
description: Whether the action has pushed to the remote.
tagged:
description: Whether the action has created a tag.

runs:
using: node12
main: lib/index.js
Expand Down
9,814 changes: 9,813 additions & 1 deletion lib/index.js

Large diffs are not rendered by default.

67 changes: 27 additions & 40 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
import {
info,
setFailed,
getInput as getInputCore,
warning,
debug,
startGroup,
endGroup,
error
endGroup
} from '@actions/core'
import axios from 'axios'
import path from 'path'
import simpleGit, { Response } from 'simple-git'

type Input =
| 'add'
| 'author_name'
| 'author_email'
| 'branch'
| 'cwd'
| 'message'
| 'pull_strategy'
| 'push'
| 'remove'
| 'signoff'
| 'tag'
import { getInput, Input, log, outputs, parseBool, setOutput } from './util'

const baseDir = path.join(process.cwd(), getInput('cwd') || '')
const git = simpleGit({ baseDir })
Expand Down Expand Up @@ -92,12 +78,18 @@ console.log(`Running in ${baseDir}`)
}
: {})
},
log
(err, data?) => {
if (data) setOutput('committed', 'true')
return log(err, data)
}
)

if (getInput('tag')) {
info('> Tagging commit...')
await git.tag(getInput('tag').split(' '), log)
await git.tag(getInput('tag').split(' '), (err, data?) => {
if (data) setOutput('tagged', 'true')
return log(err, data)
})
} else info('> No tag info provided.')

if (getInput('push')) {
Expand All @@ -106,7 +98,10 @@ console.log(`Running in ${baseDir}`)
'origin',
getInput('branch'),
{ '--set-upstream': null },
log
(err, data?) => {
if (data) setOutput('pushed', 'true')
return log(err, data)
}
)

if (getInput('tag')) {
Expand Down Expand Up @@ -139,10 +134,13 @@ console.log(`Running in ${baseDir}`)
endGroup()
info('> Working tree clean. Nothing to commit.')
}
})().catch((e) => {
endGroup()
setFailed(e)
})
})()
.then(logOutputs)
.catch((e) => {
endGroup()
logOutputs()
setFailed(e)
})

async function checkInputs() {
function setInput(input: Input, value: string | undefined) {
Expand Down Expand Up @@ -286,23 +284,6 @@ async function checkInputs() {
}
// #endregion
}

function getInput(name: Input) {
return getInputCore(name)
}

function parseBool(value: any) {
try {
const parsed = JSON.parse(value)
if (typeof parsed == 'boolean') return parsed
} catch {}
}

function log(err: any | Error, data?: any) {
if (data) console.log(data)
if (err) error(err)
}

function add({
logWarning = true,
ignoreErrors = false
Expand Down Expand Up @@ -342,3 +323,9 @@ function remove({
else throw e
})
}

function logOutputs() {
startGroup('Outputs:')
info(JSON.stringify(outputs))
endGroup()
}
42 changes: 42 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import core from '@actions/core'

export type Input =
| 'add'
| 'author_name'
| 'author_email'
| 'branch'
| 'cwd'
| 'message'
| 'pull_strategy'
| 'push'
| 'remove'
| 'signoff'
| 'tag'

export const outputs = {
committed: 'false',
pushed: 'false',
tagged: 'false'
}
export type Output = keyof typeof outputs

export function getInput(name: Input) {
return core.getInput(name)
}

export function log(err: any | Error, data?: any) {
if (data) console.log(data)
if (err) core.error(err)
}

export function parseBool(value: any) {
try {
const parsed = JSON.parse(value)
if (typeof parsed == 'boolean') return parsed
} catch {}
}

export function setOutput(name: Output, value: 'true' | 'false') {
core.setOutput(name, value)
}
for (const key in outputs) setOutput(key as Output, outputs[key])

0 comments on commit fe1fb21

Please sign in to comment.