Skip to content

Commit 7d94da2

Browse files
authored
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
1 parent f6438de commit 7d94da2

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Add a step like this to your workflow:
3131
# Default: the branch that triggered the run
3232
branch: some-branch
3333

34+
# How the action should behave when the targeted branch is missing: "create" will create a new one on the remote, "throw" will exit
35+
# Default: throw
36+
branch_mode: create
37+
3438
# The name of the custom committer you want to use, if different from the author of the commit.
3539
# Default: the name of the author (set with either author_name or default_author)
3640
committer_name: Committer Name

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ inputs:
1515
branch:
1616
description: Name of the branch to use, if different from the one that triggered the workflow
1717
required: false
18+
branch_mode:
19+
description: How the action should behave when the targeted branch is missing
20+
required: false
21+
default: throw
1822
committer_name:
1923
description: The name of the custom committer you want to use
2024
required: false

lib/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const exitErrors: Error[] = []
2020

2121
core.info(`Running in ${baseDir}`)
2222
;(async () => {
23-
await checkInputs().catch(core.setFailed)
23+
await checkInputs()
2424

2525
core.startGroup('Internal logs')
2626
core.info('> Staging files...')
@@ -57,13 +57,30 @@ core.info(`Running in ${baseDir}`)
5757
await git.fetch(['--tags', '--force'], log)
5858

5959
core.info('> Switching/creating branch...')
60+
/** This should store whether the branch already existed, of if a new one was created */
61+
let branchType!: 'existing' | 'new'
6062
await git
61-
.checkout(getInput('branch'), undefined, log)
62-
.catch(() => git.checkoutLocalBranch(getInput('branch'), log))
63-
64-
// The current default value is set here.
65-
// When the depreacted pull_strategy input is removed, the default should be set via the action manifest.
66-
const pull = getInput('pull') || getInput('pull_strategy') || '--no-rebase'
63+
.checkout(getInput('branch'))
64+
.then(() => (branchType = 'existing'))
65+
.catch(() => {
66+
if (getInput('branch_mode') == 'create') {
67+
log(
68+
undefined,
69+
`'${getInput('branch')}' branch not found, trying to create one.`
70+
)
71+
branchType = 'new'
72+
return git.checkoutLocalBranch(getInput('branch'), log)
73+
} else throw `'${getInput('branch')}' branch not found.`
74+
})
75+
76+
/*
77+
The current default value is set here: it will not pull when it has
78+
created a new branch, it will use --rebase when the branch already existed
79+
*/
80+
const pull =
81+
getInput('pull') ||
82+
getInput('pull_strategy') ||
83+
(branchType == 'new' ? 'NO-PULL' : '--no-rebase')
6784
if (pull == 'NO-PULL') core.info('> Not pulling from repo.')
6885
else {
6986
core.info('> Pulling from remote...')
@@ -338,6 +355,18 @@ async function checkInputs() {
338355
core.info(`> Running for a PR, the action will use '${branch}' as ref.`)
339356
// #endregion
340357

358+
// #region branch_mode
359+
const branch_mode_valid = ['throw', 'create']
360+
if (!branch_mode_valid.includes(getInput('branch_mode')))
361+
throw new Error(
362+
`"${getInput(
363+
'branch_mode'
364+
)}" is not a valid value for the 'branch_mode' input. Valid values are: ${branch_mode_valid.join(
365+
', '
366+
)}`
367+
)
368+
// #endregion
369+
341370
// #region pathspec_error_handling
342371
const peh_valid = ['ignore', 'exitImmediately', 'exitAtEnd']
343372
if (!peh_valid.includes(getInput('pathspec_error_handling')))

src/util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ interface InputTypes {
88
author_name: string
99
author_email: string
1010
branch: string
11+
branch_mode: 'throw' | 'create'
1112
committer_name: string
1213
committer_email: string
1314
cwd: string

0 commit comments

Comments
 (0)