feat: support new remote branch creation (#329)

* fix: don't pull on new branch creation

* fix: don't show useless errors on branch creation

* feat: add branch_mode input

* fix: properly exit after fialed input check

* docs(README): document new input
This commit is contained in:
Federico Grandi
2021-12-03 19:42:39 +01:00
committed by GitHub
parent f6438de30b
commit 7d94da29b1
5 changed files with 46 additions and 8 deletions

View File

@@ -31,6 +31,10 @@ Add a step like this to your workflow:
# Default: the branch that triggered the run # Default: the branch that triggered the run
branch: some-branch branch: some-branch
# How the action should behave when the targeted branch is missing: "create" will create a new one on the remote, "throw" will exit
# Default: throw
branch_mode: create
# The name of the custom committer you want to use, if different from the author of the commit. # The name of the custom committer you want to use, if different from the author of the commit.
# Default: the name of the author (set with either author_name or default_author) # Default: the name of the author (set with either author_name or default_author)
committer_name: Committer Name committer_name: Committer Name

View File

@@ -15,6 +15,10 @@ inputs:
branch: branch:
description: Name of the branch to use, if different from the one that triggered the workflow description: Name of the branch to use, if different from the one that triggered the workflow
required: false required: false
branch_mode:
description: How the action should behave when the targeted branch is missing
required: false
default: throw
committer_name: committer_name:
description: The name of the custom committer you want to use description: The name of the custom committer you want to use
required: false required: false

4
lib/index.js generated

File diff suppressed because one or more lines are too long

View File

@@ -20,7 +20,7 @@ const exitErrors: Error[] = []
core.info(`Running in ${baseDir}`) core.info(`Running in ${baseDir}`)
;(async () => { ;(async () => {
await checkInputs().catch(core.setFailed) await checkInputs()
core.startGroup('Internal logs') core.startGroup('Internal logs')
core.info('> Staging files...') core.info('> Staging files...')
@@ -57,13 +57,30 @@ core.info(`Running in ${baseDir}`)
await git.fetch(['--tags', '--force'], log) await git.fetch(['--tags', '--force'], log)
core.info('> Switching/creating branch...') core.info('> Switching/creating branch...')
/** This should store whether the branch already existed, of if a new one was created */
let branchType!: 'existing' | 'new'
await git await git
.checkout(getInput('branch'), undefined, log) .checkout(getInput('branch'))
.catch(() => git.checkoutLocalBranch(getInput('branch'), log)) .then(() => (branchType = 'existing'))
.catch(() => {
if (getInput('branch_mode') == 'create') {
log(
undefined,
`'${getInput('branch')}' branch not found, trying to create one.`
)
branchType = 'new'
return git.checkoutLocalBranch(getInput('branch'), log)
} else throw `'${getInput('branch')}' branch not found.`
})
// The current default value is set here. /*
// When the depreacted pull_strategy input is removed, the default should be set via the action manifest. The current default value is set here: it will not pull when it has
const pull = getInput('pull') || getInput('pull_strategy') || '--no-rebase' created a new branch, it will use --rebase when the branch already existed
*/
const pull =
getInput('pull') ||
getInput('pull_strategy') ||
(branchType == 'new' ? 'NO-PULL' : '--no-rebase')
if (pull == 'NO-PULL') core.info('> Not pulling from repo.') if (pull == 'NO-PULL') core.info('> Not pulling from repo.')
else { else {
core.info('> Pulling from remote...') core.info('> Pulling from remote...')
@@ -338,6 +355,18 @@ async function checkInputs() {
core.info(`> Running for a PR, the action will use '${branch}' as ref.`) core.info(`> Running for a PR, the action will use '${branch}' as ref.`)
// #endregion // #endregion
// #region branch_mode
const branch_mode_valid = ['throw', 'create']
if (!branch_mode_valid.includes(getInput('branch_mode')))
throw new Error(
`"${getInput(
'branch_mode'
)}" is not a valid value for the 'branch_mode' input. Valid values are: ${branch_mode_valid.join(
', '
)}`
)
// #endregion
// #region pathspec_error_handling // #region pathspec_error_handling
const peh_valid = ['ignore', 'exitImmediately', 'exitAtEnd'] const peh_valid = ['ignore', 'exitImmediately', 'exitAtEnd']
if (!peh_valid.includes(getInput('pathspec_error_handling'))) if (!peh_valid.includes(getInput('pathspec_error_handling')))

View File

@@ -8,6 +8,7 @@ interface InputTypes {
author_name: string author_name: string
author_email: string author_email: string
branch: string branch: string
branch_mode: 'throw' | 'create'
committer_name: string committer_name: string
committer_email: string committer_email: string
cwd: string cwd: string