Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support new remote branch creation #329

Merged
merged 5 commits into from Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -31,6 +31,10 @@ Add a step like this to your workflow:
# Default: the branch that triggered the run
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.
# Default: the name of the author (set with either author_name or default_author)
committer_name: Committer Name
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Expand Up @@ -15,6 +15,10 @@ inputs:
branch:
description: Name of the branch to use, if different from the one that triggered the workflow
required: false
branch_mode:
description: How the action should behave when the targeted branch is missing
required: false
default: throw
committer_name:
description: The name of the custom committer you want to use
required: false
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js

Large diffs are not rendered by default.

43 changes: 36 additions & 7 deletions src/main.ts
Expand Up @@ -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...')
Expand Down Expand Up @@ -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))

// 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'
.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: 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...')
Expand Down Expand Up @@ -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')))
Expand Down
1 change: 1 addition & 0 deletions src/util.ts
Expand Up @@ -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
Expand Down