Skip to content

Commit

Permalink
Add support for skipping certain files for backporting
Browse files Browse the repository at this point in the history
Signed-off-by: Kunal Kotwani <[email protected]>
  • Loading branch information
kotwanikunal committed Sep 21, 2022
1 parent e20a817 commit 3276ac7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const backportOnce = async ({
owner,
repo,
title,
filesToSkip,
}: {
base: string;
body: string;
Expand All @@ -104,6 +105,7 @@ const backportOnce = async ({
owner: string;
repo: string;
title: string;
filesToSkip: string[];
}) => {
const git = async (...args: string[]) => {
await exec("git", args, { cwd: repo });
Expand All @@ -112,7 +114,11 @@ 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);
if (filesToSkip) {
filesToSkip.forEach(file => await git("checkout", "HEAD", file));
}
await git("commit", "--no-edit", "-s");
} catch (error: unknown) {
await git("cherry-pick", "--abort");
throw error;
Expand Down Expand Up @@ -239,13 +245,15 @@ const backport = async ({
},
titleTemplate,
token,
filesToSkip,
}: {
branchName: string;
deleteBranch: boolean;
labelsToAdd: string[];
payload: EventPayloads.WebhookPayloadPullRequest;
titleTemplate: string;
token: string;
filesToSkip: string[];
}) => {
if (!merged) {
return;
Expand Down Expand Up @@ -310,6 +318,7 @@ const backport = async ({
owner,
repo,
title,
filesToSkip,
});
} catch (error: unknown) {
if (!(error instanceof Error)) {
Expand Down
14 changes: 14 additions & 0 deletions src/get-files-to-skip.ts
Original file line number Diff line number Diff line change
@@ -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 };
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -14,13 +15,16 @@ 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(labelsInput);
await backport({
branchName,
deleteBranch,
labelsToAdd,
payload: context.payload as EventPayloads.WebhookPayloadPullRequest,
titleTemplate,
token,
filesToSkip
});
} catch (error: unknown) {
if (typeof error !== "string" && !(error instanceof Error)) {
Expand Down

0 comments on commit 3276ac7

Please sign in to comment.