|
| 1 | +import * as core from '@actions/core' |
| 2 | +import { getUserInfo, parseInputArray, readJSON } from './util' |
| 3 | + |
| 4 | +export async function checkInputs() { |
| 5 | + function setInput(input: input, value: string | undefined) { |
| 6 | + if (value) return (process.env[`INPUT_${input.toUpperCase()}`] = value) |
| 7 | + else return delete process.env[`INPUT_${input.toUpperCase()}`] |
| 8 | + } |
| 9 | + function setDefault(input: input, value: string) { |
| 10 | + if (!getInput(input)) setInput(input, value) |
| 11 | + return getInput(input) |
| 12 | + } |
| 13 | + |
| 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 | + |
| 22 | + // #region add, remove |
| 23 | + if (!getInput('add') && !getInput('remove')) |
| 24 | + throw new Error( |
| 25 | + "Both 'add' and 'remove' are empty, the action has nothing to do." |
| 26 | + ) |
| 27 | + |
| 28 | + if (getInput('add')) { |
| 29 | + const parsed = parseInputArray(getInput('add')) |
| 30 | + if (parsed.length == 1) |
| 31 | + core.info('Add input parsed as single string, running 1 git add command.') |
| 32 | + else if (parsed.length > 1) |
| 33 | + core.info( |
| 34 | + `Add input parsed as string array, running ${parsed.length} git add commands.` |
| 35 | + ) |
| 36 | + else core.setFailed('Add input: array length < 1') |
| 37 | + } |
| 38 | + if (getInput('remove')) { |
| 39 | + const parsed = parseInputArray(getInput('remove') || '') |
| 40 | + if (parsed.length == 1) |
| 41 | + core.info( |
| 42 | + 'Remove input parsed as single string, running 1 git rm command.' |
| 43 | + ) |
| 44 | + else if (parsed.length > 1) |
| 45 | + core.info( |
| 46 | + `Remove input parsed as string array, running ${parsed.length} git rm commands.` |
| 47 | + ) |
| 48 | + else core.setFailed('Remove input: array length < 1') |
| 49 | + } |
| 50 | + // #endregion |
| 51 | + |
| 52 | + // #region default_author |
| 53 | + const default_author_valid = ['github_actor', 'user_info', 'github_actions'] |
| 54 | + if (!default_author_valid.includes(getInput('default_author'))) |
| 55 | + throw new Error( |
| 56 | + `'${getInput( |
| 57 | + 'default_author' |
| 58 | + )}' is not a valid value for default_author. Valid values: ${default_author_valid.join( |
| 59 | + ', ' |
| 60 | + )}` |
| 61 | + ) |
| 62 | + // #endregion |
| 63 | + |
| 64 | + // #region author_name, author_email |
| 65 | + let name, email |
| 66 | + switch (getInput('default_author')) { |
| 67 | + case 'github_actor': { |
| 68 | + name = process.env.GITHUB_ACTOR |
| 69 | + email = `${process.env.GITHUB_ACTOR}@users.noreply.github.com` |
| 70 | + break |
| 71 | + } |
| 72 | + |
| 73 | + case 'user_info': { |
| 74 | + if (!getInput('author_name') || !getInput('author_email')) { |
| 75 | + const res = await getUserInfo(process.env.GITHUB_ACTOR) |
| 76 | + if (!res?.name) |
| 77 | + core.warning("Couldn't fetch author name, filling with github_actor.") |
| 78 | + if (!res?.email) |
| 79 | + core.warning( |
| 80 | + "Couldn't fetch author email, filling with github_actor." |
| 81 | + ) |
| 82 | + |
| 83 | + res?.name && (name = res?.name) |
| 84 | + res?.email && (email = res.email) |
| 85 | + if (name && email) break |
| 86 | + } |
| 87 | + |
| 88 | + !name && (name = process.env.GITHUB_ACTOR) |
| 89 | + !email && (email = `${process.env.GITHUB_ACTOR}@users.noreply.github.com`) |
| 90 | + break |
| 91 | + } |
| 92 | + |
| 93 | + case 'github_actions': { |
| 94 | + name = 'github-actions' |
| 95 | + email = '41898282+github-actions[bot]@users.noreply.github.com' |
| 96 | + break |
| 97 | + } |
| 98 | + |
| 99 | + default: |
| 100 | + throw new Error( |
| 101 | + 'This should not happen, please contact the author of this action. (checkInputs.author)' |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + setDefault('author_name', name) |
| 106 | + setDefault('author_email', email) |
| 107 | + core.info( |
| 108 | + `> Using '${getInput('author_name')} <${getInput( |
| 109 | + 'author_email' |
| 110 | + )}>' as author.` |
| 111 | + ) |
| 112 | + // #endregion |
| 113 | + |
| 114 | + // #region committer_name, committer_email |
| 115 | + if (getInput('committer_name') || getInput('committer_email')) |
| 116 | + core.info( |
| 117 | + `> Using custom committer info: ${ |
| 118 | + getInput('committer_name') || |
| 119 | + getInput('author_name') + ' [from author info]' |
| 120 | + } <${ |
| 121 | + getInput('committer_email') || |
| 122 | + getInput('author_email') + ' [from author info]' |
| 123 | + }>` |
| 124 | + ) |
| 125 | + |
| 126 | + setDefault('committer_name', getInput('author_name')) |
| 127 | + setDefault('committer_email', getInput('author_email')) |
| 128 | + core.debug( |
| 129 | + `Committer: ${getInput('committer_name')} <${getInput('committer_email')}>` |
| 130 | + ) |
| 131 | + // #endregion |
| 132 | + |
| 133 | + // #region message |
| 134 | + setDefault( |
| 135 | + 'message', |
| 136 | + `Commit from GitHub Actions (${process.env.GITHUB_WORKFLOW})` |
| 137 | + ) |
| 138 | + core.info(`> Using "${getInput('message')}" as commit message.`) |
| 139 | + // #endregion |
| 140 | + |
| 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 | + |
| 147 | + // #region branch_mode |
| 148 | + const branch_mode_valid = ['throw', 'create'] |
| 149 | + if (!branch_mode_valid.includes(getInput('branch_mode'))) |
| 150 | + throw new Error( |
| 151 | + `"${getInput( |
| 152 | + 'branch_mode' |
| 153 | + )}" is not a valid value for the 'branch_mode' input. Valid values are: ${branch_mode_valid.join( |
| 154 | + ', ' |
| 155 | + )}` |
| 156 | + ) |
| 157 | + // #endregion |
| 158 | + |
| 159 | + // #region pathspec_error_handling |
| 160 | + const peh_valid = ['ignore', 'exitImmediately', 'exitAtEnd'] |
| 161 | + if (!peh_valid.includes(getInput('pathspec_error_handling'))) |
| 162 | + throw new Error( |
| 163 | + `"${getInput( |
| 164 | + 'pathspec_error_handling' |
| 165 | + )}" is not a valid value for the 'pathspec_error_handling' input. Valid values are: ${peh_valid.join( |
| 166 | + ', ' |
| 167 | + )}` |
| 168 | + ) |
| 169 | + // #endregion |
| 170 | + |
| 171 | + // #region pull |
| 172 | + if (getInput('pull') == 'NO-PULL') |
| 173 | + core.debug("NO-PULL found: won't pull from remote.") |
| 174 | + // #endregion |
| 175 | + |
| 176 | + // #region push |
| 177 | + if (getInput('push')) { |
| 178 | + // It has to be either 'true', 'false', or any other string (use as arguments) |
| 179 | + let value: string | boolean |
| 180 | + |
| 181 | + try { |
| 182 | + value = getInput('push', true) |
| 183 | + } catch { |
| 184 | + value = getInput('push') |
| 185 | + } |
| 186 | + |
| 187 | + core.debug(`Current push option: '${value}' (parsed as ${typeof value})`) |
| 188 | + } |
| 189 | + // #endregion |
| 190 | + |
| 191 | + // #region github_token |
| 192 | + if (!getInput('github_token')) |
| 193 | + core.warning( |
| 194 | + 'No github_token has been detected, the action may fail if it needs to use the API' |
| 195 | + ) |
| 196 | + // #endregion |
| 197 | +} |
| 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 | +} |
0 commit comments