@@ -5,6 +5,7 @@ import * as Effect from "effect/Effect";
55import * as Deferred from "effect/Deferred" ;
66import * as Fiber from "effect/Fiber" ;
77import * as Layer from "effect/Layer" ;
8+ import * as Option from "effect/Option" ;
89import * as Result from "effect/Result" ;
910import * as Schema from "effect/Schema" ;
1011import * as TestClock from "effect/testing/TestClock" ;
@@ -29,6 +30,9 @@ const appSignedOut = Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({
2930 token : ( ) => Effect . succeedNone ,
3031 invalidate : ( ) => Effect . void ,
3132} ) ;
33+ const mockedAppToken = vi . fn < ( host : string ) => Effect . Effect < Option . Option < string > > > (
34+ ( ) => Effect . succeedNone ,
35+ ) ;
3236
3337const encodeJson = Schema . encodeSync ( Schema . fromJsonString ( Schema . Unknown ) ) ;
3438
@@ -111,7 +115,12 @@ type ApiAnswer = Effect.Effect<GitHubRest.GitHubRestResponse, GitHubRequestPolic
111115const mockedExecute = vi . fn < ( call : ApiCall ) => ApiAnswer > ( ) ;
112116const defaultCredential = ( host : string ) =>
113117 Effect . succeed ( { token : Redacted . make ( "token" ) , fingerprint : `${ host } :token` } ) ;
114- const mockedCredential = vi . fn ( defaultCredential ) ;
118+ const mockedCredential =
119+ vi . fn <
120+ (
121+ host : string ,
122+ ) => Effect . Effect < GitHubRequestPolicy . GitHubHeldCredential , GitHubRequestPolicy . GitHubApiError >
123+ > ( defaultCredential ) ;
115124const mockedStackMemberships = vi . fn < ( call : ApiCall ) => ApiAnswer > ( ( ) =>
116125 Effect . succeed ( output ( '{"data":{}}' ) ) ,
117126) ;
@@ -180,14 +189,23 @@ const layer = it.layer(
180189 Layer . provideMerge ( GitHubAppAccess . layer ) ,
181190 Layer . provideMerge ( mockApi ) ,
182191 Layer . provideMerge ( GitHubRequestPolicy . layer ) ,
183- Layer . provideMerge ( appSignedOut ) ,
192+ Layer . provideMerge (
193+ Layer . mock ( GitHubAppSignIn . GitHubAppSignIn ) ( {
194+ enabled : false ,
195+ token : ( host ) => mockedAppToken ( host ) ,
196+ invalidate : ( ) => Effect . void ,
197+ } ) ,
198+ ) ,
184199 Layer . provideMerge ( GitHubQuota . layer ) ,
185200 Layer . provideMerge ( SourceControlRateLimit . layer ) ,
186201 Layer . provideMerge ( mockHttp ) ,
187202 Layer . provideMerge (
188203 Layer . succeed ( GitHubCredentials . GitHubCredentials , {
189204 get : ( host ) =>
190- Effect . map ( mockedCredential ( host ) , ( held ) => ( { ...held , host, source : "gh" as const } ) ) ,
205+ mockedCredential ( host ) . pipe (
206+ Effect . map ( ( held ) => ( { source : "gh" as const , ...held , host } ) ) ,
207+ Effect . mapError ( ( ) => new GitHubCredentials . GitHubCliMissingError ( { host } ) ) ,
208+ ) ,
191209 saved : ( ) => Effect . succeedNone ,
192210 fingerprint : ( host , token ) => Effect . succeed ( `${ host } :${ token } ` ) ,
193211 invalidate : ( ) => Effect . void ,
@@ -617,6 +635,7 @@ afterEach(() => {
617635 mockedExecute . mockReset ( ) ;
618636 mockedStackMemberships . mockReset ( ) ;
619637 mockedCredential . mockReset ( ) ;
638+ mockedAppToken . mockReset ( ) ;
620639 mockedCredential . mockImplementation ( defaultCredential ) ;
621640} ) ;
622641
@@ -1276,6 +1295,101 @@ layer("GitHubPullRequestApi.layer", (it) => {
12761295 } ) ,
12771296 ) ;
12781297
1298+ /**
1299+ * Lists a repository GitHub will not return, answering credential lookups in turn (the last
1300+ * answer repeats; `undefined` is no credential), and returns the error with the tokens sent.
1301+ */
1302+ const listUnreadableRepository = (
1303+ ...lookups : ReadonlyArray <
1304+ | { readonly token : string ; readonly source : GitHubCredentials . GitHubCredentialSource }
1305+ | undefined
1306+ >
1307+ ) => {
1308+ mockedExecute . mockClear ( ) ;
1309+ let lookup = 0 ;
1310+ mockedCredential . mockImplementation ( ( host ) =>
1311+ Effect . suspend ( ( ) => {
1312+ const held = lookups [ Math . min ( lookup ++ , lookups . length - 1 ) ] ;
1313+ return held === undefined
1314+ ? Effect . fail (
1315+ new GitHubRequestPolicy . GitHubApiAuthenticationError ( {
1316+ host,
1317+ operation : "credential" ,
1318+ } ) ,
1319+ )
1320+ : Effect . succeed ( {
1321+ token : Redacted . make ( held . token ) ,
1322+ fingerprint : `${ host } :${ held . token } ` ,
1323+ source : held . source ,
1324+ } ) ;
1325+ } ) ,
1326+ ) ;
1327+ route (
1328+ [ "PullRequestSearch" , { data : { search : { pageInfo : lastPage , nodes : [ ] } } } ] ,
1329+ [
1330+ "PullRequestList" ,
1331+ {
1332+ data : { repository : null } ,
1333+ errors : [
1334+ {
1335+ type : "NOT_FOUND" ,
1336+ path : [ "repository" ] ,
1337+ message : "Could not resolve to a Repository with the name 'acme/private'." ,
1338+ } ,
1339+ ] ,
1340+ } ,
1341+ ] ,
1342+ ) ;
1343+ return GitHubPullRequestProvider . make . pipe (
1344+ Effect . flatMap ( ( provider ) =>
1345+ Effect . flip (
1346+ provider . listChangeRequests ( {
1347+ cwd : "/w" ,
1348+ repository : "acme/private" ,
1349+ host : "github.com" ,
1350+ state : "open" ,
1351+ involvement : "all" ,
1352+ viewer : "bilal" ,
1353+ limit : 10 ,
1354+ } ) ,
1355+ ) ,
1356+ ) ,
1357+ Effect . map ( ( error ) => ( {
1358+ error,
1359+ sentWith : mockedExecute . mock . calls . map ( ( [ call ] ) =>
1360+ call . kind === "graphql" ? call . authorization : undefined ,
1361+ ) ,
1362+ } ) ) ,
1363+ ) ;
1364+ } ;
1365+
1366+ it . effect ( "names the credential GitHub would not return a repository to" , ( ) =>
1367+ Effect . gen ( function * ( ) {
1368+ // The saved token is replaced mid-read; every request still goes out with the one named.
1369+ const saved = yield * listUnreadableRepository (
1370+ { token : "first" , source : "settings" } ,
1371+ { token : "replaced" , source : "settings" } ,
1372+ ) ;
1373+ expect ( saved . error . accessHint ) . toContain ( "saved GitHub token" ) ;
1374+ assert . isAbove ( saved . sentWith . length , 1 ) ;
1375+ for ( const authorization of saved . sentWith ) expect ( authorization ) . toContain ( "first" ) ;
1376+
1377+ // The policy hands what the app cannot see to the gh login, which GitHub refuses too.
1378+ mockedAppToken . mockReturnValue ( Effect . succeedSome ( "app" ) ) ;
1379+ const app = { token : "app" , source : "app" } as const ;
1380+ const gh = { token : "personal" , source : "gh" } as const ;
1381+ const retried = yield * listUnreadableRepository ( app , gh ) ;
1382+ expect ( retried . error . accessHint ) . toContain ( "gh login" ) ;
1383+ expect ( retried . sentWith ) . toEqual ( [ "Bearer app" , "Bearer app" , "Bearer personal" ] ) ;
1384+
1385+ // With no gh login when the app is refused, the app made the last request; a login that
1386+ // turns up afterwards is not named.
1387+ const appOnly = yield * listUnreadableRepository ( app , undefined , gh ) ;
1388+ assert . isUndefined ( appOnly . error . accessHint ) ;
1389+ expect ( appOnly . sentWith ) . toEqual ( [ "Bearer app" , "Bearer app" ] ) ;
1390+ } ) ,
1391+ ) ;
1392+
12791393 it . effect ( "carries every repository and every qualifier into one search" , ( ) =>
12801394 Effect . gen ( function * ( ) {
12811395 mockedExecute . mockReturnValue ( Effect . succeed ( searchPage ( [ ] ) ) ) ;
0 commit comments