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:
41
src/main.ts
41
src/main.ts
@@ -20,7 +20,7 @@ const exitErrors: Error[] = []
|
||||
|
||||
core.info(`Running in ${baseDir}`)
|
||||
;(async () => {
|
||||
await checkInputs().catch(core.setFailed)
|
||||
await checkInputs()
|
||||
|
||||
core.startGroup('Internal logs')
|
||||
core.info('> Staging files...')
|
||||
@@ -57,13 +57,30 @@ core.info(`Running in ${baseDir}`)
|
||||
await git.fetch(['--tags', '--force'], log)
|
||||
|
||||
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
|
||||
.checkout(getInput('branch'), undefined, log)
|
||||
.catch(() => git.checkoutLocalBranch(getInput('branch'), log))
|
||||
.checkout(getInput('branch'))
|
||||
.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.
|
||||
const pull = getInput('pull') || getInput('pull_strategy') || '--no-rebase'
|
||||
/*
|
||||
The current default value is set here: it will not pull when it has
|
||||
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.')
|
||||
else {
|
||||
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.`)
|
||||
// #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
|
||||
const peh_valid = ['ignore', 'exitImmediately', 'exitAtEnd']
|
||||
if (!peh_valid.includes(getInput('pathspec_error_handling')))
|
||||
|
||||
@@ -8,6 +8,7 @@ interface InputTypes {
|
||||
author_name: string
|
||||
author_email: string
|
||||
branch: string
|
||||
branch_mode: 'throw' | 'create'
|
||||
committer_name: string
|
||||
committer_email: string
|
||||
cwd: string
|
||||
|
||||
Reference in New Issue
Block a user