@@ -10,6 +10,7 @@ import * as FileSystem from "effect/FileSystem";
1010import * as Layer from "effect/Layer" ;
1111import * as Option from "effect/Option" ;
1212import * as Queue from "effect/Queue" ;
13+ import * as Result from "effect/Result" ;
1314import * as Schema from "effect/Schema" ;
1415import * as TestClock from "effect/testing/TestClock" ;
1516import { HttpClient , HttpClientError , HttpClientRequest , HttpClientResponse } from "effect/http" ;
@@ -72,6 +73,7 @@ function makeLayer(input: {
7273 readonly stall ?: boolean ;
7374 readonly git ?: Partial < HostGit > ;
7475 readonly env ?: Record < string , string > ;
76+ readonly remoteUrl ?: string ;
7577} ) {
7678 const execute = vi . fn ( ( request : HttpClientRequest . HttpClientRequest ) =>
7779 input . stall
@@ -108,7 +110,7 @@ function makeLayer(input: {
108110 remotes : [
109111 {
110112 name : "origin" ,
111- url :
"[email protected] :pingdotgg/t3code.git" , 113+ url :
input . remoteUrl ?? "[email protected] :pingdotgg/t3code.git" , 112114 pushUrl : Option . none ( ) ,
113115 isPrimary : true ,
114116 } ,
@@ -337,6 +339,78 @@ it.effect("prefers an explicit repository and uses context when the repository i
337339 } ) . pipe ( Effect . provide ( layer ) ) ;
338340} ) ;
339341
342+ it . effect ( "sends a self-hosted Bitbucket remote nowhere unless an API base url is configured" , ( ) =>
343+ Effect . gen ( function * ( ) {
344+ const remoteUrl = "https://bitbucket.example.com/scm/proj/repo.git" ;
345+ const context = {
346+ provider : {
347+ kind : SourceControlProviderKind . make ( "bitbucket" ) ,
348+ name : "Bitbucket Self-Hosted" ,
349+ baseUrl : "https://bitbucket.example.com" ,
350+ } ,
351+ remoteName : "origin" ,
352+ remoteUrl,
353+ } ;
354+ const cloudEnv = {
355+ T3CODE_BITBUCKET_EMAIL :
"[email protected] " , 356+ T3CODE_BITBUCKET_API_TOKEN : "t" ,
357+ } ;
358+ const response = ( request : HttpClientRequest . HttpClientRequest ) =>
359+ Response . json ( request . url . endsWith ( "/branching-model" ) ? { } : repositoryJson ) ;
360+ const defaultBranch = ( setup : ReturnType < typeof makeLayer > , withContext : boolean ) =>
361+ BitbucketApi . BitbucketApi . pipe (
362+ Effect . flatMap ( ( bitbucket ) =>
363+ bitbucket . getDefaultBranch ( { cwd : "/repo" , ...( withContext ? { context } : { } ) } ) ,
364+ ) ,
365+ Effect . provide ( setup . layer ) ,
366+ ) ;
367+ const requestedUrls = ( setup : ReturnType < typeof makeLayer > ) =>
368+ setup . execute . mock . calls . map ( ( [ request ] ) => request . url ) . toSorted ( ) ;
369+
370+ // A self-hosted remote in the checkout, then a self-hosted context beside a Cloud remote.
371+ for ( const [ setup , withContext ] of [
372+ [ makeLayer ( { response, env : cloudEnv , remoteUrl } ) , false ] ,
373+ [ makeLayer ( { response, env : cloudEnv } ) , true ] ,
374+ ] as const ) {
375+ const result = yield * defaultBranch ( setup , withContext ) . pipe ( Effect . result ) ;
376+ assert . deepStrictEqual ( requestedUrls ( setup ) , [ ] ) ;
377+ assert . strictEqual (
378+ Result . isFailure ( result ) && result . failure . _tag ,
379+ "BitbucketRepositoryRemoteNotFoundError" ,
380+ ) ;
381+ }
382+
383+ // An explicit repository does not let a self-hosted context reach the Cloud API.
384+ const explicit = makeLayer ( { response, env : cloudEnv } ) ;
385+ const cloneUrls = yield * BitbucketApi . BitbucketApi . pipe (
386+ Effect . flatMap ( ( bitbucket ) =>
387+ bitbucket . getRepositoryCloneUrls ( { cwd : "/repo" , context, repository : "proj/repo" } ) ,
388+ ) ,
389+ Effect . provide ( explicit . layer ) ,
390+ Effect . result ,
391+ ) ;
392+ assert . deepStrictEqual ( requestedUrls ( explicit ) , [ ] ) ;
393+ assert . strictEqual (
394+ Result . isFailure ( cloneUrls ) && cloneUrls . failure . _tag ,
395+ "BitbucketRepositoryRemoteNotFoundError" ,
396+ ) ;
397+
398+ const cloud = makeLayer ( { response, env : cloudEnv } ) ;
399+ yield * defaultBranch ( cloud , false ) ;
400+ assert . deepStrictEqual ( requestedUrls ( cloud ) , [
401+ "https://api.bitbucket.org/2.0/repositories/pingdotgg/t3code" ,
402+ "https://api.bitbucket.org/2.0/repositories/pingdotgg/t3code/branching-model" ,
403+ ] ) ;
404+
405+ const configured = makeLayer ( { response, remoteUrl } ) ;
406+ yield * defaultBranch ( configured , true ) ;
407+ assert . deepStrictEqual ( requestedUrls ( configured ) , [
408+ "https://api.test.local/2.0/repositories/proj/repo" ,
409+ "https://api.test.local/2.0/repositories/proj/repo/branching-model" ,
410+ ] ) ;
411+ } ) ,
412+ ) ;
413+
340414it . effect (
341415 "prefers the Bitbucket branching model development branch as the default PR target" ,
342416 ( ) => {
0 commit comments