Skip to content

Commit 2086dc1

Browse files
committed
feat: enable GitHub auto-merge for PRs
- Try to enable auto-merge via GitHub API when merge=true - Fall back to manual merge if auto-merge fails or is not available - Only perform manual merge if auto-merge was not successfully enabled - Update log message to clarify when merge is manual vs auto This allows GitHub to automatically merge PRs when they become ready (CI passes, approval requirements met), reducing the need for manual intervention or periodic script re-runs.
1 parent ec13f93 commit 2086dc1

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

script.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,39 @@ export async function script(
362362
}
363363
}
364364

365+
let autoMergeEnabled = false;
366+
if (merge) {
367+
try {
368+
await octokit.graphql(
369+
`mutation enableAutoMerge($pullRequestId: ID!) {
370+
enablePullRequestAutoMerge(input: {
371+
pullRequestId: $pullRequestId
372+
mergeMethod: SQUASH
373+
}) {
374+
pullRequest {
375+
autoMergeRequest {
376+
enabledAt
377+
}
378+
}
379+
}
380+
}`,
381+
{
382+
pullRequestId: id,
383+
},
384+
);
385+
octokit.log.info(
386+
"auto-merge enabled, GitHub will merge when ready: %s",
387+
pr.html_url,
388+
);
389+
autoMergeEnabled = true;
390+
} catch (error) {
391+
// Auto-merge not allowed or failed, fall back to manual merge
392+
octokit.log.info(
393+
`${repository.full_name}: auto-merge not available (${error.message}), falling back to manual merge`,
394+
);
395+
}
396+
}
397+
365398
// Copied from
366399
// https://github.com/gr2m/octoherd-script-merge-pull-requests/blob/main/script.js
367400
const result = await octokit.graphql(
@@ -502,7 +535,7 @@ export async function script(
502535
}
503536
}
504537

505-
if (merge) {
538+
if (merge && !autoMergeEnabled) {
506539
const commit_title = `${pr.title} (#${pr.number})`;
507540
await octokit.request(
508541
"PUT /repos/{owner}/{repo}/pulls/{pull_number}/merge",
@@ -514,7 +547,7 @@ export async function script(
514547
merge_method: "squash",
515548
},
516549
);
517-
octokit.log.info("pull request merged: %s", pr.html_url);
550+
octokit.log.info("pull request manually merged: %s", pr.html_url);
518551
} else {
519552
octokit.log.info(
520553
"pull request ready to merge (merge disabled): %s",

0 commit comments

Comments
 (0)