12
12
const config = require ( 'config' ) ;
13
13
const _ = require ( 'lodash' ) ;
14
14
const Joi = require ( 'joi' ) ;
15
- const GitHubApi = require ( 'github' ) ;
15
+ const superagent = require ( 'superagent' ) ;
16
+ const superagentPromise = require ( 'superagent-promise' ) ;
17
+ const { Octokit} = require ( '@octokit/rest' ) ;
16
18
const logger = require ( '../utils/logger' ) ;
17
19
const errors = require ( '../utils/errors' ) ;
18
20
const helper = require ( '../utils/helper' ) ;
19
21
22
+ const request = superagentPromise ( superagent , Promise ) ;
23
+
20
24
const copilotUserSchema = Joi . object ( ) . keys ( {
21
25
accessToken : Joi . string ( ) . required ( ) ,
22
26
userProviderId : Joi . number ( ) . required ( ) ,
@@ -44,12 +48,10 @@ function _parseRepoUrl(fullName) {
44
48
*/
45
49
async function _authenticate ( accessToken ) {
46
50
try {
47
- const github = new GitHubApi ( ) ;
48
- github . authenticate ( {
49
- type : 'oauth' ,
50
- token : accessToken
51
+ const octokit = new Octokit ( {
52
+ auth : accessToken
51
53
} ) ;
52
- return github ;
54
+ return octokit . rest ;
53
55
} catch ( err ) {
54
56
throw errors . convertGitHubError ( err , 'Failed to authenticate to Github using access token of copilot.' ) ;
55
57
}
@@ -66,13 +68,11 @@ async function _authenticate(accessToken) {
66
68
*/
67
69
async function _removeAssignees ( github , owner , repo , number , assignees ) {
68
70
try {
69
- await github . issues . removeAssigneesFromIssue ( {
71
+ await github . issues . removeAssignees ( {
70
72
owner,
71
73
repo,
72
- number,
73
- body : {
74
- assignees
75
- }
74
+ issue_number : number ,
75
+ assignees
76
76
} ) ;
77
77
} catch ( err ) {
78
78
throw errors . convertGitHubError ( err , 'Error occurred during remove assignees from issue.' ) ;
@@ -81,13 +81,16 @@ async function _removeAssignees(github, owner, repo, number, assignees) {
81
81
82
82
/**
83
83
* gets the username of given user id
84
- * @param {Object } github the instance of github
85
84
* @param {Number } id the user id
86
85
* @returns {string } username if found
87
86
*/
88
- async function _getUsernameById ( github , id ) {
89
- const user = await github . users . getById ( { id} ) ;
90
- return user ? user . data . login : null ;
87
+ async function _getUsernameById ( id ) {
88
+ const user = await request
89
+ . get ( `https://api.github.com/user/${ id } ` )
90
+ . end ( )
91
+ . then ( ( res ) => res . body ) ;
92
+
93
+ return user ? user . login : null ;
91
94
}
92
95
93
96
/**
@@ -102,7 +105,7 @@ async function updateIssue(copilot, repoFullName, number, title) {
102
105
const github = await _authenticate ( copilot . accessToken ) ;
103
106
const { owner, repo} = _parseRepoUrl ( repoFullName ) ;
104
107
try {
105
- await github . issues . edit ( { owner, repo, number, title} ) ;
108
+ await github . issues . update ( { owner, repo, issue_number : number , title} ) ;
106
109
} catch ( err ) {
107
110
throw errors . convertGitHubError ( err , 'Error occurred during updating issue.' ) ;
108
111
}
@@ -128,13 +131,13 @@ async function assignUser(copilot, repoFullName, number, user) {
128
131
const github = await _authenticate ( copilot . accessToken ) ;
129
132
const { owner, repo} = _parseRepoUrl ( repoFullName ) ;
130
133
try {
131
- const issue = await github . issues . get ( { owner, repo, number} ) ;
134
+ const issue = await github . issues . get ( { owner, repo, issue_number : number } ) ;
132
135
133
136
const oldAssignees = _ ( issue . data . assignees ) . map ( 'login' ) . without ( user ) . value ( ) ;
134
137
if ( oldAssignees && oldAssignees . length > 0 ) {
135
138
await _removeAssignees ( github , owner , repo , number , oldAssignees ) ;
136
139
}
137
- await github . issues . addAssigneesToIssue ( { owner, repo, number, assignees : [ user ] } ) ;
140
+ await github . issues . addAssignees ( { owner, repo, issue_number : number , assignees : [ user ] } ) ;
138
141
} catch ( err ) {
139
142
throw errors . convertGitHubError ( err , 'Error occurred during assigning issue user.' ) ;
140
143
}
@@ -179,7 +182,7 @@ async function createComment(copilot, repoFullName, number, body) {
179
182
const { owner, repo} = _parseRepoUrl ( repoFullName ) ;
180
183
try {
181
184
body = helper . prepareAutomatedComment ( body , copilot ) ;
182
- await github . issues . createComment ( { owner, repo, number, body} ) ;
185
+ await github . issues . createComment ( { owner, repo, issue_number : number , body} ) ;
183
186
} catch ( err ) {
184
187
throw errors . convertGitHubError ( err , 'Error occurred during creating comment on issue.' ) ;
185
188
}
@@ -201,8 +204,7 @@ createComment.schema = {
201
204
*/
202
205
async function getUsernameById ( copilot , userId ) {
203
206
Joi . attempt ( { copilot, userId} , getUsernameById . schema ) ;
204
- const github = await _authenticate ( copilot . accessToken ) ;
205
- const login = await _getUsernameById ( github , userId ) ;
207
+ const login = await _getUsernameById ( userId ) ;
206
208
return login ;
207
209
}
208
210
@@ -220,7 +222,7 @@ getUsernameById.schema = {
220
222
async function getUserIdByLogin ( copilot , login ) {
221
223
Joi . attempt ( { copilot, login} , getUserIdByLogin . schema ) ;
222
224
const github = await _authenticate ( copilot . accessToken ) ;
223
- const user = await github . users . getForUser ( { username : login } ) ;
225
+ const user = await github . users . getByUsername ( { username : login } ) ;
224
226
return user . data ? user . data . id : null ;
225
227
}
226
228
@@ -247,7 +249,7 @@ async function markIssueAsPaid(copilot, repoFullName, number, challengeUUID, exi
247
249
const labels = _ ( existLabels ) . filter ( ( i ) => i !== config . FIX_ACCEPTED_ISSUE_LABEL )
248
250
. push ( config . FIX_ACCEPTED_ISSUE_LABEL , config . PAID_ISSUE_LABEL ) . value ( ) ;
249
251
try {
250
- await github . issues . edit ( { owner, repo, number, labels} ) ;
252
+ await github . issues . update ( { owner, repo, issue_number : number , labels} ) ;
251
253
let commentMessage = '' ;
252
254
commentMessage += `Payment task has been updated: ${ config . TC_URL } /challenges/${ challengeUUID } \n` ;
253
255
commentMessage += '*Payments Complete*\n' ;
@@ -258,7 +260,7 @@ async function markIssueAsPaid(copilot, repoFullName, number, challengeUUID, exi
258
260
commentMessage += `Challenge \`${ challengeUUID } \` has been paid and closed.` ;
259
261
260
262
const body = helper . prepareAutomatedComment ( commentMessage , copilot ) ;
261
- await github . issues . createComment ( { owner, repo, number, body} ) ;
263
+ await github . issues . createComment ( { owner, repo, issue_number : number , body} ) ;
262
264
} catch ( err ) {
263
265
throw errors . convertGitHubError ( err , 'Error occurred during updating issue as paid.' ) ;
264
266
}
@@ -287,7 +289,7 @@ async function changeState(copilot, repoFullName, number, state) {
287
289
const github = await _authenticate ( copilot . accessToken ) ;
288
290
const { owner, repo} = _parseRepoUrl ( repoFullName ) ;
289
291
try {
290
- await github . issues . edit ( { owner, repo, number, state} ) ;
292
+ await github . issues . update ( { owner, repo, issue_number : number , state} ) ;
291
293
} catch ( err ) {
292
294
throw errors . convertGitHubError ( err , 'Error occurred during updating status of issue.' ) ;
293
295
}
@@ -313,7 +315,7 @@ async function addLabels(copilot, repoFullName, number, labels) {
313
315
const github = await _authenticate ( copilot . accessToken ) ;
314
316
const { owner, repo} = _parseRepoUrl ( repoFullName ) ;
315
317
try {
316
- await github . issues . edit ( { owner, repo, number, labels} ) ;
318
+ await github . issues . update ( { owner, repo, issue_number : number , labels} ) ;
317
319
} catch ( err ) {
318
320
throw errors . convertGitHubError ( err , 'Error occurred during adding label in issue.' ) ;
319
321
}
0 commit comments