-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-app.js
More file actions
171 lines (117 loc) · 4.32 KB
/
github-app.js
File metadata and controls
171 lines (117 loc) · 4.32 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
'use strict'
const jwt = require('jsonwebtoken'),
{Octokit} = require('@octokit/rest'),
cacheCalls = require('docloop').cacheCalls,
serializeCalls = require('docloop').serializeCalls
function generateJwt (id, cert) {
const payload = {
iat: Math.floor(new Date() / 1000),
exp: Math.floor(new Date() / 1000) + 60,
iss: id
}
// Sign with RSA SHA256
return jwt.sign(payload, cert, {algorithm: 'RS256'})
}
/**
* Class representing minimal Github API wrapper for app interaction.
*
* @memberof module:githubAdapter
* @alias GithubApp
*
* @param {Object} config
* @param {String} config.id GithubApp Id
* @param {String} config.cert GithubApp private key data
*
* @property {Object} app Instance of github-app (TODO:add link)
*
*/
class GithubApp {
//TODO: Multiple pages!
//
//TODO: test rate limit
//TOSO: store installation tokens for some time and reuse them
constructor(config){
this.id = config.id
this.cert = config.cert
//TODO: better use webwooks and reloads no need for cache:
cacheCalls(this, 'getRepositories', 2000)
serializeCalls(this, ['createOrUpdateIssue', 'createOrUpdateComment'], 3000)
}
async authenticateAsApp(){
const octokit = new Octokit({auth: generateJwt(this.id, this.cert)})
return octokit
}
async authenticateAsInstallation(installation_id){
const octokit = await this.authenticateAsApp()
const response = await octokit.apps.createInstallationAccessToken({installation_id})
return new Octokit({auth: response.data.token})
}
/**
* Get all repositories the installation has access to.
*
* This method is augumented by {@link module:docloop#cache}(2000). Return values will be cached for 2 seconds, to prevent huge numbers of API calls.
*
* @param {String} installation_id GithubApp installation id
* @return {Object[]} For each repository a subset of the data provided by github.
*/
async getRepositories(installation_id){
const octokit = await this.authenticateAsInstallation(installation_id)
var response = await octokit.apps.listReposAccessibleToInstallation({per_page: 100}),
repos = response.data.repositories || []
//TODO: HANDLE MORE THAN 100 PAGES
return repos.map( repository => ({
name: repository.name,
full_name: repository.full_name,
owner: repository.owner,
installation_id: installation_id,
html_url: repository.html_url
}))
}
/**
* Create or Update an Issue. If issue.number is provided, update the corresponding issue, otherwise create a new one .
*
* @param {Identifier} target_identifier Identifier pointing at a github repository
* @param {Issue} issue
* @return {String} The github issue number
*/
async createOrUpdateIssue(target_identifier, issue){
console.log('creating/upadating issue', issue.title, Date.now())
const octokit = await this.authenticateAsInstallation(target_identifier.installation_id)
var params = {
owner: target_identifier.owner,
repo: target_identifier.repo,
number: issue.number,
title: issue.title,
body: issue.body,
labels: issue.labels
},
result = issue.number
? await octokit.issues.update( params )
: await octokit.issues.create( params )
console.log('done', issue.title)
return result.data.number
}
/**
* Create or update an Issue. If comment.id is provided, update the corresponding comment, otherwise create a new one.
*
* @param {Identifier} target_identifier Identifier pointing at a github repository
* @param {Comment} comment
* @return {String} The github comment number
*/
async createOrUpdateComment(target_identifier, comment){
console.log('creating/updating comment', Date.now())
const octokit = await this.authenticateAsInstallation(target_identifier.installation_id)
var params = {
owner: target_identifier.owner,
repo: target_identifier.repo,
number: comment.number,
body: comment.body,
id: comment.id
},
result = comment.id
? await octokit.issues.updateComment( params )
: await octokit.issues.createComment( params )
return result.data.id //todo?
}
}
module.exports = GithubApp