Skip to content

Commit 030487a

Browse files
alan-agius4devversion
authored andcommitted
fix(github-actions): update create-pr-for-changes to rebase on conflicts
Ensures the action automatically rebases pull requests when conflicts are detected, improving automation reliability and reducing manual intervention.
1 parent 2c7bab7 commit 030487a

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

github-actions/create-pr-for-changes/lib/main.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,34 @@ async function main(): Promise<void> {
9292

9393
if (matchingPrs.length > 0) {
9494
// A PR for the same set of changes does already exist. Exit.
95-
core.info(
96-
`Skipping PR creation, because there is already a PR: #${matchingPrs[0].number} ` +
97-
`(${matchingPrs[0].html_url})`,
98-
);
95+
for (const matchingPr of matchingPrs) {
96+
// Check whether there is a PR is conflicting
97+
const {
98+
data: {mergeable},
99+
} = await git.github.pulls.get({
100+
owner: repo.owner,
101+
repo: repo.name,
102+
pull_number: matchingPr.number,
103+
});
104+
105+
core.info(
106+
`Skipping PR creation, because there is already a PR: #${matchingPr.number} ` +
107+
`(${matchingPr.html_url})`,
108+
);
109+
110+
if (!mergeable) {
111+
core.info(`PR is not mergable, rebasing branch: #${branchName}`);
112+
// Push the local branch to update the target branch (already rebased)
113+
git.run(['checkout', '-b', branchName]);
114+
git.run([
115+
'push',
116+
'--force-with-lease',
117+
getRepositoryGitUrl(forkRepo, git.githubToken),
118+
`HEAD:refs/heads/${branchName}`,
119+
]);
120+
}
121+
}
122+
99123
return;
100124
} else {
101125
core.info(`No pre-existing PR found for branch '${branchName}'.`);

github-actions/create-pr-for-changes/main.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47972,7 +47972,24 @@ async function main() {
4797247972
state: "open"
4797347973
});
4797447974
if (matchingPrs.length > 0) {
47975-
core.info(`Skipping PR creation, because there is already a PR: #${matchingPrs[0].number} (${matchingPrs[0].html_url})`);
47975+
for (const matchingPr of matchingPrs) {
47976+
const { data: { mergeable } } = await git.github.pulls.get({
47977+
owner: repo.owner,
47978+
repo: repo.name,
47979+
pull_number: matchingPr.number
47980+
});
47981+
core.info(`Skipping PR creation, because there is already a PR: #${matchingPr.number} (${matchingPr.html_url})`);
47982+
if (!mergeable) {
47983+
core.info(`PR is not mergable, rebasing branch: #${branchName}`);
47984+
git.run(["checkout", "-b", branchName]);
47985+
git.run([
47986+
"push",
47987+
"--force-with-lease",
47988+
getRepositoryGitUrl(forkRepo, git.githubToken),
47989+
`HEAD:refs/heads/${branchName}`
47990+
]);
47991+
}
47992+
}
4797647993
return;
4797747994
} else {
4797847995
core.info(`No pre-existing PR found for branch '${branchName}'.`);

0 commit comments

Comments
 (0)