Skip to content

Commit 02ebdc7

Browse files
committed
make required args explicit instead of panic
1 parent db7a180 commit 02ebdc7

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

pkg/github/repository_resource.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
import (
44
"context"
55
"encoding/base64"
6+
"errors"
67
"mime"
78
"path/filepath"
89
"strings"
@@ -60,30 +61,41 @@ func getRepositoryResourcePrContent(client *github.Client, t translations.Transl
6061

6162
func repositoryResourceContentsHandler(client *github.Client) func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) {
6263
return func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) {
64+
// the matcher will give []string with one elemenent
65+
// https://github.com/mark3labs/mcp-go/pull/54
66+
o, ok := request.Params.Arguments["owner"].([]string)
67+
if !ok || len(o) == 0 {
68+
return nil, errors.New("owner is required")
69+
}
70+
owner := o[0]
71+
72+
r, ok := request.Params.Arguments["repo"].([]string)
73+
if !ok || len(r) == 0 {
74+
return nil, errors.New("repo is required")
75+
}
76+
repo := r[0]
6377

64-
owner := request.Params.Arguments["owner"].([]string)[0]
65-
repo := request.Params.Arguments["repo"].([]string)[0]
6678
// path should be a joined list of the path parts
6779
path := strings.Join(request.Params.Arguments["path"].([]string), "/")
6880

6981
opts := &github.RepositoryContentGetOptions{}
7082

7183
sha, ok := request.Params.Arguments["sha"].([]string)
72-
if ok {
84+
if ok && len(sha) > 0 {
7385
opts.Ref = sha[0]
7486
}
7587

7688
branch, ok := request.Params.Arguments["branch"].([]string)
77-
if ok {
89+
if ok && len(branch) > 0 {
7890
opts.Ref = "refs/heads/" + branch[0]
7991
}
8092

8193
tag, ok := request.Params.Arguments["tag"].([]string)
82-
if ok {
94+
if ok && len(tag) > 0 {
8395
opts.Ref = "refs/tags/" + tag[0]
8496
}
8597
prNumber, ok := request.Params.Arguments["pr_number"].([]string)
86-
if ok {
98+
if ok && len(prNumber) > 0 {
8799
opts.Ref = "refs/pull/" + prNumber[0] + "/head"
88100
}
89101

pkg/github/repository_resource_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ func Test_repositoryResourceContentsHandler(t *testing.T) {
7070
expectedResult any
7171
expectedErrMsg string
7272
}{
73+
{
74+
name: "missing owner",
75+
mockedClient: mock.NewMockedHTTPClient(
76+
mock.WithRequestMatch(
77+
mock.GetReposContentsByOwnerByRepoByPath,
78+
mockFileContent,
79+
),
80+
),
81+
requestArgs: map[string]any{},
82+
expectError: true,
83+
},
84+
{
85+
name: "missing repo",
86+
mockedClient: mock.NewMockedHTTPClient(
87+
mock.WithRequestMatch(
88+
mock.GetReposContentsByOwnerByRepoByPath,
89+
mockFileContent,
90+
),
91+
),
92+
requestArgs: map[string]any{
93+
"owner": []string{"owner"},
94+
},
95+
expectError: true,
96+
},
7397
{
7498
name: "successful file content fetch",
7599
mockedClient: mock.NewMockedHTTPClient(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy