Skip to content

Commit

Permalink
Make backport action more customizable for other teams
Browse files Browse the repository at this point in the history
  • Loading branch information
brianseeders committed Mar 16, 2021
1 parent e795950 commit 8b60e88
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 20 deletions.
15 changes: 12 additions & 3 deletions backport/action.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
name: 'Backport Action'
description: 'Backport merged pull requests to other branches via labels'
inputs:
branch:
description: The target branch for merged pull requests
required: true
github_token:
description: An oauth token that will be used to create the backport pull requests and perform other operations
required: true
Expand All @@ -13,6 +10,18 @@ inputs:
commit_email:
description: The e-mail address that will be used to commit the cherry-picks for the backport branches
required: true
auto_merge:
description: Enable auto-merge for created backport PRs
required: false
default: 'true'
auto_merge_method:
description: 'When enabling auto-merge, sets the auto-merge method. Possible values are: merge, rebase, squash'
required: false
default: 'squash'
manual_backport_command_template:
description: ''
required: false
default: 'node scripts/backport --pr %pullNumber%'
runs:
using: 'node12'
main: 'index.js'
Expand Down
8 changes: 4 additions & 4 deletions backport/createStatusComment.js

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

8 changes: 5 additions & 3 deletions backport/createStatusComment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { BackportResponse } from 'backport';
import { expect } from 'chai';
import { getCommentFromResponse } from './createStatusComment';

const BACKPORT_TEMPLATE = 'node scripts/backport --pr %pullNumber%';

describe('createStatusComment', () => {
describe('getCommentFromResponse', () => {
it('should create a message for all successful backports', async () => {
const comment = getCommentFromResponse(1, {
const comment = getCommentFromResponse(1, BACKPORT_TEMPLATE, {
results: [
{
success: true,
Expand All @@ -31,7 +33,7 @@ Successful backport PRs will be merged automatically after passing CI.`,
});

it('should create a message for a mix of successful and failed backports', async () => {
const comment = getCommentFromResponse(1, {
const comment = getCommentFromResponse(1, BACKPORT_TEMPLATE, {
results: [
{
success: true,
Expand Down Expand Up @@ -61,7 +63,7 @@ To backport manually, check out the target branch and run:
});

it('should create a message for a all failed backports', async () => {
const comment = getCommentFromResponse(1, {
const comment = getCommentFromResponse(1, BACKPORT_TEMPLATE, {
results: [
{
success: false,
Expand Down
13 changes: 9 additions & 4 deletions backport/createStatusComment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { BackportResponse } from 'backport';
import { Octokit } from '@octokit/rest';

export const getCommentFromResponse = (pullNumber: number, backportResponse: BackportResponse): string => {
export const getCommentFromResponse = (
pullNumber: number,
backportCommandTemplate: string,
backportResponse: BackportResponse,
): string => {
const hasAnySuccessful = !!backportResponse.results.find((r) => r.success);

const header = backportResponse.success ? '## 💚 Backport successful' : '## 💔 Backport failed';
Expand Down Expand Up @@ -31,7 +35,7 @@ export const getCommentFromResponse = (pullNumber: number, backportResponse: Bac
helpParts.push(
[
'To backport manually, check out the target branch and run:',
`\`node scripts/backport --pr ${pullNumber}\``,
`\`${backportCommandTemplate.replace('%pullNumber%', pullNumber.toString())}\``,
].join('\n'),
);
}
Expand All @@ -46,9 +50,10 @@ export default async function createStatusComment(options: {
repoOwner: string;
repoName: string;
pullNumber: number;
backportCommandTemplate: string;
backportResponse: BackportResponse;
}) {
const { accessToken, repoOwner, repoName, pullNumber, backportResponse } = options;
const { accessToken, repoOwner, repoName, pullNumber, backportCommandTemplate, backportResponse } = options;

const octokit = new Octokit({
auth: accessToken,
Expand All @@ -58,6 +63,6 @@ export default async function createStatusComment(options: {
owner: repoOwner,
repo: repoName,
issue_number: pullNumber,
body: getCommentFromResponse(pullNumber, backportResponse),
body: getCommentFromResponse(pullNumber, backportCommandTemplate, backportResponse),
});
}
14 changes: 11 additions & 3 deletions backport/index.js

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

16 changes: 13 additions & 3 deletions backport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ async function backport() {
const pullRequest = payload.pull_request;
const owner: string = pullRequest.user.login;

const branch = core.getInput('branch', { required: true });
const branch: string = pullRequest?.base?.ref;

if (!branch) {
throw Error("Can't determine PR base branch.");
}

const accessToken = core.getInput('github_token', { required: true });
const commitUser = core.getInput('commit_user', { required: true });
const commitEmail = core.getInput('commit_email', { required: true });

const autoMerge = core.getInput('auto_merge', { required: true }) === 'true';
const autoMergeMethod = core.getInput('auto_merge_method', { required: true });
const backportCommandTemplate = core.getInput('manual_backport_command_template', { required: true });

await exec(`git config --global user.name "${commitUser}"`);
await exec(`git config --global user.email "${commitEmail}"`);

Expand All @@ -40,15 +49,16 @@ async function backport() {
pullNumber: pullRequest.number,
labels: ['backport'],
assignees: [owner],
autoMerge: true,
autoMergeMethod: 'squash',
autoMerge: autoMerge,
autoMergeMethod: autoMergeMethod,
});

await createStatusComment({
accessToken,
repoOwner: repo.owner,
repoName: repo.repo,
pullNumber: pullRequest.number,
backportCommandTemplate,
backportResponse,
});
}
Expand Down

0 comments on commit 8b60e88

Please sign in to comment.