Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,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 on github.com. | `github-actions[bot] <41898282+github-actions[bot]@users.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_id }}+${{ github.actor }}@users.noreply.github.com>` |
| `signoff` | Add [`Signed-off-by`](https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---signoff) line by the committer at the end of the commit log message. | `false` |
| `branch` | The pull request branch name. | `create-pull-request/patch` |
| `branch` | The pull request branch name. Supports `${base}` placeholder. See [branch](#branch). | `create-pull-request/patch` |
| `delete-branch` | Delete the `branch` if it doesn't have an active pull request associated with it. See [delete-branch](#delete-branch). | `false` |
| `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. |
Expand Down Expand Up @@ -102,6 +102,52 @@ The action first creates a branch, and then creates a pull request for the branc
For some rare use cases it can be useful, or even necessary, to use different tokens for these operations.
It is not advisable to use this input unless you know you need to.

#### branch

The `branch` input supports a placeholder `${base}` that will be replaced with the base branch name. This is useful for "namespacing" pull requests to the branch they target.

For example, if you want to create separate pull request branches for different base branches:

```yml
- name: Create Pull Request
uses: peter-evans/create-pull-request@v7
with:
branch: ${base}/create-pull-request/patch
```

When the base branch is `main`, the pull request branch will be `main/create-pull-request/patch`.
When the base branch is `develop`, the pull request branch will be `develop/create-pull-request/patch`.

This is particularly useful for code generation workflows that run on multiple branches:

```yml
name: Code Generation
on:
push:
branches:
- main
- develop
- 'feature/**'

jobs:
generate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Run code generation
run: npm run generate

- name: Create Pull Request
uses: peter-evans/create-pull-request@v7
with:
branch: ${base}/codegen
title: '[Codegen] Update generated code for ${base}'
body: Automated code generation for the ${base} branch
```

**Note:** If the base branch name contains forward slashes (e.g., `feature/new-feature`), they will be replaced with hyphens in the branch name (e.g., `feature-new-feature/codegen`) to avoid issues with nested directory structures.

#### commit-message

In addition to a message, the `commit-message` input can also be used to populate the commit description. Leave a single blank line between the message and description.
Expand Down
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ inputs:
description: 'Add `Signed-off-by` line by the committer at the end of the commit log message.'
default: false
branch:
description: 'The pull request branch name.'
description: >
The pull request branch name.
Supports the placeholder `${base}` which will be replaced with the base branch name.
For example, `${base}/patch` becomes `main/patch` when base is `main`.
default: 'create-pull-request/patch'
delete-branch:
description: >
Expand Down
8 changes: 8 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,14 @@ function createPullRequest(inputs) {
}
// If the base is not specified it is assumed to be the working base.
const base = inputs.base ? inputs.base : workingBase;
// Substitute ${base} placeholder in branch name
if (inputs.branch.includes('${base}')) {
// Sanitize base branch name for use in branch name
// Replace forward slashes with hyphens to avoid nested directory issues
const sanitizedBase = base.replace(/\//g, '-');
inputs.branch = inputs.branch.replace(/\$\{base\}/g, sanitizedBase);
core.info(`Branch name derived from base: '${inputs.branch}'`);
}
// Throw an error if the base and branch are not different branches
// of the 'origin' remote. An identically named branch in the `fork`
// remote is perfectly fine.
Expand Down
10 changes: 10 additions & 0 deletions src/create-pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
}
// If the base is not specified it is assumed to be the working base.
const base = inputs.base ? inputs.base : workingBase

// Substitute ${base} placeholder in branch name
if (inputs.branch.includes('${base}')) {
// Sanitize base branch name for use in branch name
// Replace forward slashes with hyphens to avoid nested directory issues
const sanitizedBase = base.replace(/\//g, '-')
inputs.branch = inputs.branch.replace(/\$\{base\}/g, sanitizedBase)
core.info(`Branch name derived from base: '${inputs.branch}'`)
}

// Throw an error if the base and branch are not different branches
// of the 'origin' remote. An identically named branch in the `fork`
// remote is perfectly fine.
Expand Down