4
4
'use strict' ;
5
5
6
6
/**
7
- * This provides methods around gitlab api.
7
+ * This provides methods around azure api.
8
8
* @author TCSCODER
9
9
* @version 1.0
10
10
*/
11
11
12
12
const config = require ( 'config' ) ;
13
13
const _ = require ( 'lodash' ) ;
14
14
const Joi = require ( 'joi' ) ;
15
- const GitlabAPI = require ( 'node-gitlab-api' ) ;
16
15
const superagent = require ( 'superagent' ) ;
17
16
const superagentPromise = require ( 'superagent-promise' ) ;
18
17
const logger = require ( '../utils/logger' ) ;
@@ -31,43 +30,7 @@ const copilotUserSchema = Joi.object().keys({
31
30
} ) . required ( ) ;
32
31
33
32
/**
34
- * authenticate the gitlab using access token
35
- * @param {String } accessToken the access token of copilot
36
- * @returns {Object } the gitlab instance
37
- * @private
38
- */
39
- async function _authenticate ( accessToken ) {
40
- try {
41
- const gitlab = GitlabAPI ( {
42
- url : config . GITLAB_API_BASE_URL ,
43
- oauthToken : accessToken
44
- } ) ;
45
- return gitlab ;
46
- } catch ( err ) {
47
- throw errors . convertGitLabError ( err , 'Failed to during authenticate to Github using access token of copilot.' ) ;
48
- }
49
- }
50
-
51
- /**
52
- * Removes assignees from issue
53
- * @param {Object } gitlab the gitlab instance
54
- * @param {Number } projectId the project id
55
- * @param {Number } issueId the issue number
56
- * @param {Array } assignees the users to remove
57
- * @private
58
- */
59
- async function _removeAssignees ( gitlab , projectId , issueId , assignees ) {
60
- try {
61
- const issue = await gitlab . projects . issues . show ( projectId , issueId ) ;
62
- const oldAssignees = _ . difference ( issue . assignee_ids , assignees ) ;
63
- await gitlab . projects . issues . edit ( projectId , issueId , { assignee_ids : oldAssignees } ) ;
64
- } catch ( err ) {
65
- throw errors . convertGitLabError ( err , 'Error occurred during remove assignees from issue.' ) ;
66
- }
67
- }
68
-
69
- /**
70
- * creates the comments on gitlab issue
33
+ * creates the comments on azure issue
71
34
* @param {Object } copilot the copilot
72
35
* @param {String } repoFullName the organization/project-name
73
36
* @param {Number } workItemId the issue number
@@ -86,7 +49,7 @@ async function createComment(copilot, repoFullName, workItemId, body) {
86
49
. set ( 'Content-Type' , 'application/json' )
87
50
. end ( ) ;
88
51
} catch ( err ) {
89
- throw errors . convertGitLabError ( err , 'Error occurred during creating comment on issue.' ) ;
52
+ throw errors . convertAzureError ( err , 'Error occurred during creating comment on issue.' ) ;
90
53
}
91
54
logger . debug ( `Azure comment is added on issue with message: "${ body } "` ) ;
92
55
}
@@ -99,72 +62,95 @@ createComment.schema = {
99
62
} ;
100
63
101
64
/**
102
- * updates the title of gitlab issue
65
+ * updates the title of azure issue
103
66
* @param {Object } copilot the copilot
104
- * @param {Number } projectId the project id
67
+ * @param {Number } repoFullName the project id
105
68
* @param {Number } issueId the issue number
106
69
* @param {string } title new title
107
70
*/
108
- async function updateIssue ( copilot , projectId , issueId , title ) {
109
- Joi . attempt ( { copilot, projectId, issueId, title} , updateIssue . schema ) ;
110
- const gitlab = await _authenticate ( copilot . accessToken ) ;
71
+ async function updateIssue ( copilot , repoFullName , issueId , title ) {
72
+ Joi . attempt ( { copilot, repoFullName, issueId, title} , updateIssue . schema ) ;
111
73
try {
112
- await gitlab . projects . issues . edit ( projectId , issueId , { title} ) ;
74
+ await request
75
+ . patch ( `${ config . AZURE_DEVOPS_API_BASE_URL } /${ repoFullName } /_apis/wit/workItems/${ issueId } ?api-version=5.1` )
76
+ . send ( [ {
77
+ op : 'add' ,
78
+ path : '/fields/System.Title' ,
79
+ value : title
80
+ } ] )
81
+ . set ( 'Authorization' , `Bearer ${ copilot . accessToken } ` )
82
+ . set ( 'Content-Type' , 'application/json-patch+json' )
83
+ . end ( ) ;
113
84
} catch ( err ) {
114
- throw errors . convertGitLabError ( err , 'Error occurred during updating issue.' ) ;
85
+ throw errors . convertAzureError ( err , 'Error occurred during updating issue.' ) ;
115
86
}
116
87
logger . debug ( `Azure issue title is updated for issue number ${ issueId } ` ) ;
117
88
}
118
89
119
90
updateIssue . schema = {
120
91
copilot : copilotUserSchema ,
121
- projectId : Joi . number ( ) . positive ( ) . required ( ) ,
92
+ repoFullName : Joi . string ( ) . required ( ) ,
122
93
issueId : Joi . number ( ) . positive ( ) . required ( ) ,
123
94
title : Joi . string ( ) . required ( )
124
95
} ;
125
96
126
97
/**
127
98
* Assigns the issue to user login
128
99
* @param {Object } copilot the copilot
129
- * @param {Number } projectId the project id
100
+ * @param {Number } repoFullName the project id
130
101
* @param {Number } issueId the issue number
131
- * @param {Number } userId the user id of assignee
102
+ * @param {Number } user the user id of assignee
132
103
*/
133
- async function assignUser ( copilot , projectId , issueId , userId ) {
134
- Joi . attempt ( { copilot, projectId, issueId, userId} , assignUser . schema ) ;
135
- const gitlab = await _authenticate ( copilot . accessToken ) ;
104
+ async function assignUser ( copilot , repoFullName , issueId , user ) {
105
+ Joi . attempt ( { copilot, repoFullName, issueId, user} , assignUser . schema ) ;
136
106
try {
137
- const issue = await gitlab . projects . issues . show ( projectId , issueId ) ;
138
- const oldAssignees = _ . without ( issue . assignee_ids , userId ) ;
139
- if ( oldAssignees && oldAssignees . length > 0 ) {
140
- await _removeAssignees ( gitlab , projectId , issueId , oldAssignees ) ;
141
- }
142
- await gitlab . projects . issues . edit ( projectId , issueId , { assignee_ids : [ userId ] } ) ;
107
+ await request
108
+ . patch ( `${ config . AZURE_DEVOPS_API_BASE_URL } /${ repoFullName } /_apis/wit/workItems/${ issueId } ?api-version=5.1` )
109
+ . send ( [ {
110
+ op : 'add' ,
111
+ path : '/fields/System.AssignedTo' ,
112
+ value : user
113
+ } ] )
114
+ . set ( 'Authorization' , `Bearer ${ copilot . accessToken } ` )
115
+ . set ( 'Content-Type' , 'application/json-patch+json' )
116
+ . end ( ) ;
143
117
} catch ( err ) {
144
- throw errors . convertGitLabError ( err , 'Error occurred during assigning issue user .' ) ;
118
+ throw errors . convertAzureError ( err , 'Error occurred during update assignee .' ) ;
145
119
}
146
120
logger . debug ( `Azure issue with number ${ issueId } is assigned to ${ issueId } ` ) ;
147
121
}
148
122
149
123
assignUser . schema = {
150
124
copilot : copilotUserSchema ,
151
- projectId : Joi . number ( ) . positive ( ) . required ( ) ,
125
+ repoFullName : Joi . string ( ) . required ( ) ,
152
126
issueId : Joi . number ( ) . positive ( ) . required ( ) ,
153
- userId : Joi . number ( ) . required ( )
127
+ user : Joi . string ( )
154
128
} ;
155
129
156
130
/**
157
131
* Removes an assignee from the issue
158
132
* @param {Object } copilot the copilot
159
- * @param {Number } projectId the project id
133
+ * @param {Number } repoFullName the project id
160
134
* @param {Number } issueId the issue number
161
135
* @param {Number } userId the user id of assignee to remove
162
136
*/
163
- async function removeAssign ( copilot , projectId , issueId , userId ) {
164
- Joi . attempt ( { copilot, projectId, issueId, userId} , removeAssign . schema ) ;
165
- const gitlab = await _authenticate ( copilot . accessToken ) ;
166
- await _removeAssignees ( gitlab , projectId , issueId , [ userId ] ) ;
167
- logger . debug ( `Azure user ${ userId } is unassigned from issue number ${ issueId } ` ) ;
137
+ async function removeAssign ( copilot , repoFullName , issueId ) {
138
+ Joi . attempt ( { copilot, repoFullName, issueId} , removeAssign . schema ) ;
139
+ try {
140
+ await request
141
+ . patch ( `${ config . AZURE_DEVOPS_API_BASE_URL } /${ repoFullName } /_apis/wit/workItems/${ issueId } ?api-version=5.1` )
142
+ . send ( [ {
143
+ op : 'add' ,
144
+ path : '/fields/System.AssignedTo' ,
145
+ value : ''
146
+ } ] )
147
+ . set ( 'Authorization' , `Bearer ${ copilot . accessToken } ` )
148
+ . set ( 'Content-Type' , 'application/json-patch+json' )
149
+ . end ( ) ;
150
+ } catch ( err ) {
151
+ throw errors . convertAzureError ( err , 'Error occurred during remove assignee.' ) ;
152
+ }
153
+ logger . debug ( `Azure user is unassigned from issue number ${ issueId } ` ) ;
168
154
}
169
155
170
156
removeAssign . schema = assignUser . schema ;
@@ -191,25 +177,7 @@ getUsernameById.schema = {
191
177
} ;
192
178
193
179
/**
194
- * Gets the user id by username
195
- * @param {Object } copilot the copilot
196
- * @param {string } login the username
197
- * @returns {Number } the user id if found else null
198
- */
199
- async function getUserIdByLogin ( copilot , login ) {
200
- Joi . attempt ( { copilot, login} , getUserIdByLogin . schema ) ;
201
- const gitlab = await _authenticate ( copilot . accessToken ) ;
202
- const user = await gitlab . users . all ( { username : login } ) ;
203
- return user . length ? user [ 0 ] . id : null ;
204
- }
205
-
206
- getUserIdByLogin . schema = {
207
- copilot : copilotUserSchema ,
208
- login : Joi . string ( ) . required ( )
209
- } ;
210
-
211
- /**
212
- * updates the gitlab issue as paid and fix accepted
180
+ * updates the azure issue as paid and fix accepted
213
181
* @param {Object } copilot the copilot
214
182
* @param {Number } repoFullName the project id
215
183
* @param {Number } issueId the issue number
@@ -241,7 +209,7 @@ async function markIssueAsPaid(copilot, repoFullName, issueId, challengeId, exis
241
209
. set ( 'Content-Type' , 'application/json' )
242
210
. end ( ) ;
243
211
} catch ( err ) {
244
- throw errors . convertGitLabError ( err , 'Error occurred during updating issue as paid.' ) ;
212
+ throw errors . convertAzureError ( err , 'Error occurred during updating issue as paid.' ) ;
245
213
}
246
214
logger . debug ( `Azure issue is updated for as paid and fix accepted for ${ issueId } ` ) ;
247
215
}
@@ -254,36 +222,44 @@ markIssueAsPaid.schema = {
254
222
} ;
255
223
256
224
/**
257
- * change the state of gitlab issue
225
+ * change the state of azure issue
258
226
* @param {Object } copilot the copilot
259
- * @param {string } projectId the project id
227
+ * @param {string } repoFullName the project id
260
228
* @param {Number } issueId the issue issue id
261
229
* @param {string } state new state
262
230
*/
263
- async function changeState ( copilot , projectId , issueId , state ) {
264
- Joi . attempt ( { copilot, projectId, issueId, state} , changeState . schema ) ;
265
- const gitlab = await _authenticate ( copilot . accessToken ) ;
231
+ async function changeState ( copilot , repoFullName , issueId , state ) {
232
+ Joi . attempt ( { copilot, repoFullName, issueId, state} , changeState . schema ) ;
266
233
try {
267
- await gitlab . projects . issues . edit ( projectId , issueId , { state_event : state } ) ;
234
+ await request
235
+ . patch ( `${ config . AZURE_DEVOPS_API_BASE_URL } /${ repoFullName } /_apis/wit/workItems/${ issueId } ?api-version=5.1` )
236
+ . send ( [ {
237
+ op : 'add' ,
238
+ path : '/fields/System.State' ,
239
+ value : state
240
+ } ] )
241
+ . set ( 'Authorization' , `Bearer ${ copilot . accessToken } ` )
242
+ . set ( 'Content-Type' , 'application/json-patch+json' )
243
+ . end ( ) ;
268
244
} catch ( err ) {
269
- throw errors . convertGitLabError ( err , 'Error occurred during updating status of issue.' ) ;
245
+ throw errors . convertAzureError ( err , 'Error occurred during updating status of issue.' ) ;
270
246
}
271
247
logger . debug ( `Azure issue state is updated to '${ state } ' for issue number ${ issueId } ` ) ;
272
248
}
273
249
274
250
changeState . schema = {
275
251
copilot : copilotUserSchema ,
276
- projectId : Joi . number ( ) . positive ( ) . required ( ) ,
252
+ repoFullName : Joi . string ( ) . required ( ) ,
277
253
issueId : Joi . number ( ) . positive ( ) . required ( ) ,
278
254
state : Joi . string ( ) . required ( )
279
255
} ;
280
256
281
257
/**
282
- * updates the gitlab issue with new labels
258
+ * updates the azure issue with new labels
283
259
* @param {Object } copilot the copilot
284
260
* @param {string } repoFullName the project id
285
261
* @param {Number } issueId the issue issue id
286
- * @param {Number } labels the labels
262
+ * @param {Array } labels the labels
287
263
*/
288
264
async function addLabels ( copilot , repoFullName , issueId , labels ) {
289
265
Joi . attempt ( { copilot, repoFullName, issueId, labels} , addLabels . schema ) ;
@@ -300,7 +276,7 @@ async function addLabels(copilot, repoFullName, issueId, labels) {
300
276
. set ( 'Content-Type' , 'application/json-patch+json' )
301
277
. end ( ) ;
302
278
} catch ( err ) {
303
- throw errors . convertGitLabError ( err , 'Error occurred during adding label in issue.' ) ;
279
+ throw errors . convertAzureError ( err , 'Error occurred during adding label in issue.' ) ;
304
280
}
305
281
logger . debug ( `Azure issue is updated with new labels for ${ issueId } ` ) ;
306
282
}
@@ -357,7 +333,6 @@ module.exports = {
357
333
assignUser,
358
334
removeAssign,
359
335
getUsernameById,
360
- getUserIdByLogin,
361
336
markIssueAsPaid,
362
337
changeState,
363
338
addLabels,
0 commit comments