Skip to content

Commit

Permalink
feat: add support for merge queues (merge_group events) (nrwl#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
janeklb authored Sep 6, 2023
1 parent 349fab8 commit 8419441
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
39 changes: 36 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13943,9 +13943,15 @@ let BASE_SHA;
const headResult = spawnSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf-8' });
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;
if (['pull_request', 'pull_request_target', 'merge_group'].includes(eventName) && !github.context.payload.pull_request.merged) {
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(workflowId, runId, owner, repo, mainBranchName, lastSuccessfulEvent);
Expand Down Expand Up @@ -14029,6 +14035,33 @@ async function findSuccessfulCommit(workflow_id, run_id, owner, repo, branch, la
return await findExistingCommit(shas);
}

async function findMergeBaseRef() {
if (eventName == 'merge_group') {
const mergeQueueBranch = await findMergeQueueBranch(owner, repo, mainBranchName);
return `origin/${mergeQueueBranch}`;
} else {
return 'HEAD'
}
}

function findMergeQueuePr() {
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() {
const pull_number = findMergeQueuePr(mainBranchName);
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 Octokit();
const result = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', { owner, repo, pull_number });
return result.data.head.ref;
}

/**
* Get first existing commit
* @param {string[]} commit_shas
Expand Down
39 changes: 36 additions & 3 deletions find-successful-workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ let BASE_SHA;
const headResult = spawnSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf-8' });
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;
if (['pull_request', 'pull_request_target', 'merge_group'].includes(eventName) && !github.context.payload.pull_request.merged) {
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(workflowId, runId, owner, repo, mainBranchName, lastSuccessfulEvent);
Expand Down Expand Up @@ -113,6 +119,33 @@ async function findSuccessfulCommit(workflow_id, run_id, owner, repo, branch, la
return await findExistingCommit(shas);
}

async function findMergeBaseRef() {
if (eventName == 'merge_group') {
const mergeQueueBranch = await findMergeQueueBranch(owner, repo, mainBranchName);
return `origin/${mergeQueueBranch}`;
} else {
return 'HEAD'
}
}

function findMergeQueuePr() {
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() {
const pull_number = findMergeQueuePr(mainBranchName);
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 Octokit();
const result = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', { owner, repo, pull_number });
return result.data.head.ref;
}

/**
* Get first existing commit
* @param {string[]} commit_shas
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"private": true,
"version": "3.1.1",
"version": "3.2.0",
"license": "MIT",
"description": "This package.json is here purely to control the version of the Action, in combination with https://github.com/JamesHenry/publish-shell-action",
"scripts": {
Expand Down

0 comments on commit 8419441

Please sign in to comment.