-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow backporting to remote repository #405
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6510cd4
feat: allow backport to remote repository
tasso94 4586af2
apply review hints
tasso94 02d15db
apply review hints
tasso94 79396b7
fix: log remote in git fetch errors
korthout 12c1d74
docs: clarify experimental downstream inputs
korthout 3c984b3
dist: build new version
korthout File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,9 +38,13 @@ export type Config = { | |
|
||
type Experimental = { | ||
detect_merge_method: boolean; | ||
downstream_repo?: string; | ||
downstream_owner?: string; | ||
}; | ||
const experimentalDefaults: Experimental = { | ||
detect_merge_method: false, | ||
downstream_repo: undefined, | ||
downstream_owner: undefined, | ||
}; | ||
export { experimentalDefaults }; | ||
|
||
|
@@ -55,25 +59,53 @@ export class Backport { | |
private config; | ||
private git; | ||
|
||
private downstreamRepo; | ||
private downstreamOwner; | ||
|
||
constructor(github: GithubApi, config: Config, git: Git) { | ||
this.github = github; | ||
this.config = config; | ||
this.git = git; | ||
|
||
this.downstreamRepo = this.config.experimental.downstream_repo ?? undefined; | ||
this.downstreamOwner = | ||
this.config.experimental.downstream_owner ?? undefined; | ||
} | ||
|
||
shouldUseDownstreamRepo(): boolean { | ||
return !!this.downstreamRepo; | ||
} | ||
|
||
getRemote(): "downstream" | "origin" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same, could be private |
||
return this.shouldUseDownstreamRepo() ? "downstream" : "origin"; | ||
} | ||
|
||
async run(): Promise<void> { | ||
try { | ||
const payload = this.github.getPayload(); | ||
const owner = this.github.getRepo().owner; | ||
const repo = payload.repository?.name ?? this.github.getRepo().repo; | ||
|
||
const workflowOwner = this.github.getRepo().owner; | ||
const owner = | ||
this.shouldUseDownstreamRepo() && this.downstreamOwner // if undefined, use owner of workflow | ||
? this.downstreamOwner | ||
: workflowOwner; | ||
|
||
const workflowRepo = | ||
payload.repository?.name ?? this.github.getRepo().repo; | ||
const repo = this.shouldUseDownstreamRepo() | ||
? this.downstreamRepo | ||
: workflowRepo; | ||
|
||
if (repo === undefined) throw new Error("No repository defined!"); | ||
|
||
const pull_number = this.github.getPullNumber(); | ||
const mainpr = await this.github.getPullRequest(pull_number); | ||
|
||
if (!(await this.github.isMerged(mainpr))) { | ||
const message = "Only merged pull requests can be backported."; | ||
this.github.createComment({ | ||
owner, | ||
repo, | ||
owner: workflowOwner, | ||
repo: workflowRepo, | ||
issue_number: pull_number, | ||
body: message, | ||
}); | ||
|
@@ -172,8 +204,8 @@ export class Backport { | |
You can either backport this pull request manually, or configure the action to skip merge commits.`; | ||
console.error(message); | ||
this.github.createComment({ | ||
owner, | ||
repo, | ||
owner: workflowOwner, | ||
repo: workflowRepo, | ||
issue_number: pull_number, | ||
body: message, | ||
}); | ||
|
@@ -210,21 +242,25 @@ export class Backport { | |
`Will copy labels matching ${this.config.copy_labels_pattern}. Found matching labels: ${labelsToCopy}`, | ||
); | ||
|
||
if (this.shouldUseDownstreamRepo()) { | ||
await this.git.remoteAdd(this.config.pwd, "downstream", owner, repo); | ||
} | ||
|
||
const successByTarget = new Map<string, boolean>(); | ||
const createdPullRequestNumbers = new Array<number>(); | ||
for (const target of target_branches) { | ||
console.log(`Backporting to target branch '${target}...'`); | ||
|
||
try { | ||
await this.git.fetch(target, this.config.pwd, 1); | ||
await this.git.fetch(target, this.config.pwd, 1, this.getRemote()); | ||
} catch (error) { | ||
if (error instanceof GitRefNotFoundError) { | ||
const message = this.composeMessageForFetchTargetFailure(error.ref); | ||
console.error(message); | ||
successByTarget.set(target, false); | ||
await this.github.createComment({ | ||
owner, | ||
repo, | ||
owner: workflowOwner, | ||
repo: workflowRepo, | ||
issue_number: pull_number, | ||
body: message, | ||
}); | ||
|
@@ -245,7 +281,7 @@ export class Backport { | |
try { | ||
await this.git.checkout( | ||
branchname, | ||
`origin/${target}`, | ||
`${this.getRemote()}/${target}`, | ||
this.config.pwd, | ||
); | ||
} catch (error) { | ||
|
@@ -257,8 +293,8 @@ export class Backport { | |
console.error(message); | ||
successByTarget.set(target, false); | ||
await this.github.createComment({ | ||
owner, | ||
repo, | ||
owner: workflowOwner, | ||
repo: workflowRepo, | ||
issue_number: pull_number, | ||
body: message, | ||
}); | ||
|
@@ -276,16 +312,20 @@ export class Backport { | |
console.error(message); | ||
successByTarget.set(target, false); | ||
await this.github.createComment({ | ||
owner, | ||
repo, | ||
owner: workflowOwner, | ||
repo: workflowRepo, | ||
issue_number: pull_number, | ||
body: message, | ||
}); | ||
continue; | ||
} | ||
|
||
console.info(`Push branch to origin`); | ||
const pushExitCode = await this.git.push(branchname, this.config.pwd); | ||
console.info(`Push branch to ${this.getRemote()}`); | ||
const pushExitCode = await this.git.push( | ||
branchname, | ||
this.getRemote(), | ||
this.config.pwd, | ||
); | ||
if (pushExitCode != 0) { | ||
const message = this.composeMessageForGitPushFailure( | ||
target, | ||
|
@@ -294,8 +334,8 @@ export class Backport { | |
console.error(message); | ||
successByTarget.set(target, false); | ||
await this.github.createComment({ | ||
owner, | ||
repo, | ||
owner: workflowOwner, | ||
repo: workflowRepo, | ||
issue_number: pull_number, | ||
body: message, | ||
}); | ||
|
@@ -320,8 +360,8 @@ export class Backport { | |
const message = | ||
this.composeMessageForCreatePRFailed(new_pr_response); | ||
await this.github.createComment({ | ||
owner, | ||
repo, | ||
owner: workflowOwner, | ||
repo: workflowRepo, | ||
issue_number: pull_number, | ||
body: message, | ||
}); | ||
|
@@ -350,6 +390,10 @@ export class Backport { | |
const set_assignee_response = await this.github.setAssignees( | ||
new_pr.number, | ||
assignees, | ||
{ | ||
owner, | ||
repo, | ||
}, | ||
); | ||
if (set_assignee_response.status != 201) { | ||
console.error(JSON.stringify(set_assignee_response)); | ||
|
@@ -364,7 +408,8 @@ export class Backport { | |
if (reviewers?.length > 0) { | ||
console.info("Setting reviewers " + reviewers); | ||
const reviewRequest = { | ||
...this.github.getRepo(), | ||
owner, | ||
repo, | ||
pull_number: new_pr.number, | ||
reviewers: reviewers, | ||
}; | ||
|
@@ -380,19 +425,27 @@ export class Backport { | |
const label_response = await this.github.labelPR( | ||
new_pr.number, | ||
labelsToCopy, | ||
{ | ||
owner, | ||
repo, | ||
}, | ||
); | ||
if (label_response.status != 200) { | ||
console.error(JSON.stringify(label_response)); | ||
// The PR was still created so let's still comment on the original. | ||
} | ||
} | ||
|
||
const message = this.composeMessageForSuccess(new_pr.number, target); | ||
const message = this.composeMessageForSuccess( | ||
new_pr.number, | ||
target, | ||
this.shouldUseDownstreamRepo() ? `${owner}/${repo}` : "", | ||
); | ||
successByTarget.set(target, true); | ||
createdPullRequestNumbers.push(new_pr.number); | ||
await this.github.createComment({ | ||
owner, | ||
repo, | ||
owner: workflowOwner, | ||
repo: workflowRepo, | ||
issue_number: pull_number, | ||
body: message, | ||
}); | ||
|
@@ -401,8 +454,8 @@ export class Backport { | |
console.error(error.message); | ||
successByTarget.set(target, false); | ||
await this.github.createComment({ | ||
owner, | ||
repo, | ||
owner: workflowOwner, | ||
repo: workflowRepo, | ||
issue_number: pull_number, | ||
body: error.message, | ||
}); | ||
|
@@ -515,9 +568,13 @@ export class Backport { | |
(see action log for full response)`; | ||
} | ||
|
||
private composeMessageForSuccess(pr_number: number, target: string) { | ||
private composeMessageForSuccess( | ||
pr_number: number, | ||
target: string, | ||
downstream: string, | ||
) { | ||
return dedent`Successfully created backport PR for \`${target}\`: | ||
- #${pr_number}`; | ||
- ${downstream}#${pr_number}`; | ||
} | ||
|
||
private createOutput( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: if this doesn't need to be used externally, better make it private