Skip to content

Commit 8419441

Browse files
authored
feat: add support for merge queues (merge_group events) (#100)
1 parent 349fab8 commit 8419441

3 files changed

Lines changed: 73 additions & 7 deletions

File tree

dist/index.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13943,9 +13943,15 @@ let BASE_SHA;
1394313943
const headResult = spawnSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf-8' });
1394413944
const HEAD_SHA = headResult.stdout;
1394513945

13946-
if (['pull_request', 'pull_request_target'].includes(eventName) && !github.context.payload.pull_request.merged) {
13947-
const baseResult = spawnSync('git', ['merge-base', `origin/${mainBranchName}`, 'HEAD'], { encoding: 'utf-8' });
13948-
BASE_SHA = baseResult.stdout;
13946+
if (['pull_request', 'pull_request_target', 'merge_group'].includes(eventName) && !github.context.payload.pull_request.merged) {
13947+
try {
13948+
const mergeBaseRef = await findMergeBaseRef();
13949+
const baseResult = spawnSync('git', ['merge-base', `origin/${mainBranchName}`, mergeBaseRef], { encoding: 'utf-8' });
13950+
BASE_SHA = baseResult.stdout;
13951+
} catch (e) {
13952+
core.setFailed(e.message);
13953+
return;
13954+
}
1394913955
} else {
1395013956
try {
1395113957
BASE_SHA = await findSuccessfulCommit(workflowId, runId, owner, repo, mainBranchName, lastSuccessfulEvent);
@@ -14029,6 +14035,33 @@ async function findSuccessfulCommit(workflow_id, run_id, owner, repo, branch, la
1402914035
return await findExistingCommit(shas);
1403014036
}
1403114037

14038+
async function findMergeBaseRef() {
14039+
if (eventName == 'merge_group') {
14040+
const mergeQueueBranch = await findMergeQueueBranch(owner, repo, mainBranchName);
14041+
return `origin/${mergeQueueBranch}`;
14042+
} else {
14043+
return 'HEAD'
14044+
}
14045+
}
14046+
14047+
function findMergeQueuePr() {
14048+
const { head_ref, base_sha } = github.context.payload.merge_group;
14049+
const result = new RegExp(`^refs/heads/gh-readonly-queue/${mainBranchName}/pr-(\\d+)-${base_sha}$`).exec(head_ref);
14050+
return result ? result.at(1) : undefined;
14051+
}
14052+
14053+
async function findMergeQueueBranch() {
14054+
const pull_number = findMergeQueuePr(mainBranchName);
14055+
if (!pull_number) {
14056+
throw new Error('Failed to determine PR number')
14057+
}
14058+
process.stdout.write('\n');
14059+
process.stdout.write(`Found PR #${pull_number} from merge queue branch\n`);
14060+
const octokit = new Octokit();
14061+
const result = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', { owner, repo, pull_number });
14062+
return result.data.head.ref;
14063+
}
14064+
1403214065
/**
1403314066
* Get first existing commit
1403414067
* @param {string[]} commit_shas

find-successful-workflow.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ let BASE_SHA;
2727
const headResult = spawnSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf-8' });
2828
const HEAD_SHA = headResult.stdout;
2929

30-
if (['pull_request', 'pull_request_target'].includes(eventName) && !github.context.payload.pull_request.merged) {
31-
const baseResult = spawnSync('git', ['merge-base', `origin/${mainBranchName}`, 'HEAD'], { encoding: 'utf-8' });
32-
BASE_SHA = baseResult.stdout;
30+
if (['pull_request', 'pull_request_target', 'merge_group'].includes(eventName) && !github.context.payload.pull_request.merged) {
31+
try {
32+
const mergeBaseRef = await findMergeBaseRef();
33+
const baseResult = spawnSync('git', ['merge-base', `origin/${mainBranchName}`, mergeBaseRef], { encoding: 'utf-8' });
34+
BASE_SHA = baseResult.stdout;
35+
} catch (e) {
36+
core.setFailed(e.message);
37+
return;
38+
}
3339
} else {
3440
try {
3541
BASE_SHA = await findSuccessfulCommit(workflowId, runId, owner, repo, mainBranchName, lastSuccessfulEvent);
@@ -113,6 +119,33 @@ async function findSuccessfulCommit(workflow_id, run_id, owner, repo, branch, la
113119
return await findExistingCommit(shas);
114120
}
115121

122+
async function findMergeBaseRef() {
123+
if (eventName == 'merge_group') {
124+
const mergeQueueBranch = await findMergeQueueBranch(owner, repo, mainBranchName);
125+
return `origin/${mergeQueueBranch}`;
126+
} else {
127+
return 'HEAD'
128+
}
129+
}
130+
131+
function findMergeQueuePr() {
132+
const { head_ref, base_sha } = github.context.payload.merge_group;
133+
const result = new RegExp(`^refs/heads/gh-readonly-queue/${mainBranchName}/pr-(\\d+)-${base_sha}$`).exec(head_ref);
134+
return result ? result.at(1) : undefined;
135+
}
136+
137+
async function findMergeQueueBranch() {
138+
const pull_number = findMergeQueuePr(mainBranchName);
139+
if (!pull_number) {
140+
throw new Error('Failed to determine PR number')
141+
}
142+
process.stdout.write('\n');
143+
process.stdout.write(`Found PR #${pull_number} from merge queue branch\n`);
144+
const octokit = new Octokit();
145+
const result = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', { owner, repo, pull_number });
146+
return result.data.head.ref;
147+
}
148+
116149
/**
117150
* Get first existing commit
118151
* @param {string[]} commit_shas

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"private": true,
3-
"version": "3.1.1",
3+
"version": "3.2.0",
44
"license": "MIT",
55
"description": "This package.json is here purely to control the version of the Action, in combination with https://github.com/JamesHenry/publish-shell-action",
66
"scripts": {

0 commit comments

Comments
 (0)