forked from Xposed-Modules-Repo/submission
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.js
123 lines (110 loc) · 2.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
const { context, GitHub } = 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 = new GitHub(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.issues.get({
...getRepo(),
issue_number: issueNumber
})
return data
}
async function createAndInviteToRepo (token, owner, username, repo) {
const octokit = new GitHub(token)
try {
await octokit.repos.createInOrg({
org: owner,
name: repo
})
} 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.repos.addCollaborator({
owner,
repo,
username,
permission: 'admin'
})
break;
} catch (err) {
let e = err
if (e.status == 404) {
await new Promise(r => setTimeout(r, 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 = new GitHub(token)
await octokit.issues.addLabels({
owner,
repo,
issue_number: issueNumber,
labels: [label]
})
}
async function leaveComment (token, owner, repo, issueNumber, comment) {
const octokit = new GitHub(token)
await octokit.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: comment
})
}
async function closeIssue (token, owner, repo, issueNumber) {
const octokit = new GitHub(token)
await octokit.issues.update({
owner,
repo,
issue_number: issueNumber,
state: 'closed'
})
}
module.exports = {
getRepo,
getIssue,
createAndInviteToRepo,
addLabel,
leaveComment,
closeIssue
}