Skip to content
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

fix: return correct base_sha when merge_group event happens #145

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 6 additions & 49 deletions find-successful-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,12 @@ let BASE_SHA: string;
});
const HEAD_SHA = headResult.stdout;

if (
(['pull_request', 'pull_request_target'].includes(eventName) &&
!github.context.payload.pull_request.merged) ||
eventName == 'merge_group'
) {
try {
const mergeBaseRef = await findMergeBaseRef();
const baseResult = spawnSync(
'git',
['merge-base', `origin/${mainBranchName}`, mergeBaseRef],
{ encoding: 'utf-8' },
);
BASE_SHA = baseResult.stdout;
} catch (e) {
core.setFailed(e.message);
return;
}
if (['pull_request', 'pull_request_target'].includes(eventName) && !github.context.payload.pull_request.merged) {
const baseResult = spawnSync('git', ['merge-base', `origin/${mainBranchName}`, 'HEAD'], { encoding: 'utf-8' });
BASE_SHA = baseResult.stdout;
} else if (eventName == "merge_group") {
const baseResult = spawnSync('git', ['rev-parse', 'HEAD^1'], { encoding: 'utf-8' });
BASE_SHA = baseResult.stdout;
} else {
try {
BASE_SHA = await findSuccessfulCommit(
Expand Down Expand Up @@ -204,38 +193,6 @@ async function findSuccessfulCommit(
return await findExistingCommit(octokit, branch, shas);
}

async function findMergeBaseRef(): Promise<string> {
if (eventName == 'merge_group') {
const mergeQueueBranch = await findMergeQueueBranch();
return `origin/${mergeQueueBranch}`;
} else {
return 'HEAD';
}
}

function findMergeQueuePr(): string {
const { head_ref, base_sha } = github.context.payload.merge_group;
const result = new RegExp(
`^refs/heads/gh-readonly-queue/${mainBranchName}/pr-(\\d+)-${base_sha}$`,
).exec(head_ref);
return result ? result.at(1) : undefined;
}

async function findMergeQueueBranch(): Promise<string> {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In merge-queue scenario, base will always be the previous commit in the queue. That's why we can remove these functions here.

const pull_number = findMergeQueuePr();
if (!pull_number) {
throw new Error('Failed to determine PR number');
}
process.stdout.write('\n');
process.stdout.write(`Found PR #${pull_number} from merge queue branch\n`);
const octokit = new ProxifiedClient();
const result = await octokit.request(
`GET /repos/${owner}/${repo}/pulls/${pull_number}`,
{ owner, repo, pull_number: +pull_number },
);
return result.data.head.ref;
}

/**
* Get first existing commit
*/
Expand Down