Skip to content

Commit

Permalink
Add support for skipping certain files for backporting (#4)
Browse files Browse the repository at this point in the history
Signed-off-by: Kunal Kotwani <[email protected]>

Signed-off-by: Kunal Kotwani <[email protected]>
  • Loading branch information
kotwanikunal authored and VachaShah committed Oct 22, 2022
1 parent 5b70a85 commit 058a08c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 49 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ inputs:
- mergeCommitSha: SHA of the original PR's merge commit
- number: original PR's number
default: "Backport <%= mergeCommitSha %> from #<%= number %>."
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
59 changes: 10 additions & 49 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const backportOnce = async ({
base,
body,
commitSha,
filesToSkip,
github,
head,
labels,
Expand All @@ -89,6 +90,7 @@ const backportOnce = async ({
base: string;
body: string;
commitSha: string;
filesToSkip: string[];
github: InstanceType<typeof GitHub>;
head: string;
labels: readonly string[];
Expand All @@ -104,6 +106,11 @@ const backportOnce = async ({
await git("switch", "--create", head);
try {
await git("cherry-pick", "-x", commitSha);
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;
Expand Down Expand Up @@ -176,45 +183,9 @@ const getFailedBackportCommentBody = ({
].join("\n");
};

const deleteBackportBranchIfMerged = async ({
base,
github,
head,
owner,
pullRequestNumber,
repo,
}: {
base: string;
github: InstanceType<typeof GitHub>;
head: string;
owner: string;
pullRequestNumber: number;
repo: string;
}) => {
const git = async (...args: string[]) => {
await exec("git", args, { cwd: repo });
};

let isMerged = false;
try {
await github.pulls.checkIfMerged({
owner,
pull_number: pullRequestNumber,
repo,
});
isMerged = true;
} catch {
isMerged = false;
}

if (isMerged) {
info(`The backport PR is merged, deleting the backport branch`);
await git("push", "--delete", "--force", base, head);
}
};

const backport = async ({
branchName,
filesToSkip,
getBody,
getHead,
getLabels,
Expand All @@ -224,6 +195,7 @@ const backport = async ({
token,
}: {
branchName: string,
filesToSkip: string[];
getBody: (
props: Readonly<{
base: string;
Expand Down Expand Up @@ -329,6 +301,7 @@ const backport = async ({
base,
body,
commitSha: mergeCommitSha,
filesToSkip,
github,
head,
labels,
Expand Down Expand Up @@ -357,18 +330,6 @@ const backport = async ({
);
}
});

if (deleteBranch) {
info(`Deleting backport branch ${head}`);
await deleteBackportBranchIfMerged({
base,
github,
head,
owner,
pullRequestNumber,
repo,
});
}
}

return createdPullRequestBaseBranchToNumber;
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 type { PullRequestEvent } from "@octokit/webhooks-types";
import ensureError from "ensure-error";
import { template } from "lodash-es";
import { backport } from "./backport.js";
import { getFilesToSkip } from "./get-files-to-skip";

const run = async () => {
try {
Expand Down Expand Up @@ -34,6 +35,8 @@ const run = async () => {

const token = getInput("github_token", { required: true });
const branchName = getInput("branch_name");
const filesInput = getInput("files_to_skip");
const filesToSkip = getFilesToSkip(filesInput);

if (!context.payload.pull_request) {
throw new Error(`Unsupported event action: ${context.payload.action}.`);
Expand All @@ -49,6 +52,7 @@ const run = async () => {

const createdPullRequestBaseBranchToNumber = await backport({
branchName,
filesToSkip
getBody,
getHead,
getLabels,
Expand Down

0 comments on commit 058a08c

Please sign in to comment.