-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-target.js
More file actions
336 lines (273 loc) · 9.03 KB
/
github-target.js
File metadata and controls
336 lines (273 loc) · 9.03 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
'use strict'
const DocloopEndpoint = require('docloop').DocloopEndpoint,
Promise = require('bluebird')
/**
*Class representing the Github issue tracker.
*
* @memberof module:githubAdapter
* @alias GithubTarget
*
* @extends DocloopEndpoint
*
* @throws {TODO} TODO
*
*/
class GithubTarget extends DocloopEndpoint{
constructor(adapter, {id, _id, identifier, config, decor, data}){
//TODO: this should never happen, should it?
if(arguments[1] instanceof GithubTarget) return arguments[1]
super(adapter, {
id,
_id,
identifier,
config,
data,
decor: decor || {
image: null,
title: 'Github Repository',
details: 'unknown'
},
})
if(!identifier.repo) throw new ReferenceError("GithubTarget.constructor() missing identifier.repo")
if(!identifier.installation_id) throw new ReferenceError("GithubTarget.constructor() missing identifier.installation_id")
if(!identifier.owner) throw new ReferenceError("GithubTarget.constructor() missing identifier.owner")
if(adapter.id != identifier.adapter) throw new Error("GithubTarget.constructor() adapter mismatch")
}
/**
* Creates a new Endpoint from a set of repository data,
*
* @static
*
* @param {Adapter} adapter
* @param {Object} repo Repository data
* @return {Endpoint}
*/
static fromRepo(adapter, repo){
if(!repo) throw new ReferenceError("GithubTarget.fromRepo() missing repo")
if(!repo.name) throw new ReferenceError("GithubTarget.fromRepo() missing repo.name")
if(!repo.installation_id) throw new ReferenceError("GithubTarget.fromRepo() missing repo.installation_id")
if(!repo.owner) throw new ReferenceError("GithubTarget.fromRepo() missing repo.owner")
if(!repo.owner.login) throw new ReferenceError("GithubTarget.fromRepo() missing repo.owner.login")
var identifier = {
adapter: adapter.id,
repo: repo.name,
installation_id: repo.installation_id,
owner: repo.owner.login
},
decor = GithubTarget.repoToDecor(repo)
return new GithubTarget(adapter, {identifier, decor})
}
/**
* Extract decoration data from repositpry data.
*
* @static
* @param {Object} repo Repository data
* @return {Decoration}
*/
static repoToDecor(repo){
return {
image: repo.owner.avatar_url,
title: repo.name,
details: repo.owner.login
}
}
/**
* Check if the current session has access to the repository the endpoint's identifier points to.
*
* @async
* @param {SessionData} session_data
* @return {undefined}
*
* @throws {DocloopError|403} If current session cannot access the repository
*/
async validate(session_data){
var valid_endpoints = await this.adapter.getEndpoints(session_data),
match = valid_endpoints.some( endpoint => this.match(endpoint) )
if(!match) throw new DocloopError('GithubTarget.validate() no valid endpoint match', 403)
}
/**
* TODO: Rethink decoration
*
* @param {SessionData}
* @return {undefined}
*
* @throws {TODO} If TODO
*/
async updateDecor(session_data){
var valid_endpoints = await this.adapter.getEndpoints(session_data)
valid_endpoints.forEach( endpoint => {
if(this.match(endpoint)) this.decor = endpoint.decor
})
}
/**
* Set of data representing a github issue
*
* @typedef {Issue}
* @param {String} title
* @param {String} body
* @param {String[]} ?labels List of label names
* @param {String} ?number Issue number
*/
/**
* Turn an {@link Annotation} into isuue data ready to be posted to the github API.
*
* @param {Annotation} annotation
* @return {Issue}
*/
annotation2Issue(annotation){
var title = annotation.title + ' [via '+annotation.sourceName+'@'+this.adapter.core.config.name+']',
import_line = '_Annotation imported from <a href ="'+annotation.sourceHome+'">'+annotation.sourceName+'</a>._'+'\n\n',
target_block = annotation.respectiveContent
? 'Regarding this part:\n'
+ '<blockquote>'
+ annotation.respectiveContent
+ '</blockquote>\n\n'
: '',
annotation_block = annotation.author+' wrote:'+'\n'
+ '<blockquote>\n'
+ annotation.body+'\n'
+ '</blockquote>'+'\n\n',
footer_line = '_Link to <a href = "'+annotation.original+'">original comment</a>. About <a href ="'+this.adapter.core.config.home+'">docloop</a>._',
body = import_line
+ target_block
+ annotation_block
+ footer_line,
labels = this.config.label && [this.config.label]
return {title, body, labels}
}
/**
* Handle annotation event. Update or create a corresponding issue on Github.
*
* @param {Annotation} annotation
* @return {undefined}
*
* @throws {TODO} If TODO
*/
async handleAnnotation(annotation){
console.log(new Date().toString(), 'Trying to handle annotation: ', annotation.title, annotation.id)
var issue_number = await this.getIssueNumber(annotation.id),
issue = {
...this.annotation2Issue(annotation),
number: issue_number
}
issue_number = await this.adapter.githubApp.createOrUpdateIssue(this.identifier, issue)
await this.storeIssueNumber(annotation.id, issue_number)
}
/**
* Stores connection between an annotation and an issue number. This serves two purposes:
* If the same annotation event fires again the same issue will be updated/overwritten.
* If a reply event fires, a comment can be placed into the corresponding issue.
*
* @async
*
* @param {String} annotation_id Id from the original source.
* @param {String} issue_number Github issue number
* @return {undefined}
*
* @throws {TODO} If TODO
*/
async storeIssueNumber(annotation_id, issue_number){
return this.setData('issueMap.'+annotation_id, issue_number)
}
/**
* Get a stored issue number correspoding to an annotation id.
*
* @async
*
* @param {annotation_id} Id from the original source.
* @return {String} Corresponding issue number
*
* @throws {TODO} If TODO
*/
async getIssueNumber(annotation_id){
return await this.getData('issueMap.'+annotation_id)
}
/**
* Makes sure there is an issue corresponding to the provided annotation id.
* If there is no such issue existing yet, then create a dummy issue and return its issue number.
* This is useful if a reply event fires before its parent annotation event.
* This can happen if the annotation is old and has never been imported or the import has been delayed for some reason.
*
* @async
* @param {String} annotation_id Id from the original source
* @return {String} Github issue number
* @throws {TODO} If TODO
*/
async ensureIssueNumber(annotation_id){
var issue_number = await this.getIssueNumber(annotation_id)
|| await this.adapter.githubApp.createOrUpdateIssue(
this.identifier,
{
title: this.adapter.dummy.title,
body: this.adapter.body,
}
)
await this.storeIssueNumber(annotation_id, issue_number)
return issue_number
}
/**
* Set of data representing a github comment.
*
* @typedef {Comment}
* @param {String} body
* @param {String} !number Github issue number
* @param {String} ?id Comment id
*/
/**
* Turn an {@link Reply} into comment data ready to be posted to the github API.
*
* @param {Reply} reply
* @return {Object} Comment data
*/
reply2Comment(reply){
return { body: this.annotation2Issue(reply).body }
}
/**
* Handle reply event. Update or create a corresponding comment on Github.
*
* @param {Reply} reply
* @return {undefined}
*
* @throws {TODO} If TODO
*/
async handleReply(reply){
console.log(new Date().toString(), 'Trying to handle annotation: ', reply.parentId, reply.id)
var issue_number = await this.ensureIssueNumber(reply.parentId),
comment = {
...this.reply2Comment(reply),
id: await this.getCommentId(reply.id),
number: issue_number
},
comment_id = await this.adapter.githubApp.createOrUpdateComment(this.identifier, comment)
await this.storeCommentId(reply.id, comment_id)
}
/**
* Stores connection between an reply and a coment number.
* If the same reply event fires again the correspondin comment will be updated/overwritten.
*
* @async
*
* @param {String} reply_id Reply id from the original source.
* @param {String} comment_id Github comment_id
* @return {undefined}
*
* @throws {TODO} If TODO
*/
async storeCommentId(reply_id, comment_id){
return this.setData('commentMap.'+reply_id, comment_id)
}
/**
* Get a stored comment id correspoding to a reply id.
*
* @async
*
* @param {reply_id} Reply Id from the original source.
* @return {String} Corresponding comment id
*
* @throws {TODO} If TODO
*/
async getCommentId(reply_id){
return await this.getData('commentMap.'+reply_id)
}
}
module.exports = GithubTarget