From eff36ec856be5f47230fdce47d74ac44ba620091 Mon Sep 17 00:00:00 2001 From: Kunal Kotwani Date: Wed, 21 Sep 2022 14:04:06 -0700 Subject: [PATCH] Add support for skipping certain files for backporting Signed-off-by: Kunal Kotwani --- action.yml | 3 +++ src/backport.ts | 12 +++++++++++- src/get-files-to-skip.ts | 14 ++++++++++++++ src/index.ts | 4 ++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/get-files-to-skip.ts diff --git a/action.yml b/action.yml index cbe0c96..7b15b01 100644 --- a/action.yml +++ b/action.yml @@ -5,6 +5,9 @@ inputs: add_labels: description: Comma separated list of labels to add to the backport PR. required: false + files_to_skip: + description: Comma seperated list of files to be skipped from the backport + required: false branch_name: description: Custom branch name for the backport PR. required: false diff --git a/src/backport.ts b/src/backport.ts index afce466..1414f62 100644 --- a/src/backport.ts +++ b/src/backport.ts @@ -88,6 +88,7 @@ const backportOnce = async ({ base, body, commitToBackport, + filesToSkip, github, head, labelsToAdd, @@ -98,6 +99,7 @@ const backportOnce = async ({ base: string; body: string; commitToBackport: string; + filesToSkip: string[]; github: InstanceType; head: string; labelsToAdd: string[]; @@ -112,7 +114,12 @@ const backportOnce = async ({ await git("switch", base); await git("switch", "--create", head); try { - await git("cherry-pick", "-x", commitToBackport); + await git("cherry-pick", "-x", "-n", commitToBackport); + for (const file of filesToSkip) { + await git("checkout", "HEAD", file); + } + + await git("commit", "--no-edit", "-s"); } catch (error: unknown) { await git("cherry-pick", "--abort"); throw error; @@ -221,6 +228,7 @@ const deleteBackportBranchIfMerged = async ({ const backport = async ({ branchName, deleteBranch, + filesToSkip, labelsToAdd, payload: { action, @@ -242,6 +250,7 @@ const backport = async ({ }: { branchName: string; deleteBranch: boolean; + filesToSkip: string[]; labelsToAdd: string[]; payload: EventPayloads.WebhookPayloadPullRequest; titleTemplate: string; @@ -304,6 +313,7 @@ const backport = async ({ base, body, commitToBackport, + filesToSkip, github, head, labelsToAdd, diff --git a/src/get-files-to-skip.ts b/src/get-files-to-skip.ts new file mode 100644 index 0000000..3bb456b --- /dev/null +++ b/src/get-files-to-skip.ts @@ -0,0 +1,14 @@ +/** + * Get an array of files to be skipped from the backport PR. + * Filtering out empty strings. + */ +const getFilesToSkip = (input: string | undefined): string[] => { + if (input === undefined || input === "") { + return []; + } + + const files_to_skip = input.split(","); + return files_to_skip.map((v) => v.trim()).filter((v) => v !== ""); +}; + +export { getFilesToSkip }; diff --git a/src/index.ts b/src/index.ts index b3c3c81..07201fc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { EventPayloads } from "@octokit/webhooks"; import { backport } from "./backport"; import { getLabelsToAdd } from "./get-labels-to-add"; +import { getFilesToSkip } from "./get-files-to-skip"; const run = async () => { try { @@ -14,9 +15,12 @@ const run = async () => { debug(JSON.stringify(context, undefined, 2)); const labelsInput = getInput("add_labels"); const labelsToAdd = getLabelsToAdd(labelsInput); + const filesInput = getInput("files_to_skip"); + const filesToSkip = getFilesToSkip(filesInput); await backport({ branchName, deleteBranch, + filesToSkip, labelsToAdd, payload: context.payload as EventPayloads.WebhookPayloadPullRequest, titleTemplate,