@@ -110,74 +110,83 @@ describe("GitHubGraphQlAuth", () => {
110110 } ,
111111 ) ;
112112
113- it . effect ( "batches a repository's lookups only with lookups read as the same account" , ( ) => {
114- const requests : Array < HttpClientRequest . HttpClientRequest > = [ ] ;
115- const layer = GitHubRest . layer . pipe (
116- Layer . provideMerge (
117- Layer . mergeAll (
118- Layer . succeed ( GitHubCredentials . GitHubCredentials , {
119- get : ( host , repository ) =>
120- Effect . succeed ( {
121- host,
122- token : Redacted . make ( repository === "acme/web" ? "work-token" : "personal-token" ) ,
123- source : "gh" as const ,
124- fingerprint : `${ host } :${ repository === "acme/web" ? "work" : "personal" } ` ,
125- } ) ,
126- accountFor : ( _host , repository ) =>
127- Effect . succeed ( repository === "acme/web" ? "work" : undefined ) ,
128- invalidate : ( ) => Effect . void ,
129- } ) ,
130- Layer . succeed (
131- HttpClient . HttpClient ,
132- HttpClient . make ( ( request ) => {
133- requests . push ( request ) ;
134- return Effect . succeed (
135- HttpClientResponse . fromWeb (
136- request ,
137- json ( {
138- data : {
139- _0_repository : { pullRequest : stats } ,
140- _1_repository : { pullRequest : stats } ,
141- } ,
142- } ) ,
143- ) ,
144- ) ;
113+ it . effect (
114+ "batches a repository's lookups only with lookups read with the same credential" ,
115+ ( ) => {
116+ const requests : Array < HttpClientRequest . HttpClientRequest > = [ ] ;
117+ const layer = GitHubRest . layer . pipe (
118+ Layer . provideMerge (
119+ Layer . mergeAll (
120+ Layer . succeed ( GitHubCredentials . GitHubCredentials , {
121+ get : ( host , repository ) =>
122+ Effect . succeed ( {
123+ host,
124+ token : Redacted . make ( repository === "acme/web" ? "work-token" : "personal-token" ) ,
125+ source : "gh" as const ,
126+ fingerprint : `${ host } :${ repository === "acme/web" ? "work" : "personal" } ` ,
127+ } ) ,
128+ // Every repository names a login gh no longer holds, and each falls back to the
129+ // credential above: batches follow that credential, not the name.
130+ accountFor : ( ) => Effect . succeed ( "gone" ) ,
131+ invalidate : ( ) => Effect . void ,
145132 } ) ,
133+ Layer . succeed (
134+ HttpClient . HttpClient ,
135+ HttpClient . make ( ( request ) => {
136+ requests . push ( request ) ;
137+ return Effect . succeed (
138+ HttpClientResponse . fromWeb (
139+ request ,
140+ json ( {
141+ data : {
142+ _0_repository : { pullRequest : stats } ,
143+ _1_repository : { pullRequest : stats } ,
144+ } ,
145+ } ) ,
146+ ) ,
147+ ) ;
148+ } ) ,
149+ ) ,
150+ GitHubQuota . layer ,
151+ SourceControlRateLimit . layer ,
146152 ) ,
147- GitHubQuota . layer ,
148- SourceControlRateLimit . layer ,
149153 ) ,
150- ) ,
151- ) ;
152- return Effect . gen ( function * ( ) {
153- const policy = yield * GitHubRequestPolicy . GitHubRequestPolicy ;
154- const client = yield * GraphQLClient . make (
155- PullRequestBatchesGraphQLGroup . middleware ( GitHubGraphQlClient . GitHubGraphQlAuth ) ,
156- ) . pipe ( Effect . provide ( GitHubGraphQlClient . layer ) ) ;
157- const resolver = GraphQLResolver . aliased ( client . PullRequestStats , {
158- batchKey : GitHubGraphQlClient . batchKey ,
159- } ) ;
160- const lookup = ( owner : string , number : number ) =>
161- policy . withRepositoryAccount (
162- { host : "github.com" , repository : `${ owner } /web` } ,
163- GraphQLResolver . request ( { owner, name : "web" , number } , resolver ) . pipe (
164- Effect . provideService ( GitHubGraphQlClient . GitHubGraphQlTarget , {
165- host : "github.com" ,
166- operation : "stats" ,
167- } ) ,
168- ) ,
154+ ) ;
155+ return Effect . gen ( function * ( ) {
156+ const policy = yield * GitHubRequestPolicy . GitHubRequestPolicy ;
157+ const client = yield * GraphQLClient . make (
158+ PullRequestBatchesGraphQLGroup . middleware ( GitHubGraphQlClient . GitHubGraphQlAuth ) ,
159+ ) . pipe ( Effect . provide ( GitHubGraphQlClient . layer ) ) ;
160+ const resolver = GraphQLResolver . aliased ( client . PullRequestStats , {
161+ batchKey : GitHubGraphQlClient . batchKey ,
162+ } ) ;
163+ const lookup = ( owner : string , number : number ) =>
164+ policy . withRepositoryAccount (
165+ { host : "github.com" , repository : `${ owner } /web` } ,
166+ GraphQLResolver . request ( { owner, name : "web" , number } , resolver ) . pipe (
167+ Effect . provideService ( GitHubGraphQlClient . GitHubGraphQlTarget , {
168+ host : "github.com" ,
169+ operation : "stats" ,
170+ } ) ,
171+ ) ,
172+ ) ;
173+ yield * Effect . all ( [ lookup ( "acme" , 1 ) , lookup ( "someone" , 2 ) , lookup ( "other" , 3 ) ] , {
174+ concurrency : "unbounded" ,
175+ } ) ;
176+ // The company's lookup went alone under its credential; the two read with the personal
177+ // one still shared a document.
178+ expect ( requests . map ( ( request ) => request . headers [ "authorization" ] ) . toSorted ( ) ) . toEqual ( [
179+ "Bearer personal-token" ,
180+ "Bearer work-token" ,
181+ ] ) ;
182+ // The Pull Requests list groups repositories the same way.
183+ expect ( yield * policy . credentialScope ( "github.com" , "acme/web" ) ) . toBe ( "github.com:work" ) ;
184+ expect ( yield * policy . credentialScope ( "github.com" , "someone/web" ) ) . toBe (
185+ "github.com:personal" ,
169186 ) ;
170- yield * Effect . all ( [ lookup ( "acme" , 1 ) , lookup ( "someone" , 2 ) , lookup ( "other" , 3 ) ] , {
171- concurrency : "unbounded" ,
172- } ) ;
173- // The company's lookup went alone under its account; the two read as the default account
174- // still shared a document.
175- expect ( requests . map ( ( request ) => request . headers [ "authorization" ] ) . toSorted ( ) ) . toEqual ( [
176- "Bearer personal-token" ,
177- "Bearer work-token" ,
178- ] ) ;
179- } ) . pipe ( Effect . provide ( layer ) ) ;
180- } ) ;
187+ } ) . pipe ( Effect . provide ( layer ) ) ;
188+ } ,
189+ ) ;
181190
182191 it . effect ( "pauses the host on a RATE_LIMITED answer sent with HTTP 200" , ( ) => {
183192 const reset = Math . floor ( NOW / 1000 ) + 600 ;
0 commit comments