Skip to content

Commit e358289

Browse files
feat: add support for pull_request events (kt3k#13)
1 parent fef8b17 commit e358289

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

index.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,58 @@ const run = async () => {
66
const prTitle = core.getInput('pr_title');
77
const prBody = core.getInput('pr_body');
88
const baseBranch = core.getInput('destination_branch');
9-
const sourceBranch = github.context.ref.replace(/^refs\/heads\//, '');
9+
// On 'push' events, github.context.ref is in format 'refs/heads/<sourceBranch>'
10+
// On 'pull_request' events, github.contest.ref is in format 'refs/pull/<pullRequestNumber>/merge'
11+
// See https://docs.github.com/en/actions/learn-github-actions/environment-variables
12+
let sourceBranch = github.context.ref.match(/^refs\/heads\/(.?)$/)?.[1];
13+
const sourcePullRequestNumber = github.context.ref.match(/^refs\/pull\/(\d+)\/merge/)?.[1];
1014

1115
const credentials = {
1216
owner: github.context.repo.owner,
1317
repo: github.context.repo.repo,
1418
};
1519

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

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

26-
if (pulls.length === 0) {
27-
throw new Error(`No pull request found for a source branch "${sourceBranch || '<not found>'}" and a base branch "${baseBranch || '<not specified>'}"`);
28-
}
26+
const branchHead = `${credentials.owner}:${sourceBranch}`;
27+
const { data: pulls } = await octokit.rest.pulls.list({
28+
...credentials,
29+
base: baseBranch,
30+
head: branchHead,
31+
});
32+
33+
if (pulls.length === 0) {
34+
throw new Error(`No pull request found for a source branch "${sourceBranch || '<not found>'}" and a base branch "${baseBranch || '<not specified>'}"`);
35+
}
36+
37+
pullRequest = pulls.find((p) => p.state === 'open');
38+
if (pullRequest == null) {
39+
throw new Error(`No open pull requests found for a source branch "${sourceBranch || '<not found>'}" and a base branch "${baseBranch || '<not specified>'}"`);
40+
}
41+
} else { // 'pull_request' event
42+
core.info(`Looking up a pull request #${sourcePullRequestNumber}`);
43+
44+
({ data: pullRequest } = await octokit.rest.pulls.get({
45+
...credentials,
46+
pull_number: sourcePullRequestNumber,
47+
}));
48+
49+
if (pullRequest == null) {
50+
throw new Error(`Pull request #${sourcePullRequestNumber} was not found`);
51+
}
52+
if (pullRequest.state !== 'open') {
53+
throw new Error(`Pull request #${sourcePullRequestNumber} is not open`);
54+
}
2955

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

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

3862
const params = {
3963
...credentials,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "update-pull-request-description-on-push",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Update pull request description on push",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)