Skip to content

Commit 2753783

Browse files
authored
Make some improvements (#46)
1 parent 5370ddc commit 2753783

File tree

5 files changed

+1213
-684
lines changed

5 files changed

+1213
-684
lines changed

package.json

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "backport",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"license": "MIT",
55
"files": [
66
"action.yml",
@@ -15,27 +15,27 @@
1515
"prettier": "prettier --ignore-path .gitignore \"./**/*.{js,json,md,ts,yml}\""
1616
},
1717
"devDependencies": {
18-
"@actions/core": "^1.1.1",
19-
"@actions/exec": "^1.0.1",
20-
"@actions/github": "^1.1.0",
21-
"@octokit/webhooks": "^6.3.0",
18+
"@actions/core": "^1.2.5",
19+
"@actions/exec": "^1.0.4",
20+
"@actions/github": "^4.0.0",
21+
"@octokit/webhooks": "^7.11.2",
2222
"@types/lodash.escaperegexp": "^4.1.6",
23-
"@types/node": "^10.0.3",
24-
"@types/promise-retry": "^1.1.1",
25-
"@typescript-eslint/eslint-plugin": "^2.3.1",
26-
"@typescript-eslint/parser": "^2.3.1",
27-
"@zeit/ncc": "^0.20.5",
28-
"eslint": "^6.5.0",
29-
"eslint-config-prettier": "^6.3.0",
30-
"eslint-config-xo": "^0.27.1",
31-
"eslint-config-xo-typescript": "^0.19.0",
32-
"eslint-import-resolver-typescript": "^1.1.1",
33-
"eslint-plugin-import": "^2.18.2",
34-
"eslint-plugin-sort-destructure-keys": "^1.3.3",
35-
"eslint-plugin-typescript-sort-keys": "^0.4.0",
36-
"eslint-plugin-unicorn": "^12.0.1",
23+
"@types/node": "^14.6.4",
24+
"@types/promise-retry": "^1.1.3",
25+
"@typescript-eslint/eslint-plugin": "^4.0.1",
26+
"@typescript-eslint/parser": "^4.0.1",
27+
"@vercel/ncc": "^0.24.0",
28+
"eslint": "^7.8.1",
29+
"eslint-config-prettier": "^6.11.0",
30+
"eslint-config-xo": "^0.32.1",
31+
"eslint-config-xo-typescript": "^0.32.0",
32+
"eslint-import-resolver-typescript": "^2.3.0",
33+
"eslint-plugin-import": "^2.22.0",
34+
"eslint-plugin-sort-destructure-keys": "^1.3.5",
35+
"eslint-plugin-typescript-sort-keys": "^1.3.0",
36+
"eslint-plugin-unicorn": "^21.0.0",
3737
"lodash.escaperegexp": "^4.1.2",
38-
"prettier": "^1.18.2",
39-
"typescript": "^3.6.3"
38+
"prettier": "^2.1.1",
39+
"typescript": "^4.0.2"
4040
}
4141
}

src/backport.ts

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { error as logError, group, warning, info } from "@actions/core";
22
import { exec } from "@actions/exec";
3-
import { GitHub } from "@actions/github";
4-
import { WebhookPayloadPullRequest } from "@octokit/webhooks";
3+
import { getOctokit } from "@actions/github";
4+
import { GitHub } from "@actions/github/lib/utils";
5+
import { EventPayloads } from "@octokit/webhooks";
56
import escapeRegExp from "lodash.escaperegexp";
67

78
const labelRegExp = /^backport ([^ ]+)(?: ([^ ]+))?$/;
@@ -11,9 +12,9 @@ const getLabelNames = ({
1112
label,
1213
labels,
1314
}: {
14-
action: WebhookPayloadPullRequest["action"];
15+
action: EventPayloads.WebhookPayloadPullRequest["action"];
1516
label: { name: string };
16-
labels: WebhookPayloadPullRequest["pull_request"]["labels"];
17+
labels: EventPayloads.WebhookPayloadPullRequest["pull_request"]["labels"];
1718
}): string[] => {
1819
switch (action) {
1920
case "closed":
@@ -31,27 +32,35 @@ const getBackportBaseToHead = ({
3132
labels,
3233
pullRequestNumber,
3334
}: {
34-
action: WebhookPayloadPullRequest["action"];
35+
action: EventPayloads.WebhookPayloadPullRequest["action"];
3536
label: { name: string };
36-
labels: WebhookPayloadPullRequest["pull_request"]["labels"];
37+
labels: EventPayloads.WebhookPayloadPullRequest["pull_request"]["labels"];
3738
pullRequestNumber: number;
38-
}): { [base: string]: string } =>
39-
getLabelNames({ action, label, labels }).reduce((baseToHead, labelName) => {
39+
}): { [base: string]: string } => {
40+
const baseToHead: { [base: string]: string } = {};
41+
42+
getLabelNames({ action, label, labels }).forEach((labelName) => {
4043
const matches = labelRegExp.exec(labelName);
41-
if (matches === null) {
42-
return baseToHead;
44+
45+
if (matches !== null) {
46+
const [
47+
,
48+
base,
49+
head = `backport-${pullRequestNumber}-to-${base}`,
50+
] = matches;
51+
baseToHead[base] = head;
4352
}
53+
});
4454

45-
const [, base, head = `backport-${pullRequestNumber}-to-${base}`] = matches;
46-
return { ...baseToHead, [base]: head };
47-
}, {});
55+
return baseToHead;
56+
};
4857

4958
const warnIfSquashIsNotTheOnlyAllowedMergeMethod = async ({
5059
github,
5160
owner,
5261
repo,
5362
}: {
54-
github: GitHub;
63+
github: InstanceType<typeof GitHub>;
5564
owner: string;
5665
repo: string;
5766
}) => {
@@ -84,7 +93,7 @@ const backportOnce = async ({
8493
base: string;
8594
body: string;
8695
commitToBackport: string;
87-
github: GitHub;
96+
github: InstanceType<typeof GitHub>;
8897
head: string;
8998
labelsToAdd: string[];
9099
owner: string;
@@ -153,7 +162,7 @@ const getFailedBackportCommentBody = ({
153162
"# Create a new branch",
154163
`git switch --create ${head}`,
155164
"# Cherry-pick the merged commit of this pull request and resolve the conflicts",
156-
`git cherry-pick ${commitToBackport}`,
165+
`git cherry-pick ---mainline 1 ${commitToBackport}`,
157166
"# Push it to GitHub",
158167
`git push --set-upstream origin ${head}`,
159168
"# Go back to the original working tree",
@@ -169,8 +178,6 @@ const backport = async ({
169178
labelsToAdd,
170179
payload: {
171180
action,
172-
// The payload has a label property when the action is "labeled".
173-
// @ts-ignore
174181
label,
175182
pull_request: {
176183
labels,
@@ -188,7 +195,7 @@ const backport = async ({
188195
token,
189196
}: {
190197
labelsToAdd: string[];
191-
payload: WebhookPayloadPullRequest;
198+
payload: EventPayloads.WebhookPayloadPullRequest;
192199
titleTemplate: string;
193200
token: string;
194201
}) => {
@@ -198,7 +205,8 @@ const backport = async ({
198205

199206
const backportBaseToHead = getBackportBaseToHead({
200207
action,
201-
label,
208+
// The payload has a label property when the action is "labeled".
209+
label: label!,
202210
labels,
203211
pullRequestNumber,
204212
});
@@ -207,7 +215,7 @@ const backport = async ({
207215
return;
208216
}
209217

210-
const github = new GitHub(token);
218+
const github = getOctokit(token);
211219

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

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

230238
for (const [base, head] of Object.entries(backportBaseToHead)) {
231239
const body = `Backport ${commitToBackport} from #${pullRequestNumber}`;
232-
const titleVariables = {
240+
241+
let title = titleTemplate;
242+
Object.entries({
233243
base,
234244
originalTitle,
235-
};
236-
const title = Object.entries(titleVariables).reduce(
237-
(variable, [name, value]) =>
238-
variable.replace(new RegExp(escapeRegExp(`{{${name}}}`), "g"), value),
239-
titleTemplate,
240-
);
245+
}).forEach(([name, value]) => {
246+
title = title.replace(
247+
new RegExp(escapeRegExp(`{{${name}}}`), "g"),
248+
value,
249+
);
250+
});
251+
241252
await group(`Backporting to ${base} on ${head}`, async () => {
242253
try {
243254
await backportOnce({
@@ -252,8 +263,8 @@ const backport = async ({
252263
title,
253264
});
254265
} catch (error) {
255-
const errorMessage = error.message;
256-
logError(`Backport failed: ${errorMessage}`);
266+
const errorMessage: string = error.message;
267+
logError(error);
257268
await github.issues.createComment({
258269
body: getFailedBackportCommentBody({
259270
base,

src/get-labels-to-add.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ export const getLabelsToAdd = (input: string | undefined): string[] => {
88
}
99

1010
const labels = input.split(",");
11-
return labels.map(v => v.trim()).filter(v => v !== "");
11+
return labels.map((v) => v.trim()).filter((v) => v !== "");
1212
};

src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { debug, getInput, setFailed } from "@actions/core";
1+
import { debug, error as logError, getInput, setFailed } from "@actions/core";
22
import { context } from "@actions/github";
3-
import { WebhookPayloadPullRequest } from "@octokit/webhooks";
3+
import { EventPayloads } from "@octokit/webhooks";
44

55
import { backport } from "./backport";
66
import { getLabelsToAdd } from "./get-labels-to-add";
@@ -9,18 +9,19 @@ const run = async () => {
99
try {
1010
const token = getInput("github_token", { required: true });
1111
const titleTemplate = getInput("title_template");
12-
debug(JSON.stringify(context, null, 2));
12+
debug(JSON.stringify(context, undefined, 2));
1313
const labelsInput = getInput("labels");
1414
const labelsToAdd = getLabelsToAdd(labelsInput);
1515
await backport({
1616
labelsToAdd,
17-
payload: context.payload as WebhookPayloadPullRequest,
17+
payload: context.payload as EventPayloads.WebhookPayloadPullRequest,
1818
titleTemplate,
1919
token,
2020
});
2121
} catch (error) {
22+
logError(error);
2223
setFailed(error.message);
2324
}
2425
};
2526

26-
run();
27+
void run();

0 commit comments

Comments
 (0)