-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
98 lines (85 loc) · 2.56 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const core = require('@actions/core')
const github = require('@actions/github')
const transformTeamName = teamName => teamName.toLowerCase().replace(' ', '-')
const run = async () => {
try {
const organization = core.getInput('organization', { required: true })
const teamName = core.getInput('team')
const comment = core.getInput('comment')
const { ACCESS_TOKEN } = process.env
if (!ACCESS_TOKEN)
return core.setFailed('ENV required and not supplied: ACCESS_TOKEN')
const { payload, sha } = github.context
const { repository } = payload
const octokit = new github.GitHub(ACCESS_TOKEN)
const commit = await octokit.git.getCommit({
owner: repository.owner.login,
repo: repository.name,
commit_sha: sha
})
const isMergeCommit = commit.data.parents.length > 1
if (!isMergeCommit) return
const {
data: [pullRequest]
} = await octokit.repos.listPullRequestsAssociatedWithCommit({
owner: repository.owner.login,
repo: repository.name,
commit_sha: sha
})
const contributor = pullRequest.user
if (teamName) {
const team = await octokit.teams.getByName({
org: organization,
team_slug: transformTeamName(teamName)
})
try {
await octokit.teams.getMembership({
team_id: team.data.id,
username: contributor.login
})
} catch (_) {
try {
await octokit.teams.addOrUpdateMembership({
team_id: team.data.id,
username: contributor.login
})
if (comment)
await octokit.issues.createComment({
owner: repository.owner.login,
repo: repository.name,
issue_number: pullRequest.number,
body: comment
})
} catch (error) {
core.setFailed(error.message)
}
}
} else {
try {
await octokit.orgs.checkMembership({
org: organization,
username: contributor.login
})
} catch (_) {
try {
await octokit.orgs.createInvitation({
org: organization,
invitee_id: contributor.id
})
if (comment)
await octokit.issues.createComment({
owner: repository.owner.login,
repo: repository.name,
issue_number: pullRequest.number,
body: comment
})
} catch (error) {
core.setFailed(error.message)
}
}
}
} catch (error) {
core.setFailed(error.message)
}
}
run()