diff --git a/action.yml b/action.yml index 2fc3bd1..a6bbc3c 100644 --- a/action.yml +++ b/action.yml @@ -2,12 +2,16 @@ name: Backporting author: Thibault Derousseaux 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 diff --git a/package.json b/package.json index a3c53de..ae497f6 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } diff --git a/src/backport.ts b/src/backport.ts index 5bb9821..c91cd61 100644 --- a/src/backport.ts +++ b/src/backport.ts @@ -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 ([^ ]+)(?: ([^ ]+))?$/; @@ -183,10 +184,12 @@ const backport = async ({ owner: { login: owner }, }, }, + titleTemplate, token, }: { labelsToAdd: string[]; payload: WebhookPayloadPullRequest; + titleTemplate: string; token: string; }) => { if (!merged) { @@ -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({ diff --git a/src/index.ts b/src/index.ts index e4199a6..67894aa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) { diff --git a/yarn.lock b/yarn.lock index 41c384c..09e254d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"