fix!: don't switch branch unless explicitly told so

This commit is contained in:
Federico Grandi
2021-12-09 18:35:35 +01:00
parent 249b415d1b
commit d4d39927bf
4 changed files with 86 additions and 115 deletions

View File

@@ -13,12 +13,12 @@ inputs:
description: The email of the user that will be displayed as the author of the commit
required: false
branch:
description: Name of the branch to use, if different from the one that triggered the workflow
description: Name of the branch to switch to.
required: false
branch_mode:
description: How the action should behave when the targeted branch is missing
description: How the action should behave when the targeted branch is missing ("throw" or "create")
required: false
default: throw
default: create
commit:
description: Additional arguments for the git commit command
required: false
@@ -44,7 +44,7 @@ inputs:
required: false
default: ignore
pull:
description: Arguments for the git pull command. Use NO-PULL to avoid the action pulling at all.
description: Arguments for the git pull command. By default, the action does not pull.
required: false
push:
description: Whether to push the commit and, if any, its tags to the repo. It can also be used to set the git push arguments (more info in the README)

4
lib/index.js generated

File diff suppressed because one or more lines are too long

155
src/io.ts
View File

@@ -1,5 +1,72 @@
import * as core from '@actions/core'
import { getUserInfo, parseInputArray, readJSON } from './util'
import { getUserInfo, parseInputArray } from './util'
interface InputTypes {
add: string
author_name: string
author_email: string
branch: string | undefined
branch_mode: 'throw' | 'create'
commit: string | undefined
committer_name: string
committer_email: string
cwd: string
default_author: 'github_actor' | 'user_info' | 'github_actions'
message: string
pathspec_error_handling: 'ignore' | 'exitImmediately' | 'exitAtEnd'
pull: string | undefined
push: string
remove: string | undefined
tag: string | undefined
github_token: string | undefined
}
export type input = keyof InputTypes
interface OutputTypes {
committed: 'true' | 'false'
commit_sha: string | undefined
pushed: 'true' | 'false'
tagged: 'true' | 'false'
}
export type output = keyof OutputTypes
export const outputs: OutputTypes = {
committed: 'false',
commit_sha: undefined,
pushed: 'false',
tagged: 'false'
}
// Setup default output values
Object.entries(outputs).forEach(([name, value]) => core.setOutput(name, value))
export function getInput<T extends input>(name: T, parseAsBool: true): boolean
export function getInput<T extends input>(
name: T,
parseAsBool?: false
): InputTypes[T]
export function getInput<T extends input>(
name: T,
parseAsBool = false
): InputTypes[T] | boolean {
if (parseAsBool) return core.getBooleanInput(name)
// @ts-expect-error
return core.getInput(name)
}
export function setOutput<T extends output>(name: T, value: OutputTypes[T]) {
core.debug(`Setting output: ${name}=${value}`)
outputs[name] = value
core.setOutput(name, value)
}
export function logOutputs() {
core.startGroup('Outputs')
for (const key in outputs) {
core.info(`${key}: ${outputs[key]}`)
}
core.endGroup()
}
export async function checkInputs() {
function setInput(input: input, value: string | undefined) {
@@ -11,14 +78,6 @@ export async function checkInputs() {
return getInput(input)
}
const eventPath = process.env.GITHUB_EVENT_PATH,
event = eventPath && readJSON(eventPath)
const isPR = process.env.GITHUB_EVENT_NAME?.includes('pull_request'),
defaultBranch = isPR
? (event?.pull_request?.head?.ref as string)
: process.env.GITHUB_REF?.substring(11)
// #region add, remove
if (!getInput('add') && !getInput('remove'))
throw new Error(
@@ -138,12 +197,6 @@ export async function checkInputs() {
core.info(`> Using "${getInput('message')}" as commit message.`)
// #endregion
// #region branch
const branch = setDefault('branch', defaultBranch || '')
if (isPR)
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')))
@@ -168,11 +221,6 @@ export async function checkInputs() {
)
// #endregion
// #region pull
if (getInput('pull') == 'NO-PULL')
core.debug("NO-PULL found: won't pull from remote.")
// #endregion
// #region push
if (getInput('push')) {
// It has to be either 'true', 'false', or any other string (use as arguments)
@@ -195,70 +243,3 @@ export async function checkInputs() {
)
// #endregion
}
interface InputTypes {
add: string
author_name: string
author_email: string
branch: string
branch_mode: 'throw' | 'create'
commit: string | undefined
committer_name: string
committer_email: string
cwd: string
default_author: 'github_actor' | 'user_info' | 'github_actions'
message: string
pathspec_error_handling: 'ignore' | 'exitImmediately' | 'exitAtEnd'
pull: string | undefined
push: string
remove: string | undefined
tag: string | undefined
github_token: string | undefined
}
export type input = keyof InputTypes
interface OutputTypes {
committed: 'true' | 'false'
commit_sha: string | undefined
pushed: 'true' | 'false'
tagged: 'true' | 'false'
}
export type output = keyof OutputTypes
export const outputs: OutputTypes = {
committed: 'false',
commit_sha: undefined,
pushed: 'false',
tagged: 'false'
}
// Setup default output values
Object.entries(outputs).forEach(([name, value]) => core.setOutput(name, value))
export function getInput<T extends input>(name: T, parseAsBool: true): boolean
export function getInput<T extends input>(
name: T,
parseAsBool?: false
): InputTypes[T]
export function getInput<T extends input>(
name: T,
parseAsBool = false
): InputTypes[T] | boolean {
if (parseAsBool) return core.getBooleanInput(name)
// @ts-expect-error
return core.getInput(name)
}
export function setOutput<T extends output>(name: T, value: OutputTypes[T]) {
core.debug(`Setting output: ${name}=${value}`)
outputs[name] = value
core.setOutput(name, value)
}
export function logOutputs() {
core.startGroup('Outputs')
for (const key in outputs) {
core.info(`${key}: ${outputs[key]}`)
}
core.endGroup()
}

View File

@@ -47,41 +47,31 @@ 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'))
.then(() => (branchType = 'existing'))
.catch(() => {
const targetBranch = getInput('branch')
if (targetBranch) {
await git.checkout(targetBranch).catch(() => {
if (getInput('branch_mode') == 'create') {
log(
undefined,
`'${getInput('branch')}' branch not found, trying to create one.`
`'${targetBranch}' branch not found, trying to create one.`
)
branchType = 'new'
return git.checkoutLocalBranch(getInput('branch'), log)
} else throw `'${getInput('branch')}' branch not found.`
return git.checkoutLocalBranch(targetBranch, log)
} else throw `'${targetBranch}' 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') || (branchType == 'new' ? 'NO-PULL' : '--no-rebase')
if (pull == 'NO-PULL') core.info('> Not pulling from repo.')
else {
const pullOption = getInput('pull')
if (pullOption) {
core.info('> Pulling from remote...')
core.debug(`Current git pull arguments: ${pull}`)
core.debug(`Current git pull arguments: ${pullOption}`)
await git
.fetch(undefined, log)
.pull(undefined, undefined, matchGitArgs(pull), log)
}
core.info('> Re-staging files...')
if (getInput('add')) await add('all')
if (getInput('remove')) await remove('all')
.pull(undefined, undefined, matchGitArgs(pullOption), log)
} else core.info('> Not pulling from repo.')
core.info('> Creating commit...')
await git.commit(