This repository has been archived by the owner on May 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
/
stale.js
227 lines (187 loc) · 7.43 KB
/
stale.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const schema = require('./schema')
const maxActionsPerRun = 30
module.exports = class Stale {
constructor (github, { owner, repo, logger = console, ...config }) {
this.github = github
this.logger = logger
this.remainingActions = 0
const { error, value } = schema.validate(config)
this.config = value
if (error) {
// Report errors to sentry
logger.warn({ err: new Error(error), owner, repo }, 'Invalid config')
}
Object.assign(this.config, { owner, repo })
}
async markAndSweep (type) {
const { only } = this.config
if (only && only !== type) {
return
}
if (!this.getConfigValue(type, 'perform')) {
return
}
this.logger.info(this.config, `starting mark and sweep of ${type}`)
const limitPerRun = this.getConfigValue(type, 'limitPerRun') || maxActionsPerRun
this.remainingActions = Math.min(limitPerRun, maxActionsPerRun)
await this.mark(type)
await this.sweep(type)
}
async mark (type) {
await this.ensureStaleLabelExists(type)
const staleItems = (await this.getStale(type)).data.items
await Promise.all(
staleItems
.filter(issue => !issue.locked && issue.state !== 'closed')
.map(issue => this.markIssue(type, issue))
)
}
async sweep (type) {
const { owner, repo } = this.config
const daysUntilClose = this.getConfigValue(type, 'daysUntilClose')
if (daysUntilClose) {
this.logger.trace({ owner, repo }, 'Configured to close stale issues')
const closableItems = (await this.getClosable(type)).data.items
await Promise.all(
closableItems
.filter(issue => !issue.locked && issue.state !== 'closed')
.map(issue => this.close(type, issue))
)
} else {
this.logger.trace({ owner, repo }, 'Configured to leave stale issues open')
}
}
getStale (type) {
const onlyLabels = this.getConfigValue(type, 'onlyLabels')
const staleLabel = this.getConfigValue(type, 'staleLabel')
const exemptLabels = this.getConfigValue(type, 'exemptLabels')
const exemptProjects = this.getConfigValue(type, 'exemptProjects')
const exemptMilestones = this.getConfigValue(type, 'exemptMilestones')
const exemptAssignees = this.getConfigValue(type, 'exemptAssignees')
const labels = [staleLabel].concat(exemptLabels)
const queryParts = labels.map(label => `-label:"${label}"`)
queryParts.push(...onlyLabels.map(label => `label:"${label}"`))
queryParts.push(Stale.getQueryTypeRestriction(type))
queryParts.push(exemptProjects ? 'no:project' : '')
queryParts.push(exemptMilestones ? 'no:milestone' : '')
queryParts.push(exemptAssignees ? 'no:assignee' : '')
const query = queryParts.join(' ')
const days = this.getConfigValue(type, 'days') || this.getConfigValue(type, 'daysUntilStale')
return this.search(type, days, query)
}
getClosable (type) {
const staleLabel = this.getConfigValue(type, 'staleLabel')
const queryTypeRestriction = Stale.getQueryTypeRestriction(type)
const query = `label:"${staleLabel}" ${queryTypeRestriction}`
const days = this.getConfigValue(type, 'days') || this.getConfigValue(type, 'daysUntilClose')
return this.search(type, days, query)
}
static getQueryTypeRestriction (type) {
if (type === 'pulls') {
return 'is:pr'
} else if (type === 'issues') {
return 'is:issue'
}
throw new Error(`Unknown type: ${type}. Valid types are 'pulls' and 'issues'`)
}
search (type, days, query) {
const { owner, repo } = this.config
const timestamp = this.since(days).toISOString().replace(/\.\d{3}\w$/, '')
query = `repo:${owner}/${repo} is:open updated:<${timestamp} ${query}`
const params = { q: query, sort: 'updated', order: 'desc', per_page: maxActionsPerRun }
this.logger.info(params, 'searching %s/%s for stale issues', owner, repo)
return this.github.search.issues(params)
}
async markIssue (type, issue) {
if (this.remainingActions === 0) {
return
}
this.remainingActions--
const { owner, repo } = this.config
const perform = this.getConfigValue(type, 'perform')
const staleLabel = this.getConfigValue(type, 'staleLabel')
const markComment = this.getConfigValue(type, 'markComment')
const number = issue.number
if (perform) {
this.logger.info('%s/%s#%d is being marked', owner, repo, number)
if (markComment) {
await this.github.issues.createComment({ owner, repo, number, body: markComment })
}
return this.github.issues.addLabels({ owner, repo, number, labels: [staleLabel] })
} else {
this.logger.info('%s/%s#%d would have been marked (dry-run)', owner, repo, number)
}
}
async close (type, issue) {
if (this.remainingActions === 0) {
return
}
this.remainingActions--
const { owner, repo } = this.config
const perform = this.getConfigValue(type, 'perform')
const closeComment = this.getConfigValue(type, 'closeComment')
const number = issue.number
if (perform) {
this.logger.info('%s/%s#%d is being closed', owner, repo, number)
if (closeComment) {
await this.github.issues.createComment({ owner, repo, number, body: closeComment })
}
return this.github.issues.edit({ owner, repo, number, state: 'closed' })
} else {
this.logger.info('%s/%s#%d would have been closed (dry-run)', owner, repo, number)
}
}
async unmarkIssue (type, issue) {
const { owner, repo } = this.config
const perform = this.getConfigValue(type, 'perform')
const staleLabel = this.getConfigValue(type, 'staleLabel')
const unmarkComment = this.getConfigValue(type, 'unmarkComment')
const number = issue.number
if (perform) {
this.logger.info('%s/%s#%d is being unmarked', owner, repo, number)
if (unmarkComment) {
await this.github.issues.createComment({ owner, repo, number, body: unmarkComment })
}
return this.github.issues.removeLabel({ owner, repo, number, name: staleLabel }).catch((err) => {
// ignore if it's a 404 because then the label was already removed
if (err.code !== 404) {
throw err
}
})
} else {
this.logger.info('%s/%s#%d would have been unmarked (dry-run)', owner, repo, number)
}
}
// Returns true if at least one exempt label is present.
hasExemptLabel (type, issue) {
const exemptLabels = this.getConfigValue(type, 'exemptLabels')
return issue.labels.some(label => exemptLabels.includes(label.name))
}
hasStaleLabel (type, issue) {
const staleLabel = this.getConfigValue(type, 'staleLabel')
return issue.labels.map(label => label.name).includes(staleLabel)
}
// returns a type-specific config value if it exists, otherwise returns the top-level value.
getConfigValue (type, key) {
if (this.config[type] && typeof this.config[type][key] !== 'undefined') {
return this.config[type][key]
}
return this.config[key]
}
async ensureStaleLabelExists (type) {
const { owner, repo } = this.config
const staleLabel = this.getConfigValue(type, 'staleLabel')
return this.github.issues.getLabel({ owner, repo, name: staleLabel }).catch(() => {
return this.github.issues.createLabel({ owner, repo, name: staleLabel, color: 'ffffff' })
})
}
since (days) {
const ttl = days * 24 * 60 * 60 * 1000
let date = new Date(new Date() - ttl)
// GitHub won't allow it
if (date < new Date(0)) {
date = new Date(0)
}
return date
}
}