feat: add default_author (#181)
* feat: add deafult_author and start using actions-toolkit * fix: go back to @actions/core for logs * chore: add debug after fetching user * test: keep build not minified * fix: don't use tools.exit * fix: author_displayname * fix: check github_token * fix: back to minified builds * fix: fetch email too * fix: change option names * docs: document changes * docs: update "github-actions" example
This commit is contained in:
21
README.md
21
README.md
@@ -20,11 +20,11 @@ Add a step like this to your workflow:
|
|||||||
add: 'src'
|
add: 'src'
|
||||||
|
|
||||||
# The name of the user that will be displayed as the author of the commit
|
# The name of the user that will be displayed as the author of the commit
|
||||||
# Default: author of the commit that triggered the run
|
# Default: depends on the default_author input
|
||||||
author_name: Your Name
|
author_name: Your Name
|
||||||
|
|
||||||
# The email of the user that will be displayed as the author of the commit
|
# The email of the user that will be displayed as the author of the commit
|
||||||
# Default: author of the commit that triggered the run
|
# Default: depends on the default_author input
|
||||||
author_email: mail@example.com
|
author_email: mail@example.com
|
||||||
|
|
||||||
# Name of the branch to use, if different from the one that triggered the workflow
|
# Name of the branch to use, if different from the one that triggered the workflow
|
||||||
@@ -35,6 +35,13 @@ Add a step like this to your workflow:
|
|||||||
# Default: '.'
|
# Default: '.'
|
||||||
cwd: './path/to/the/repo'
|
cwd: './path/to/the/repo'
|
||||||
|
|
||||||
|
# Determines the way the action fills missing author name and email. Three options are available:
|
||||||
|
# - github_actor -> UserName <UserName@users.noreply.github.com>
|
||||||
|
# - user_info -> Your Display Name <your-actual@email.com>
|
||||||
|
# - github_actions -> github-actions <email associated with the github logo>
|
||||||
|
# Default:
|
||||||
|
default_author: github_actor
|
||||||
|
|
||||||
# The message for the commit
|
# The message for the commit
|
||||||
# Default: 'Commit from GitHub Actions (name of the workflow)'
|
# Default: 'Commit from GitHub Actions (name of the workflow)'
|
||||||
message: 'Your commit message'
|
message: 'Your commit message'
|
||||||
@@ -97,6 +104,8 @@ You can use the `tag` option to enter the arguments for a `git add` command. In
|
|||||||
When pushing, the action uses the token that the local git repository has been configured with: that means that if you want to change it you'll need to do it in the steps that run before this action. For example: if you set up your repo with [`actions/checkout`](https://github.com/actions/checkout/) then you have to add the token there.
|
When pushing, the action uses the token that the local git repository has been configured with: that means that if you want to change it you'll need to do it in the steps that run before this action. For example: if you set up your repo with [`actions/checkout`](https://github.com/actions/checkout/) then you have to add the token there.
|
||||||
Changing the token with which the repo is configured can be useful if you want to run CI checks on the commit pushed by this action; anyway, it has to be set up outside of this action.
|
Changing the token with which the repo is configured can be useful if you want to run CI checks on the commit pushed by this action; anyway, it has to be set up outside of this action.
|
||||||
|
|
||||||
|
The action automatically gets the GitHub token from a `github_token` input: this input should not be modified by the user, since it doesn't affect the commits as it's only used to access the GitHub API to get user info, in case they selected that option for the commit author.
|
||||||
|
|
||||||
### About `actions/checkout`
|
### About `actions/checkout`
|
||||||
|
|
||||||
The token you use when setting up the repo with this action will determine what token `add-and-commit` will use.
|
The token you use when setting up the repo with this action will determine what token `add-and-commit` will use.
|
||||||
@@ -120,7 +129,9 @@ For more info on how to use outputs, see ["Context and expression syntax"](https
|
|||||||
|
|
||||||
### Examples:
|
### Examples:
|
||||||
|
|
||||||
If you don't want to use your GitHub username for the CI commits, you can [also use the user provided by GitHub for this task](https://github.com/actions/checkout/#push-a-commit-using-the-built-in-token):
|
If you don't want to use your GitHub username for the CI commits, you can use the `default_author` option to make it appear as if it was made by "github-actions"
|
||||||
|
|
||||||
|
<img src="https://user-images.githubusercontent.com/26386270/115738624-80b51780-a38d-11eb-9bbe-77461654274c.png" height=40/>
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
on: push
|
on: push
|
||||||
@@ -130,11 +141,9 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: EndBug/add-and-commit@v7.0.0
|
- uses: EndBug/add-and-commit@v7.0.0
|
||||||
with:
|
with:
|
||||||
author_name: github-actions
|
default_author: github_actions
|
||||||
author_email: 41898282+github-actions[bot]@users.noreply.github.com
|
|
||||||
```
|
```
|
||||||
|
|
||||||
`41898282+github-actions[bot]@users.noreply.github.com` is the mail of the original GitHub Actions bot. If you use that, [the GitHub avatar is shown for the commits](https://github.community/t/github-actions-bot-email-address/17204).
|
|
||||||
|
|
||||||
Do you want to lint your JavaScript files, located in the `src` folder, with ESLint, so that fixable changes are done without your intervention? You can use a workflow like this:
|
Do you want to lint your JavaScript files, located in the `src` folder, with ESLint, so that fixable changes are done without your intervention? You can use a workflow like this:
|
||||||
|
|
||||||
|
|||||||
12
action.yml
12
action.yml
@@ -9,11 +9,9 @@ inputs:
|
|||||||
author_name:
|
author_name:
|
||||||
description: The name of the user that will be displayed as the author of the commit
|
description: The name of the user that will be displayed as the author of the commit
|
||||||
required: false
|
required: false
|
||||||
default: ${{ github.actor }}
|
|
||||||
author_email:
|
author_email:
|
||||||
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
|
||||||
default: ${{ github.actor }}@users.noreply.github.com
|
|
||||||
branch:
|
branch:
|
||||||
description: Name of the branch to use, if different from the one that triggered the workflow
|
description: Name of the branch to use, if different from the one that triggered the workflow
|
||||||
required: false
|
required: false
|
||||||
@@ -21,6 +19,10 @@ inputs:
|
|||||||
description: The directory where your repository is located. You should use actions/checkout first to set it up
|
description: The directory where your repository is located. You should use actions/checkout first to set it up
|
||||||
required: false
|
required: false
|
||||||
default: '.'
|
default: '.'
|
||||||
|
default_author:
|
||||||
|
description: How the action should fill missing author name or email.
|
||||||
|
required: false
|
||||||
|
default: 'github_actor'
|
||||||
message:
|
message:
|
||||||
description: The message for the commit
|
description: The message for the commit
|
||||||
required: false
|
required: false
|
||||||
@@ -42,6 +44,12 @@ inputs:
|
|||||||
description: Arguments for the git tag command (the tag name always needs to be the first word not preceded by a hyphen)
|
description: Arguments for the git tag command (the tag name always needs to be the first word not preceded by a hyphen)
|
||||||
required: false
|
required: false
|
||||||
|
|
||||||
|
# Input not required from the user
|
||||||
|
github_token:
|
||||||
|
description: The token used to make requests to the GitHub API. It's NOT used to make commits and should not be changed.
|
||||||
|
required: false
|
||||||
|
default: ${{ github.token }}
|
||||||
|
|
||||||
outputs:
|
outputs:
|
||||||
committed:
|
committed:
|
||||||
description: Whether the action has created a commit.
|
description: Whether the action has created a commit.
|
||||||
|
|||||||
2
lib/index.js
generated
2
lib/index.js
generated
File diff suppressed because one or more lines are too long
1302
package-lock.json
generated
1302
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -32,6 +32,7 @@
|
|||||||
"homepage": "https://github.com/EndBug/add-and-commit#readme",
|
"homepage": "https://github.com/EndBug/add-and-commit#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/core": "^1.2.7",
|
"@actions/core": "^1.2.7",
|
||||||
|
"actions-toolkit": "github:EndBug/actions-toolkit#core-actions",
|
||||||
"js-yaml": "^4.1.0",
|
"js-yaml": "^4.1.0",
|
||||||
"simple-git": "^2.27.0",
|
"simple-git": "^2.27.0",
|
||||||
"string-argv": "^0.3.1"
|
"string-argv": "^0.3.1"
|
||||||
|
|||||||
194
src/main.ts
194
src/main.ts
@@ -1,66 +1,61 @@
|
|||||||
import {
|
import * as core from '@actions/core'
|
||||||
info,
|
|
||||||
setFailed,
|
|
||||||
warning,
|
|
||||||
debug,
|
|
||||||
startGroup,
|
|
||||||
endGroup
|
|
||||||
} from '@actions/core'
|
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import simpleGit, { Response } from 'simple-git'
|
import simpleGit, { Response } from 'simple-git'
|
||||||
import YAML from 'js-yaml'
|
import YAML from 'js-yaml'
|
||||||
import {
|
import {
|
||||||
getInput,
|
getInput,
|
||||||
|
getUserInfo,
|
||||||
Input,
|
Input,
|
||||||
log,
|
log,
|
||||||
matchGitArgs,
|
matchGitArgs,
|
||||||
outputs,
|
|
||||||
parseBool,
|
parseBool,
|
||||||
setOutput
|
setOutput,
|
||||||
|
tools
|
||||||
} from './util'
|
} from './util'
|
||||||
|
|
||||||
const baseDir = path.join(process.cwd(), getInput('cwd') || '')
|
const baseDir = path.join(process.cwd(), getInput('cwd') || '')
|
||||||
const git = simpleGit({ baseDir })
|
const git = simpleGit({ baseDir })
|
||||||
console.log(`Running in ${baseDir}`)
|
core.info(`Running in ${baseDir}`)
|
||||||
;(async () => {
|
;(async () => {
|
||||||
await checkInputs().catch(setFailed)
|
await checkInputs().catch(core.setFailed)
|
||||||
|
|
||||||
startGroup('Internal logs')
|
core.startGroup('Internal logs')
|
||||||
info('> Staging files...')
|
core.info('> Staging files...')
|
||||||
|
|
||||||
if (getInput('add')) {
|
if (getInput('add')) {
|
||||||
info('> Adding files...')
|
core.info('> Adding files...')
|
||||||
await add()
|
await add()
|
||||||
} else info('> No files to add.')
|
} else core.info('> No files to add.')
|
||||||
|
|
||||||
if (getInput('remove')) {
|
if (getInput('remove')) {
|
||||||
info('> Removing files...')
|
core.info('> Removing files...')
|
||||||
await remove()
|
await remove()
|
||||||
} else info('> No files to remove.')
|
} else core.info('> No files to remove.')
|
||||||
|
|
||||||
info('> Checking for uncommitted changes in the git working tree...')
|
core.info('> Checking for uncommitted changes in the git working tree...')
|
||||||
const changedFiles = (await git.diffSummary(['--cached'])).files.length
|
const changedFiles = (await git.diffSummary(['--cached'])).files.length
|
||||||
if (changedFiles > 0) {
|
if (changedFiles > 0) {
|
||||||
info(`> Found ${changedFiles} changed files.`)
|
core.info(`> Found ${changedFiles} changed files.`)
|
||||||
|
|
||||||
await git
|
await git
|
||||||
.addConfig('user.email', getInput('author_email'), undefined, log)
|
.addConfig('user.email', getInput('author_email'), undefined, log)
|
||||||
.addConfig('user.name', getInput('author_name'), undefined, log)
|
.addConfig('user.name', getInput('author_name'), undefined, log)
|
||||||
debug(
|
core.debug(
|
||||||
'> Current git config\n' +
|
'> Current git config\n' +
|
||||||
JSON.stringify((await git.listConfig()).all, null, 2)
|
JSON.stringify((await git.listConfig()).all, null, 2)
|
||||||
)
|
)
|
||||||
|
|
||||||
await git.fetch(['--tags', '--force'], log)
|
await git.fetch(['--tags', '--force'], log)
|
||||||
|
|
||||||
info('> Switching/creating branch...')
|
core.info('> Switching/creating branch...')
|
||||||
await git
|
await git
|
||||||
.checkout(getInput('branch'), undefined, log)
|
.checkout(getInput('branch'), undefined, log)
|
||||||
.catch(() => git.checkoutLocalBranch(getInput('branch'), log))
|
.catch(() => git.checkoutLocalBranch(getInput('branch'), log))
|
||||||
|
|
||||||
if (getInput('pull_strategy') == 'NO-PULL') info('> Not pulling from repo.')
|
if (getInput('pull_strategy') == 'NO-PULL')
|
||||||
|
core.info('> Not pulling from repo.')
|
||||||
else {
|
else {
|
||||||
info('> Pulling from remote...')
|
core.info('> Pulling from remote...')
|
||||||
await git.fetch(undefined, log).pull(
|
await git.fetch(undefined, log).pull(
|
||||||
undefined,
|
undefined,
|
||||||
undefined,
|
undefined,
|
||||||
@@ -71,11 +66,11 @@ console.log(`Running in ${baseDir}`)
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
info('> Re-staging files...')
|
core.info('> Re-staging files...')
|
||||||
if (getInput('add')) await add({ ignoreErrors: true })
|
if (getInput('add')) await add({ ignoreErrors: true })
|
||||||
if (getInput('remove')) await remove({ ignoreErrors: true })
|
if (getInput('remove')) await remove({ ignoreErrors: true })
|
||||||
|
|
||||||
info('> Creating commit...')
|
core.info('> Creating commit...')
|
||||||
await git.commit(
|
await git.commit(
|
||||||
getInput('message'),
|
getInput('message'),
|
||||||
undefined,
|
undefined,
|
||||||
@@ -96,7 +91,7 @@ console.log(`Running in ${baseDir}`)
|
|||||||
)
|
)
|
||||||
|
|
||||||
if (getInput('tag')) {
|
if (getInput('tag')) {
|
||||||
info('> Tagging commit...')
|
core.info('> Tagging commit...')
|
||||||
await git
|
await git
|
||||||
.tag(matchGitArgs(getInput('tag')), (err, data?) => {
|
.tag(matchGitArgs(getInput('tag')), (err, data?) => {
|
||||||
if (data) setOutput('tagged', 'true')
|
if (data) setOutput('tagged', 'true')
|
||||||
@@ -106,16 +101,18 @@ console.log(`Running in ${baseDir}`)
|
|||||||
setOutput('tagged', 'true')
|
setOutput('tagged', 'true')
|
||||||
return log(null, data)
|
return log(null, data)
|
||||||
})
|
})
|
||||||
.catch((err) => setFailed(err))
|
.catch((err) => core.setFailed(err))
|
||||||
} else info('> No tag info provided.')
|
} else core.info('> No tag info provided.')
|
||||||
|
|
||||||
const pushOption = parseBool(getInput('push')) ?? getInput('push')
|
const pushOption = parseBool(getInput('push')) ?? getInput('push')
|
||||||
if (pushOption) {
|
if (pushOption) {
|
||||||
// If the options is `true | string`...
|
// If the options is `true | string`...
|
||||||
info('> Pushing commit to repo...')
|
core.info('> Pushing commit to repo...')
|
||||||
|
|
||||||
if (pushOption === true) {
|
if (pushOption === true) {
|
||||||
debug(`Running: git push origin ${getInput('branch')} --set-upstream`)
|
core.debug(
|
||||||
|
`Running: git push origin ${getInput('branch')} --set-upstream`
|
||||||
|
)
|
||||||
await git.push(
|
await git.push(
|
||||||
'origin',
|
'origin',
|
||||||
getInput('branch'),
|
getInput('branch'),
|
||||||
@@ -126,7 +123,7 @@ console.log(`Running in ${baseDir}`)
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
debug(`Running: git push ${pushOption}`)
|
core.debug(`Running: git push ${pushOption}`)
|
||||||
await git.push(
|
await git.push(
|
||||||
undefined,
|
undefined,
|
||||||
undefined,
|
undefined,
|
||||||
@@ -139,11 +136,13 @@ console.log(`Running in ${baseDir}`)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (getInput('tag')) {
|
if (getInput('tag')) {
|
||||||
info('> Pushing tags to repo...')
|
core.info('> Pushing tags to repo...')
|
||||||
await git
|
await git
|
||||||
.pushTags('origin', undefined, (e, d?) => log(undefined, e || d))
|
.pushTags('origin', undefined, (e, d?) => log(undefined, e || d))
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
info('> Tag push failed: deleting remote tag and re-pushing...')
|
core.info(
|
||||||
|
'> Tag push failed: deleting remote tag and re-pushing...'
|
||||||
|
)
|
||||||
return git
|
return git
|
||||||
.push(
|
.push(
|
||||||
undefined,
|
undefined,
|
||||||
@@ -159,21 +158,21 @@ console.log(`Running in ${baseDir}`)
|
|||||||
)
|
)
|
||||||
.pushTags('origin', undefined, log)
|
.pushTags('origin', undefined, log)
|
||||||
})
|
})
|
||||||
} else info('> No tags to push.')
|
} else core.info('> No tags to push.')
|
||||||
} else info('> Not pushing anything.')
|
} else core.info('> Not pushing anything.')
|
||||||
|
|
||||||
endGroup()
|
core.endGroup()
|
||||||
info('> Task completed.')
|
core.info('> Task completed.')
|
||||||
} else {
|
} else {
|
||||||
endGroup()
|
core.endGroup()
|
||||||
info('> Working tree clean. Nothing to commit.')
|
core.info('> Working tree clean. Nothing to commit.')
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
.then(logOutputs)
|
.then(logOutputs)
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
endGroup()
|
core.endGroup()
|
||||||
logOutputs()
|
logOutputs()
|
||||||
setFailed(e)
|
core.setFailed(e)
|
||||||
})
|
})
|
||||||
|
|
||||||
async function checkInputs() {
|
async function checkInputs() {
|
||||||
@@ -202,32 +201,82 @@ async function checkInputs() {
|
|||||||
if (getInput('add')) {
|
if (getInput('add')) {
|
||||||
const parsed = parseInputArray(getInput('add'))
|
const parsed = parseInputArray(getInput('add'))
|
||||||
if (parsed.length == 1)
|
if (parsed.length == 1)
|
||||||
info('Add input parsed as single string, running 1 git add command.')
|
core.info('Add input parsed as single string, running 1 git add command.')
|
||||||
else if (parsed.length > 1)
|
else if (parsed.length > 1)
|
||||||
info(
|
core.info(
|
||||||
`Add input parsed as string array, running ${parsed.length} git add commands.`
|
`Add input parsed as string array, running ${parsed.length} git add commands.`
|
||||||
)
|
)
|
||||||
else setFailed('Add input: array length < 1')
|
else core.setFailed('Add input: array length < 1')
|
||||||
}
|
}
|
||||||
if (getInput('remove')) {
|
if (getInput('remove')) {
|
||||||
const parsed = parseInputArray(getInput('remove'))
|
const parsed = parseInputArray(getInput('remove'))
|
||||||
if (parsed.length == 1)
|
if (parsed.length == 1)
|
||||||
info('Remove input parsed as single string, running 1 git rm command.')
|
core.info(
|
||||||
|
'Remove input parsed as single string, running 1 git rm command.'
|
||||||
|
)
|
||||||
else if (parsed.length > 1)
|
else if (parsed.length > 1)
|
||||||
info(
|
core.info(
|
||||||
`Remove input parsed as string array, running ${parsed.length} git rm commands.`
|
`Remove input parsed as string array, running ${parsed.length} git rm commands.`
|
||||||
)
|
)
|
||||||
else setFailed('Remove input: array length < 1')
|
else core.setFailed('Remove input: array length < 1')
|
||||||
}
|
}
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
|
// #region default_author
|
||||||
|
const default_author_valid = ['github_actor', 'user_info', 'github_actions']
|
||||||
|
if (!default_author_valid.includes(getInput('default_author')))
|
||||||
|
throw new Error(
|
||||||
|
`'${getInput(
|
||||||
|
'default_author'
|
||||||
|
)}' is not a valid value for default_author. Valid values: ${default_author_valid.join(
|
||||||
|
', '
|
||||||
|
)}`
|
||||||
|
)
|
||||||
|
|
||||||
// #region author_name, author_email
|
// #region author_name, author_email
|
||||||
setDefault('author_name', `${process.env.GITHUB_ACTOR}`)
|
let name, email
|
||||||
setDefault(
|
switch (getInput('default_author')) {
|
||||||
'author_email',
|
case 'github_actor': {
|
||||||
`${process.env.GITHUB_ACTOR}@users.noreply.github.com`
|
name = process.env.GITHUB_ACTOR
|
||||||
)
|
email = `${process.env.GITHUB_ACTOR}@users.noreply.github.com`
|
||||||
info(
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'user_info': {
|
||||||
|
if (!getInput('author_name') || !getInput('author_email')) {
|
||||||
|
const res = await getUserInfo(process.env.GITHUB_ACTOR)
|
||||||
|
if (!res?.name)
|
||||||
|
core.warning("Couldn't fetch author name, filling with github_actor.")
|
||||||
|
if (!res?.email)
|
||||||
|
core.warning(
|
||||||
|
"Couldn't fetch author email, filling with github_actor."
|
||||||
|
)
|
||||||
|
|
||||||
|
res?.name && (name = res?.name)
|
||||||
|
res?.email && (email = res.email)
|
||||||
|
if (name && email) break
|
||||||
|
}
|
||||||
|
|
||||||
|
!name && (name = process.env.GITHUB_ACTOR)
|
||||||
|
!email && (email = `${process.env.GITHUB_ACTOR}@users.noreply.github.com`)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'github_actions': {
|
||||||
|
name = 'github-actions'
|
||||||
|
email = '41898282+github-actions[bot]@users.noreply.github.com'
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new Error(
|
||||||
|
'This should not happen, please contact the author of this action. (checkInputs.author)'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
setDefault('author_name', name)
|
||||||
|
setDefault('author_email', email)
|
||||||
|
core.info(
|
||||||
`> Using '${getInput('author_name')} <${getInput(
|
`> Using '${getInput('author_name')} <${getInput(
|
||||||
'author_email'
|
'author_email'
|
||||||
)}>' as author.`
|
)}>' as author.`
|
||||||
@@ -239,12 +288,13 @@ async function checkInputs() {
|
|||||||
'message',
|
'message',
|
||||||
`Commit from GitHub Actions (${process.env.GITHUB_WORKFLOW})`
|
`Commit from GitHub Actions (${process.env.GITHUB_WORKFLOW})`
|
||||||
)
|
)
|
||||||
info(`> Using "${getInput('message')}" as commit message.`)
|
core.info(`> Using "${getInput('message')}" as commit message.`)
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
// #region branch
|
// #region branch
|
||||||
const branch = setDefault('branch', defaultBranch || '')
|
const branch = setDefault('branch', defaultBranch || '')
|
||||||
if (isPR) info(`> Running for a PR, the action will use '${branch}' as ref.`)
|
if (isPR)
|
||||||
|
core.info(`> Running for a PR, the action will use '${branch}' as ref.`)
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
// #region signoff
|
// #region signoff
|
||||||
@@ -260,7 +310,7 @@ async function checkInputs() {
|
|||||||
|
|
||||||
if (!parsed) setInput('signoff', undefined)
|
if (!parsed) setInput('signoff', undefined)
|
||||||
|
|
||||||
debug(
|
core.debug(
|
||||||
`Current signoff option: ${getInput('signoff')} (${typeof getInput(
|
`Current signoff option: ${getInput('signoff')} (${typeof getInput(
|
||||||
'signoff'
|
'signoff'
|
||||||
)})`
|
)})`
|
||||||
@@ -270,7 +320,7 @@ async function checkInputs() {
|
|||||||
|
|
||||||
// #region pull_strategy
|
// #region pull_strategy
|
||||||
if (getInput('pull_strategy') == 'NO-PULL')
|
if (getInput('pull_strategy') == 'NO-PULL')
|
||||||
debug("NO-PULL found: won't pull from remote.")
|
core.debug("NO-PULL found: won't pull from remote.")
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
// #region push
|
// #region push
|
||||||
@@ -278,11 +328,17 @@ async function checkInputs() {
|
|||||||
// 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)
|
||||||
const parsed = parseBool(getInput('push'))
|
const parsed = parseBool(getInput('push'))
|
||||||
|
|
||||||
debug(
|
core.debug(
|
||||||
`Current push option: '${getInput('push')}' (parsed as ${typeof parsed})`
|
`Current push option: '${getInput('push')}' (parsed as ${typeof parsed})`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
|
// #region github_token
|
||||||
|
if (!getInput('github_token'))
|
||||||
|
core.warning(
|
||||||
|
'No github_token has been detected, the action may fail if it needs to use the API'
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function add({ logWarning = true, ignoreErrors = false } = {}): Promise<
|
async function add({ logWarning = true, ignoreErrors = false } = {}): Promise<
|
||||||
@@ -309,7 +365,9 @@ async function add({ logWarning = true, ignoreErrors = false } = {}): Promise<
|
|||||||
e.message.includes('did not match any files') &&
|
e.message.includes('did not match any files') &&
|
||||||
logWarning
|
logWarning
|
||||||
)
|
)
|
||||||
warning(`Add command did not match any file:\n git add ${args}`)
|
core.warning(
|
||||||
|
`Add command did not match any file:\n git add ${args}`
|
||||||
|
)
|
||||||
else throw e
|
else throw e
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@@ -343,7 +401,7 @@ async function remove({
|
|||||||
e.message.includes('did not match any files')
|
e.message.includes('did not match any files')
|
||||||
)
|
)
|
||||||
logWarning &&
|
logWarning &&
|
||||||
warning(
|
core.warning(
|
||||||
`Remove command did not match any file:\n git rm ${args}`
|
`Remove command did not match any file:\n git rm ${args}`
|
||||||
)
|
)
|
||||||
else throw e
|
else throw e
|
||||||
@@ -366,7 +424,7 @@ function parseInputArray(input: string): string[] {
|
|||||||
Array.isArray(json) &&
|
Array.isArray(json) &&
|
||||||
json.every((e) => typeof e == 'string')
|
json.every((e) => typeof e == 'string')
|
||||||
) {
|
) {
|
||||||
debug(`Input parsed as JSON array of length ${json.length}`)
|
core.debug(`Input parsed as JSON array of length ${json.length}`)
|
||||||
return json
|
return json
|
||||||
}
|
}
|
||||||
} catch {}
|
} catch {}
|
||||||
@@ -378,19 +436,19 @@ function parseInputArray(input: string): string[] {
|
|||||||
Array.isArray(yaml) &&
|
Array.isArray(yaml) &&
|
||||||
yaml.every((e) => typeof e == 'string')
|
yaml.every((e) => typeof e == 'string')
|
||||||
) {
|
) {
|
||||||
debug(`Input parsed as YAML array of length ${yaml.length}`)
|
core.debug(`Input parsed as YAML array of length ${yaml.length}`)
|
||||||
return yaml
|
return yaml
|
||||||
}
|
}
|
||||||
} catch {}
|
} catch {}
|
||||||
|
|
||||||
debug('Input parsed as single string')
|
core.debug('Input parsed as single string')
|
||||||
return [input]
|
return [input]
|
||||||
}
|
}
|
||||||
|
|
||||||
function logOutputs() {
|
function logOutputs() {
|
||||||
startGroup('Outputs')
|
core.startGroup('Outputs')
|
||||||
for (const key in outputs) {
|
for (const key in tools.outputs) {
|
||||||
info(`${key}: ${outputs[key]}`)
|
core.info(`${key}: ${tools.outputs[key]}`)
|
||||||
}
|
}
|
||||||
endGroup()
|
core.endGroup()
|
||||||
}
|
}
|
||||||
|
|||||||
40
src/util.ts
40
src/util.ts
@@ -1,5 +1,6 @@
|
|||||||
import * as core from '@actions/core'
|
|
||||||
import { parseArgsStringToArgv } from 'string-argv'
|
import { parseArgsStringToArgv } from 'string-argv'
|
||||||
|
import * as core from '@actions/core'
|
||||||
|
import { Toolkit } from 'actions-toolkit'
|
||||||
|
|
||||||
export type Input =
|
export type Input =
|
||||||
| 'add'
|
| 'add'
|
||||||
@@ -7,22 +8,49 @@ export type Input =
|
|||||||
| 'author_email'
|
| 'author_email'
|
||||||
| 'branch'
|
| 'branch'
|
||||||
| 'cwd'
|
| 'cwd'
|
||||||
|
| 'default_author'
|
||||||
| 'message'
|
| 'message'
|
||||||
| 'pull_strategy'
|
| 'pull_strategy'
|
||||||
| 'push'
|
| 'push'
|
||||||
| 'remove'
|
| 'remove'
|
||||||
| 'signoff'
|
| 'signoff'
|
||||||
| 'tag'
|
| 'tag'
|
||||||
|
| 'github_token'
|
||||||
|
|
||||||
export const outputs = {
|
export type Output = 'committed' | 'pushed' | 'tagged'
|
||||||
|
|
||||||
|
type RecordOf<T extends string> = Record<T, string | undefined>
|
||||||
|
export const tools = new Toolkit<RecordOf<Input>, RecordOf<Output>>({
|
||||||
|
secrets: [
|
||||||
|
'GITHUB_EVENT_PATH',
|
||||||
|
'GITHUB_EVENT_NAME',
|
||||||
|
'GITHUB_REF',
|
||||||
|
'GITHUB_ACTOR'
|
||||||
|
]
|
||||||
|
})
|
||||||
|
tools.outputs = {
|
||||||
committed: 'false',
|
committed: 'false',
|
||||||
pushed: 'false',
|
pushed: 'false',
|
||||||
tagged: 'false'
|
tagged: 'false'
|
||||||
}
|
}
|
||||||
export type Output = keyof typeof outputs
|
|
||||||
|
|
||||||
export function getInput(name: Input) {
|
export function getInput(name: Input) {
|
||||||
return core.getInput(name)
|
return tools.inputs[name] || ''
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getUserInfo(username?: string) {
|
||||||
|
if (!username) return undefined
|
||||||
|
|
||||||
|
const res = await tools.github.users.getByUsername({ username })
|
||||||
|
|
||||||
|
core.debug(
|
||||||
|
`Fetched github actor from the API: ${JSON.stringify(res?.data, null, 2)}`
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: res?.data?.name,
|
||||||
|
email: res?.data?.email
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function log(err: any | Error, data?: any) {
|
export function log(err: any | Error, data?: any) {
|
||||||
@@ -72,7 +100,5 @@ export function parseBool(value: any) {
|
|||||||
|
|
||||||
export function setOutput(name: Output, value: 'true' | 'false') {
|
export function setOutput(name: Output, value: 'true' | 'false') {
|
||||||
core.debug(`Setting output: ${name}=${value}`)
|
core.debug(`Setting output: ${name}=${value}`)
|
||||||
outputs[name] = value
|
tools.outputs[name] = value
|
||||||
return core.setOutput(name, value)
|
|
||||||
}
|
}
|
||||||
for (const key in outputs) setOutput(key as Output, outputs[key])
|
|
||||||
|
|||||||
Reference in New Issue
Block a user