Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always retrieve pr data #14

Merged
merged 9 commits into from
May 12, 2021
Merged
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
63 changes: 54 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
# Backport action

> This project is still in a very early development stage
> This project is still in an early development stage

This is a GitHub action to backport a pull request (PR) after merging it onto branches that are specified using labels.
This is a GitHub action to backport merged pull requests (PR) onto branches.
For example, to patch an older version with changes that you're merging into your main branch, without the manual labor of cherry-picking the individual commits.

A backport consists of creating a new branch, cherry-picking the changes of the original PR and creating a new PR to merge them.

This backport action is able to deal with so called `octopus` merges (i.e. merges of multiple branches with a single commit).
Therefore, this action is compatible with [Bors](https://bors.tech/) and similar tools.

## Usage

Simply mark a PR with backport labels: `backport <branchname>`.

For example, a PR with labels

Expand All @@ -11,20 +21,20 @@ backport stable/0.24
backport release-0.23
```

will be backported on branches `stable/0.24` and `release-0.23`.

A backport consists of creating a branch with the changes of the original PR and creating a new PR to merge them.
will be backported to branches `stable/0.24` and `release-0.23` when merged.

If something goes wrong, the bot will comment on your PR.
It will also comment after successfully backporting.
Links are created between the original and the new PRs.

## Usage
It's also possible to configure the bot to trigger a backport using a comment on a PR.

## Installation

Add the following workflow configuration to your repository's `.github/workflows` folder.

```yaml
name: Backport labeled PRs after merge
name: Backport labeled PRs
on:
pull_request:
types: [closed]
Expand All @@ -47,7 +57,37 @@ jobs:

> `version:` must refer to the same version as the `uses`

## Code in Main

### Trigger using a comment
The backport action can also be triggered by writing a `/backport` comment on a merged pull request.
To enable this, add the following workflow configuration to your repository's `.github/workflows` folder.

```yaml
name: Backport labeled PRs triggered by comment
on:
issue_comment:
types: [created, edited]
jobs:
build:
if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, '/backport') }}
name: Create backport PRs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
# required to find all branches
fetch-depth: 0
- name: Create backport PRs
uses: zeebe-io/backport-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
github_workspace: ${{ github.workspace }}
version: master
```

> `version:` must refer to the same version as the `uses`

## Local compilation

Install the dependencies
```bash
Expand All @@ -71,4 +111,9 @@ npm test
Run all tests with additional console output
```bash
npm run test-verbose
```
```

## Releases

The distribution is hosted in this repository under `dist`.
Simply build and package the distribution and commit the changes to release a new version.
70 changes: 60 additions & 10 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

28 changes: 19 additions & 9 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ export async function run(): Promise<void> {

const owner = github.getRepo().owner;
const repo = payload.repository.name;
const pull_number = github.getPullNumber();
const mainpr = await github.getPullRequest(pull_number, token);

if (!(await github.isMerged(mainpr, token))) {
const message = "Only merged pull requests can be backported.";
github.createComment(
{ owner, repo, issue_number: pull_number, body: message },
token
);
return;
}

const mainpr = payload.pull_request;
const headref = mainpr.head.sha;
const baseref = mainpr.base.sha;
const labels = mainpr.labels;
Expand All @@ -46,7 +56,7 @@ export async function run(): Promise<void> {
console.log(`Found target in label: ${target}`);

try {
const branchname = `backport-${mainpr.number}-to-${target}`;
const branchname = `backport-${pull_number}-to-${target}`;

console.log(`Start backport to ${branchname}`);
const scriptExitCode = await exec.callBackportScript(
Expand All @@ -65,7 +75,7 @@ export async function run(): Promise<void> {
);
console.error(message);
await github.createComment(
{ owner, repo, issue_number: mainpr.number, body: message },
{ owner, repo, issue_number: pull_number, body: message },
token
);
continue;
Expand All @@ -79,7 +89,7 @@ export async function run(): Promise<void> {
const message = composeMessageForGitPushFailure(target, pushExitCode);
console.error(message);
await github.createComment(
{ owner, repo, issue_number: mainpr.number, body: message },
{ owner, repo, issue_number: pull_number, body: message },
token
);
continue;
Expand All @@ -89,7 +99,7 @@ export async function run(): Promise<void> {
const { title, body } = composePRContent(
target,
mainpr.title,
mainpr.number
pull_number
);
const new_pr_response = await github.createPR(
{
Expand All @@ -108,7 +118,7 @@ export async function run(): Promise<void> {
console.error(JSON.stringify(new_pr_response));
const message = composeMessageForCreatePRFailed(new_pr_response);
await github.createComment(
{ owner, repo, issue_number: mainpr.number, body: message },
{ owner, repo, issue_number: pull_number, body: message },
token
);
continue;
Expand All @@ -126,15 +136,15 @@ export async function run(): Promise<void> {
target
);
await github.createComment(
{ owner, repo, issue_number: mainpr.number, body: message },
{ owner, repo, issue_number: pull_number, body: message },
token
);
continue;
}

const message = composeMessageForSuccess(new_pr.number, target);
await github.createComment(
{ owner, repo, issue_number: mainpr.number, body: message },
{ owner, repo, issue_number: pull_number, body: message },
token
);
} catch (error) {
Expand All @@ -143,7 +153,7 @@ export async function run(): Promise<void> {
{
owner,
repo,
issue_number: mainpr.number,
issue_number: pull_number,
body: error.message,
},
token
Expand Down
Loading