Skip to content

Commit

Permalink
feat: add support for pull_request events (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
yevhenii-ponomarenko authored Nov 3, 2022
1 parent fef8b17 commit e358289
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
54 changes: 39 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,58 @@ const run = async () => {
const prTitle = core.getInput('pr_title');
const prBody = core.getInput('pr_body');
const baseBranch = core.getInput('destination_branch');
const sourceBranch = github.context.ref.replace(/^refs\/heads\//, '');
// On 'push' events, github.context.ref is in format 'refs/heads/<sourceBranch>'
// On 'pull_request' events, github.contest.ref is in format 'refs/pull/<pullRequestNumber>/merge'
// See https://docs.github.com/en/actions/learn-github-actions/environment-variables
let sourceBranch = github.context.ref.match(/^refs\/heads\/(.?)$/)?.[1];
const sourcePullRequestNumber = github.context.ref.match(/^refs\/pull\/(\d+)\/merge/)?.[1];

const credentials = {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
};

const octokit = github.getOctokit(githubToken);
core.info(`Looking up a pull request with a source branch "${sourceBranch || '<not found>'}" and a base branch "${baseBranch || '<not specified>'}"`);

const branchHead = `${credentials.owner}:${sourceBranch}`;
const { data: pulls } = await octokit.rest.pulls.list({
...credentials,
base: baseBranch,
head: branchHead,
});
let pullRequest;
if (sourceBranch) { // 'push' event
core.info(`Looking up a pull request with a source branch "${sourceBranch || '<not found>'}" and a base branch "${baseBranch || '<not specified>'}"`);

if (pulls.length === 0) {
throw new Error(`No pull request found for a source branch "${sourceBranch || '<not found>'}" and a base branch "${baseBranch || '<not specified>'}"`);
}
const branchHead = `${credentials.owner}:${sourceBranch}`;
const { data: pulls } = await octokit.rest.pulls.list({
...credentials,
base: baseBranch,
head: branchHead,
});

if (pulls.length === 0) {
throw new Error(`No pull request found for a source branch "${sourceBranch || '<not found>'}" and a base branch "${baseBranch || '<not specified>'}"`);
}

pullRequest = pulls.find((p) => p.state === 'open');
if (pullRequest == null) {
throw new Error(`No open pull requests found for a source branch "${sourceBranch || '<not found>'}" and a base branch "${baseBranch || '<not specified>'}"`);
}
} else { // 'pull_request' event
core.info(`Looking up a pull request #${sourcePullRequestNumber}`);

({ data: pullRequest } = await octokit.rest.pulls.get({
...credentials,
pull_number: sourcePullRequestNumber,
}));

if (pullRequest == null) {
throw new Error(`Pull request #${sourcePullRequestNumber} was not found`);
}
if (pullRequest.state !== 'open') {
throw new Error(`Pull request #${sourcePullRequestNumber} is not open`);
}

const pullRequest = pulls.find((p) => p.state === 'open');
if (pullRequest == null) {
throw new Error(`No open pull requests found for a source branch "${sourceBranch || '<not found>'}" and a base branch "${baseBranch || '<not specified>'}"`);
sourceBranch = pullRequest.head.ref;
}

const { number: pullNumber, base: { ref: pullRequestTargetBranch } } = pullRequest;
core.info(`Pull request #${pullNumber} has been found for a source branch "${sourceBranch || '<not found>'}" and a base branch "${baseBranch || '<not specified>'}"`);
core.info(`Pull request #${pullNumber} has been found for a source branch "${sourceBranch || '<not found>'}" and a base branch "${baseBranch || '<not specified>'}"`);

const params = {
...credentials,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "update-pull-request-description-on-push",
"version": "2.0.0",
"version": "2.1.0",
"description": "Update pull request description on push",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit e358289

Please sign in to comment.