Skip to content

Commit

Permalink
Update find-successful-workflow.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo9mv authored and jakemhiller committed Jan 8, 2025
1 parent e2e6dc8 commit 97e3b04
Showing 1 changed file with 6 additions and 49 deletions.
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> {
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

0 comments on commit 97e3b04

Please sign in to comment.