-
Notifications
You must be signed in to change notification settings - Fork 555
/
Copy pathGitHub.js
204 lines (175 loc) · 5.28 KB
/
GitHub.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
'use strict'
const config = require('../config')
const errorHandler = require('./ErrorHandler')
const GithubApi = require('@octokit/rest')
const { App } = require('@octokit/app')
const { request } = require('@octokit/request')
const GitService = require('./GitService')
const Review = require('./models/Review')
const User = require('./models/User')
const normalizeResponse = ({data}) => data
class GitHub extends GitService {
constructor (options = {}) {
super(options.username, options.repository, options.branch)
return (async () => {
const isAppAuth = config.get('githubAppID') &&
config.get('githubPrivateKey')
const isLegacyAuth = config.get('githubToken') &&
['1', '2'].includes(options.version)
let authToken
if (options.oauthToken) {
authToken = options.oauthToken
} else if (isLegacyAuth) {
authToken = config.get('githubToken')
} else if (isAppAuth) {
authToken = await this._authenticate(options.username, options.repository)
} else {
throw new Error('Require an `oauthToken` or `token` option')
}
this.api = GithubApi({
auth: `token ${authToken}`,
userAgent: 'Staticman',
baseUrl: config.get('githubBaseUrl'),
request: {
timeout: 5000
}
})
return this
})()
}
async _authenticate (username, repository) {
const app = new App(
{
id: config.get('githubAppID'),
privateKey: config.get('githubPrivateKey'),
baseUrl: config.get('githubBaseUrl')
}
)
const jwt = app.getSignedJsonWebToken()
const {data} = await request('GET /repos/:owner/:repo/installation', {
owner: username,
repo: repository,
headers: {
authorization: `Bearer ${jwt}`,
accept: 'application/vnd.github.machine-man-preview+json'
}
})
const installationId = data.id
let token = await app.getInstallationAccessToken({installationId})
return token
}
_pullFile (filePath, branch) {
return this.api.repos.getContents({
owner: this.username,
repo: this.repository,
path: filePath,
ref: branch
})
.then(normalizeResponse)
.catch(err => Promise.reject(errorHandler('GITHUB_READING_FILE', {err})))
}
_commitFile (filePath, content, commitMessage, branch) {
return this.api.repos.createOrUpdateFile({
owner: this.username,
repo: this.repository,
path: filePath,
message: commitMessage,
content,
branch
})
.then(normalizeResponse)
}
writeFile (filePath, data, targetBranch, commitTitle) {
return super.writeFile(filePath, data, targetBranch, commitTitle)
.catch(err => {
try {
const message = err && err.message
if (message) {
const parsedError = JSON.parse(message)
if (
parsedError &&
parsedError.message &&
parsedError.message.includes('"sha" wasn\'t supplied')
) {
return Promise.reject(errorHandler('GITHUB_FILE_ALREADY_EXISTS', {err}))
}
}
} catch (err) {
console.log(err)
}
return Promise.reject(errorHandler('GITHUB_WRITING_FILE'))
})
}
getBranchHeadCommit (branch) {
return this.api.repos.getBranch({
owner: this.username,
repo: this.repository,
branch
})
.then(res => res.data.commit.sha)
}
createBranch (branch, sha) {
return this.api.git.createRef({
owner: this.username,
repo: this.repository,
ref: `refs/heads/${branch}`,
sha
})
.then(normalizeResponse)
}
deleteBranch (branch) {
return this.api.git.deleteRef({
owner: this.username,
repo: this.repository,
ref: `heads/${branch}`
})
}
createReview (reviewTitle, branch, reviewBody) {
return this.api.pullRequests.create({
owner: this.username,
repo: this.repository,
title: reviewTitle,
head: branch,
base: this.branch,
body: reviewBody
})
.then(normalizeResponse)
}
getReview (reviewId) {
return this.api.pulls.get({
owner: this.username,
repo: this.repository,
pull_number: reviewId
})
.then(normalizeResponse)
.then(({base, body, head, merged, state, title}) =>
new Review(
title,
body,
(merged && state === 'closed') ? 'merged' : state,
head.ref,
base.ref
)
)
}
async readFile (filePath, getFullResponse) {
try {
return await super.readFile(filePath, getFullResponse)
} catch (err) {
throw errorHandler('GITHUB_READING_FILE', {err})
}
}
writeFileAndSendReview (filePath, data, branch, commitTitle, reviewBody) {
return super.writeFileAndSendReview(filePath, data, branch, commitTitle, reviewBody)
.catch(err => Promise.reject(errorHandler('GITHUB_CREATING_PR', {err})))
}
getCurrentUser () {
return this.api.users.getAuthenticated({})
.then(normalizeResponse)
.then(({login, email, avatar_url, name, bio, company, blog}) =>
new User('github', login, email, name, avatar_url, bio, blog, company)
)
.catch(err => Promise.reject(errorHandler('GITHUB_GET_USER', {err})))
}
}
module.exports = GitHub