Skip to content

Commit

Permalink
Adding an option for custom branch name for backport (#1)
Browse files Browse the repository at this point in the history
Signed-off-by: Vacha Shah <vachshah@amazon.com>
  • Loading branch information
VachaShah committed Jan 5, 2022
1 parent 7eb9608 commit 727f307
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { GitHub } from "@actions/github/lib/utils";
import { EventPayloads } from "@octokit/webhooks";
import escapeRegExp from "lodash/escapeRegExp";

export interface Inputs {
branchName: string
}

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

const getLabelNames = ({
Expand All @@ -31,11 +35,13 @@ const getBackportBaseToHead = ({
label,
labels,
pullRequestNumber,
branchName,
}: {
action: EventPayloads.WebhookPayloadPullRequest["action"];
label: { name: string };
labels: EventPayloads.WebhookPayloadPullRequest["pull_request"]["labels"];
pullRequestNumber: number;
branchName: string;
}): Record<string, string> => {
const baseToHead: Record<string, string> = {};

Expand All @@ -46,7 +52,7 @@ const getBackportBaseToHead = ({
const [
,
base,
head = `backport-${pullRequestNumber}-to-${base}`,
head = (branchName != null) ? branchName : `backport-${pullRequestNumber}-to-${base}`,
] = matches;
baseToHead[base] = head;
}
Expand Down Expand Up @@ -193,11 +199,13 @@ const backport = async ({
},
titleTemplate,
token,
inputs,
}: {
labelsToAdd: string[];
payload: EventPayloads.WebhookPayloadPullRequest;
titleTemplate: string;
token: string;
inputs: Inputs;
}) => {
if (merged !== true) {
return;
Expand All @@ -209,6 +217,7 @@ const backport = async ({
label: label!,
labels,
pullRequestNumber,
inputs.branchName,
});

if (Object.keys(backportBaseToHead).length === 0) {
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { debug, error as logError, getInput, setFailed } from "@actions/core";
import { context } from "@actions/github";
import { EventPayloads } from "@octokit/webhooks";

import { backport } from "./backport";
import { Inputs, backport } from "./backport";
import { getLabelsToAdd } from "./get-labels-to-add";

const run = async () => {
try {
const inputs: Inputs = {
branchName: getInput('branch-name'),
}
const token = getInput("github_token", { required: true });
const titleTemplate = getInput("title_template");
debug(JSON.stringify(context, undefined, 2));
Expand All @@ -17,6 +20,7 @@ const run = async () => {
payload: context.payload as EventPayloads.WebhookPayloadPullRequest,
titleTemplate,
token,
inputs
});
} catch (error: unknown) {
if (typeof error !== "string" && !(error instanceof Error)) {
Expand Down

0 comments on commit 727f307

Please sign in to comment.