Skip to content

Commit

Permalink
feat(push): add push option
Browse files Browse the repository at this point in the history
re #86
  • Loading branch information
EndBug committed Nov 11, 2020
1 parent 0c6f4a1 commit b369695
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ inputs:
description: The flag used on the pull strategy
required: false
default: '--no-rebase'
push:
description: Whether to push the commit to the repo
required: false
remove:
description: Arguments for the git rm command
required: false
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// WARNING: this file is auto-generated by scripts/inputs.ts (npm run inputs), any manual edit will be overwritten.

export type Input = 'add' | 'author_name' | 'author_email' | 'branch' | 'cwd' | 'message' | 'pull_strategy' | 'remove' | 'signoff' | 'tag'
export type Input = 'add' | 'author_name' | 'author_email' | 'branch' | 'cwd' | 'message' | 'pull_strategy' | 'push' | 'remove' | 'signoff' | 'tag'
69 changes: 49 additions & 20 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,23 @@ console.log(`Running in ${baseDir}`);
await git.tag(getInput('tag').split(' '), log)
} else info('> No tag info provided.')

info('> Pushing commit to repo...')
await git.push('origin', getInput('branch'), { '--set-upstream': null }, log)

if (getInput('tag')) {
info('> Pushing tags to repo...')
await git.pushTags('origin', (e, d?) => log(undefined, e || d)).catch(() => {
info('> Tag push failed: deleting remote tag and re-pushing...')
return git.push(undefined, undefined, {
'--delete': null,
'origin': null,
[getInput('tag').split(' ').filter(w => !w.startsWith('-'))[0]]: null
}, log)
.pushTags('origin', log)
})
} else info('> No tags to push.')
if (getInput('push')) {
info('> Pushing commit to repo...')
await git.push('origin', getInput('branch'), { '--set-upstream': null }, log)

if (getInput('tag')) {
info('> Pushing tags to repo...')
await git.pushTags('origin', (e, d?) => log(undefined, e || d)).catch(() => {
info('> Tag push failed: deleting remote tag and re-pushing...')
return git.push(undefined, undefined, {
'--delete': null,
'origin': null,
[getInput('tag').split(' ').filter(w => !w.startsWith('-'))[0]]: null
}, log)
.pushTags('origin', log)
})
} else info('> No tags to push.')
} else info('> Not pushing anything.')

endGroup()
info('> Task completed.')
Expand Down Expand Up @@ -175,13 +177,32 @@ async function checkInputs() {
// #endregion

// #region signoff
if (getInput('signoff')) try {
const parsed = JSON.parse(getInput('signoff'))
if (typeof parsed == 'boolean' && !parsed)
if (getInput('signoff')) {
const parsed = parseBool(getInput('signoff'))

if (parsed === undefined)
throw new Error(`"${getInput('signoff')}" is not a valid value for the 'signoff' input: only "true" and "false" are allowed.`)

if (!parsed)
setInput('signoff', undefined)

debug(`Current signoff option: ${getInput('signoff')} (${typeof getInput('signoff')})`)
} catch {
throw new Error(`"${getInput('signoff')}" is not a valid value for the 'signoff' input: only "true" and "false" are allowed.`)
}

// #endregion

// #region push
setDefault('push', 'true')
if (getInput('push')) { // It's just to scope the parsed constant
const parsed = parseBool(getInput('push'))

if (parsed === undefined)
throw new Error(`"${getInput('push')}" is not a valid value for the 'push' input: only "true" and "false" are allowed.`)

if (!parsed)
setInput('push', undefined)

debug(`Current push option: ${getInput('push')} (${typeof getInput('push')})`)
}
// #endregion
}
Expand All @@ -190,6 +211,14 @@ 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)
Expand Down

0 comments on commit b369695

Please sign in to comment.