Skip to content

Commit

Permalink
Allow configuring title (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanVann authored Sep 5, 2020
1 parent 5c6b52c commit 5370ddc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
8 changes: 6 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ name: Backporting
author: Thibault Derousseaux <[email protected]>
description: Automatically backport PRs to other branches by simply labeling them.
inputs:
add_labels:
description: Comma separated list of labels to add to the backport PR.
required: false
github_token:
description: Token for the GitHub API.
required: true
add_labels:
description: A comma separated list of labels to add to the backport PR.
title_template:
description: Template for the title of the backport PR.
required: false
default: "[Backport {{base}}] {{originalTitle}}"
runs:
using: node12
main: dist/index.js
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@actions/exec": "^1.0.1",
"@actions/github": "^1.1.0",
"@octokit/webhooks": "^6.3.0",
"@types/lodash.escaperegexp": "^4.1.6",
"@types/node": "^10.0.3",
"@types/promise-retry": "^1.1.1",
"@typescript-eslint/eslint-plugin": "^2.3.1",
Expand All @@ -33,6 +34,7 @@
"eslint-plugin-sort-destructure-keys": "^1.3.3",
"eslint-plugin-typescript-sort-keys": "^0.4.0",
"eslint-plugin-unicorn": "^12.0.1",
"lodash.escaperegexp": "^4.1.2",
"prettier": "^1.18.2",
"typescript": "^3.6.3"
}
Expand Down
13 changes: 12 additions & 1 deletion src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 escapeRegExp from "lodash.escaperegexp";

const labelRegExp = /^backport ([^ ]+)(?: ([^ ]+))?$/;

Expand Down Expand Up @@ -183,10 +184,12 @@ const backport = async ({
owner: { login: owner },
},
},
titleTemplate,
token,
}: {
labelsToAdd: string[];
payload: WebhookPayloadPullRequest;
titleTemplate: string;
token: string;
}) => {
if (!merged) {
Expand Down Expand Up @@ -226,7 +229,15 @@ const backport = async ({

for (const [base, head] of Object.entries(backportBaseToHead)) {
const body = `Backport ${commitToBackport} from #${pullRequestNumber}`;
const title = `[Backport ${base}] ${originalTitle}`;
const titleVariables = {
base,
originalTitle,
};
const title = Object.entries(titleVariables).reduce(
(variable, [name, value]) =>
variable.replace(new RegExp(escapeRegExp(`{{${name}}}`), "g"), value),
titleTemplate,
);
await group(`Backporting to ${base} on ${head}`, async () => {
try {
await backportOnce({
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { getLabelsToAdd } from "./get-labels-to-add";
const run = async () => {
try {
const token = getInput("github_token", { required: true });
const titleTemplate = getInput("title_template");
debug(JSON.stringify(context, null, 2));
const labelsInput = getInput("labels");
const labelsToAdd = getLabelsToAdd(labelsInput);
await backport({
labelsToAdd,
payload: context.payload as WebhookPayloadPullRequest,
titleTemplate,
token,
});
} catch (error) {
Expand Down
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/lodash.escaperegexp@^4.1.6":
version "4.1.6"
resolved "https://registry.yarnpkg.com/@types/lodash.escaperegexp/-/lodash.escaperegexp-4.1.6.tgz#3e2326aa33e95b2f3801de027f04a4f7aba2af4f"
integrity sha512-uENiqxLlqh6RzeE1cC6Z2gHqakToN9vKlTVCFkSVjAfeMeh2fY0916tHwJHeeKs28qB/hGYvKuampGYH5QDVCw==
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.14.149"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.149.tgz#1342d63d948c6062838fbf961012f74d4e638440"
integrity sha512-ijGqzZt/b7BfzcK9vTrS6MFljQRPn5BFWOx8oE0GYxribu6uV+aA9zZuXI1zc/etK9E8nrgdoF2+LgUw7+9tJQ==

"@types/node@>= 8":
version "12.12.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.8.tgz#dab418655af39ce2fa99286a0bed21ef8072ac9d"
Expand Down Expand Up @@ -1065,6 +1077,11 @@ lodash.defaultsdeep@^4.6.1:
resolved "https://registry.yarnpkg.com/lodash.defaultsdeep/-/lodash.defaultsdeep-4.6.1.tgz#512e9bd721d272d94e3d3a63653fa17516741ca6"
integrity sha512-3j8wdDzYuWO3lM3Reg03MuQR957t287Rpcxp1njpEa8oDrikb+FwGdW3n+FELh/A6qib6yPit0j/pv9G/yeAqA==

lodash.escaperegexp@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz#64762c48618082518ac3df4ccf5d5886dae20347"
integrity sha1-ZHYsSGGAglGKw99Mz11YhtriA0c=

lodash.get@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
Expand Down

0 comments on commit 5370ddc

Please sign in to comment.