Skip to content

Commit

Permalink
Add add_labels feature (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanVann authored Sep 5, 2020
1 parent ce3759c commit 5c6b52c
Show file tree
Hide file tree
Showing 4 changed files with 35 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:
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.
required: false
runs:
using: node12
main: dist/index.js
Expand Down
17 changes: 16 additions & 1 deletion src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const backportOnce = async ({
commitToBackport,
github,
head,
labelsToAdd,
owner,
repo,
title,
Expand All @@ -84,6 +85,7 @@ const backportOnce = async ({
commitToBackport: string;
github: GitHub;
head: string;
labelsToAdd: string[];
owner: string;
repo: string;
title: string;
Expand All @@ -102,14 +104,24 @@ const backportOnce = async ({
}

await git("push", "--set-upstream", "origin", head);
await github.pulls.create({
const {
data: { number: pullRequestNumber },
} = await github.pulls.create({
base,
body,
head,
owner,
repo,
title,
});
if (labelsToAdd.length > 0) {
await github.issues.addLabels({
issue_number: pullRequestNumber,
labels: labelsToAdd,
owner,
repo,
});
}
};

const getFailedBackportCommentBody = ({
Expand Down Expand Up @@ -153,6 +165,7 @@ const getFailedBackportCommentBody = ({
};

const backport = async ({
labelsToAdd,
payload: {
action,
// The payload has a label property when the action is "labeled".
Expand All @@ -172,6 +185,7 @@ const backport = async ({
},
token,
}: {
labelsToAdd: string[];
payload: WebhookPayloadPullRequest;
token: string;
}) => {
Expand Down Expand Up @@ -221,6 +235,7 @@ const backport = async ({
commitToBackport,
github,
head,
labelsToAdd,
owner,
repo,
title,
Expand Down
12 changes: 12 additions & 0 deletions src/get-labels-to-add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Get an array of labels to be added to the PR.
* Filtering out empty strings.
*/
export const getLabelsToAdd = (input: string | undefined): string[] => {
if (input === undefined || input === "") {
return [];
}

const labels = input.split(",");
return labels.map(v => v.trim()).filter(v => v !== "");
};
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { context } from "@actions/github";
import { WebhookPayloadPullRequest } from "@octokit/webhooks";

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

const run = async () => {
try {
const token = getInput("github_token", { required: true });
debug(JSON.stringify(context, null, 2));
const labelsInput = getInput("labels");
const labelsToAdd = getLabelsToAdd(labelsInput);
await backport({
labelsToAdd,
payload: context.payload as WebhookPayloadPullRequest,
token,
});
Expand Down

0 comments on commit 5c6b52c

Please sign in to comment.