This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgithub.ts
461 lines (399 loc) · 12.1 KB
/
github.ts
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/**
* houston/src/lib/service/github.ts
* Handles interaction with GitHub repositories.
*
* NOTE: If you are using a personal auth token, the url should be like so:
* "https://x-access-token:asdf1234@github.com/elementary/houston"
*/
import * as fileType from 'file-type'
import * as fs from 'fs-extra'
import { inject, injectable, LazyServiceIdentifer } from 'inversify'
import * as jsonwebtoken from 'jsonwebtoken'
import * as Git from 'nodegit'
import * as os from 'os'
import * as path from 'path'
import * as agent from 'superagent'
import * as URL from 'url'
import * as uuid from 'uuid/v4'
import { App } from '../app'
import { Cache, ICache, ICacheFactory } from '../cache'
import { Config } from '../config'
import { sanitize } from '../utility/rdnn'
import * as type from './type'
export const github = Symbol('GitHub')
export type IGitHubFactory = (url: string) => GitHub
@injectable()
export class GitHub implements type.ICodeRepository, type.IPackageRepository, type.ILogRepository {
/**
* Folder to use as scratch space for cloning repos
*
* @var {string}
*/
public tmpFolder = path.resolve(os.tmpdir(), 'houston')
/**
* The human readable name of the service.
*
* @var {String}
*/
public serviceName = 'GitHub'
/**
* The host for the repo. This is almost always "github.com" but can change if
* you have an enterprise instance.
*
* @var {string}
*/
public host = 'github.com'
/**
* The GitHub username or organization.
*
* @var {string}
*/
public username: string
/**
* The GitHub user's repository name
*
* @var {string}
*/
public repository: string
/**
* The http username
*
* @var {string|null}
*/
public authUsername?: string
/**
* The http password authentication string.
* NOTE: When the `authUsername` is 'installation', this will be an
* installation id. Not an accurate http password.
*
* @var {string|null}
*/
public authPassword?: string
/**
* The reference to branch or tag.
*
* @var {string}
*/
public reference = 'refs/heads/master'
/**
* The application configuration
*
* @var {Config}
*/
protected config: Config
/**
* A cache store for GitHub authentication tokens
*
* @var {Cache}
*/
protected cache: ICache
/**
* Tries to get the file mime type by reading the first chunk of a file.
* Required by GitHub API
*
* Code taken from examples of:
* https://github.com/sindresorhus/file-type
* https://github.com/sindresorhus/read-chunk/blob/master/index.js
*
* @async
* @param {String} p Full file path
* @return {String} The file mime type
*/
protected static async getFileType (p: string): Promise<string> {
const buffer = await new Promise((resolve, reject) => {
fs.open(p, 'r', (openErr, fd) => {
if (openErr != null) {
return reject(openErr)
}
fs.read(fd, Buffer.alloc(4100), 0, 4100, 0, (readErr, bytesRead, buff) => {
fs.close(fd)
if (readErr != null) {
return reject(readErr)
}
if (bytesRead < 4100) {
return resolve(buff.slice(0, bytesRead))
} else {
return resolve(buff)
}
})
})
})
return fileType(buffer).mime
}
/**
* Creates a new GitHub Repository
*
* @param {App} app - The application IOC instance
*/
constructor (@inject(new LazyServiceIdentifer(() => App)) app: App) {
this.config = app.get(Config)
this.cache = app.get<ICacheFactory>(Cache)('lib/service/github')
}
/**
* Returns the default RDNN value for this repository
*
* @return {string}
*/
public get rdnn () {
return sanitize(`com.github.${this.username}.${this.repository}`)
}
/**
* Returns the Git URL for the repository
*
* @return {string}
*/
public get url (): string {
let auth = null
if (this.authUsername !== 'installation') {
if (this.authUsername != null || this.authPassword != null) {
auth = `${this.authUsername}:${this.authPassword}`
}
}
return URL.format({
auth,
host: this.host,
pathname: `/${this.username}/${this.repository}.git`,
protocol: 'https'
})
}
/**
* Sets the Git URL for the repository
* NOTE: Auth code is case sensitive, so we can't lowercase the whole url
*
* @return {string}
*/
public set url (p: string) {
if (p.indexOf('github') === -1) {
throw new Error('Given URL is not a GitHub repository')
}
// NOTE: This is A+ 10/10 string logic. Will not break ever.
const httpPath = (p.startsWith('git@') ? p.replace('git@', 'https://') : p)
.replace('://github.com:', '://github.com/')
.replace(/\.git$/, '')
const url = new URL.URL(httpPath)
const [host, username, repository] = url.pathname.split('/')
this.host = url.host
this.username = username
this.repository = repository
this.authUsername = (url.username !== '') ? url.username : null
this.authPassword = (url.password !== '') ? url.password : null
// GitHub never sends auth codes as the username due to http security
// Headers. This probably means it was parsed weird so we should fix it.
if (this.authUsername != null && this.authPassword == null) {
this.authPassword = this.authUsername
this.authUsername = 'x-access-token'
}
}
/**
* Clones the repository to a folder
*
* @async
* @param {string} p - The path to clone to
* @param {string} [reference] - The branch to clone
* @return {void}
*/
public async clone (p: string, reference = this.reference): Promise<void> {
const repo = await Git.Clone(this.url, p)
const ref = await repo.getReference(reference)
const commit = await ref.peel(Git.Object.TYPE.COMMIT)
const branch = await repo.createBranch('houston', commit, true)
await repo.checkoutBranch(branch, {})
await this.recursiveClone(p)
await fs.remove(path.resolve(p, '.git'))
}
/**
* Returns a list of references this repository has
* TODO: Try to figure out a more optimized way
*
* @async
* @return {string[]}
*/
public async references (): Promise<string[]> {
const p = path.resolve(this.tmpFolder, uuid())
const repo = await Git.Clone(this.url, p)
const branches = await repo.getReferenceNames(Git.Reference.TYPE.LISTALL)
await fs.remove(p)
return branches
}
/**
* Uploads an asset to a GitHub release.
*
* @async
* @param {IPackage} pkg Package to upload
* @param {IStage} stage
* @param {string} [reference]
* @return {IPackage}
*/
public async uploadPackage (pkg: type.IPackage, stage: type.IStage, reference?: string) {
if (pkg.githubId != null) {
return pkg
}
return pkg
const auth = await this.getAuthorization()
const url = `${this.username}/${this.repository}/releases/tags/${reference}`
const { body } = await agent
.get(`https://api.github.com/repos/${url}`)
.set('accept', 'application/vnd.github.v3+json')
.set('user-agent', 'elementary-houston')
.set('authorization', auth)
if (body.upload_url == null) {
throw new Error('No Upload URL for GitHub release')
}
// TODO: Should we remove existing assets that would conflict?
const mime = await GitHub.getFileType(pkg.path)
const stat = await fs.stat(pkg.path)
const file = await fs.createReadStream(pkg.path)
const res = await new Promise((resolve, reject) => {
let data = ''
const req = agent
.post(body.upload_url.replace('{?name,label}', ''))
.set('content-type', mime)
.set('content-length', stat.size)
.set('user-agent', 'elementary-houston')
.set('authorization', auth)
.query({ name: pkg.name })
.query((pkg.description != null) ? { label: pkg.description } : {})
.parse((response, fn) => {
response.on('data', (chunk) => { data += chunk })
response.on('end', fn)
})
.on('error', reject)
.on('end', (err, response) => {
if (err != null) {
return reject(err)
}
try {
return resolve(JSON.parse(data))
} catch (err) {
return reject(err)
}
})
file
.pipe(req)
.on('error', reject)
.on('close', () => resolve(data))
})
return { ...pkg }
}
/**
* Uploads a log to GitHub as an issue with the 'AppCenter' label
*
* @async
* @param {ILog} log
* @param {IStage} stage
* @param {string} [reference]
* @return {ILog}
*/
public async uploadLog (log: type.ILog, stage: type.IStage, reference?: string): Promise<type.ILog> {
if (log.githubId != null) {
return
}
const auth = await this.getAuthorization()
const hasLabel = await agent
.get(`https://api.github.com/repos/${this.username}/${this.repository}/labels/AppCenter`)
.set('user-agent', 'elementary-houston')
.set('authorization', auth)
.then(() => true)
.catch(() => false)
if (!hasLabel) {
await agent
.post(`https://api.github.com/repos/${this.username}/${this.repository}/labels`)
.set('user-agent', 'elementary-houston')
.set('authorization', auth)
.send({
color: '4c158a',
description: 'Issues related to releasing on AppCenter',
name: 'AppCenter'
})
}
const { body } = await agent
.post(`https://api.github.com/repos/${this.username}/${this.repository}/issues`)
.set('user-agent', 'elementary-houston')
.set('authorization', auth)
.send({
body: log.body,
labels: ['AppCenter'],
title: log.title
})
return { ...log, githubId: body.id }
}
/**
* Returns the http Authorization header value
* NOTE: This should be private, but is public for easier testing.
*
* @async
* @return {string}
*/
public async getAuthorization (): Promise<string> {
if (this.authUsername !== 'installation') {
return `Bearer ${this.authPassword}`
} else {
const cachedToken = await this.cache.get(this.authPassword)
if (cachedToken == null) {
const token = await this.generateToken(Number(this.authPassword))
await this.cache.set(this.authPassword, token)
return `token ${token}`
} else {
return `token ${cachedToken}`
}
}
}
/**
* Generates a json web token used for making app requests to GitHub.
*
* @async
* @return {string}
*/
protected async generateJwt (): Promise<string> {
const keyPath = this.config.get('service.github.key')
const key = await fs.readFile(keyPath, 'utf-8')
const payload = {
iat: Math.floor(Date.now() / 1000),
iss: this.config.get('service.github.app')
}
const options = {
algorithm: 'RS256',
expiresIn: '1m'
}
return new Promise<string>((resolve, reject) => {
jsonwebtoken.sign(payload, key, options, (err, token) => {
if (err != null) {
return reject(err)
} else {
return resolve(token)
}
})
})
}
/**
* Uses a json web token to generate an API usable authentication token for
* GitHub.
*
* @async
* @param {number} installation The GitHub app installation number
* @return {string}
*/
protected async generateToken (installation: number): Promise<string> {
const jwt = await this.generateJwt()
return agent
.post(`https://api.github.com/app/installations/${installation}/access_tokens`)
.set('accept', 'application/vnd.github.machine-man-preview+json')
.set('user-agent', 'elementary-houston')
.set('authorization', `Bearer ${jwt}`)
.then((res) => res.body.token)
}
/**
* Clones all of the Git submodules for a given repo path
*
* @async
* @param {String} clonePath - Path of the repository
* @return {void}
*/
protected async recursiveClone (clonePath) {
const repo = await Git.Repository.open(clonePath)
await Git.Submodule.foreach(repo, async (submodule) => {
await submodule.update(1, new Git.SubmoduleUpdateOptions())
await this.recursiveClone(path.join(clonePath, submodule.path()))
})
}
}