@@ -23,17 +23,14 @@ import (
2323)
2424
2525var defaultGQLClient * githubv4.Client = githubv4 .NewClient (newRepoAccessHTTPClient ())
26- var repoAccessCache * lockdown.RepoAccessCache = stubRepoAccessCache (defaultGQLClient , 15 * time .Minute )
2726
2827type repoAccessKey struct {
29- owner string
30- repo string
31- username string
28+ owner string
29+ repo string
3230}
3331
3432type repoAccessValue struct {
35- isPrivate bool
36- permission string
33+ isPrivate bool
3734}
3835
3936type repoAccessMockTransport struct {
@@ -42,8 +39,8 @@ type repoAccessMockTransport struct {
4239
4340func newRepoAccessHTTPClient () * http.Client {
4441 responses := map [repoAccessKey ]repoAccessValue {
45- {owner : "owner2" , repo : "repo2" , username : "testuser2" }: {isPrivate : true },
46- {owner : "owner" , repo : "repo" , username : "testuser" }: {isPrivate : false , permission : "READ" },
42+ {owner : "owner2" , repo : "repo2" }: {isPrivate : true },
43+ {owner : "owner" , repo : "repo" }: {isPrivate : false },
4744 }
4845
4946 return & http.Client {Transport : & repoAccessMockTransport {responses : responses }}
@@ -66,30 +63,19 @@ func (rt *repoAccessMockTransport) RoundTrip(req *http.Request) (*http.Response,
6663
6764 owner := toString (payload .Variables ["owner" ])
6865 repo := toString (payload .Variables ["name" ])
69- username := toString (payload .Variables ["username" ])
7066
71- value , ok := rt .responses [repoAccessKey {owner : owner , repo : repo , username : username }]
67+ value , ok := rt .responses [repoAccessKey {owner : owner , repo : repo }]
7268 if ! ok {
73- value = repoAccessValue {isPrivate : false , permission : "WRITE" }
74- }
75-
76- edges := []any {}
77- if value .permission != "" {
78- edges = append (edges , map [string ]any {
79- "permission" : value .permission ,
80- "node" : map [string ]any {
81- "login" : username ,
82- },
83- })
69+ value = repoAccessValue {isPrivate : false }
8470 }
8571
8672 responseBody , err := json .Marshal (map [string ]any {
8773 "data" : map [string ]any {
74+ "viewer" : map [string ]any {
75+ "login" : "test-viewer" ,
76+ },
8877 "repository" : map [string ]any {
8978 "isPrivate" : value .isPrivate ,
90- "collaborators" : map [string ]any {
91- "edges" : edges ,
92- },
9379 },
9480 },
9581 })
@@ -170,13 +156,13 @@ func Test_GetIssue(t *testing.T) {
170156 tests := []struct {
171157 name string
172158 mockedClient * http.Client
173- gqlHTTPClient * http.Client
174159 requestArgs map [string ]any
175160 expectHandlerError bool
176161 expectResultError bool
177162 expectedIssue * github.Issue
178163 expectedErrMsg string
179164 lockdownEnabled bool
165+ restPermission string
180166 }{
181167 {
182168 name : "successful issue retrieval" ,
@@ -210,36 +196,6 @@ func Test_GetIssue(t *testing.T) {
210196 mockedClient : MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {
211197 GetReposIssuesByOwnerByRepoByIssueNumber : mockResponse (t , http .StatusOK , mockIssue2 ),
212198 }),
213- gqlHTTPClient : githubv4mock .NewMockedHTTPClient (
214- githubv4mock .NewQueryMatcher (
215- struct {
216- Repository struct {
217- IsPrivate githubv4.Boolean
218- Collaborators struct {
219- Edges []struct {
220- Permission githubv4.String
221- Node struct {
222- Login githubv4.String
223- }
224- }
225- } `graphql:"collaborators(query: $username, first: 1)"`
226- } `graphql:"repository(owner: $owner, name: $name)"`
227- }{},
228- map [string ]any {
229- "owner" : githubv4 .String ("owner2" ),
230- "name" : githubv4 .String ("repo2" ),
231- "username" : githubv4 .String ("testuser2" ),
232- },
233- githubv4mock .DataResponse (map [string ]any {
234- "repository" : map [string ]any {
235- "isPrivate" : true ,
236- "collaborators" : map [string ]any {
237- "edges" : []any {},
238- },
239- },
240- }),
241- ),
242- ),
243199 requestArgs : map [string ]any {
244200 "method" : "get" ,
245201 "owner" : "owner2" ,
@@ -248,49 +204,13 @@ func Test_GetIssue(t *testing.T) {
248204 },
249205 expectedIssue : mockIssue2 ,
250206 lockdownEnabled : true ,
207+ restPermission : "none" ,
251208 },
252209 {
253210 name : "lockdown enabled - user lacks push access" ,
254211 mockedClient : MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {
255212 GetReposIssuesByOwnerByRepoByIssueNumber : mockResponse (t , http .StatusOK , mockIssue ),
256213 }),
257- gqlHTTPClient : githubv4mock .NewMockedHTTPClient (
258- githubv4mock .NewQueryMatcher (
259- struct {
260- Repository struct {
261- IsPrivate githubv4.Boolean
262- Collaborators struct {
263- Edges []struct {
264- Permission githubv4.String
265- Node struct {
266- Login githubv4.String
267- }
268- }
269- } `graphql:"collaborators(query: $username, first: 1)"`
270- } `graphql:"repository(owner: $owner, name: $name)"`
271- }{},
272- map [string ]any {
273- "owner" : githubv4 .String ("owner" ),
274- "name" : githubv4 .String ("repo" ),
275- "username" : githubv4 .String ("testuser" ),
276- },
277- githubv4mock .DataResponse (map [string ]any {
278- "repository" : map [string ]any {
279- "isPrivate" : false ,
280- "collaborators" : map [string ]any {
281- "edges" : []any {
282- map [string ]any {
283- "permission" : "READ" ,
284- "node" : map [string ]any {
285- "login" : "testuser" ,
286- },
287- },
288- },
289- },
290- },
291- }),
292- ),
293- ),
294214 requestArgs : map [string ]any {
295215 "method" : "get" ,
296216 "owner" : "owner" ,
@@ -300,20 +220,21 @@ func Test_GetIssue(t *testing.T) {
300220 expectResultError : true ,
301221 expectedErrMsg : "access to issue details is restricted by lockdown mode" ,
302222 lockdownEnabled : true ,
223+ restPermission : "read" ,
303224 },
304225 }
305226
306227 for _ , tc := range tests {
307228 t .Run (tc .name , func (t * testing.T ) {
308229 client := github .NewClient (tc .mockedClient )
309230
310- var gqlClient * githubv4. Client
311- cache := repoAccessCache
312- if tc .gqlHTTPClient != nil {
313- gqlClient = githubv4 . NewClient ( tc .gqlHTTPClient )
314- cache = stubRepoAccessCache ( gqlClient , 15 * time .Minute )
231+ gqlClient := defaultGQLClient
232+ var cache * lockdown. RepoAccessCache
233+ if tc .restPermission != "" {
234+ restClient := mockRESTPermissionServer ( t , tc .restPermission , nil )
235+ cache = stubLockdownCache ( t , restClient , 15 * time .Minute )
315236 } else {
316- gqlClient = githubv4 . NewClient ( nil )
237+ cache = stubRepoAccessCache ( gqlClient , 15 * time . Minute )
317238 }
318239
319240 flags := stubFeatureFlags (map [string ]bool {"lockdown-mode" : tc .lockdownEnabled })
@@ -1997,7 +1918,6 @@ func Test_GetIssueComments(t *testing.T) {
19971918 tests := []struct {
19981919 name string
19991920 mockedClient * http.Client
2000- gqlHTTPClient * http.Client
20011921 requestArgs map [string ]any
20021922 expectError bool
20031923 expectedComments []* github.IssueComment
@@ -2069,7 +1989,6 @@ func Test_GetIssueComments(t *testing.T) {
20691989 },
20701990 }),
20711991 }),
2072- gqlHTTPClient : newRepoAccessHTTPClient (),
20731992 requestArgs : map [string ]any {
20741993 "method" : "get_comments" ,
20751994 "owner" : "owner" ,
@@ -2092,13 +2011,17 @@ func Test_GetIssueComments(t *testing.T) {
20922011 t .Run (tc .name , func (t * testing.T ) {
20932012 // Setup client with mock
20942013 client := github .NewClient (tc .mockedClient )
2095- var gqlClient * githubv4.Client
2096- if tc .gqlHTTPClient != nil {
2097- gqlClient = githubv4 .NewClient (tc .gqlHTTPClient )
2014+ gqlClient := defaultGQLClient
2015+ var cache * lockdown.RepoAccessCache
2016+ if tc .lockdownEnabled {
2017+ restClient := mockRESTPermissionServer (t , "read" , map [string ]string {
2018+ "maintainer" : "write" ,
2019+ "testuser" : "read" ,
2020+ })
2021+ cache = stubLockdownCache (t , restClient , 15 * time .Minute )
20982022 } else {
2099- gqlClient = githubv4 . NewClient ( nil )
2023+ cache = stubRepoAccessCache ( gqlClient , 15 * time . Minute )
21002024 }
2101- cache := stubRepoAccessCache (gqlClient , 15 * time .Minute )
21022025 flags := stubFeatureFlags (map [string ]bool {"lockdown-mode" : tc .lockdownEnabled })
21032026 deps := BaseDeps {
21042027 Client : client ,
0 commit comments