Skip to content

Commit

Permalink
Add output and improve logging (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
tibdex committed Apr 10, 2022
1 parent 257e83a commit 73b72b8
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 24 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/backport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ on:

jobs:
backport:
runs-on: ubuntu-18.04
name: Backport
runs-on: ubuntu-latest
# Only react to merged PRs for security reasons.
# See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target.
if: >
Expand All @@ -21,7 +21,6 @@ jobs:
)
)
steps:
- name: Backport
uses: tibdex/backport@v2
- uses: tibdex/backport@v2
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
jobs:
publish:
name: Publish
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: dylanvann/publish-github-action@v1.1.49
Expand Down
14 changes: 9 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Backporting
author: Thibault Derousseaux <tibdex@gmail.com>
description: Automatically backport PRs to other branches by simply labeling them.
description: >
Automatically backport PRs to other branches by simply labeling them.
inputs:
body_template:
description: >
Expand All @@ -11,7 +12,10 @@ inputs:
- body: original PR's body
- mergeCommitSha: SHA of the original PR's merge commit
- number: original PR's number
default: "Backport <%= mergeCommitSha %> from #<%= number %>"
default: "Backport <%= mergeCommitSha %> from #<%= number %>."
github_token:
description: Token for the GitHub API.
required: true
head_template:
description: >
Lodash template for the backport PR's head branch.
Expand All @@ -33,9 +37,6 @@ inputs:
- base: backport PR's base branch
- labels: array containing the original PR's labels, excluding those matching `label_pattern`.
default: "[]"
github_token:
description: Token for the GitHub API.
required: true
title_template:
description: >
Lodash template for the backport PR's title.
Expand All @@ -45,6 +46,9 @@ inputs:
- number: original PR's number
- title: original PR's title
default: "[Backport <%= base %>] <%= title %>"
outputs:
created_pull_requests:
description: A JSON stringified object mapping the base branch of the created pull requests to their number.
runs:
using: node16
main: dist/index.js
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "backport",
"version": "2.0.1",
"version": "2.0.2",
"license": "MIT",
"main": "dist/index.js",
"files": [
Expand Down
25 changes: 17 additions & 8 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ const backportOnce = async ({
commitSha: string;
github: InstanceType<typeof GitHub>;
head: string;
labels: string[];
labels: readonly string[];
owner: string;
repo: string;
title: string;
}>) => {
}>): Promise<number> => {
const git = async (...args: string[]) => {
await exec("git", args, { cwd: repo });
};
Expand Down Expand Up @@ -125,12 +125,15 @@ const backportOnce = async ({
"PUT /repos/{owner}/{repo}/issues/{issue_number}/labels",
{
issue_number: number,
labels,
labels: [...labels],
owner,
repo,
},
);
}

info(`PR #${number} has been created.`);
return number;
};

const getFailedBackportCommentBody = ({
Expand Down Expand Up @@ -212,7 +215,7 @@ const backport = async ({
labelRegExp: RegExp;
payload: PullRequestClosedEvent | PullRequestLabeledEvent;
token: string;
}) => {
}): Promise<{ [base: string]: number }> => {
const {
pull_request: {
body: originalBody,
Expand All @@ -238,14 +241,15 @@ const backport = async ({
const baseBranches = getBaseBranches({ labelRegExp, payload });

if (baseBranches.length === 0) {
return;
info("No backports required.");
return {};
}

const github = getOctokit(token);

await warnIfSquashIsNotTheOnlyAllowedMergeMethod({ github, owner, repo });

info(`Backporting ${mergeCommitSha} from #${number}`);
info(`Backporting ${mergeCommitSha} from #${number}.`);

await exec("git", [
"clone",
Expand All @@ -259,6 +263,8 @@ const backport = async ({
]);
await exec("git", ["config", "--global", "user.name", "github-actions[bot]"]);

const createdPullRequestBaseBranchToNumber: { [base: string]: number } = {};

for (const base of baseBranches) {
const body = getBody({
base,
Expand All @@ -277,9 +283,9 @@ const backport = async ({

// PRs are handled sequentially to avoid breaking GitHub's log grouping feature.
// eslint-disable-next-line no-await-in-loop
await group(`Backporting to ${base} on ${head}`, async () => {
await group(`Backporting to ${base} on ${head}.`, async () => {
try {
await backportOnce({
const backportPullRequestNumber = await backportOnce({
base,
body,
commitSha: mergeCommitSha,
Expand All @@ -290,6 +296,7 @@ const backport = async ({
repo,
title,
});
createdPullRequestBaseBranchToNumber[base] = backportPullRequestNumber;
} catch (_error: unknown) {
const error = ensureError(_error);
logError(error);
Expand All @@ -311,6 +318,8 @@ const backport = async ({
}
});
}

return createdPullRequestBaseBranchToNumber;
};

export { backport };
18 changes: 13 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getInput, setFailed } from "@actions/core";
import { getInput, setFailed, setOutput } from "@actions/core";
import { context } from "@actions/github";
import type { PullRequestEvent } from "@octokit/webhooks-types";
import ensureError from "ensure-error";
Expand All @@ -14,9 +14,6 @@ const run = async () => {
"title_template",
].map((name) => template(getInput(name)));

const labelPattern = getInput("label_pattern");
const token = getInput("github_token", { required: true });

const getLabels = ({
base,
labels,
Expand All @@ -32,8 +29,15 @@ const run = async () => {
}
};

const labelPattern = getInput("label_pattern");
const labelRegExp = new RegExp(labelPattern);

const token = getInput("github_token", { required: true });

if (!context.payload.pull_request) {
throw new Error(`Unsupported event action: ${context.payload.action}.`);
}

const payload = context.payload as PullRequestEvent;

if (payload.action !== "closed" && payload.action !== "labeled") {
Expand All @@ -42,7 +46,7 @@ const run = async () => {
);
}

await backport({
const createdPullRequestBaseBranchToNumber = await backport({
getBody,
getHead,
getLabels,
Expand All @@ -51,6 +55,10 @@ const run = async () => {
payload,
token,
});
setOutput(
"created_pull_requests",
JSON.stringify(createdPullRequestBaseBranchToNumber),
);
} catch (_error: unknown) {
const error = ensureError(_error);
setFailed(error);
Expand Down
9 changes: 8 additions & 1 deletion xo.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ module.exports = {
extends: ["prettier"],
overrides: [
{
files: "**/*.{ts,tsx}",
files: "**/*.ts",
rules: {
// Index signatures are slightly more legible since the key must be named.
"@typescript-eslint/consistent-indexed-object-style": [
"error",
"index-signature",
],
// Too restrictive.
"@typescript-eslint/restrict-template-expressions": "off",
// Covered by TypeScript.
"default-case": "off",
},
Expand Down

0 comments on commit 73b72b8

Please sign in to comment.