-
Notifications
You must be signed in to change notification settings - Fork 10
/
github.js
166 lines (149 loc) · 3.54 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const { context, getOctokit } = require('@actions/github')
function getPrNumber () {
const pullRequest = context.payload.pull_request
if (!pullRequest) {
return undefined
}
return pullRequest.number
}
function getIssueNumber () {
const issue = context.payload.issue
if (!issue) {
return undefined
}
return issue.number
}
function getRepo () {
return context.repo
}
async function getIssue (token) {
const octokit = getOctokit(token)
let issueNumber
if (getIssueNumber() !== undefined) {
issueNumber = getIssueNumber()
} else if (getPrNumber() !== undefined) {
issueNumber = getPrNumber()
} else {
throw new Error('No Issue Provided')
}
const { data } = await octokit.rest.issues.get({
...getRepo(),
issue_number: issueNumber
})
return data
}
async function createAndInviteToRepo (token, owner, username, repo) {
const octokit = getOctokit(token)
try {
await octokit.rest.repos.createInOrg({
org: owner,
name: repo,
has_issues: false,
has_projects: false,
has_wiki: false
})
} catch (err) {
let e = err
if (e.name === 'HttpError') e = e.response.data
if (e && e.errors && e.errors.length && e.errors[0].field === 'name') {
return false
}
console.log('嘤嘤嘤', JSON.stringify(err))
throw err
}
for (let i = 0; i < 3; i++) {
try {
await octokit.rest.repos.addCollaborator({
owner,
repo,
username,
permission: 'admin'
})
break
} catch (err) {
let e = err
if (e.status === 404) {
await new Promise(resolve => setTimeout(resolve, 2000))
continue
}
if (e.name === 'HttpError') e = e.response.data
if (e && e.errors && e.errors.length && e.errors[0].field === 'name') {
return false
}
console.log('呜呜呜', JSON.stringify(err))
throw err
}
}
return true
}
async function addLabel (token, owner, repo, issueNumber, label) {
const octokit = getOctokit(token)
await octokit.rest.issues.addLabels({
owner,
repo,
issue_number: issueNumber,
labels: [label]
})
}
async function setLabel (token, owner, repo, issueNumber, label) {
const octokit = getOctokit(token)
await octokit.rest.issues.setLabels({
owner,
repo,
issue_number: issueNumber,
labels: [label]
})
}
async function leaveComment (token, owner, repo, issueNumber, comment) {
const octokit = getOctokit(token)
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: comment
})
}
async function closeIssue (token, owner, repo, issueNumber, isCompleted = false) {
const octokit = getOctokit(token)
await octokit.rest.issues.update({
owner,
repo,
issue_number: issueNumber,
state: 'closed',
state_reason: isCompleted ? 'completed' : 'not_planned'
})
}
async function lockSpamIssue (token, owner, repo, issueNumber) {
const octokit = getOctokit(token)
await octokit.rest.issues.lock({
owner,
repo,
issue_number: issueNumber,
lock_reason: 'spam'
})
}
async function orgBlockUser (token, owner, username) {
const octokit = getOctokit(token)
await octokit.rest.orgs.blockUser({
org: owner,
username
})
}
async function getUser (token, username) {
const octokit = getOctokit(token)
return await octokit.rest.users.getByUsername({
username
})
}
module.exports = {
getRepo,
getIssue,
createAndInviteToRepo,
addLabel,
setLabel,
leaveComment,
closeIssue,
lockSpamIssue,
orgBlockUser,
getUser
}