-
Notifications
You must be signed in to change notification settings - Fork 10
/
github.js
61 lines (57 loc) · 1.7 KB
/
github.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
const { context, getOctokit } = require('@actions/github')
/**
* Creates Octokit instance and run assign and review.
*
* @param {string} token - GitHub token
* @param {string} reviewers - GitHub usernames
* @param {string} teamReviewers - GitHub teams
*/
const handle = async (token, reviewers, teamReviewers) => {
if (context.eventName === 'pull_request') {
const octokit = getOctokit(token)
await assign(octokit)
if (reviewers || teamReviewers) await review(octokit, reviewers, teamReviewers)
} else {
throw new Error('Sorry, this Action only works with pull requests.')
}
}
/**
* Auto assign pull requests.
*
* @param {Octokit} octokit - Octokit instance
*/
const assign = async (octokit) => {
try {
const { owner, repo, number } = context.issue
await octokit.issues.addAssignees({
owner: owner,
repo: repo,
issue_number: number,
assignees: [context.actor]
})
} catch (err) {
throw new Error(`Couldn't assign pull request.\n Error: ${err}`)
}
}
/**
* Request PR review to given reviewers.
*
* @param {Octokit} octokit - Octokit instance
* @param {string} reviewers - GitHub usernames
* @param {string} teamReviewers - GitHub teams
*/
const review = async (octokit, reviewers, teamReviewers) => {
try {
const { owner, repo } = context.issue
await octokit.pulls.requestReviewers({
owner: owner,
repo: repo,
pull_number: context.payload.pull_request.number,
reviewers: reviewers.split(',').filter(x => x !== context.actor) || undefined,
team_reviewers: teamReviewers.split(',') || undefined
})
} catch (err) {
throw new Error(`Couldn't request review.\n Error: ${err}`)
}
}
module.exports = { handle }