-
Notifications
You must be signed in to change notification settings - Fork 15
/
createComments.js
296 lines (260 loc) · 9.26 KB
/
createComments.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
const request = require('request-promise')
const moment = require('moment')
const fs = require('fs-extra')
const glob = require('glob')
const { sleep } = require('./utils')
const config = require('./config')
const users = require('./users')
const createMessage = require('./createMessage')
const processImages = require('./processImages')
const api = `${config.target.baseUrl}/${config.target.org}/${config.target.repo}`
const headers = {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'node.js'
}
if (config.target.token) {
headers['Authorization'] = `token ${config.target.commentToken || config.target.token}`
}
const post = async (url, body) => {
return request({
method: 'POST',
headers,
url,
body,
json: true
})
}
let commits = []
/**
* Figure out if a commit exists
* @param {string} sha
*/
const commitExists = async (sha) => {
if (!commits.length) {
commits = JSON.parse(await fs.readFile(`${config.source.repo}/commits.json`))
}
return !!commits.find(commit => commit.sha === sha)
}
const setCommentProcessed = async (id, newId = true) => {
console.log(`Setting ${id} as processed`)
const state = JSON.parse(await fs.readFile(`./${config.source.repo}/state.json`))
state.comments = state.comments || {}
state.comments[id] = newId
await fs.writeFile(`./${config.source.repo}/state.json`, JSON.stringify(state, null, ' '))
}
const setReviewProcessed = async (id, newId = true) => {
console.log(`Setting ${id} as processed`)
const state = JSON.parse(await fs.readFile(`./${config.source.repo}/state.json`))
state.reviews = state.reviews || {}
state.reviews[id] = newId
await fs.writeFile(`./${config.source.repo}/state.json`, JSON.stringify(state, null, ' '))
}
const isCommentProcessed = async (id) => {
const state = JSON.parse(await fs.readFile(`./${config.source.repo}/state.json`))
return !!(state.comments || {})[id]
}
const isReviewProcessed = async (id) => {
const state = JSON.parse(await fs.readFile(`./${config.source.repo}/state.json`))
return !!(state.reviews || {})[id]
}
const isReviewComment = comment => !!comment.state
const isPullRequestComment = comment => !!comment.pull_request_url
const isCommitComment = comment => !!comment.commit_id && !comment.pull_request_url
const logError = (comment, err) => {
console.log(`Could not create comment: ${comment.id}`)
console.log(`Message: ${err}`)
// process.exit(1)
}
const createComment = async (comment, comments) => {
const { id } = comment
if (isReviewComment(comment)) {
await createReview(comment)
} else if (isPullRequestComment(comment)) {
await createPullRequestComment(comment, comments)
} else if (isCommitComment(comment)) {
await createCommitComment(comment, comments)
} else {
await createIssueComment(comment)
}
}
const getEvent = (state) => {
switch(state) {
case 'APPROVED':
return 'APPROVE'
case 'CHANGES_REQUESTED':
return 'REQUEST_CHANGES'
default:
return ''
}
}
const createReview = async (comment) => {
const { id } = comment
if (await isReviewProcessed(id)) {
console.log(`Review ${id} already processed`)
} else if (comment.state === 'COMMENTED') {
console.log(`Review ${id} is a comment - skipping`)
} else if (comment.state === 'DISMISSED') {
console.log(`Review ${id} was dismissed - skipping`)
} else {
const number = comment.pull_request_url.split('/').pop()
const url = `${api}/pulls/${number}/reviews`
const event = getEvent(comment.state)
if (event) {
const body = {
commit_id: comment.commit_id,
body: comment.body,
event,
comments: []
}
await post(url, body)
.then(async response => {
await setReviewProcessed(id, response.id)
return response
})
.catch(err => {
console.log(`Could not create review ${id}`)
logError(comment, err)
})
} else {
console.log(`Review ${id} has no event type ${comment} - skipping`)
}
}
}
const createPullRequestComment = async (comment, comments = []) => {
const { id } = comment
const issueNumber = comment.pull_request_url.split('/').pop()
const url = `${api}/pulls/${issueNumber}/comments`
if (await isCommentProcessed(id)) {
console.log(`Comment ${id} already processed`)
} else {
const commentBody = await processImages(comment.body)
console.log(`Adding comment ${id} to ${url}`)
let body
const reply = comments.find(c => c.original_commit_id === comment.original_commit_id && c.original_position === comment.original_position && c.diff_hunk === comment.diff_hunk)
if (reply) {
const state = JSON.parse(await fs.readFile(`./${config.source.repo}/state.json`))
body = {
body: `${createMessage(comment)}\n\n\n${commentBody}`,
in_reply_to: (state.comments || {})[reply.id],
}
} else {
body = {
body: `${createMessage(comment)}\n\n\n${commentBody}`,
commit_id: comment.original_commit_id,
path: comment.path,
position: comment.original_position,
}
}
await post(url, body)
.then(async response => {
await setCommentProcessed(id, response.id)
})
.catch(err => {
console.log(`Commit ${comment.original_commit_id} no longer exists`)
const body = {
body: `${createMessage(comment)}\n> **Outdated (history rewrite)** - original diff\n---\n\`\`\`diff\n${comment.diff_hunk}\n\`\`\`\n\n\n${commentBody}`,
commit_id: comment.commit_id,
path: comment.path,
position: comment.position == null ? comment.original_position : comment.position,
}
return post(url, body)
.then(async response => {
await setCommentProcessed(id, response.id)
})
.catch(async err => {
if (err.error && err.error && err.error.errors && err.error.errors[0].field) {
console.log(`Commit ${comment.commit_id} no longer exists (someone did a force push)`)
console.log('Skipping')
} else {
logError(comment, err)
}
await setCommentProcessed(id, true)
})
})
await sleep(60 * 60 * 1000 / config.apiCallsPerHour)
}
}
const createCommitComment = async (comment) => {
const { id } = comment
const sha = comment.commit_id;
const url = `${api}/commits/${sha}/comments`
const commentBody = await processImages(comment.body)
if (await isCommentProcessed(id)) {
console.log(`Comment ${id} already processed`)
} else if (!await commitExists(sha)) {
console.log(`Commit ${sha} no longer exists`)
} else {
console.log(`Adding comment ${id} to ${url}`)
const body = {
body: `${createMessage(comment)}\n\n\n${commentBody}`,
sha,
path: comment.path,
position: comment.position
}
await post(url, body)
.then(async response => {
await setCommentProcessed(id, response.id)
return response
})
.catch(err => {
logError(comment, err)
})
await sleep(60 * 60 * 1000 / config.apiCallsPerHour)
}
}
const createIssueComment = async (comment) => {
const { id } = comment
const issueNumber = comment.issue_url.split('/').pop()
const url = `${api}/issues/${issueNumber}/comments`
const commentBody = await processImages(comment.body)
if (await isCommentProcessed(id)) {
console.log(`Comment ${id} already processed`)
} else {
console.log(`Adding comment ${id} to ${url}`)
const body = {
body: `${createMessage(comment)}\n\n\n${commentBody}`
}
await post(url, body)
.then(async response => {
await setCommentProcessed(id, response.id)
return response
})
.catch(err => {
logError(comment, err)
})
await sleep(60 * 60 * 1000 / config.apiCallsPerHour)
}
}
/**
* Takes milliseconds of elapsed time and creates a string like '05m 10s'
* @param duration milliseconds
*/
const formatDuration = (duration) => {
const seconds = duration / 1000
return new Date((seconds % 86400) * 1000)
.toUTCString()
.replace(/.*(\d{2}):(\d{2}):(\d{2}).*/, '$1h $2m $3s')
.replace('00h ', '')
.replace('00m ', '')
.replace('00s', '')
.trim()
}
const main = async () => {
const issueComments = JSON.parse(await fs.readFile(`${config.source.repo}/issue-comments.json`))
const commitComments = JSON.parse(await fs.readFile(`${config.source.repo}/comments.json`))
const pullComments = JSON.parse(await fs.readFile(`${config.source.repo}/pull-comments.json`))
const reviewComments = JSON.parse(await fs.readFile(`${config.source.repo}/review-comments.json`))
const reviews = JSON.parse(await fs.readFile(`${config.source.repo}/reviews.json`))
const comments = []
.concat(issueComments, commitComments, pullComments, reviewComments, reviews)
.sort((a, b) => (a.created_at || a.submitted_at) > (b.created_at || b.submitted_at) ? 1 : -1)
console.log(`Comments to process: ${comments.length}`)
let processed = 0
for (let comment of comments) {
console.log(`ETA: ${formatDuration((comments.length - processed) / config.apiCallsPerHour * 60 * 60 * 1000)}`)
await createComment(comment, comments)
processed++
}
// await fs.writeFile(`${config.source.repo}/all-comments.json`, JSON.stringify(comments, null, ' '))
}
main().catch(console.error)