-
Notifications
You must be signed in to change notification settings - Fork 561
/
Copy pathGitService.js
104 lines (80 loc) · 2.6 KB
/
GitService.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
'use strict'
const errorHandler = require('./ErrorHandler')
const yaml = require('js-yaml')
class GitService {
constructor (username, repository, branch) {
this.username = username
this.repository = repository
this.branch = branch
}
_pullFile (filePath, branch) {
throw new Error('Abstract method `_pullFile` should be implemented')
}
_commitFile (filePath, contents, commitTitle, branch) {
throw new Error('Abstract method `_commitFile` should be implemented')
}
getBranchHeadCommit (branch) {
throw new Error('Abstract method `getBranchHeadCommit` should be implemented')
}
createBranch (branch, sha) {
throw new Error('Abstract method `createBranch` should be implemented')
}
deleteBranch (branch) {
throw new Error('Abstract method `deleteBranch` should be implemented')
}
createReview (commitTitle, branch, reviewBody) {
throw new Error('Abstract method `createReview` should be implemented')
}
getReview (reviewId) {
throw new Error('Abstract method `getReview` should be implemented')
}
getCurrentUser () {
throw new Error('Abstract method `getCurrentUser` should be implemented')
}
async readFile (path, getFullResponse) {
const extension = path.split('.').pop()
let res = await this._pullFile(path, this.branch)
let content
try {
content = Buffer.from(res.content, 'base64').toString()
} catch (err) {
throw errorHandler('GITHUB_READING_FILE', {err})
}
try {
switch (extension) {
case 'yml':
case 'yaml':
content = yaml.safeLoad(content, 'utf8')
break
case 'json':
content = JSON.parse(content)
break
}
if (getFullResponse) {
return {
content: content,
file: {
content: res.content
}
}
}
return content
} catch (err) {
let errorData = {err}
if (err.message) {
errorData.data = err.message
}
throw errorHandler('PARSING_ERROR', errorData)
}
}
writeFile (filePath, data, branch = this.branch, commitTitle = 'Add Staticman file') {
return this._commitFile(filePath, Buffer.from(data).toString('base64'), commitTitle, branch)
}
writeFileAndSendReview (filePath, data, branch, commitTitle = 'Add Staticman file', reviewBody = '') {
return this.getBranchHeadCommit(this.branch)
.then(sha => this.createBranch(branch, sha))
.then(() => this.writeFile(filePath, data, branch, commitTitle))
.then(() => this.createReview(commitTitle, branch, reviewBody))
}
}
module.exports = GitService