Skip to content

Commit

Permalink
adding merge-pr-to-branch action
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyj-deliveroo committed Jan 24, 2020
1 parent c0bd7bc commit f40dd05
Show file tree
Hide file tree
Showing 17 changed files with 5,918 additions and 1 deletion.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
11 changes: 11 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": false,
"trailingComma": "none",
"bracketSpacing": true,
"arrowParens": "avoid",
"parser": "typescript"
}
18 changes: 18 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"[javascriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
}
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DOCKER_TAG = pr-labeled:local

build:
docker build -t $(DOCKER_TAG) .

run: build
docker run -t $(DOCKER_TAG)
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
# merge-pr-to-branch
# About

`merge-pr-to-branch` is a Github action that will reset the `target-branch` (defaults to `staging`) to the base branch (eg. `master`) and merge every pull request with the `deploy` label. This is triggered as defined in the Github workflow.

# Getting started

## Add Github Workflow
To enable this github action, add the following workflow to your repo:

`.github/workflows/merge-pr-to-branch.yml`:

```yaml
on:
pull_request:
branches:
- master
types: [labeled, unlabeled, closed, reopened]
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: master
- uses: deliveroo/merge-pr-to-branch@v1
```
## Use the action
1. To merge a pull request to the `target-branch`, add the `deploy` label.
2. `merge-pr-to-branch` will run and attempt to merge the pull request
* If successful, the `deployed` label will be added along with a comment
* If unsuccessful, a comment with the error will be added and the `deploy` label will be removed

# Contributing

Before commiting your change, ensure you build the distributed js file. Github Actions require bundled output in the repo.

```
npm run build
```
8 changes: 8 additions & 0 deletions __integrationtests__/githubHelpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createGithubClient, mergeDeployablePullRequests } from "../src/githubHelpers";

describe("main", () => {
it("mergeDeployablePullRequests intergration test", async () => {
const client = createGithubClient();
await mergeDeployablePullRequests(client, "deliveroo", "dev-glue", "sandbox", "master");
}, 100000);
});
60 changes: 60 additions & 0 deletions __tests__/githubHelpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as githubHelpers from "../src/githubHelpers";

describe("githubHelpers", () => {
it("getBranchFromRef returns last segment of ref", () => {
expect(githubHelpers.getBranchFromRef("foo/bar")).toBe("bar");
});
it("getBranchFromRef returns ref when no separators", () => {
expect(githubHelpers.getBranchFromRef("foo")).toBe("foo");
});
it("hasLabel returns true when exists in array of strings", () => {
expect(githubHelpers.hasLabel(["bar"], "bar")).toBeTruthy();
});
it("hasLabel returns true when exists in array of objects", () => {
expect(githubHelpers.hasLabel([{ name: "a" }], "a")).toBeTruthy();
});
it("hasLabel returns false when not exists in array of strings", () => {
expect(githubHelpers.hasLabel(["bar"], "foo")).toBeFalsy();
});
it("hasLabel returns false when not exists in array of objects", () => {
expect(githubHelpers.hasLabel([{ name: "foo" }], "a")).toBeFalsy();
});
it("getBranchRef calls getRef as expected", async () => {
const expectedResult = {};
const githubClient = {
git: {
getRef: jest.fn().mockResolvedValue(expectedResult)
}
};
const owner = "owner";
const repo = "repo";
const branch = "branch";
const result = await githubHelpers.getBranchRef(githubClient as any, owner, repo, branch);
expect(githubClient.git.getRef).toBeCalledTimes(1);
expect(githubClient.git.getRef).lastCalledWith({
owner,
repo,
ref: `heads/${branch}`
});
expect(result).toBe(expectedResult);
});
it("getBranchRef does not throw 404s", async () => {
const expectedResult = { status: 404 };
const githubClient = {
git: {
getRef: jest.fn().mockRejectedValue(expectedResult)
}
};
const owner = "owner";
const repo = "repo";
const branch = "branch";
const result = await githubHelpers.getBranchRef(githubClient as any, owner, repo, branch);
expect(githubClient.git.getRef).toBeCalledTimes(1);
expect(githubClient.git.getRef).lastCalledWith({
owner,
repo,
ref: `heads/${branch}`
});
expect(result).toEqual(expectedResult);
});
});
13 changes: 13 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: 'Merge PR to Branch'
description: "Handles merging PRs with 'deploy' label to a target branch."
author: 'Deliveroo'
inputs:
target-branch:
description: 'The target branch to merge PRs to'
default: staging
repo-token:
description: 'The access token used to access github api'
default: ${{ github.token }}
runs:
using: 'node12'
main: 'dist/index.js'
1 change: 1 addition & 0 deletions dist/index.js

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true
}
Loading

0 comments on commit f40dd05

Please sign in to comment.