|
1 | 1 | import * as core from '@actions/core' |
2 | 2 | import * as github from '@actions/github' |
3 | | -import {Octokit} from '../lib' |
| 3 | +import {Octokit, optional, required} from '../lib' |
4 | 4 | import {track} from '../lib/track/track' |
5 | 5 |
|
| 6 | +const jobID = optional('codeball-job-id') |
| 7 | + |
6 | 8 | async function run(): Promise<void> { |
7 | | - try { |
8 | | - const pullRequestURL = github.context.payload?.pull_request?.html_url |
9 | | - if (!pullRequestURL) { |
10 | | - throw new Error('No pull request URL found') |
11 | | - } |
| 9 | + const pullRequestURL = github.context.payload?.pull_request?.html_url |
| 10 | + if (!pullRequestURL) throw new Error('No pull request URL found') |
12 | 11 |
|
13 | | - const pullRequestNumber = github.context.payload?.pull_request?.number |
14 | | - if (!pullRequestNumber) { |
15 | | - throw new Error('No pull request number found') |
16 | | - } |
| 12 | + const pullRequestNumber = github.context.payload?.pull_request?.number |
| 13 | + if (!pullRequestNumber) throw new Error('No pull request number found') |
17 | 14 |
|
18 | | - const commitId = github.context.payload.pull_request?.head.sha |
19 | | - if (!commitId) { |
20 | | - throw new Error('No commit ID found') |
21 | | - } |
| 15 | + const commitId = github.context.payload.pull_request?.head.sha |
| 16 | + if (!commitId) throw new Error('No commit ID found') |
22 | 17 |
|
23 | | - const repoOwner = github.context.payload.repository?.owner.login |
24 | | - if (!repoOwner) { |
25 | | - throw new Error('No repo owner found') |
26 | | - } |
| 18 | + const repoOwner = github.context.payload.repository?.owner.login |
| 19 | + if (!repoOwner) throw new Error('No repo owner found') |
27 | 20 |
|
28 | | - const repoName = github.context.payload.repository?.name |
29 | | - if (!repoName) { |
30 | | - throw new Error('No repo name found') |
31 | | - } |
| 21 | + const repoName = github.context.payload.repository?.name |
| 22 | + if (!repoName) throw new Error('No repo name found') |
32 | 23 |
|
33 | | - const githubToken = core.getInput('GITHUB_TOKEN') |
34 | | - if (!githubToken) { |
35 | | - core.setFailed('No GITHUB_TOKEN found') |
36 | | - return |
37 | | - } |
| 24 | + const githubToken = required('GITHUB_TOKEN') |
38 | 25 |
|
39 | | - const jobID = core.getInput('codeball-job-id') // jobID is not required |
| 26 | + const octokit = new Octokit({auth: githubToken}) |
40 | 27 |
|
41 | | - const octokit = new Octokit({auth: githubToken}) |
42 | | - |
43 | | - await octokit.pulls.createReview({ |
44 | | - owner: repoOwner, |
45 | | - repo: repoName, |
46 | | - pull_number: pullRequestNumber, |
47 | | - commit_id: commitId, |
48 | | - body: 'Codeball: LGTM! :+1:', |
49 | | - event: 'APPROVE' |
50 | | - }) |
| 28 | + await octokit.pulls.createReview({ |
| 29 | + owner: repoOwner, |
| 30 | + repo: repoName, |
| 31 | + pull_number: pullRequestNumber, |
| 32 | + commit_id: commitId, |
| 33 | + body: 'Codeball: LGTM! :+1:', |
| 34 | + event: 'APPROVE' |
| 35 | + }) |
| 36 | +} |
51 | 37 |
|
52 | | - await track(jobID, 'approver') |
53 | | - } catch (error) { |
| 38 | +run() |
| 39 | + .then(() => track({jobID, actionName: 'approver'})) |
| 40 | + .catch(error => { |
54 | 41 | if (error instanceof Error) { |
| 42 | + track({jobID, actionName: 'approver', error: error.message}) |
55 | 43 | if (error.message === 'Resource not accessible by integration') { |
56 | | - core.error( |
| 44 | + core.setFailed( |
57 | 45 | 'Codeball Approver failed to access GitHub. Check the "GITHUB_TOKEN Permissions" of this job and make sure that the job has WRITE permissions to Pull Requests.' |
58 | 46 | ) |
59 | | - core.error(error) |
60 | 47 | } else { |
61 | 48 | core.setFailed(error.message) |
62 | 49 | } |
63 | 50 | } |
64 | | - } |
65 | | -} |
66 | | - |
67 | | -run() |
| 51 | + }) |
0 commit comments