Skip to content

Commit

Permalink
Make some improvements (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
tibdex authored Sep 5, 2020
1 parent 5370ddc commit 2753783
Show file tree
Hide file tree
Showing 5 changed files with 1,213 additions and 684 deletions.
42 changes: 21 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "backport",
"version": "1.0.0",
"version": "1.1.0",
"license": "MIT",
"files": [
"action.yml",
Expand All @@ -15,27 +15,27 @@
"prettier": "prettier --ignore-path .gitignore \"./**/*.{js,json,md,ts,yml}\""
},
"devDependencies": {
"@actions/core": "^1.1.1",
"@actions/exec": "^1.0.1",
"@actions/github": "^1.1.0",
"@octokit/webhooks": "^6.3.0",
"@actions/core": "^1.2.5",
"@actions/exec": "^1.0.4",
"@actions/github": "^4.0.0",
"@octokit/webhooks": "^7.11.2",
"@types/lodash.escaperegexp": "^4.1.6",
"@types/node": "^10.0.3",
"@types/promise-retry": "^1.1.1",
"@typescript-eslint/eslint-plugin": "^2.3.1",
"@typescript-eslint/parser": "^2.3.1",
"@zeit/ncc": "^0.20.5",
"eslint": "^6.5.0",
"eslint-config-prettier": "^6.3.0",
"eslint-config-xo": "^0.27.1",
"eslint-config-xo-typescript": "^0.19.0",
"eslint-import-resolver-typescript": "^1.1.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-sort-destructure-keys": "^1.3.3",
"eslint-plugin-typescript-sort-keys": "^0.4.0",
"eslint-plugin-unicorn": "^12.0.1",
"@types/node": "^14.6.4",
"@types/promise-retry": "^1.1.3",
"@typescript-eslint/eslint-plugin": "^4.0.1",
"@typescript-eslint/parser": "^4.0.1",
"@vercel/ncc": "^0.24.0",
"eslint": "^7.8.1",
"eslint-config-prettier": "^6.11.0",
"eslint-config-xo": "^0.32.1",
"eslint-config-xo-typescript": "^0.32.0",
"eslint-import-resolver-typescript": "^2.3.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-sort-destructure-keys": "^1.3.5",
"eslint-plugin-typescript-sort-keys": "^1.3.0",
"eslint-plugin-unicorn": "^21.0.0",
"lodash.escaperegexp": "^4.1.2",
"prettier": "^1.18.2",
"typescript": "^3.6.3"
"prettier": "^2.1.1",
"typescript": "^4.0.2"
}
}
71 changes: 41 additions & 30 deletions src/backport.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { error as logError, group, warning, info } from "@actions/core";
import { exec } from "@actions/exec";
import { GitHub } from "@actions/github";
import { WebhookPayloadPullRequest } from "@octokit/webhooks";
import { getOctokit } from "@actions/github";
import { GitHub } from "@actions/github/lib/utils";
import { EventPayloads } from "@octokit/webhooks";
import escapeRegExp from "lodash.escaperegexp";

const labelRegExp = /^backport ([^ ]+)(?: ([^ ]+))?$/;
Expand All @@ -11,9 +12,9 @@ const getLabelNames = ({
label,
labels,
}: {
action: WebhookPayloadPullRequest["action"];
action: EventPayloads.WebhookPayloadPullRequest["action"];
label: { name: string };
labels: WebhookPayloadPullRequest["pull_request"]["labels"];
labels: EventPayloads.WebhookPayloadPullRequest["pull_request"]["labels"];
}): string[] => {
switch (action) {
case "closed":
Expand All @@ -31,27 +32,35 @@ const getBackportBaseToHead = ({
labels,
pullRequestNumber,
}: {
action: WebhookPayloadPullRequest["action"];
action: EventPayloads.WebhookPayloadPullRequest["action"];
label: { name: string };
labels: WebhookPayloadPullRequest["pull_request"]["labels"];
labels: EventPayloads.WebhookPayloadPullRequest["pull_request"]["labels"];
pullRequestNumber: number;
}): { [base: string]: string } =>
getLabelNames({ action, label, labels }).reduce((baseToHead, labelName) => {
}): { [base: string]: string } => {
const baseToHead: { [base: string]: string } = {};

getLabelNames({ action, label, labels }).forEach((labelName) => {
const matches = labelRegExp.exec(labelName);
if (matches === null) {
return baseToHead;

if (matches !== null) {
const [
,
base,
head = `backport-${pullRequestNumber}-to-${base}`,
] = matches;
baseToHead[base] = head;
}
});

const [, base, head = `backport-${pullRequestNumber}-to-${base}`] = matches;
return { ...baseToHead, [base]: head };
}, {});
return baseToHead;
};

const warnIfSquashIsNotTheOnlyAllowedMergeMethod = async ({
github,
owner,
repo,
}: {
github: GitHub;
github: InstanceType<typeof GitHub>;
owner: string;
repo: string;
}) => {
Expand Down Expand Up @@ -84,7 +93,7 @@ const backportOnce = async ({
base: string;
body: string;
commitToBackport: string;
github: GitHub;
github: InstanceType<typeof GitHub>;
head: string;
labelsToAdd: string[];
owner: string;
Expand Down Expand Up @@ -153,7 +162,7 @@ const getFailedBackportCommentBody = ({
"# Create a new branch",
`git switch --create ${head}`,
"# Cherry-pick the merged commit of this pull request and resolve the conflicts",
`git cherry-pick ${commitToBackport}`,
`git cherry-pick ---mainline 1 ${commitToBackport}`,
"# Push it to GitHub",
`git push --set-upstream origin ${head}`,
"# Go back to the original working tree",
Expand All @@ -169,8 +178,6 @@ const backport = async ({
labelsToAdd,
payload: {
action,
// The payload has a label property when the action is "labeled".
// @ts-ignore
label,
pull_request: {
labels,
Expand All @@ -188,7 +195,7 @@ const backport = async ({
token,
}: {
labelsToAdd: string[];
payload: WebhookPayloadPullRequest;
payload: EventPayloads.WebhookPayloadPullRequest;
titleTemplate: string;
token: string;
}) => {
Expand All @@ -198,7 +205,8 @@ const backport = async ({

const backportBaseToHead = getBackportBaseToHead({
action,
label,
// The payload has a label property when the action is "labeled".
label: label!,
labels,
pullRequestNumber,
});
Expand All @@ -207,7 +215,7 @@ const backport = async ({
return;
}

const github = new GitHub(token);
const github = getOctokit(token);

await warnIfSquashIsNotTheOnlyAllowedMergeMethod({ github, owner, repo });

Expand All @@ -229,15 +237,18 @@ const backport = async ({

for (const [base, head] of Object.entries(backportBaseToHead)) {
const body = `Backport ${commitToBackport} from #${pullRequestNumber}`;
const titleVariables = {

let title = titleTemplate;
Object.entries({
base,
originalTitle,
};
const title = Object.entries(titleVariables).reduce(
(variable, [name, value]) =>
variable.replace(new RegExp(escapeRegExp(`{{${name}}}`), "g"), value),
titleTemplate,
);
}).forEach(([name, value]) => {
title = title.replace(
new RegExp(escapeRegExp(`{{${name}}}`), "g"),
value,
);
});

await group(`Backporting to ${base} on ${head}`, async () => {
try {
await backportOnce({
Expand All @@ -252,8 +263,8 @@ const backport = async ({
title,
});
} catch (error) {
const errorMessage = error.message;
logError(`Backport failed: ${errorMessage}`);
const errorMessage: string = error.message;
logError(error);
await github.issues.createComment({
body: getFailedBackportCommentBody({
base,
Expand Down
2 changes: 1 addition & 1 deletion src/get-labels-to-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export const getLabelsToAdd = (input: string | undefined): string[] => {
}

const labels = input.split(",");
return labels.map(v => v.trim()).filter(v => v !== "");
return labels.map((v) => v.trim()).filter((v) => v !== "");
};
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { debug, getInput, setFailed } from "@actions/core";
import { debug, error as logError, getInput, setFailed } from "@actions/core";
import { context } from "@actions/github";
import { WebhookPayloadPullRequest } from "@octokit/webhooks";
import { EventPayloads } from "@octokit/webhooks";

import { backport } from "./backport";
import { getLabelsToAdd } from "./get-labels-to-add";
Expand All @@ -9,18 +9,19 @@ const run = async () => {
try {
const token = getInput("github_token", { required: true });
const titleTemplate = getInput("title_template");
debug(JSON.stringify(context, null, 2));
debug(JSON.stringify(context, undefined, 2));
const labelsInput = getInput("labels");
const labelsToAdd = getLabelsToAdd(labelsInput);
await backport({
labelsToAdd,
payload: context.payload as WebhookPayloadPullRequest,
payload: context.payload as EventPayloads.WebhookPayloadPullRequest,
titleTemplate,
token,
});
} catch (error) {
logError(error);
setFailed(error.message);
}
};

run();
void run();
Loading

0 comments on commit 2753783

Please sign in to comment.