9
9
IRepoListResponse ,
10
10
IMRDiffResponse ,
11
11
IMRDetailResponse ,
12
+ IMRActivitiesResponse ,
13
+ IMRReviewersResponse ,
12
14
} from 'src/typings/respResult' ;
13
15
import { PromiseAdapter , promiseFromEvent , parseQuery , parseCloneUrl } from 'src/common/utils' ;
14
16
import { GitService } from 'src/common/gitService' ;
@@ -38,7 +40,7 @@ export class CodingServer {
38
40
private _pendingStates = new Map < string , string [ ] > ( ) ;
39
41
private _codeExchangePromises = new Map < string , Promise < AuthSuccessResult > > ( ) ;
40
42
41
- private _loggedIn : boolean = false ;
43
+ private _loggedIn = false ;
42
44
private _context : vscode . ExtensionContext ;
43
45
private _session : ISessionData | null = null ;
44
46
@@ -236,6 +238,14 @@ export class CodingServer {
236
238
return parseCloneUrl ( url || `` ) ;
237
239
}
238
240
241
+ public getApiPrefix ( ) {
242
+ const repoInfo = this . _context . workspaceState . get ( `repoInfo` ) as IRepoInfo ;
243
+ if ( ! repoInfo ?. team ) {
244
+ throw new Error ( `team not exist` ) ;
245
+ }
246
+ return `https://${ repoInfo . team } .coding.net/api/user/${ this . _session ?. user ?. team } /project/${ repoInfo . project } /depot/${ repoInfo . repo } ` ;
247
+ }
248
+
239
249
public async getMRList ( repo ?: string , status ?: string ) : Promise < CodingResponse > {
240
250
try {
241
251
const repoInfo = this . _context . workspaceState . get ( `repoInfo` ) as IRepoInfo ;
@@ -298,20 +308,13 @@ export class CodingServer {
298
308
299
309
public async getMRDiff ( iid : number ) {
300
310
try {
301
- const repoInfo = this . _context . workspaceState . get ( `repoInfo` ) as IRepoInfo ;
302
- if ( ! repoInfo ?. team ) {
303
- throw new Error ( `team not exist` ) ;
304
- }
305
-
311
+ const url = this . getApiPrefix ( ) ;
306
312
const diff : IMRDiffResponse = await got
307
- . get (
308
- `https://${ repoInfo . team } .coding.net/api/user/${ this . _session ?. user ?. team } /project/${ repoInfo . project } /depot/${ repoInfo . repo } /git/merge/${ iid } /diff` ,
309
- {
310
- searchParams : {
311
- access_token : this . _session ?. accessToken ,
312
- } ,
313
+ . get ( `${ url } /git/merge/${ iid } /diff` , {
314
+ searchParams : {
315
+ access_token : this . _session ?. accessToken ,
313
316
} ,
314
- )
317
+ } )
315
318
. json ( ) ;
316
319
if ( diff . code ) {
317
320
return Promise . reject ( diff ) ;
@@ -324,20 +327,13 @@ export class CodingServer {
324
327
325
328
public async getMRDetail ( iid : string ) {
326
329
try {
327
- const repoInfo = this . _context . workspaceState . get ( `repoInfo` ) as IRepoInfo ;
328
- if ( ! repoInfo ?. team ) {
329
- throw new Error ( `team not exist` ) ;
330
- }
331
-
330
+ const url = this . getApiPrefix ( ) ;
332
331
const diff : IMRDetailResponse = await got
333
- . get (
334
- `https://${ repoInfo . team } .coding.net/api/user/${ this . _session ?. user ?. team } /project/${ repoInfo . project } /depot/${ repoInfo . repo } /git/merge/${ iid } /detail` ,
335
- {
336
- searchParams : {
337
- access_token : this . _session ?. accessToken ,
338
- } ,
332
+ . get ( `${ url } /git/merge/${ iid } /detail` , {
333
+ searchParams : {
334
+ access_token : this . _session ?. accessToken ,
339
335
} ,
340
- )
336
+ } )
341
337
. json ( ) ;
342
338
343
339
if ( diff . code ) {
@@ -350,6 +346,203 @@ export class CodingServer {
350
346
}
351
347
}
352
348
349
+ public async getMRActivities ( iid : string ) {
350
+ try {
351
+ const url = this . getApiPrefix ( ) ;
352
+ const result : IMRActivitiesResponse = await got
353
+ . get ( `${ url } /git/merge/${ iid } /activities` , {
354
+ searchParams : {
355
+ access_token : this . _session ?. accessToken ,
356
+ } ,
357
+ } )
358
+ . json ( ) ;
359
+
360
+ if ( result . code ) {
361
+ return Promise . reject ( result ) ;
362
+ }
363
+ return result ;
364
+ } catch ( err ) {
365
+ return Promise . reject ( err ) ;
366
+ }
367
+ }
368
+
369
+ public async getMRReviewers ( iid : string ) {
370
+ try {
371
+ const url = this . getApiPrefix ( ) ;
372
+ const result : IMRReviewersResponse = await got
373
+ . get ( `${ url } /git/merge/${ iid } /reviewers` , {
374
+ searchParams : {
375
+ access_token : this . _session ?. accessToken ,
376
+ } ,
377
+ } )
378
+ . json ( ) ;
379
+
380
+ if ( result . code ) {
381
+ return Promise . reject ( result ) ;
382
+ }
383
+ return result ;
384
+ } catch ( err ) {
385
+ return Promise . reject ( err ) ;
386
+ }
387
+ }
388
+
389
+ public async getMRComments ( iid : string ) {
390
+ try {
391
+ const url = this . getApiPrefix ( ) ;
392
+ const result : CodingResponse = await got
393
+ . get ( `${ url } /git/merge/${ iid } /comments` , {
394
+ searchParams : {
395
+ access_token : this . _session ?. accessToken ,
396
+ } ,
397
+ } )
398
+ . json ( ) ;
399
+
400
+ if ( result . code ) {
401
+ return Promise . reject ( result ) ;
402
+ }
403
+ return result ;
404
+ } catch ( err ) {
405
+ return Promise . reject ( err ) ;
406
+ }
407
+ }
408
+
409
+ public async closeMR ( iid : string ) {
410
+ try {
411
+ const url = this . getApiPrefix ( ) ;
412
+ const result : CodingResponse = await got
413
+ . post ( `${ url } /git/merge/${ iid } /refuse` , {
414
+ searchParams : {
415
+ access_token : this . _session ?. accessToken ,
416
+ } ,
417
+ } )
418
+ . json ( ) ;
419
+
420
+ if ( result . code ) {
421
+ return Promise . reject ( result ) ;
422
+ }
423
+ return result ;
424
+ } catch ( err ) {
425
+ return Promise . reject ( err ) ;
426
+ }
427
+ }
428
+
429
+ public async approveMR ( iid : string ) {
430
+ try {
431
+ const url = this . getApiPrefix ( ) ;
432
+ const result : CodingResponse = await got
433
+ . post ( `${ url } /git/merge/${ iid } /good` , {
434
+ searchParams : {
435
+ access_token : this . _session ?. accessToken ,
436
+ } ,
437
+ } )
438
+ . json ( ) ;
439
+
440
+ if ( result . code ) {
441
+ return Promise . reject ( result ) ;
442
+ }
443
+ return result ;
444
+ } catch ( err ) {
445
+ return Promise . reject ( err ) ;
446
+ }
447
+ }
448
+
449
+ public async disapproveMR ( iid : string ) {
450
+ try {
451
+ const url = this . getApiPrefix ( ) ;
452
+ const result : CodingResponse = await got
453
+ . delete ( `${ url } /git/merge/${ iid } /good` , {
454
+ searchParams : {
455
+ access_token : this . _session ?. accessToken ,
456
+ } ,
457
+ } )
458
+ . json ( ) ;
459
+
460
+ if ( result . code ) {
461
+ return Promise . reject ( result ) ;
462
+ }
463
+ return result ;
464
+ } catch ( err ) {
465
+ return Promise . reject ( err ) ;
466
+ }
467
+ }
468
+
469
+ public async mergeMR ( iid : string ) {
470
+ try {
471
+ const url = this . getApiPrefix ( ) ;
472
+ const result : CodingResponse = await got
473
+ . post ( `${ url } /git/merge/${ iid } /merge` , {
474
+ searchParams : {
475
+ access_token : this . _session ?. accessToken ,
476
+ } ,
477
+ headers : {
478
+ 'content-type' : 'application/x-www-form-urlencoded;charset=UTF-8' ,
479
+ } ,
480
+ } )
481
+ . json ( ) ;
482
+
483
+ if ( result . code ) {
484
+ return Promise . reject ( result ) ;
485
+ }
486
+ return result ;
487
+ } catch ( err ) {
488
+ return Promise . reject ( err ) ;
489
+ }
490
+ }
491
+
492
+ public async updateMRTitle ( iid : string , title : string ) {
493
+ try {
494
+ const url = this . getApiPrefix ( ) ;
495
+ const result : CodingResponse = await got
496
+ . put ( `${ url } /git/merge/${ iid } /update-title` , {
497
+ searchParams : {
498
+ access_token : this . _session ?. accessToken ,
499
+ title,
500
+ } ,
501
+ headers : {
502
+ 'content-type' : 'application/x-www-form-urlencoded;charset=UTF-8' ,
503
+ } ,
504
+ } )
505
+ . json ( ) ;
506
+
507
+ if ( result . code ) {
508
+ return Promise . reject ( result ) ;
509
+ }
510
+ return result ;
511
+ } catch ( err ) {
512
+ return Promise . reject ( err ) ;
513
+ }
514
+ }
515
+
516
+ public async commentMR ( mrId : number , comment : string ) {
517
+ try {
518
+ const url = this . getApiPrefix ( ) ;
519
+ const result : CodingResponse = await got
520
+ . post ( `${ url } /git/line_notes` , {
521
+ searchParams : {
522
+ access_token : this . _session ?. accessToken ,
523
+ line : 0 ,
524
+ change_type : 0 ,
525
+ position : 0 ,
526
+ content : comment ,
527
+ noteable_type : 'MergeRequestBean' ,
528
+ noteable_id : mrId ,
529
+ parent_id : 0 ,
530
+ } ,
531
+ headers : {
532
+ 'content-type' : 'application/x-www-form-urlencoded;charset=UTF-8' ,
533
+ } ,
534
+ } )
535
+ . json ( ) ;
536
+
537
+ if ( result . code ) {
538
+ return Promise . reject ( result ) ;
539
+ }
540
+ return result ;
541
+ } catch ( err ) {
542
+ return Promise . reject ( err ) ;
543
+ }
544
+ }
545
+
353
546
public async getRemoteFileContent ( path : string ) {
354
547
try {
355
548
const repoInfo = this . _context . workspaceState . get ( `repoInfo` ) as IRepoInfo ;
@@ -358,14 +551,11 @@ export class CodingServer {
358
551
}
359
552
360
553
const url = `https://${ repoInfo . team } .coding.net/p/${ repoInfo . project } /d/${ repoInfo . repo } /git/raw/${ path } ` ;
361
- const { body } = await got . get (
362
- url ,
363
- {
364
- searchParams : {
365
- access_token : this . _session ?. accessToken ,
366
- } ,
554
+ const { body } = await got . get ( url , {
555
+ searchParams : {
556
+ access_token : this . _session ?. accessToken ,
367
557
} ,
368
- ) ;
558
+ } ) ;
369
559
370
560
return body ;
371
561
} catch ( err ) {
0 commit comments