Skip to content

Commit

Permalink
Add source_pr_number input for use with workflow_dispatch events.
Browse files Browse the repository at this point in the history
Fixes korthout#433

Signed-off-by: Chance Zibolski <chance.zibolski@gmail.com>
  • Loading branch information
chancez committed Jul 25, 2024
1 parent 924c817 commit eddb81c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
16 changes: 10 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ author: korthout
inputs:
branch_name:
description: >
Template used as the name for branches created by this action.
Template used as the name for branches created by this action.
Placeholders can be used to define variable values.
These are indicated by a dollar sign and curly braces (`${placeholder}`).
Please refer to this action's README for all available placeholders.
Expand Down Expand Up @@ -52,23 +52,23 @@ inputs:
#### `conflict_resolution`
Specifies how the action will handle a conflict occuring during the cherry-pick.
Specifies how the action will handle a conflict occuring during the cherry-pick.
In all cases, the action will stop the cherry-pick at the first conflict encountered.
Behavior is defined by the option selected.
- When set to `fail` the backport fails when the cherry-pick encounters a conflict.
- When set to `draft_commit_conflicts` the backport will always create a draft pull request with the first conflict encountered committed.
Instructions are provided on the original pull request on how to resolve the conflict and continue the cherry-pick.
#### `downstream_repo`
Define if you want to backport to a repository other than where the workflow runs.
By default, the action always backports to the repository in which the workflow runs.
#### `downstream_owner`
Define if you want to backport to another owner than the owner of the repository the workflow runs on.
Only takes effect if the `downstream_repo` property is also defined.
Expand Down Expand Up @@ -122,6 +122,10 @@ inputs:
Note that the pull request's headref is excluded automatically.
Can be used in addition to backport labels.
By default, only backport labels are used to specify the target branches.
source_pr_number:
description: >
Specifty the pull request number to backport instead of detecting based on the pull request closed event.
Only valid when github.event_type == "workflow_dispatch".
outputs:
created_pull_numbers:
Expand Down
15 changes: 13 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ class Backport {
: workflowRepo;
if (repo === undefined)
throw new Error("No repository defined!");
const pull_number = this.github.getPullNumber();
if (this.config.source_pr_number !== undefined &&
this.github.getEventName() !== "workflow_dispatch") {
throw new Error("source_pr_number can only be specified for workflow_dispatch events!");
}
const pull_number = this.config.source_pr_number === undefined
? this.github.getPullNumber()
: this.config.source_pr_number;
const mainpr = yield this.github.getPullRequest(pull_number);
if (!(yield this.github.isMerged(mainpr))) {
const message = "Only merged pull requests can be backported.";
Expand Down Expand Up @@ -467,7 +473,7 @@ class Backport {
const suggestionToResolve = this.composeMessageToResolveCommittedConflicts(target, branchname, commitShasToCherryPick, conflictResolution);
return (0, dedent_1.default) `Created backport PR for \`${target}\`:
- ${downstream}#${pr_number} with remaining conflicts!

${suggestionToResolve}`;
}
createOutput(successByTarget, createdPullRequestNumbers) {
Expand Down Expand Up @@ -767,6 +773,9 @@ class Github {
getPayload() {
return __classPrivateFieldGet(this, _Github_context, "f").payload;
}
getEventName() {
return __classPrivateFieldGet(this, _Github_context, "f").eventName;
}
getPullNumber() {
if (__classPrivateFieldGet(this, _Github_context, "f").payload.pull_request) {
return __classPrivateFieldGet(this, _Github_context, "f").payload.pull_request.number;
Expand Down Expand Up @@ -1078,6 +1087,7 @@ function run() {
const copy_milestone = core.getInput("copy_milestone");
const copy_requested_reviewers = core.getInput("copy_requested_reviewers");
const experimental = JSON.parse(core.getInput("experimental"));
const source_pr_number = core.getInput("source_pr_number");
if (cherry_picking !== "auto" && cherry_picking !== "pull_request_head") {
const message = `Expected input 'cherry_picking' to be either 'auto' or 'pull_request_head', but was '${cherry_picking}'`;
console.error(message);
Expand Down Expand Up @@ -1124,6 +1134,7 @@ function run() {
copy_milestone: copy_milestone === "true",
copy_requested_reviewers: copy_requested_reviewers === "true",
experimental: Object.assign(Object.assign({}, backport_1.experimentalDefaults), experimental),
source_pr_number: source_pr_number === "" ? undefined : parseInt(source_pr_number),
};
const backport = new backport_1.Backport(github, config, git);
return backport.run();
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type Config = {
labels: {
pattern?: RegExp;
};
source_pr_number?: number;
pull: {
description: string;
title: string;
Expand Down Expand Up @@ -104,7 +105,19 @@ export class Backport {

if (repo === undefined) throw new Error("No repository defined!");

const pull_number = this.github.getPullNumber();
if (
this.config.source_pr_number !== undefined &&
this.github.getEventName() !== "workflow_dispatch"
) {
throw new Error(
"source_pr_number can only be specified for workflow_dispatch events!",
);
}

const pull_number =
this.config.source_pr_number === undefined
? this.github.getPullNumber()
: this.config.source_pr_number;
const mainpr = await this.github.getPullRequest(pull_number);

if (!(await this.github.isMerged(mainpr))) {
Expand Down Expand Up @@ -677,7 +690,7 @@ export class Backport {
);
return dedent`Created backport PR for \`${target}\`:
- ${downstream}#${pr_number} with remaining conflicts!
${suggestionToResolve}`;
}

Expand Down
5 changes: 5 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as github from "@actions/github";
export interface GithubApi {
getRepo(): Repo;
getPayload(): Payload;
getEventName(): string;
getPullNumber(): number;
createComment(comment: Comment): Promise<{}>;
getPullRequest(pull_number: number): Promise<PullRequest>;
Expand Down Expand Up @@ -54,6 +55,10 @@ export class Github implements GithubApi {
return this.#context.payload;
}

public getEventName() {
return this.#context.eventName;
}

public getPullNumber() {
if (this.#context.payload.pull_request) {
return this.#context.payload.pull_request.number;
Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async function run(): Promise<void> {
const copy_milestone = core.getInput("copy_milestone");
const copy_requested_reviewers = core.getInput("copy_requested_reviewers");
const experimental = JSON.parse(core.getInput("experimental"));
const source_pr_number = core.getInput("source_pr_number");

if (cherry_picking !== "auto" && cherry_picking !== "pull_request_head") {
const message = `Expected input 'cherry_picking' to be either 'auto' or 'pull_request_head', but was '${cherry_picking}'`;
Expand Down Expand Up @@ -85,6 +86,8 @@ async function run(): Promise<void> {
copy_milestone: copy_milestone === "true",
copy_requested_reviewers: copy_requested_reviewers === "true",
experimental: { ...experimentalDefaults, ...experimental },
source_pr_number:
source_pr_number === "" ? undefined : parseInt(source_pr_number),
};
const backport = new Backport(github, config, git);

Expand Down

0 comments on commit eddb81c

Please sign in to comment.