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 Mar 14, 2024
1 parent eb057ff commit 5b51ae1
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions find-successful-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,23 @@ let BASE_SHA: string;
});
const HEAD_SHA = headResult.stdout;

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;
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;
}
} else {
try {
BASE_SHA = await findSuccessfulCommit(
Expand Down Expand Up @@ -175,6 +186,38 @@ 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 5b51ae1

Please sign in to comment.