Skip to content

Commit

Permalink
feat: generate output regarding backport result (korthout#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
alxgrk committed Jan 15, 2023
1 parent aead3d0 commit ef180e5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
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
24 changes: 24 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,15 @@ 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 ef180e5

Please sign in to comment.