-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
78 lines (63 loc) · 2.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const github = require('@actions/github');
const core = require('@actions/core');
const run = async () => {
const githubToken = core.getInput('github_token', { required: true });
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\//, '');
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,
});
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 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>'}"`);
}
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>'}"`);
const params = {
...credentials,
pull_number: pullNumber,
};
if (prTitle) {
core.info(`Pull request #${pullNumber}'s title will be set to "${prTitle}"`);
params.title = prTitle;
}
if (prBody) {
core.info(`Pull request #${pullNumber}'s body will be set to "${prBody}"`);
params.body = prBody;
}
if (baseBranch && baseBranch !== pullRequestTargetBranch) {
core.info(`Pull request #${pullNumber}'s base branch will be set to "${baseBranch}"`);
params.title = prTitle;
}
const url = `/repos/${credentials.owner}/${credentials.repo}/pulls/${pullNumber}`;
core.info(`Making a PATCH request to "${url}" with params "${JSON.stringify(params)}"`);
await octokit.request(`PATCH ${url}`, params);
};
// Github boolean inputs are strings https://github.com/actions/runner/issues/1483
const failOnError = core.getInput('fail_on_error') == 'true';
run()
.then(() => {
core.info('Done.');
})
.catch((e) => {
core.error('Cannot update the pull request.');
if (failOnError) {
core.setFailed(e.stack || e.message);
} else {
core.error(e.stack || e.message);
}
});