-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (77 loc) · 3.01 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
79
80
81
82
const core = require('@actions/core')
const github = require('@actions/github')
// function to get issue comments
async function getIssueComments (octokit, owner, repo, issueNumber) {
const response = await octokit.request('GET /repos/{owner}/{repo}/issues/{issueNumber}/comments{?since,per_page,page}', {
owner,
repo,
issueNumber
})
return response.data
}
// function to get team members
async function getTeamMembers (octokit, org, teamSlug) {
const response = await octokit.request('GET /orgs/{org}/teams/{teamSlug}/members', {
org,
teamSlug
})
return response.data
}
async function main () {
try {
const octokit = github.getOctokit(core.getInput('repo-token'))
const mode = core.getInput('mode')
let teamMembers
if (mode === 'team') {
teamMembers = await getTeamMembers(octokit, github.context.repo.owner, core.getInput('approvers'))
core.info(JSON.stringify(teamMembers, undefined, 2))
} else {
teamMembers = core.getInput('approvers').split(',').map((x) => x.trim())
}
const issueNumber = github.context.payload.issue.number
core.info(`The issue number is: ${issueNumber}`)
core.info(`The repo is: ${github.context.repo.repo}`)
core.info(`The owner is: ${github.context.repo.owner}`)
const declineWords = core.getInput('decline-words').split(',').map(word => word.trim())
const approveWords = core.getInput('approve-words').split(',').map(word => word.trim())
core.info(`Decline words: ${declineWords}`)
core.info(`Approve words: ${approveWords}`)
const comments = await getIssueComments(octokit, github.context.repo.owner, github.context.repo.repo, issueNumber)
core.info(Array.isArray(comments))
comments.sort((a, b) => b.id - a.id)
const approvals = []
for (const comment of comments) {
if (declineWords.some(word => comment.body.includes(word))) {
core.setOutput('approved', false)
core.setOutput('justification', 'Issue was declined')
core.info('Issue was declined')
if (core.getBooleanInput('fail-on-decline')) {
core.setFailed('The action has been declined')
}
return
}
if (approveWords.some(word => comment.body.includes(word))) {
teamMembers.forEach(member => {
if (comment.user.login === member && !approvals.includes(member)) {
approvals.push(member)
core.info(`The approvals are: ${approvals}`)
}
})
}
if (approvals.length >= core.getInput('minimum-approvals')) {
core.setOutput('approved', true)
core.setOutput('justification', 'Issue was approved')
core.info('Issue was approved')
return
}
}
if (approvals.length < core.getInput('minimum-approvals')) {
core.setOutput('approved', 'undefined')
core.setOutput('justification', 'Count of approvals is less than minimum approvals')
core.info('Count of approvals is less than minimum approvals')
}
} catch (error) {
core.setFailed(error.message)
}
}
main()