Skip to content

Commit

Permalink
Merge pull request #317 from alxgrk/main
Browse files Browse the repository at this point in the history
feat: generate output regarding backport result (#211)
  • Loading branch information
korthout authored Jan 17, 2023
2 parents aead3d0 + 369f654 commit 9797e06
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
9 changes: 9 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ inputs:
Please refer to this action's README for all available placeholders.
default: >-
[Backport ${target_branch}] ${pull_title}
outputs:
was_successful:
description: >
Whether or not the changes could be backported successfully to all targets.
Either 'true' or 'false'.
was_successful_by_target:
description: >
Whether or not the changes could be backported successfully to all targets - broken down by target.
Follows the pattern '{{label}}=true|false'.
runs:
using: "node16"
main: "dist/index.js"
Expand Down
20 changes: 20 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ const core = __importStar(__nccwpck_require__(2186));
const dedent_1 = __importDefault(__nccwpck_require__(5281));
const git = __importStar(__nccwpck_require__(3374));
const utils = __importStar(__nccwpck_require__(918));
var Output;
(function (Output) {
Output["wasSuccessful"] = "was_successful";
Output["wasSuccessfulByTarget"] = "was_successful_by_target";
})(Output || (Output = {}));
class Backport {
constructor(github, config) {
this.github = github;
Expand Down Expand Up @@ -85,6 +90,7 @@ class Backport {
console.log("Determining first and last commit shas, so we can cherry-pick the commit range");
const commitShas = yield this.github.getCommits(mainpr);
console.log(`Found commits: ${commitShas}`);
const successByTarget = new Map();
for (const label of labels) {
console.log(`Working on label ${label.name}`);
// we are looking for labels like "backport stable/0.24"
Expand All @@ -110,6 +116,7 @@ class Backport {
if (error instanceof git.GitRefNotFoundError) {
const message = this.composeMessageForFetchTargetFailure(error.ref);
console.error(message);
successByTarget.set(target, false);
yield this.github.createComment({
owner,
repo,
Expand All @@ -131,6 +138,7 @@ class Backport {
catch (error) {
const message = this.composeMessageForBackportScriptFailure(target, 3, baseref, headref, branchname);
console.error(message);
successByTarget.set(target, false);
yield this.github.createComment({
owner,
repo,
Expand All @@ -145,6 +153,7 @@ class Backport {
catch (error) {
const message = this.composeMessageForBackportScriptFailure(target, 4, baseref, headref, branchname);
console.error(message);
successByTarget.set(target, false);
yield this.github.createComment({
owner,
repo,
Expand All @@ -158,6 +167,7 @@ class Backport {
if (pushExitCode != 0) {
const message = this.composeMessageForGitPushFailure(target, pushExitCode);
console.error(message);
successByTarget.set(target, false);
yield this.github.createComment({
owner,
repo,
Expand All @@ -179,6 +189,7 @@ class Backport {
});
if (new_pr_response.status != 201) {
console.error(JSON.stringify(new_pr_response));
successByTarget.set(target, false);
const message = this.composeMessageForCreatePRFailed(new_pr_response);
yield this.github.createComment({
owner,
Expand All @@ -190,6 +201,7 @@ class Backport {
}
const new_pr = new_pr_response.data;
const message = this.composeMessageForSuccess(new_pr.number, target);
successByTarget.set(target, true);
yield this.github.createComment({
owner,
repo,
Expand All @@ -200,6 +212,7 @@ class Backport {
catch (error) {
if (error instanceof Error) {
console.error(error.message);
successByTarget.set(target, false);
yield this.github.createComment({
owner,
repo,
Expand All @@ -212,6 +225,7 @@ class Backport {
}
}
}
this.createOutput(successByTarget);
}
catch (error) {
if (error instanceof Error) {
Expand Down Expand Up @@ -275,6 +289,12 @@ class Backport {
return (0, dedent_1.default) `Successfully created backport PR for \`${target}\`:
- #${pr_number}`;
}
createOutput(successByTarget) {
const anyTargetFailed = Array.from(successByTarget.values()).includes(false);
core.setOutput(Output.wasSuccessful, !anyTargetFailed);
const byTargetOutput = Array.from(successByTarget.entries()).reduce((i, [target, result]) => `${i}${target}=${result}\n`, "");
core.setOutput(Output.wasSuccessfulByTarget, byTargetOutput);
}
}
exports.Backport = Backport;
/**
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ type Config = {
};
};

enum Output {
wasSuccessful = "was_successful",
wasSuccessfulByTarget = "was_successful_by_target",
}

export class Backport {
private github;
private config;
Expand Down Expand Up @@ -82,6 +87,7 @@ export class Backport {

console.log(`Found commits: ${commitShas}`);

const successByTarget = new Map<string, boolean>();
for (const label of labels) {
console.log(`Working on label ${label.name}`);

Expand Down Expand Up @@ -112,6 +118,7 @@ export class Backport {
if (error instanceof git.GitRefNotFoundError) {
const message = this.composeMessageForFetchTargetFailure(error.ref);
console.error(message);
successByTarget.set(target, false);
await this.github.createComment({
owner,
repo,
Expand Down Expand Up @@ -139,6 +146,7 @@ export class Backport {
branchname
);
console.error(message);
successByTarget.set(target, false);
await this.github.createComment({
owner,
repo,
Expand All @@ -159,6 +167,7 @@ export class Backport {
branchname
);
console.error(message);
successByTarget.set(target, false);
await this.github.createComment({
owner,
repo,
Expand All @@ -176,6 +185,7 @@ export class Backport {
pushExitCode
);
console.error(message);
successByTarget.set(target, false);
await this.github.createComment({
owner,
repo,
Expand All @@ -199,6 +209,7 @@ export class Backport {

if (new_pr_response.status != 201) {
console.error(JSON.stringify(new_pr_response));
successByTarget.set(target, false);
const message =
this.composeMessageForCreatePRFailed(new_pr_response);
await this.github.createComment({
Expand All @@ -212,6 +223,7 @@ export class Backport {
const new_pr = new_pr_response.data;

const message = this.composeMessageForSuccess(new_pr.number, target);
successByTarget.set(target, true);
await this.github.createComment({
owner,
repo,
Expand All @@ -221,6 +233,7 @@ export class Backport {
} catch (error) {
if (error instanceof Error) {
console.error(error.message);
successByTarget.set(target, false);
await this.github.createComment({
owner,
repo,
Expand All @@ -232,6 +245,8 @@ export class Backport {
}
}
}

this.createOutput(successByTarget);
} catch (error) {
if (error instanceof Error) {
console.error(error.message);
Expand Down Expand Up @@ -321,6 +336,19 @@ export class Backport {
return dedent`Successfully created backport PR for \`${target}\`:
- #${pr_number}`;
}

private createOutput(successByTarget: Map<string, boolean>) {
const anyTargetFailed = Array.from(successByTarget.values()).includes(
false
);
core.setOutput(Output.wasSuccessful, !anyTargetFailed);

const byTargetOutput = Array.from(successByTarget.entries()).reduce<string>(
(i, [target, result]) => `${i}${target}=${result}\n`,
""
);
core.setOutput(Output.wasSuccessfulByTarget, byTargetOutput);
}
}

/**
Expand Down

0 comments on commit 9797e06

Please sign in to comment.