Restore the branch-suffix input

This commit is contained in:
Peter Evans 2020-07-20 19:14:42 +09:00
parent 3c6aade49b
commit 8c01dce3ac
6 changed files with 65 additions and 17 deletions

View file

@ -46,6 +46,7 @@ All inputs are **optional**. If not set, sensible defaults will be used.
| `committer` | The committer name and email address in the format `Display Name <email@address.com>`. Defaults to the GitHub Actions bot user. | `GitHub <noreply@github.com>` |
| `author` | The author name and email address in the format `Display Name <email@address.com>`. Defaults to the user who triggered the workflow run. | `${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>` |
| `branch` | The pull request branch name. | `create-pull-request/patch` |
| `branch-suffix` | The branch suffix type when using the alternative branching strategy. Valid values are `random`, `timestamp` and `short-commit-hash`. See [Alternative strategy](#alternative-strategy---always-create-a-new-pull-request-branch) for details. | |
| `base` | Sets the pull request base branch. | Defaults to the branch checked out in the workflow. |
| `push-to-fork` | A fork of the checked out parent repository to which the pull request branch will be pushed. e.g. `owner/repo-fork`. The pull request will be created to merge the fork's branch into the parent's base. See [push pull request branches to a fork](https://github.com/peter-evans/create-pull-request/blob/master/docs/concepts-guidelines.md#push-pull-request-branches-to-a-fork) for details. | |
| `title` | The title of the pull request. | `Changes by create-pull-request action` |
@ -84,7 +85,7 @@ If there is some reason you need to use `actions/checkout@v1` the following step
### Action behaviour
The action creates a pull request that will be continually updated with new changes until it is merged or closed.
The default behaviour of the action is to create a pull request that will be continually updated with new changes until it is merged or closed.
Changes are committed and pushed to a fixed-name branch, the name of which can be configured with the `branch` input.
Any subsequent changes will be committed to the *same* branch and reflected in the open pull request.
@ -97,6 +98,19 @@ How the action behaves:
For further details about how the action works and usage guidelines, see [Concepts, guidelines and advanced usage](docs/concepts-guidelines.md).
#### Alternative strategy - Always create a new pull request branch
For some use cases it may be desirable to always create a new unique branch each time there are changes to be committed.
This strategy is *not recommended* because if not used carefully it could result in multiple pull requests being created unnecessarily. If in doubt, use the [default strategy](#action-behaviour) of creating an updating a fixed-name branch.
To use this strategy, set input `branch-suffix` with one of the following options.
- `random` - Commits will be made to a branch suffixed with a random alpha-numeric string. e.g. `create-pull-request/patch-6qj97jr`, `create-pull-request/patch-5jrjhvd`
- `timestamp` - Commits will be made to a branch suffixed by a timestamp. e.g. `create-pull-request/patch-1569322532`, `create-pull-request/patch-1569322552`
- `short-commit-hash` - Commits will be made to a branch suffixed with the short SHA1 commit hash. e.g. `create-pull-request/patch-fcdfb59`, `create-pull-request/patch-394710b`
### Controlling commits
As well as relying on the action to handle uncommitted changes, you can additionally make your own commits before the action runs.

View file

@ -24,6 +24,8 @@ inputs:
branch:
description: 'The pull request branch name.'
default: 'create-pull-request/patch'
branch-suffix:
description: 'The branch suffix type when using the alternative branching strategy.'
base:
description: >
The pull request base branch.

22
dist/index.js vendored
View file

@ -1297,6 +1297,7 @@ function run() {
committer: core.getInput('committer'),
author: core.getInput('author'),
branch: core.getInput('branch'),
branchSuffix: core.getInput('branch-suffix'),
base: core.getInput('base'),
pushToFork: core.getInput('push-to-fork'),
title: core.getInput('title'),
@ -10535,6 +10536,27 @@ function createPullRequest(inputs) {
throw new Error(`Working base branch '${workingBase}' was created by this action. Unable to continue.`);
}
core.endGroup();
// Apply the branch suffix if set
if (inputs.branchSuffix) {
switch (inputs.branchSuffix) {
case 'short-commit-hash':
// Suffix with the short SHA1 hash
inputs.branch = `${inputs.branch}-${yield git.revParse('HEAD', [
'--short'
])}`;
break;
case 'timestamp':
// Suffix with the current timestamp
inputs.branch = `${inputs.branch}-${utils.secondsSinceEpoch()}`;
break;
case 'random':
// Suffix with a 7 character random string
inputs.branch = `${inputs.branch}-${utils.randomString()}`;
break;
default:
throw new Error(`Branch suffix '${inputs.branchSuffix}' is not a valid value. Unable to continue.`);
}
}
// Output head branch
core.info(`Pull request branch to create or update set to '${inputs.branch}'`);
// Configure the committer and author

View file

@ -31,22 +31,6 @@
push-to-fork: machine-user/fork-of-repository
```
- Input `branch-suffix` has been removed to simplify the action and make it easier to understand its behaviour. The same functionality can be achieved by modifying the `branch` name before the action runs. See the following example. If you were using `branch-suffix` and need help to update to `v3`, please create an issue.
e.g.
```yaml
- name: Return a 7 character random string
uses: actions/github-script@v2
id: random-string
with:
result-encoding: string
script: return Math.random().toString(36).substr(2, 7)
- uses: peter-evans/create-pull-request@v3
with:
branch: my-branch-${{ steps.random-string.outputs.result }}
```
### New features
- The action has been converted to Typescript and is much faster than `v2`.

View file

@ -12,6 +12,7 @@ export interface Inputs {
committer: string
author: string
branch: string
branchSuffix: string
base: string
pushToFork: string
title: string
@ -107,6 +108,30 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
}
core.endGroup()
// Apply the branch suffix if set
if (inputs.branchSuffix) {
switch (inputs.branchSuffix) {
case 'short-commit-hash':
// Suffix with the short SHA1 hash
inputs.branch = `${inputs.branch}-${await git.revParse('HEAD', [
'--short'
])}`
break
case 'timestamp':
// Suffix with the current timestamp
inputs.branch = `${inputs.branch}-${utils.secondsSinceEpoch()}`
break
case 'random':
// Suffix with a 7 character random string
inputs.branch = `${inputs.branch}-${utils.randomString()}`
break
default:
throw new Error(
`Branch suffix '${inputs.branchSuffix}' is not a valid value. Unable to continue.`
)
}
}
// Output head branch
core.info(
`Pull request branch to create or update set to '${inputs.branch}'`

View file

@ -12,6 +12,7 @@ async function run(): Promise<void> {
committer: core.getInput('committer'),
author: core.getInput('author'),
branch: core.getInput('branch'),
branchSuffix: core.getInput('branch-suffix'),
base: core.getInput('base'),
pushToFork: core.getInput('push-to-fork'),
title: core.getInput('title'),