@@ -14,6 +14,9 @@ import {
14
14
ICreateMRBody ,
15
15
ICreateMRResp ,
16
16
IBranchListResp ,
17
+ IMemberListResp ,
18
+ IMRContentResp ,
19
+ ICreateCommentResp ,
17
20
} from 'src/typings/respResult' ;
18
21
import { PromiseAdapter , promiseFromEvent , parseQuery , parseCloneUrl } from 'src/common/utils' ;
19
22
import { GitService } from 'src/common/gitService' ;
@@ -25,7 +28,7 @@ const AUTH_SERVER = `https://x5p7m.csb.app`;
25
28
const ClientId = `ff768664c96d04235b1cc4af1e3b37a8` ;
26
29
const ClientSecret = `d29ebb32cab8b5f0a643b5da7dcad8d1469312c7` ;
27
30
28
- export const ScopeList = [ `user` , `user:email` , `project` , `project:depot` ] ;
31
+ export const ScopeList = [ `user` , `user:email` , `project` , `project:depot` , `project:members` ] ;
29
32
const SCOPES = ScopeList . join ( `,` ) ;
30
33
const NETWORK_ERROR = 'network error' ;
31
34
@@ -246,14 +249,20 @@ export class CodingServer {
246
249
throw new Error ( `team not exist` ) ;
247
250
}
248
251
249
- return `https://${ repoInfo . team } .coding.net/api/user/${ this . _session ?. user ?. team } /project/${ repoInfo . project } /depot/${ repoInfo . repo } ` ;
252
+ const projectApiPrefix = `https://${ repoInfo . team } .coding.net/api/user/${ this . _session ?. user ?. team } /project/${ repoInfo . project } ` ;
253
+ return {
254
+ projectApiPrefix,
255
+ repoApiPrefix : `${ projectApiPrefix } /depot/${ repoInfo . repo } /git` ,
256
+ userApiPrefix : `https://${ repoInfo . team } .coding.net/api/user/${ this . _session ?. user ?. global_key } ` ,
257
+ rawFilePrefix : `https://${ repoInfo . team } .coding.net/p/${ repoInfo . project } /d/${ repoInfo . repo } /git/raw` ,
258
+ } ;
250
259
}
251
260
252
261
public async getMRList ( repo ?: string , status ?: string ) : Promise < CodingResponse > {
253
262
try {
254
- const url = await this . getApiPrefix ( ) ;
263
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
255
264
const result : CodingResponse = await got
256
- . get ( `${ url } /git /merges/query` , {
265
+ . get ( `${ repoApiPrefix } /merges/query` , {
257
266
searchParams : {
258
267
status,
259
268
sort : `action_at` ,
@@ -272,20 +281,13 @@ export class CodingServer {
272
281
273
282
public async getRepoList ( ) {
274
283
try {
275
- const repoInfo = this . _context . workspaceState . get ( `repoInfo` ) as IRepoInfo ;
276
- if ( ! repoInfo ?. team ) {
277
- throw new Error ( `team not exist` ) ;
278
- }
279
-
284
+ const { userApiPrefix } = await this . getApiPrefix ( ) ;
280
285
const { code, data, msg } : IRepoListResponse = await got
281
- . get (
282
- `https://${ repoInfo . team } .coding.net/api/user/${ this . _session ?. user ?. global_key } /depots` ,
283
- {
284
- searchParams : {
285
- access_token : this . _session ?. accessToken ,
286
- } ,
286
+ . get ( `${ userApiPrefix } /depots` , {
287
+ searchParams : {
288
+ access_token : this . _session ?. accessToken ,
287
289
} ,
288
- )
290
+ } )
289
291
. json ( ) ;
290
292
if ( code ) {
291
293
return Promise . reject ( { code, msg } ) ;
@@ -302,9 +304,9 @@ export class CodingServer {
302
304
303
305
public async getMRDiff ( iid : number ) {
304
306
try {
305
- const url = await this . getApiPrefix ( ) ;
307
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
306
308
const diff : IMRDiffResponse = await got
307
- . get ( `${ url } /git /merge/${ iid } /diff` , {
309
+ . get ( `${ repoApiPrefix } /merge/${ iid } /diff` , {
308
310
searchParams : {
309
311
access_token : this . _session ?. accessToken ,
310
312
} ,
@@ -321,9 +323,9 @@ export class CodingServer {
321
323
322
324
public async getMRDetail ( iid : string ) {
323
325
try {
324
- const url = await this . getApiPrefix ( ) ;
326
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
325
327
const resp : IMRDetailResponse = await got
326
- . get ( `${ url } /git /merge/${ iid } /detail` , {
328
+ . get ( `${ repoApiPrefix } /merge/${ iid } /detail` , {
327
329
searchParams : {
328
330
access_token : this . _session ?. accessToken ,
329
331
} ,
@@ -342,9 +344,9 @@ export class CodingServer {
342
344
343
345
public async getMRActivities ( iid : string ) {
344
346
try {
345
- const url = await this . getApiPrefix ( ) ;
347
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
346
348
const result : IMRActivitiesResponse = await got
347
- . get ( `${ url } /git /merge/${ iid } /activities` , {
349
+ . get ( `${ repoApiPrefix } /merge/${ iid } /activities` , {
348
350
searchParams : {
349
351
access_token : this . _session ?. accessToken ,
350
352
} ,
@@ -362,9 +364,9 @@ export class CodingServer {
362
364
363
365
public async getMRReviewers ( iid : string ) {
364
366
try {
365
- const url = await this . getApiPrefix ( ) ;
367
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
366
368
const result : IMRReviewersResponse = await got
367
- . get ( `${ url } /git /merge/${ iid } /reviewers` , {
369
+ . get ( `${ repoApiPrefix } /merge/${ iid } /reviewers` , {
368
370
searchParams : {
369
371
access_token : this . _session ?. accessToken ,
370
372
} ,
@@ -382,9 +384,9 @@ export class CodingServer {
382
384
383
385
public async getMRComments ( iid : string ) {
384
386
try {
385
- const url = await this . getApiPrefix ( ) ;
387
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
386
388
const result : CodingResponse = await got
387
- . get ( `${ url } /git /merge/${ iid } /comments` , {
389
+ . get ( `${ repoApiPrefix } /merge/${ iid } /comments` , {
388
390
searchParams : {
389
391
access_token : this . _session ?. accessToken ,
390
392
} ,
@@ -402,9 +404,9 @@ export class CodingServer {
402
404
403
405
public async closeMR ( iid : string ) {
404
406
try {
405
- const url = await this . getApiPrefix ( ) ;
407
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
406
408
const result : CodingResponse = await got
407
- . post ( `${ url } /git /merge/${ iid } /refuse` , {
409
+ . post ( `${ repoApiPrefix } /merge/${ iid } /refuse` , {
408
410
searchParams : {
409
411
access_token : this . _session ?. accessToken ,
410
412
} ,
@@ -422,9 +424,9 @@ export class CodingServer {
422
424
423
425
public async approveMR ( iid : string ) {
424
426
try {
425
- const url = await this . getApiPrefix ( ) ;
427
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
426
428
const result : CodingResponse = await got
427
- . post ( `${ url } /git /merge/${ iid } /good` , {
429
+ . post ( `${ repoApiPrefix } /merge/${ iid } /good` , {
428
430
searchParams : {
429
431
access_token : this . _session ?. accessToken ,
430
432
} ,
@@ -442,9 +444,9 @@ export class CodingServer {
442
444
443
445
public async disapproveMR ( iid : string ) {
444
446
try {
445
- const url = await this . getApiPrefix ( ) ;
447
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
446
448
const result : CodingResponse = await got
447
- . delete ( `${ url } /git /merge/${ iid } /good` , {
449
+ . delete ( `${ repoApiPrefix } /merge/${ iid } /good` , {
448
450
searchParams : {
449
451
access_token : this . _session ?. accessToken ,
450
452
} ,
@@ -462,9 +464,9 @@ export class CodingServer {
462
464
463
465
public async mergeMR ( iid : string ) {
464
466
try {
465
- const url = await this . getApiPrefix ( ) ;
467
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
466
468
const result : CodingResponse = await got
467
- . post ( `${ url } /git /merge/${ iid } /merge` , {
469
+ . post ( `${ repoApiPrefix } /merge/${ iid } /merge` , {
468
470
searchParams : {
469
471
access_token : this . _session ?. accessToken ,
470
472
} ,
@@ -485,9 +487,9 @@ export class CodingServer {
485
487
486
488
public async updateMRTitle ( iid : string , title : string ) {
487
489
try {
488
- const url = await this . getApiPrefix ( ) ;
490
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
489
491
const result : CodingResponse = await got
490
- . put ( `${ url } /git /merge/${ iid } /update-title` , {
492
+ . put ( `${ repoApiPrefix } /merge/${ iid } /update-title` , {
491
493
searchParams : {
492
494
access_token : this . _session ?. accessToken ,
493
495
title,
@@ -509,9 +511,9 @@ export class CodingServer {
509
511
510
512
public async commentMR ( mrId : number , comment : string ) {
511
513
try {
512
- const url = await this . getApiPrefix ( ) ;
513
- const result : CodingResponse = await got
514
- . post ( `${ url } /git /line_notes` , {
514
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
515
+ const result : ICreateCommentResp = await got
516
+ . post ( `${ repoApiPrefix } /line_notes` , {
515
517
searchParams : {
516
518
access_token : this . _session ?. accessToken ,
517
519
line : 0 ,
@@ -539,12 +541,8 @@ export class CodingServer {
539
541
540
542
public async getRemoteFileContent ( path : string ) {
541
543
try {
542
- const repoInfo = this . _context . workspaceState . get ( `repoInfo` ) as IRepoInfo ;
543
- if ( ! repoInfo ?. team ) {
544
- throw new Error ( `team not exist` ) ;
545
- }
546
-
547
- const url = `https://${ repoInfo . team } .coding.net/p/${ repoInfo . project } /d/${ repoInfo . repo } /git/raw/${ path } ` ;
544
+ const { rawFilePrefix } = await this . getApiPrefix ( ) ;
545
+ const url = `${ rawFilePrefix } /${ path } ` ;
548
546
const { body } = await got . get ( url , {
549
547
searchParams : {
550
548
access_token : this . _session ?. accessToken ,
@@ -559,8 +557,8 @@ export class CodingServer {
559
557
560
558
public async createMR ( data : ICreateMRBody ) {
561
559
try {
562
- const url = await this . getApiPrefix ( ) ;
563
- const resp : ICreateMRResp = await got . post ( `${ url } /git /merge` , {
560
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
561
+ const resp : ICreateMRResp = await got . post ( `${ repoApiPrefix } /merge` , {
564
562
resolveBodyOnly : true ,
565
563
responseType : `json` ,
566
564
searchParams : {
@@ -579,9 +577,9 @@ export class CodingServer {
579
577
580
578
public async getBranchList ( ) {
581
579
try {
582
- const url = await this . getApiPrefix ( ) ;
580
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
583
581
const resp : IBranchListResp = await got
584
- . get ( `${ url } /git /list_branches` , {
582
+ . get ( `${ repoApiPrefix } /list_branches` , {
585
583
searchParams : {
586
584
access_token : this . _session ?. accessToken ,
587
585
} ,
@@ -596,6 +594,98 @@ export class CodingServer {
596
594
}
597
595
}
598
596
597
+ public async getProjectMembers ( ) {
598
+ try {
599
+ const { projectApiPrefix } = await this . getApiPrefix ( ) ;
600
+ const resp : IMemberListResp = await got
601
+ . get ( `${ projectApiPrefix } /members` , {
602
+ searchParams : {
603
+ pageSize : 9999 ,
604
+ access_token : this . _session ?. accessToken ,
605
+ } ,
606
+ } )
607
+ . json ( ) ;
608
+
609
+ if ( resp . code ) {
610
+ return Promise . reject ( resp ) ;
611
+ }
612
+
613
+ return resp ;
614
+ } catch ( err ) {
615
+ return Promise . reject ( err ) ;
616
+ }
617
+ }
618
+
619
+ public async addMRReviewers ( iid : string , ids : number [ ] ) : Promise < number [ ] > {
620
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
621
+ const tasks : Promise < CodingResponse > [ ] = ids . map ( ( id ) => {
622
+ return got
623
+ . post ( `${ repoApiPrefix } /merge/${ iid } /reviewers` , {
624
+ searchParams : {
625
+ user_id : id ,
626
+ access_token : this . _session ?. accessToken ,
627
+ } ,
628
+ } )
629
+ . json ( ) ;
630
+ } ) ;
631
+ const result : PromiseSettledResult < CodingResponse > [ ] = await Promise . allSettled ( tasks ) ;
632
+ const fulfilled = ids . reduce ( ( res , cur , idx ) => {
633
+ if ( result [ idx ] . status === `fulfilled` ) {
634
+ res = res . concat ( cur ) ;
635
+ }
636
+
637
+ return res ;
638
+ } , [ ] as number [ ] ) ;
639
+ return fulfilled ;
640
+ }
641
+
642
+ public async removeMRReviewers ( iid : string , ids : number [ ] ) : Promise < number [ ] > {
643
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
644
+ const tasks : Promise < CodingResponse > [ ] = ids . map ( ( id ) => {
645
+ return got
646
+ . delete ( `${ repoApiPrefix } /merge/${ iid } /reviewers` , {
647
+ searchParams : {
648
+ user_id : id ,
649
+ access_token : this . _session ?. accessToken ,
650
+ } ,
651
+ } )
652
+ . json ( ) ;
653
+ } ) ;
654
+ const result : PromiseSettledResult < CodingResponse > [ ] = await Promise . allSettled ( tasks ) ;
655
+ const fulfilled = ids . reduce ( ( res , cur , idx ) => {
656
+ if ( result [ idx ] . status === `fulfilled` ) {
657
+ res = res . concat ( cur ) ;
658
+ }
659
+
660
+ return res ;
661
+ } , [ ] as number [ ] ) ;
662
+ return fulfilled ;
663
+ }
664
+
665
+ public async updateMRDesc ( iid : string , content : string ) {
666
+ try {
667
+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
668
+ const resp : IMRContentResp = await got
669
+ . put ( `${ repoApiPrefix } /merge/${ iid } /update-content` , {
670
+ form : {
671
+ content,
672
+ } ,
673
+ searchParams : {
674
+ access_token : this . _session ?. accessToken ,
675
+ } ,
676
+ } )
677
+ . json ( ) ;
678
+
679
+ if ( resp . code ) {
680
+ return Promise . reject ( resp ) ;
681
+ }
682
+
683
+ return resp ;
684
+ } catch ( e ) {
685
+ return Promise . reject ( e ) ;
686
+ }
687
+ }
688
+
599
689
get loggedIn ( ) {
600
690
return this . _loggedIn ;
601
691
}
0 commit comments