Skip to content

Commit 77ef6d0

Browse files
committed
Merge two graphql queries into one
1 parent 5a05b1a commit 77ef6d0

File tree

3 files changed

+27
-54
lines changed

3 files changed

+27
-54
lines changed

pkg/github/issues.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,10 @@ func GetIssue(ctx context.Context, client *github.Client, gqlClient *githubv4.Cl
327327

328328
if flags.LockdownMode {
329329
if issue.User != nil && issue.Repository != nil && issue.Repository.Owner != nil && issue.Repository.Name != nil {
330-
shouldRemoveContent := lockdown.ShouldRemoveContent(ctx, gqlClient, *issue.User.Login, *issue.Repository.Owner.Login, *issue.Repository.Name)
330+
shouldRemoveContent, err := lockdown.ShouldRemoveContent(ctx, gqlClient, *issue.User.Login, *issue.Repository.Owner.Login, *issue.Repository.Name)
331+
if err != nil {
332+
return mcp.NewToolResultError(fmt.Sprintf("failed to check lockdown mode: %v", err)), nil
333+
}
331334
if shouldRemoveContent {
332335
return mcp.NewToolResultError("access to issue details is restricted by lockdown mode"), nil
333336
}

pkg/github/issues_test.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,28 @@ func Test_GetIssue(t *testing.T) {
108108
githubv4mock.NewQueryMatcher(
109109
struct {
110110
Repository struct {
111-
IsPrivate githubv4.Boolean
111+
IsPrivate githubv4.Boolean
112+
Collaborators struct {
113+
Edges []struct {
114+
Permission githubv4.String
115+
Node struct {
116+
Login githubv4.String
117+
}
118+
}
119+
} `graphql:"collaborators(query: $username, first: 1)"`
112120
} `graphql:"repository(owner: $owner, name: $name)"`
113121
}{},
114122
map[string]any{
115-
"owner": githubv4.String("owner"),
116-
"name": githubv4.String("repo"),
123+
"owner": githubv4.String("owner"),
124+
"name": githubv4.String("repo"),
125+
"username": githubv4.String("testuser"),
117126
},
118127
githubv4mock.DataResponse(map[string]any{
119128
"repository": map[string]any{
120129
"isPrivate": true,
130+
"collaborators": map[string]any{
131+
"edges": []any{},
132+
},
121133
},
122134
}),
123135
),
@@ -143,22 +155,7 @@ func Test_GetIssue(t *testing.T) {
143155
githubv4mock.NewQueryMatcher(
144156
struct {
145157
Repository struct {
146-
IsPrivate githubv4.Boolean
147-
} `graphql:"repository(owner: $owner, name: $name)"`
148-
}{},
149-
map[string]any{
150-
"owner": githubv4.String("owner"),
151-
"name": githubv4.String("repo"),
152-
},
153-
githubv4mock.DataResponse(map[string]any{
154-
"repository": map[string]any{
155-
"isPrivate": false,
156-
},
157-
}),
158-
),
159-
githubv4mock.NewQueryMatcher(
160-
struct {
161-
Repository struct {
158+
IsPrivate githubv4.Boolean
162159
Collaborators struct {
163160
Edges []struct {
164161
Permission githubv4.String
@@ -176,6 +173,7 @@ func Test_GetIssue(t *testing.T) {
176173
},
177174
githubv4mock.DataResponse(map[string]any{
178175
"repository": map[string]any{
176+
"isPrivate": false,
179177
"collaborators": map[string]any{
180178
"edges": []any{
181179
map[string]any{

pkg/lockdown/lockdown.go

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func ShouldRemoveContent(ctx context.Context, client *githubv4.Client, username, owner, repo string) (bool, error) {
12-
isPrivate, err := IsPrivateRepo(ctx, client, owner, repo)
12+
isPrivate, hasPushAccess, err := repoAccessInfo(ctx, client, username, owner, repo)
1313
if err != nil {
1414
return false, err
1515
}
@@ -18,21 +18,18 @@ func ShouldRemoveContent(ctx context.Context, client *githubv4.Client, username,
1818
if isPrivate {
1919
return false, nil
2020
}
21-
hasPushAccess, err := HasPushAccess(ctx, client, username, owner, repo)
22-
if err != nil {
23-
return false, err
24-
}
2521

2622
return !hasPushAccess, nil
2723
}
2824

29-
func HasPushAccess(ctx context.Context, client *githubv4.Client, username, owner, repo string) (bool, error) {
25+
func repoAccessInfo(ctx context.Context, client *githubv4.Client, username, owner, repo string) (bool, bool, error) {
3026
if client == nil {
31-
return false, fmt.Errorf("nil GraphQL client")
27+
return false, false, fmt.Errorf("nil GraphQL client")
3228
}
3329

3430
var query struct {
3531
Repository struct {
32+
IsPrivate githubv4.Boolean
3633
Collaborators struct {
3734
Edges []struct {
3835
Permission githubv4.String
@@ -52,7 +49,7 @@ func HasPushAccess(ctx context.Context, client *githubv4.Client, username, owner
5249

5350
err := client.Query(ctx, &query, variables)
5451
if err != nil {
55-
return false, fmt.Errorf("failed to query user permissions: %w", err)
52+
return false, false, fmt.Errorf("failed to query repository access info: %w", err)
5653
}
5754

5855
// Check if the user has push access
@@ -67,30 +64,5 @@ func HasPushAccess(ctx context.Context, client *githubv4.Client, username, owner
6764
}
6865
}
6966

70-
return hasPush, nil
71-
}
72-
73-
// IsPrivateRepo checks if a repository is private using GraphQL
74-
func IsPrivateRepo(ctx context.Context, client *githubv4.Client, owner, repo string) (bool, error) {
75-
if client == nil {
76-
return false, fmt.Errorf("nil GraphQL client")
77-
}
78-
79-
var query struct {
80-
Repository struct {
81-
IsPrivate githubv4.Boolean
82-
} `graphql:"repository(owner: $owner, name: $name)"`
83-
}
84-
85-
variables := map[string]interface{}{
86-
"owner": githubv4.String(owner),
87-
"name": githubv4.String(repo),
88-
}
89-
90-
err := client.Query(ctx, &query, variables)
91-
if err != nil {
92-
return false, fmt.Errorf("failed to query repository visibility: %w", err)
93-
}
94-
95-
return bool(query.Repository.IsPrivate), nil
67+
return bool(query.Repository.IsPrivate), hasPush, nil
9668
}

0 commit comments

Comments
 (0)