Skip to content

Commit d4d3992

Browse files
committed
fix!: don't switch branch unless explicitly told so
1 parent 249b415 commit d4d3992

File tree

4 files changed

+86
-115
lines changed

4 files changed

+86
-115
lines changed

action.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ inputs:
1313
description: The email of the user that will be displayed as the author of the commit
1414
required: false
1515
branch:
16-
description: Name of the branch to use, if different from the one that triggered the workflow
16+
description: Name of the branch to switch to.
1717
required: false
1818
branch_mode:
19-
description: How the action should behave when the targeted branch is missing
19+
description: How the action should behave when the targeted branch is missing ("throw" or "create")
2020
required: false
21-
default: throw
21+
default: create
2222
commit:
2323
description: Additional arguments for the git commit command
2424
required: false
@@ -44,7 +44,7 @@ inputs:
4444
required: false
4545
default: ignore
4646
pull:
47-
description: Arguments for the git pull command. Use NO-PULL to avoid the action pulling at all.
47+
description: Arguments for the git pull command. By default, the action does not pull.
4848
required: false
4949
push:
5050
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)

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/io.ts

Lines changed: 68 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,72 @@
11
import * as core from '@actions/core'
2-
import { getUserInfo, parseInputArray, readJSON } from './util'
2+
import { getUserInfo, parseInputArray } from './util'
3+
4+
interface InputTypes {
5+
add: string
6+
author_name: string
7+
author_email: string
8+
branch: string | undefined
9+
branch_mode: 'throw' | 'create'
10+
commit: string | undefined
11+
committer_name: string
12+
committer_email: string
13+
cwd: string
14+
default_author: 'github_actor' | 'user_info' | 'github_actions'
15+
message: string
16+
pathspec_error_handling: 'ignore' | 'exitImmediately' | 'exitAtEnd'
17+
pull: string | undefined
18+
push: string
19+
remove: string | undefined
20+
tag: string | undefined
21+
22+
github_token: string | undefined
23+
}
24+
export type input = keyof InputTypes
25+
26+
interface OutputTypes {
27+
committed: 'true' | 'false'
28+
commit_sha: string | undefined
29+
pushed: 'true' | 'false'
30+
tagged: 'true' | 'false'
31+
}
32+
export type output = keyof OutputTypes
33+
34+
export const outputs: OutputTypes = {
35+
committed: 'false',
36+
commit_sha: undefined,
37+
pushed: 'false',
38+
tagged: 'false'
39+
}
40+
// Setup default output values
41+
Object.entries(outputs).forEach(([name, value]) => core.setOutput(name, value))
42+
43+
export function getInput<T extends input>(name: T, parseAsBool: true): boolean
44+
export function getInput<T extends input>(
45+
name: T,
46+
parseAsBool?: false
47+
): InputTypes[T]
48+
export function getInput<T extends input>(
49+
name: T,
50+
parseAsBool = false
51+
): InputTypes[T] | boolean {
52+
if (parseAsBool) return core.getBooleanInput(name)
53+
// @ts-expect-error
54+
return core.getInput(name)
55+
}
56+
57+
export function setOutput<T extends output>(name: T, value: OutputTypes[T]) {
58+
core.debug(`Setting output: ${name}=${value}`)
59+
outputs[name] = value
60+
core.setOutput(name, value)
61+
}
62+
63+
export function logOutputs() {
64+
core.startGroup('Outputs')
65+
for (const key in outputs) {
66+
core.info(`${key}: ${outputs[key]}`)
67+
}
68+
core.endGroup()
69+
}
370

471
export async function checkInputs() {
572
function setInput(input: input, value: string | undefined) {
@@ -11,14 +78,6 @@ export async function checkInputs() {
1178
return getInput(input)
1279
}
1380

14-
const eventPath = process.env.GITHUB_EVENT_PATH,
15-
event = eventPath && readJSON(eventPath)
16-
17-
const isPR = process.env.GITHUB_EVENT_NAME?.includes('pull_request'),
18-
defaultBranch = isPR
19-
? (event?.pull_request?.head?.ref as string)
20-
: process.env.GITHUB_REF?.substring(11)
21-
2281
// #region add, remove
2382
if (!getInput('add') && !getInput('remove'))
2483
throw new Error(
@@ -138,12 +197,6 @@ export async function checkInputs() {
138197
core.info(`> Using "${getInput('message')}" as commit message.`)
139198
// #endregion
140199

141-
// #region branch
142-
const branch = setDefault('branch', defaultBranch || '')
143-
if (isPR)
144-
core.info(`> Running for a PR, the action will use '${branch}' as ref.`)
145-
// #endregion
146-
147200
// #region branch_mode
148201
const branch_mode_valid = ['throw', 'create']
149202
if (!branch_mode_valid.includes(getInput('branch_mode')))
@@ -168,11 +221,6 @@ export async function checkInputs() {
168221
)
169222
// #endregion
170223

171-
// #region pull
172-
if (getInput('pull') == 'NO-PULL')
173-
core.debug("NO-PULL found: won't pull from remote.")
174-
// #endregion
175-
176224
// #region push
177225
if (getInput('push')) {
178226
// It has to be either 'true', 'false', or any other string (use as arguments)
@@ -195,70 +243,3 @@ export async function checkInputs() {
195243
)
196244
// #endregion
197245
}
198-
199-
interface InputTypes {
200-
add: string
201-
author_name: string
202-
author_email: string
203-
branch: string
204-
branch_mode: 'throw' | 'create'
205-
commit: string | undefined
206-
committer_name: string
207-
committer_email: string
208-
cwd: string
209-
default_author: 'github_actor' | 'user_info' | 'github_actions'
210-
message: string
211-
pathspec_error_handling: 'ignore' | 'exitImmediately' | 'exitAtEnd'
212-
pull: string | undefined
213-
push: string
214-
remove: string | undefined
215-
tag: string | undefined
216-
217-
github_token: string | undefined
218-
}
219-
export type input = keyof InputTypes
220-
221-
interface OutputTypes {
222-
committed: 'true' | 'false'
223-
commit_sha: string | undefined
224-
pushed: 'true' | 'false'
225-
tagged: 'true' | 'false'
226-
}
227-
export type output = keyof OutputTypes
228-
229-
export const outputs: OutputTypes = {
230-
committed: 'false',
231-
commit_sha: undefined,
232-
pushed: 'false',
233-
tagged: 'false'
234-
}
235-
// Setup default output values
236-
Object.entries(outputs).forEach(([name, value]) => core.setOutput(name, value))
237-
238-
export function getInput<T extends input>(name: T, parseAsBool: true): boolean
239-
export function getInput<T extends input>(
240-
name: T,
241-
parseAsBool?: false
242-
): InputTypes[T]
243-
export function getInput<T extends input>(
244-
name: T,
245-
parseAsBool = false
246-
): InputTypes[T] | boolean {
247-
if (parseAsBool) return core.getBooleanInput(name)
248-
// @ts-expect-error
249-
return core.getInput(name)
250-
}
251-
252-
export function setOutput<T extends output>(name: T, value: OutputTypes[T]) {
253-
core.debug(`Setting output: ${name}=${value}`)
254-
outputs[name] = value
255-
core.setOutput(name, value)
256-
}
257-
258-
export function logOutputs() {
259-
core.startGroup('Outputs')
260-
for (const key in outputs) {
261-
core.info(`${key}: ${outputs[key]}`)
262-
}
263-
core.endGroup()
264-
}

src/main.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,41 +47,31 @@ core.info(`Running in ${baseDir}`)
4747

4848
await git.fetch(['--tags', '--force'], log)
4949

50-
core.info('> Switching/creating branch...')
51-
/** This should store whether the branch already existed, of if a new one was created */
52-
let branchType!: 'existing' | 'new'
53-
await git
54-
.checkout(getInput('branch'))
55-
.then(() => (branchType = 'existing'))
56-
.catch(() => {
50+
const targetBranch = getInput('branch')
51+
if (targetBranch) {
52+
await git.checkout(targetBranch).catch(() => {
5753
if (getInput('branch_mode') == 'create') {
5854
log(
5955
undefined,
60-
`'${getInput('branch')}' branch not found, trying to create one.`
56+
`'${targetBranch}' branch not found, trying to create one.`
6157
)
62-
branchType = 'new'
63-
return git.checkoutLocalBranch(getInput('branch'), log)
64-
} else throw `'${getInput('branch')}' branch not found.`
58+
return git.checkoutLocalBranch(targetBranch, log)
59+
} else throw `'${targetBranch}' branch not found.`
6560
})
61+
}
6662

6763
/*
6864
The current default value is set here: it will not pull when it has
6965
created a new branch, it will use --rebase when the branch already existed
7066
*/
71-
const pull =
72-
getInput('pull') || (branchType == 'new' ? 'NO-PULL' : '--no-rebase')
73-
if (pull == 'NO-PULL') core.info('> Not pulling from repo.')
74-
else {
67+
const pullOption = getInput('pull')
68+
if (pullOption) {
7569
core.info('> Pulling from remote...')
76-
core.debug(`Current git pull arguments: ${pull}`)
70+
core.debug(`Current git pull arguments: ${pullOption}`)
7771
await git
7872
.fetch(undefined, log)
79-
.pull(undefined, undefined, matchGitArgs(pull), log)
80-
}
81-
82-
core.info('> Re-staging files...')
83-
if (getInput('add')) await add('all')
84-
if (getInput('remove')) await remove('all')
73+
.pull(undefined, undefined, matchGitArgs(pullOption), log)
74+
} else core.info('> Not pulling from repo.')
8575

8676
core.info('> Creating commit...')
8777
await git.commit(

0 commit comments

Comments
 (0)