1- import { createBitbucketCloudClient } from "@coderabbitai/bitbucket/cloud" ;
2- import { createBitbucketServerClient } from "@coderabbitai/bitbucket/server" ;
1+ import { createBitbucketCloudClient as createBitbucketCloudClientBase } from "@coderabbitai/bitbucket/cloud" ;
2+ import { createBitbucketServerClient as createBitbucketServerClientBase } from "@coderabbitai/bitbucket/server" ;
33import { BitbucketConnectionConfig } from "@sourcebot/schemas/v3/bitbucket.type" ;
44import type { ClientOptions , ClientPathsWithMethod } from "openapi-fetch" ;
55import { createLogger } from "@sourcebot/shared" ;
@@ -36,10 +36,10 @@ interface BitbucketClient {
3636 shouldExcludeRepo : ( repo : BitbucketRepository , config : BitbucketConnectionConfig ) => boolean ;
3737}
3838
39- type CloudAPI = ReturnType < typeof createBitbucketCloudClient > ;
39+ type CloudAPI = ReturnType < typeof createBitbucketCloudClientBase > ;
4040type CloudGetRequestPath = ClientPathsWithMethod < CloudAPI , "get" > ;
4141
42- type ServerAPI = ReturnType < typeof createBitbucketServerClient > ;
42+ type ServerAPI = ReturnType < typeof createBitbucketServerClientBase > ;
4343type ServerGetRequestPath = ClientPathsWithMethod < ServerAPI , "get" > ;
4444
4545type CloudPaginatedResponse < T > = {
@@ -70,8 +70,8 @@ export const getBitbucketReposFromConfig = async (config: BitbucketConnectionCon
7070 }
7171
7272 const client = config . deploymentType === 'server' ?
73- serverClient ( config . url ! , config . user , token ) :
74- cloudClient ( config . user , token ) ;
73+ createBitbucketServerClient ( config . url ! , config . user , token ) :
74+ createBitbucketCloudClient ( config . user , token ) ;
7575
7676 let allRepos : BitbucketRepository [ ] = [ ] ;
7777 let allWarnings : string [ ] = [ ] ;
@@ -104,11 +104,10 @@ export const getBitbucketReposFromConfig = async (config: BitbucketConnectionCon
104104 } ;
105105}
106106
107- function cloudClient ( user : string | undefined , token : string | undefined ) : BitbucketClient {
108-
107+ export function createBitbucketCloudClient ( user : string | undefined , token : string | undefined ) : BitbucketClient {
109108 const authorizationString =
110109 token
111- ? ! user || user == "x-token-auth"
110+ ? ( ! user || user === "x-token-auth" )
112111 ? `Bearer ${ token } `
113112 : `Basic ${ Buffer . from ( `${ user } :${ token } ` ) . toString ( 'base64' ) } `
114113 : undefined ;
@@ -121,7 +120,7 @@ function cloudClient(user: string | undefined, token: string | undefined): Bitbu
121120 } ,
122121 } ;
123122
124- const apiClient = createBitbucketCloudClient ( clientOptions ) ;
123+ const apiClient = createBitbucketCloudClientBase ( clientOptions ) ;
125124 var client : BitbucketClient = {
126125 deploymentType : BITBUCKET_CLOUD ,
127126 token : token ,
@@ -380,7 +379,7 @@ export function cloudShouldExcludeRepo(repo: BitbucketRepository, config: Bitbuc
380379 return false ;
381380}
382381
383- function serverClient ( url : string , user : string | undefined , token : string | undefined ) : BitbucketClient {
382+ function createBitbucketServerClient ( url : string , user : string | undefined , token : string | undefined ) : BitbucketClient {
384383 const authorizationString = ( ( ) => {
385384 // If we're not given any credentials we return an empty auth string. This will only work if the project/repos are public
386385 if ( ! user && ! token ) {
@@ -402,7 +401,7 @@ function serverClient(url: string, user: string | undefined, token: string | und
402401 } ,
403402 } ;
404403
405- const apiClient = createBitbucketServerClient ( clientOptions ) ;
404+ const apiClient = createBitbucketServerClientBase ( clientOptions ) ;
406405 var client : BitbucketClient = {
407406 deploymentType : BITBUCKET_SERVER ,
408407 token : token ,
@@ -604,22 +603,14 @@ export function serverShouldExcludeRepo(repo: BitbucketRepository, config: Bitbu
604603 * @see https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-repositories-workspace-repo-slug-permissions-config-users-get
605604 */
606605export const getExplicitUserPermissionsForCloudRepo = async (
606+ client : BitbucketClient ,
607607 workspace : string ,
608608 repoSlug : string ,
609- token : string | undefined ,
610609) : Promise < Array < { accountId : string } > > => {
611- const apiClient = createBitbucketCloudClient ( {
612- baseUrl : BITBUCKET_CLOUD_API ,
613- headers : {
614- Accept : "application/json" ,
615- ...( token ? { Authorization : `Bearer ${ token } ` } : { } ) ,
616- } ,
617- } ) ;
618-
619610 const path = `/repositories/${ workspace } /${ repoSlug } /permissions-config/users` as CloudGetRequestPath ;
620611
621612 const users = await getPaginatedCloud < CloudRepositoryUserPermission > ( path , async ( p , query ) => {
622- const response = await apiClient . GET ( p , {
613+ const response = await client . apiClient . GET ( p , {
623614 params : {
624615 path : { workspace, repo_slug : repoSlug } ,
625616 query,
@@ -644,20 +635,12 @@ export const getExplicitUserPermissionsForCloudRepo = async (
644635 * @see https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-user-permissions-repositories-get
645636 */
646637export const getReposForAuthenticatedBitbucketCloudUser = async (
647- accessToken : string ,
638+ client : BitbucketClient ,
648639) : Promise < Array < { uuid : string } > > => {
649- const apiClient = createBitbucketCloudClient ( {
650- baseUrl : BITBUCKET_CLOUD_API ,
651- headers : {
652- Accept : "application/json" ,
653- Authorization : `Bearer ${ accessToken } ` ,
654- } ,
655- } ) ;
656-
657640 const path = `/user/permissions/repositories` as CloudGetRequestPath ;
658641
659642 const permissions = await getPaginatedCloud < CloudRepositoryPermission > ( path , async ( p , query ) => {
660- const response = await apiClient . GET ( p , {
643+ const response = await client . apiClient . GET ( p , {
661644 params : { query } ,
662645 } ) ;
663646 const { data, error } = response ;
0 commit comments