fix!: don't switch branch unless explicitly told so
This commit is contained in:
@@ -13,12 +13,12 @@ inputs:
|
|||||||
description: The email of the user that will be displayed as the author of the commit
|
description: The email of the user that will be displayed as the author of the commit
|
||||||
required: false
|
required: false
|
||||||
branch:
|
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
|
required: false
|
||||||
branch_mode:
|
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
|
required: false
|
||||||
default: throw
|
default: create
|
||||||
commit:
|
commit:
|
||||||
description: Additional arguments for the git commit command
|
description: Additional arguments for the git commit command
|
||||||
required: false
|
required: false
|
||||||
@@ -44,7 +44,7 @@ inputs:
|
|||||||
required: false
|
required: false
|
||||||
default: ignore
|
default: ignore
|
||||||
pull:
|
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
|
required: false
|
||||||
push:
|
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)
|
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
4
lib/index.js
generated
File diff suppressed because one or more lines are too long
155
src/io.ts
155
src/io.ts
@@ -1,5 +1,72 @@
|
|||||||
import * as core from '@actions/core'
|
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() {
|
export async function checkInputs() {
|
||||||
function setInput(input: input, value: string | undefined) {
|
function setInput(input: input, value: string | undefined) {
|
||||||
@@ -11,14 +78,6 @@ export async function checkInputs() {
|
|||||||
return getInput(input)
|
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
|
// #region add, remove
|
||||||
if (!getInput('add') && !getInput('remove'))
|
if (!getInput('add') && !getInput('remove'))
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -138,12 +197,6 @@ export async function checkInputs() {
|
|||||||
core.info(`> Using "${getInput('message')}" as commit message.`)
|
core.info(`> Using "${getInput('message')}" as commit message.`)
|
||||||
// #endregion
|
// #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
|
// #region branch_mode
|
||||||
const branch_mode_valid = ['throw', 'create']
|
const branch_mode_valid = ['throw', 'create']
|
||||||
if (!branch_mode_valid.includes(getInput('branch_mode')))
|
if (!branch_mode_valid.includes(getInput('branch_mode')))
|
||||||
@@ -168,11 +221,6 @@ export async function checkInputs() {
|
|||||||
)
|
)
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
// #region pull
|
|
||||||
if (getInput('pull') == 'NO-PULL')
|
|
||||||
core.debug("NO-PULL found: won't pull from remote.")
|
|
||||||
// #endregion
|
|
||||||
|
|
||||||
// #region push
|
// #region push
|
||||||
if (getInput('push')) {
|
if (getInput('push')) {
|
||||||
// It has to be either 'true', 'false', or any other string (use as arguments)
|
// It has to be either 'true', 'false', or any other string (use as arguments)
|
||||||
@@ -195,70 +243,3 @@ export async function checkInputs() {
|
|||||||
)
|
)
|
||||||
// #endregion
|
// #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()
|
|
||||||
}
|
|
||||||
|
|||||||
34
src/main.ts
34
src/main.ts
@@ -47,41 +47,31 @@ core.info(`Running in ${baseDir}`)
|
|||||||
|
|
||||||
await git.fetch(['--tags', '--force'], log)
|
await git.fetch(['--tags', '--force'], log)
|
||||||
|
|
||||||
core.info('> Switching/creating branch...')
|
const targetBranch = getInput('branch')
|
||||||
/** This should store whether the branch already existed, of if a new one was created */
|
if (targetBranch) {
|
||||||
let branchType!: 'existing' | 'new'
|
await git.checkout(targetBranch).catch(() => {
|
||||||
await git
|
|
||||||
.checkout(getInput('branch'))
|
|
||||||
.then(() => (branchType = 'existing'))
|
|
||||||
.catch(() => {
|
|
||||||
if (getInput('branch_mode') == 'create') {
|
if (getInput('branch_mode') == 'create') {
|
||||||
log(
|
log(
|
||||||
undefined,
|
undefined,
|
||||||
`'${getInput('branch')}' branch not found, trying to create one.`
|
`'${targetBranch}' branch not found, trying to create one.`
|
||||||
)
|
)
|
||||||
branchType = 'new'
|
return git.checkoutLocalBranch(targetBranch, log)
|
||||||
return git.checkoutLocalBranch(getInput('branch'), log)
|
} else throw `'${targetBranch}' branch not found.`
|
||||||
} else throw `'${getInput('branch')}' branch not found.`
|
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
The current default value is set here: it will not pull when it has
|
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
|
created a new branch, it will use --rebase when the branch already existed
|
||||||
*/
|
*/
|
||||||
const pull =
|
const pullOption = getInput('pull')
|
||||||
getInput('pull') || (branchType == 'new' ? 'NO-PULL' : '--no-rebase')
|
if (pullOption) {
|
||||||
if (pull == 'NO-PULL') core.info('> Not pulling from repo.')
|
|
||||||
else {
|
|
||||||
core.info('> Pulling from remote...')
|
core.info('> Pulling from remote...')
|
||||||
core.debug(`Current git pull arguments: ${pull}`)
|
core.debug(`Current git pull arguments: ${pullOption}`)
|
||||||
await git
|
await git
|
||||||
.fetch(undefined, log)
|
.fetch(undefined, log)
|
||||||
.pull(undefined, undefined, matchGitArgs(pull), log)
|
.pull(undefined, undefined, matchGitArgs(pullOption), log)
|
||||||
}
|
} else core.info('> Not pulling from repo.')
|
||||||
|
|
||||||
core.info('> Re-staging files...')
|
|
||||||
if (getInput('add')) await add('all')
|
|
||||||
if (getInput('remove')) await remove('all')
|
|
||||||
|
|
||||||
core.info('> Creating commit...')
|
core.info('> Creating commit...')
|
||||||
await git.commit(
|
await git.commit(
|
||||||
|
|||||||
Reference in New Issue
Block a user