Skip to content

Commit 198bc16

Browse files
paulcakefaceSamMorrowDrumsCopilot
authored
fix: allow public_repo for public contribution tools (#3140)
* fix: allow public_repo for contribution tools * test: tighten public repo scope coverage Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * fix: separate public repo visibility from OAuth scopes Keep public_repo as a fixed-token visibility capability while requiring repo for per-call OAuth challenges. Challenge only missing scopes for workflow pushes. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Paulcake <171492698+paulcakeface@users.noreply.github.com> Co-authored-by: Sam Morrow <sammorrowdrums@github.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 5a3c558 commit 198bc16

8 files changed

Lines changed: 126 additions & 10 deletions

File tree

‎pkg/github/issues.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,7 @@ func AddIssueComment(t translations.TranslationHelperFunc) inventory.ServerTool
14031403
Required: []string{"owner", "repo", "issue_number"},
14041404
},
14051405
},
1406-
scopes.RequireAll(scopes.Repo),
1406+
publicRepositoryWriteScopeAccess(),
14071407
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
14081408
owner, err := RequiredParam[string](args, "owner")
14091409
if err != nil {
@@ -2524,7 +2524,7 @@ Options are:
25242524
Required: []string{"method", "owner", "repo"},
25252525
},
25262526
},
2527-
scopes.RequireAll(scopes.Repo),
2527+
publicRepositoryWriteScopeAccess(),
25282528
func(ctx context.Context, deps ToolDependencies, req *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
25292529
method, err := RequiredParam[string](args, "method")
25302530
if err != nil {
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package github
2+
3+
import (
4+
"testing"
5+
6+
"github.com/github/github-mcp-server/pkg/inventory"
7+
"github.com/github/github-mcp-server/pkg/scopes"
8+
"github.com/github/github-mcp-server/pkg/translations"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestPublicRepoContributionToolScopeAccess(t *testing.T) {
14+
t.Parallel()
15+
16+
tools := []struct {
17+
name string
18+
tool inventory.ServerTool
19+
}{
20+
{name: "fork_repository", tool: ForkRepository(translations.NullTranslationHelper)},
21+
{name: "create_branch", tool: CreateBranch(translations.NullTranslationHelper)},
22+
{name: "create_pull_request", tool: CreatePullRequest(translations.NullTranslationHelper)},
23+
{name: "issue_write", tool: IssueWrite(translations.NullTranslationHelper)},
24+
{name: "add_issue_comment", tool: AddIssueComment(translations.NullTranslationHelper)},
25+
}
26+
27+
for _, tt := range tools {
28+
t.Run(tt.name, func(t *testing.T) {
29+
assert.Equal(t, []string{string(scopes.Repo)}, tt.tool.ScopeAccess.Scopes)
30+
require.NotNil(t, tt.tool.ScopeAccess.Visible)
31+
assert.False(t, tt.tool.ScopeAccess.Visible(nil))
32+
assert.True(t, tt.tool.ScopeAccess.Visible([]string{string(scopes.PublicRepo)}))
33+
assert.True(t, tt.tool.ScopeAccess.Visible([]string{string(scopes.Repo)}))
34+
35+
require.NotNil(t, tt.tool.ScopeAccess.Challenge)
36+
assert.Equal(t, []string{string(scopes.Repo)}, tt.tool.ScopeAccess.Challenge(nil, nil))
37+
assert.Equal(t, []string{string(scopes.Repo)}, tt.tool.ScopeAccess.Challenge(nil, []string{string(scopes.PublicRepo)}))
38+
assert.Empty(t, tt.tool.ScopeAccess.Challenge(nil, []string{string(scopes.Repo)}))
39+
})
40+
}
41+
}
42+
43+
func TestPublicRepoContributionToolsVisibleToPATs(t *testing.T) {
44+
t.Parallel()
45+
46+
tools := []inventory.ServerTool{
47+
ForkRepository(translations.NullTranslationHelper),
48+
CreateBranch(translations.NullTranslationHelper),
49+
PushFiles(translations.NullTranslationHelper),
50+
CreatePullRequest(translations.NullTranslationHelper),
51+
IssueWrite(translations.NullTranslationHelper),
52+
AddIssueComment(translations.NullTranslationHelper),
53+
}
54+
55+
tests := []struct {
56+
name string
57+
tokenScopes []string
58+
wantVisible bool
59+
}{
60+
{name: "no scopes"},
61+
{name: "public_repo", tokenScopes: []string{string(scopes.PublicRepo)}, wantVisible: true},
62+
{name: "repo", tokenScopes: []string{string(scopes.Repo)}, wantVisible: true},
63+
}
64+
65+
for _, tt := range tests {
66+
t.Run(tt.name, func(t *testing.T) {
67+
filter := CreateToolScopeFilter(tt.tokenScopes)
68+
for i := range tools {
69+
included, err := filter(t.Context(), &tools[i])
70+
require.NoError(t, err)
71+
assert.Equal(t, tt.wantVisible, included, tools[i].Tool.Name)
72+
}
73+
})
74+
}
75+
}
76+
77+
func TestPushFilesOAuthScopeChallenges(t *testing.T) {
78+
t.Parallel()
79+
80+
tool := PushFiles(translations.NullTranslationHelper)
81+
regularFiles := map[string]any{
82+
"files": []any{map[string]any{"path": "README.md"}},
83+
}
84+
workflowFiles := map[string]any{
85+
"files": []any{map[string]any{"path": ".github/workflows/ci.yml"}},
86+
}
87+
88+
assert.Equal(t, []string{string(scopes.Repo), string(scopes.Workflow)}, tool.ScopeAccess.Scopes)
89+
require.NotNil(t, tool.ScopeAccess.Visible)
90+
assert.True(t, tool.ScopeAccess.Visible([]string{string(scopes.PublicRepo)}))
91+
assert.True(t, tool.ScopeAccess.Visible([]string{string(scopes.Repo)}))
92+
93+
assert.Equal(t, []string{string(scopes.Repo)}, tool.ScopeAccess.Challenge(regularFiles, nil))
94+
assert.Equal(t, []string{string(scopes.Repo)}, tool.ScopeAccess.Challenge(regularFiles, []string{string(scopes.PublicRepo)}))
95+
assert.Empty(t, tool.ScopeAccess.Challenge(regularFiles, []string{string(scopes.Repo)}))
96+
97+
assert.Equal(t, []string{string(scopes.Repo), string(scopes.Workflow)}, tool.ScopeAccess.Challenge(workflowFiles, nil))
98+
assert.Equal(t, []string{string(scopes.Repo), string(scopes.Workflow)}, tool.ScopeAccess.Challenge(workflowFiles, []string{string(scopes.PublicRepo)}))
99+
assert.Equal(t, []string{string(scopes.Workflow)}, tool.ScopeAccess.Challenge(workflowFiles, []string{string(scopes.Repo)}))
100+
assert.Equal(t, []string{string(scopes.Repo)}, tool.ScopeAccess.Challenge(workflowFiles, []string{string(scopes.Workflow)}))
101+
assert.Equal(t, []string{string(scopes.Repo)}, tool.ScopeAccess.Challenge(workflowFiles, []string{string(scopes.PublicRepo), string(scopes.Workflow)}))
102+
assert.Empty(t, tool.ScopeAccess.Challenge(workflowFiles, []string{string(scopes.Repo), string(scopes.Workflow)}))
103+
}

‎pkg/github/pullrequests.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ func CreatePullRequest(t translations.TranslationHelperFunc) inventory.ServerToo
706706
Required: []string{"owner", "repo", "title", "head", "base"},
707707
},
708708
},
709-
scopes.RequireAll(scopes.Repo),
709+
publicRepositoryWriteScopeAccess(),
710710
func(ctx context.Context, deps ToolDependencies, req *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
711711
owner, err := RequiredParam[string](args, "owner")
712712
if err != nil {

‎pkg/github/repositories.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ func ForkRepository(t translations.TranslationHelperFunc) inventory.ServerTool {
12321232
Required: []string{"owner", "repo"},
12331233
},
12341234
},
1235-
scopes.RequireAll(scopes.Repo),
1235+
publicRepositoryWriteScopeAccess(),
12361236
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
12371237
owner, err := RequiredParam[string](args, "owner")
12381238
if err != nil {
@@ -1529,7 +1529,7 @@ func CreateBranch(t translations.TranslationHelperFunc) inventory.ServerTool {
15291529
Required: []string{"owner", "repo", "branch"},
15301530
},
15311531
},
1532-
scopes.RequireAll(scopes.Repo),
1532+
publicRepositoryWriteScopeAccess(),
15331533
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
15341534
owner, err := RequiredParam[string](args, "owner")
15351535
if err != nil {
@@ -1661,7 +1661,7 @@ func PushFiles(t translations.TranslationHelperFunc) inventory.ServerTool {
16611661
Required: []string{"owner", "repo", "branch", "files", "message"},
16621662
},
16631663
},
1664-
scopes.RequireAll(scopes.Repo),
1664+
publicRepositoryWriteScopeAccess(),
16651665
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
16661666
owner, err := RequiredParam[string](args, "owner")
16671667
if err != nil {

‎pkg/github/repository_path.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ func workflowScopeChallengeForFiles(arguments map[string]any, activeScopes []str
7575
containsWorkflow = true
7676
}
7777
}
78-
if containsWorkflow {
79-
return scopes.ChallengeAll(activeScopes, scopes.Repo, scopes.Workflow)
78+
var challenge []string
79+
if !scopes.HasAll(activeScopes, scopes.Repo) {
80+
challenge = append(challenge, string(scopes.Repo))
8081
}
81-
return scopes.ChallengeAll(activeScopes, scopes.Repo)
82+
if containsWorkflow && !scopes.HasAll(activeScopes, scopes.Workflow) {
83+
challenge = append(challenge, string(scopes.Workflow))
84+
}
85+
return challenge
8286
}

‎pkg/github/repository_path_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestFileWriteWorkflowScopeChallenges(t *testing.T) {
8484
map[string]any{"path": "README.md"},
8585
map[string]any{"path": ".github/workflows/ci.yml"},
8686
}},
87-
want: []string{"repo", "workflow"},
87+
want: []string{"workflow"},
8888
},
8989
{
9090
name: "push validates entries after workflow",

‎pkg/github/tool_scopes.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import (
55
"github.com/github/github-mcp-server/pkg/scopes"
66
)
77

8+
func publicRepositoryWriteScopeAccess() inventory.ScopeAccess {
9+
access := scopes.RequireAll(scopes.Repo)
10+
access.Visible = func(activeScopes []string) bool {
11+
return scopes.HasAll(activeScopes, scopes.PublicRepo)
12+
}
13+
return access
14+
}
15+
816
func repositoryOrOrganizationScopeAccess() inventory.ScopeAccess {
917
return scopes.DynamicChallenge(
1018
[]scopes.Scope{scopes.Repo, scopes.ReadOrg},

‎pkg/scopes/scopes_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestOAuthScopeCatalog(t *testing.T) {
2121

2222
func TestScopeChecks(t *testing.T) {
2323
assert.True(t, HasAll([]string{"repo", "workflow"}, Repo, Workflow))
24+
assert.True(t, HasAll([]string{"repo"}, PublicRepo))
2425
assert.False(t, HasAll([]string{"repo"}, Repo, Workflow))
2526
assert.True(t, HasAll([]string{"admin:org"}, ReadOrg))
2627
assert.True(t, HasAllScopeNames([]string{"admin:org"}, []string{"read:org"}))

0 commit comments

Comments
 (0)