@@ -14,6 +14,9 @@ import {
1414 ICreateMRBody ,
1515 ICreateMRResp ,
1616 IBranchListResp ,
17+ IMemberListResp ,
18+ IMRContentResp ,
19+ ICreateCommentResp ,
1720} from 'src/typings/respResult' ;
1821import { PromiseAdapter , promiseFromEvent , parseQuery , parseCloneUrl } from 'src/common/utils' ;
1922import { GitService } from 'src/common/gitService' ;
@@ -25,7 +28,7 @@ const AUTH_SERVER = `https://x5p7m.csb.app`;
2528const ClientId = `ff768664c96d04235b1cc4af1e3b37a8` ;
2629const ClientSecret = `d29ebb32cab8b5f0a643b5da7dcad8d1469312c7` ;
2730
28- export const ScopeList = [ `user` , `user:email` , `project` , `project:depot` ] ;
31+ export const ScopeList = [ `user` , `user:email` , `project` , `project:depot` , `project:members` ] ;
2932const SCOPES = ScopeList . join ( `,` ) ;
3033const NETWORK_ERROR = 'network error' ;
3134
@@ -246,14 +249,20 @@ export class CodingServer {
246249 throw new Error ( `team not exist` ) ;
247250 }
248251
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+ } ;
250259 }
251260
252261 public async getMRList ( repo ?: string , status ?: string ) : Promise < CodingResponse > {
253262 try {
254- const url = await this . getApiPrefix ( ) ;
263+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
255264 const result : CodingResponse = await got
256- . get ( `${ url } /git /merges/query` , {
265+ . get ( `${ repoApiPrefix } /merges/query` , {
257266 searchParams : {
258267 status,
259268 sort : `action_at` ,
@@ -272,20 +281,13 @@ export class CodingServer {
272281
273282 public async getRepoList ( ) {
274283 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 ( ) ;
280285 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 ,
287289 } ,
288- )
290+ } )
289291 . json ( ) ;
290292 if ( code ) {
291293 return Promise . reject ( { code, msg } ) ;
@@ -302,9 +304,9 @@ export class CodingServer {
302304
303305 public async getMRDiff ( iid : number ) {
304306 try {
305- const url = await this . getApiPrefix ( ) ;
307+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
306308 const diff : IMRDiffResponse = await got
307- . get ( `${ url } /git /merge/${ iid } /diff` , {
309+ . get ( `${ repoApiPrefix } /merge/${ iid } /diff` , {
308310 searchParams : {
309311 access_token : this . _session ?. accessToken ,
310312 } ,
@@ -321,9 +323,9 @@ export class CodingServer {
321323
322324 public async getMRDetail ( iid : string ) {
323325 try {
324- const url = await this . getApiPrefix ( ) ;
326+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
325327 const resp : IMRDetailResponse = await got
326- . get ( `${ url } /git /merge/${ iid } /detail` , {
328+ . get ( `${ repoApiPrefix } /merge/${ iid } /detail` , {
327329 searchParams : {
328330 access_token : this . _session ?. accessToken ,
329331 } ,
@@ -342,9 +344,9 @@ export class CodingServer {
342344
343345 public async getMRActivities ( iid : string ) {
344346 try {
345- const url = await this . getApiPrefix ( ) ;
347+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
346348 const result : IMRActivitiesResponse = await got
347- . get ( `${ url } /git /merge/${ iid } /activities` , {
349+ . get ( `${ repoApiPrefix } /merge/${ iid } /activities` , {
348350 searchParams : {
349351 access_token : this . _session ?. accessToken ,
350352 } ,
@@ -362,9 +364,9 @@ export class CodingServer {
362364
363365 public async getMRReviewers ( iid : string ) {
364366 try {
365- const url = await this . getApiPrefix ( ) ;
367+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
366368 const result : IMRReviewersResponse = await got
367- . get ( `${ url } /git /merge/${ iid } /reviewers` , {
369+ . get ( `${ repoApiPrefix } /merge/${ iid } /reviewers` , {
368370 searchParams : {
369371 access_token : this . _session ?. accessToken ,
370372 } ,
@@ -382,9 +384,9 @@ export class CodingServer {
382384
383385 public async getMRComments ( iid : string ) {
384386 try {
385- const url = await this . getApiPrefix ( ) ;
387+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
386388 const result : CodingResponse = await got
387- . get ( `${ url } /git /merge/${ iid } /comments` , {
389+ . get ( `${ repoApiPrefix } /merge/${ iid } /comments` , {
388390 searchParams : {
389391 access_token : this . _session ?. accessToken ,
390392 } ,
@@ -402,9 +404,9 @@ export class CodingServer {
402404
403405 public async closeMR ( iid : string ) {
404406 try {
405- const url = await this . getApiPrefix ( ) ;
407+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
406408 const result : CodingResponse = await got
407- . post ( `${ url } /git /merge/${ iid } /refuse` , {
409+ . post ( `${ repoApiPrefix } /merge/${ iid } /refuse` , {
408410 searchParams : {
409411 access_token : this . _session ?. accessToken ,
410412 } ,
@@ -422,9 +424,9 @@ export class CodingServer {
422424
423425 public async approveMR ( iid : string ) {
424426 try {
425- const url = await this . getApiPrefix ( ) ;
427+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
426428 const result : CodingResponse = await got
427- . post ( `${ url } /git /merge/${ iid } /good` , {
429+ . post ( `${ repoApiPrefix } /merge/${ iid } /good` , {
428430 searchParams : {
429431 access_token : this . _session ?. accessToken ,
430432 } ,
@@ -442,9 +444,9 @@ export class CodingServer {
442444
443445 public async disapproveMR ( iid : string ) {
444446 try {
445- const url = await this . getApiPrefix ( ) ;
447+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
446448 const result : CodingResponse = await got
447- . delete ( `${ url } /git /merge/${ iid } /good` , {
449+ . delete ( `${ repoApiPrefix } /merge/${ iid } /good` , {
448450 searchParams : {
449451 access_token : this . _session ?. accessToken ,
450452 } ,
@@ -462,9 +464,9 @@ export class CodingServer {
462464
463465 public async mergeMR ( iid : string ) {
464466 try {
465- const url = await this . getApiPrefix ( ) ;
467+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
466468 const result : CodingResponse = await got
467- . post ( `${ url } /git /merge/${ iid } /merge` , {
469+ . post ( `${ repoApiPrefix } /merge/${ iid } /merge` , {
468470 searchParams : {
469471 access_token : this . _session ?. accessToken ,
470472 } ,
@@ -485,9 +487,9 @@ export class CodingServer {
485487
486488 public async updateMRTitle ( iid : string , title : string ) {
487489 try {
488- const url = await this . getApiPrefix ( ) ;
490+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
489491 const result : CodingResponse = await got
490- . put ( `${ url } /git /merge/${ iid } /update-title` , {
492+ . put ( `${ repoApiPrefix } /merge/${ iid } /update-title` , {
491493 searchParams : {
492494 access_token : this . _session ?. accessToken ,
493495 title,
@@ -509,9 +511,9 @@ export class CodingServer {
509511
510512 public async commentMR ( mrId : number , comment : string ) {
511513 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` , {
515517 searchParams : {
516518 access_token : this . _session ?. accessToken ,
517519 line : 0 ,
@@ -539,12 +541,8 @@ export class CodingServer {
539541
540542 public async getRemoteFileContent ( path : string ) {
541543 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 } ` ;
548546 const { body } = await got . get ( url , {
549547 searchParams : {
550548 access_token : this . _session ?. accessToken ,
@@ -559,8 +557,8 @@ export class CodingServer {
559557
560558 public async createMR ( data : ICreateMRBody ) {
561559 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` , {
564562 resolveBodyOnly : true ,
565563 responseType : `json` ,
566564 searchParams : {
@@ -579,9 +577,9 @@ export class CodingServer {
579577
580578 public async getBranchList ( ) {
581579 try {
582- const url = await this . getApiPrefix ( ) ;
580+ const { repoApiPrefix } = await this . getApiPrefix ( ) ;
583581 const resp : IBranchListResp = await got
584- . get ( `${ url } /git /list_branches` , {
582+ . get ( `${ repoApiPrefix } /list_branches` , {
585583 searchParams : {
586584 access_token : this . _session ?. accessToken ,
587585 } ,
@@ -596,6 +594,98 @@ export class CodingServer {
596594 }
597595 }
598596
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+
599689 get loggedIn ( ) {
600690 return this . _loggedIn ;
601691 }
0 commit comments