-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.js
161 lines (133 loc) Β· 3.39 KB
/
helpers.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
const fs = require('fs')
module.exports.readFilePromise = function (filename) {
return new Promise((resolve, reject) => {
fs.readFile(filename, 'utf8', (err, data) => {
if (err) reject(err)
else resolve(data)
})
}).catch((err) => {
console.log(err)
})
}
module.exports.getOwner = function (eventOwnerAndRepo) {
const slicePos1 = eventOwnerAndRepo.indexOf('/')
return eventOwnerAndRepo.slice(0, slicePos1)
}
module.exports.getRepo = function (eventOwnerAndRepo) {
const slicePos1 = eventOwnerAndRepo.indexOf('/')
return eventOwnerAndRepo.slice(slicePos1 + 1, eventOwnerAndRepo.length)
}
module.exports.getIssues = async function (octokit, eventOwner, eventRepo, label) {
const filterLabel = label ? label : null
let options = octokit.issues.listForRepo.endpoint.merge({
owner: eventOwner,
repo: eventRepo,
labels: [filterLabel],
})
let allIssuesForRepo = await octokit
.paginate(options)
.then((data) => {
return data
})
.catch((err) => {
console.log(err)
})
allIssuesForRepo = allIssuesForRepo.filter((issue) => {
if (!issue.pull_request) {
return true
}
})
return allIssuesForRepo
}
module.exports.createLabelInRepo = async function (
octokit,
eventOwner,
eventRepo,
labelToAddName,
labelToAddColor
) {
const options = octokit.issues.listLabelsForRepo.endpoint.merge({
owner: eventOwner,
repo: eventRepo,
})
let allLabelsForRepo = await octokit
.paginate(options)
.then((data) => {
return data
})
.catch((err) => {
console.log(err)
})
let labelExists = false
allLabelsForRepo.forEach((label) => {
const labelName = label.name
if (labelName === labelToAddName) {
labelExists = true
}
})
if (!labelExists) {
octokit.issues.createLabel({
owner: eventOwner,
repo: eventRepo,
name: labelToAddName,
color: labelToAddColor,
})
}
}
module.exports.addLabelToIssue = function (octokit, eventOwner, eventRepo, issue, labelToAdd) {
let existingLabels = issue.labels
let labelAlreadyOnIssue = false
existingLabels.forEach((label) => {
if (label.name === labelToAdd) {
labelAlreadyOnIssue = true
}
})
if (!labelAlreadyOnIssue) {
octokit.issues.addLabels({
owner: eventOwner,
repo: eventRepo,
issue_number: issue.number,
labels: [labelToAdd],
})
}
}
module.exports.getTopIssues = async function (issues, reaction, count) {
let topIssues = issues
topIssues = topIssues.filter((issue) => {
if (!issue.pull_request) {
return true
}
})
topIssues = topIssues.filter((i) => i.reactions[reaction] > 0)
topIssues.sort((a, b) => parseInt(b.reactions[reaction]) - parseInt(a.reactions[reaction]))
if (topIssues.length >= count) {
topIssues = topIssues.slice(0, count)
}
return topIssues
}
module.exports.pruneOldLabels = async function (
octokit,
eventOwner,
eventRepo,
issuesToLabel,
issuesWithLabel,
label
) {
let keepLabel
issuesWithLabel.forEach((labeledIssue) => {
keepLabel = false
issuesToLabel.forEach((issueToLabel) => {
if (issueToLabel.number === labeledIssue.number) {
keepLabel = true
}
})
if (!keepLabel) {
octokit.issues.removeLabel({
owner: eventOwner,
repo: eventRepo,
issue_number: labeledIssue.number,
name: label,
})
}
})
}