Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 383c2d2

Browse files
committed
refactor to make testing easier
1 parent b3a3d15 commit 383c2d2

File tree

2 files changed

+108
-83
lines changed

2 files changed

+108
-83
lines changed

pkg/github/repository_resource.go

Lines changed: 103 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -14,109 +14,136 @@ import (
1414
)
1515

1616
// getRepositoryContent defines the resource template and handler for the Repository Content API.
17-
func getRepositoryContent(client *github.Client, t translations.TranslationHelperFunc) (mainTemplate mcp.ResourceTemplate, reftemplate mcp.ResourceTemplate, shaTemplate mcp.ResourceTemplate, tagTemplate mcp.ResourceTemplate, prTemplate mcp.ResourceTemplate, handler server.ResourceTemplateHandlerFunc) {
18-
17+
func getRepositoryContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
1918
return mcp.NewResourceTemplate(
2019
"repo://{owner}/{repo}/contents{/path*}", // Resource template
2120
t("RESOURCE_REPOSITORY_CONTENT_DESCRIPTION", "Repository Content"),
22-
), mcp.NewResourceTemplate(
21+
),
22+
handlerFunc(client, t)
23+
}
24+
25+
// getRepositoryContent defines the resource template and handler for the Repository Content API.
26+
func getRepositoryBranchContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
27+
return mcp.NewResourceTemplate(
2328
"repo://{owner}/{repo}/refs/heads/{branch}/contents{/path*}", // Resource template
2429
t("RESOURCE_REPOSITORY_CONTENT_BRANCH_DESCRIPTION", "Repository Content for specific branch"),
25-
), mcp.NewResourceTemplate(
30+
),
31+
handlerFunc(client, t)
32+
}
33+
34+
// getRepositoryContent defines the resource template and handler for the Repository Content API.
35+
func getRepositoryCommitContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
36+
return mcp.NewResourceTemplate(
2637
"repo://{owner}/{repo}/sha/{sha}/contents{/path*}", // Resource template
2738
t("RESOURCE_REPOSITORY_CONTENT_COMMIT_DESCRIPTION", "Repository Content for specific commit"),
28-
), mcp.NewResourceTemplate(
39+
),
40+
handlerFunc(client, t)
41+
}
42+
43+
// getRepositoryContent defines the resource template and handler for the Repository Content API.
44+
func getRepositoryTagContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
45+
return mcp.NewResourceTemplate(
2946
"repo://{owner}/{repo}/refs/tags/{tag}/contents{/path*}", // Resource template
3047
t("RESOURCE_REPOSITORY_CONTENT_TAG_DESCRIPTION", "Repository Content for specific tag"),
31-
), mcp.NewResourceTemplate(
48+
),
49+
handlerFunc(client, t)
50+
}
51+
52+
// getRepositoryContent defines the resource template and handler for the Repository Content API.
53+
func getRepositoryPrContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
54+
return mcp.NewResourceTemplate(
3255
"repo://{owner}/{repo}/refs/pull/{pr_number}/head/contents{/path*}", // Resource template
3356
t("RESOURCE_REPOSITORY_CONTENT_PR_DESCRIPTION", "Repository Content for specific pull request"),
34-
), func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) {
35-
// Extract parameters from request.Params.URI
57+
),
58+
handlerFunc(client, t)
59+
}
3660

37-
owner := request.Params.Arguments["owner"].([]string)[0]
38-
repo := request.Params.Arguments["repo"].([]string)[0]
39-
// path should be a joined list of the path parts
40-
path := strings.Join(request.Params.Arguments["path"].([]string), "/")
61+
func handlerFunc(client *github.Client, _ translations.TranslationHelperFunc) func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) {
62+
return func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) { // Extract parameters from request.Params.URI
4163

42-
opts := &github.RepositoryContentGetOptions{}
64+
owner := request.Params.Arguments["owner"].([]string)[0]
65+
repo := request.Params.Arguments["repo"].([]string)[0]
66+
// path should be a joined list of the path parts
67+
path := strings.Join(request.Params.Arguments["path"].([]string), "/")
4368

44-
sha, ok := request.Params.Arguments["sha"].([]string)
45-
if ok {
46-
opts.Ref = sha[0]
47-
}
69+
opts := &github.RepositoryContentGetOptions{}
4870

49-
branch, ok := request.Params.Arguments["branch"].([]string)
50-
if ok {
51-
opts.Ref = "refs/heads/" + branch[0]
52-
}
71+
sha, ok := request.Params.Arguments["sha"].([]string)
72+
if ok {
73+
opts.Ref = sha[0]
74+
}
5375

54-
tag, ok := request.Params.Arguments["tag"].([]string)
55-
if ok {
56-
opts.Ref = "refs/tags/" + tag[0]
57-
}
58-
prNumber, ok := request.Params.Arguments["pr_number"].([]string)
59-
if ok {
60-
opts.Ref = "refs/pull/" + prNumber[0] + "/head"
61-
}
76+
branch, ok := request.Params.Arguments["branch"].([]string)
77+
if ok {
78+
opts.Ref = "refs/heads/" + branch[0]
79+
}
80+
81+
tag, ok := request.Params.Arguments["tag"].([]string)
82+
if ok {
83+
opts.Ref = "refs/tags/" + tag[0]
84+
}
85+
prNumber, ok := request.Params.Arguments["pr_number"].([]string)
86+
if ok {
87+
opts.Ref = "refs/pull/" + prNumber[0] + "/head"
88+
}
89+
90+
// Use the GitHub client to fetch repository content
91+
fileContent, directoryContent, _, err := client.Repositories.GetContents(ctx, owner, repo, path, opts)
92+
if err != nil {
93+
return nil, err
94+
}
95+
96+
if directoryContent != nil {
97+
// Process the directory content and return it as resource contents
98+
var resources []mcp.ResourceContents
99+
for _, entry := range directoryContent {
100+
mimeType := "text/directory"
101+
if entry.GetType() == "file" {
102+
mimeType = mime.TypeByExtension(filepath.Ext(entry.GetName()))
103+
}
104+
resources = append(resources, mcp.TextResourceContents{
105+
URI: entry.GetHTMLURL(),
106+
MIMEType: mimeType,
107+
Text: entry.GetName(),
108+
})
62109

63-
// Use the GitHub client to fetch repository content
64-
fileContent, directoryContent, _, err := client.Repositories.GetContents(ctx, owner, repo, path, opts)
65-
if err != nil {
66-
return nil, err
67110
}
111+
return resources, nil
68112

69-
if directoryContent != nil {
70-
// Process the directory content and return it as resource contents
71-
var resources []mcp.ResourceContents
72-
for _, entry := range directoryContent {
73-
mimeType := "text/directory"
74-
if entry.GetType() == "file" {
75-
mimeType = mime.TypeByExtension(filepath.Ext(entry.GetName()))
76-
}
77-
resources = append(resources, mcp.TextResourceContents{
78-
URI: entry.GetHTMLURL(),
79-
MIMEType: mimeType,
80-
Text: entry.GetName(),
81-
})
113+
} else if fileContent != nil {
114+
// Process the file content and return it as a binary resource
82115

116+
if fileContent.Content != nil {
117+
decodedContent, err := fileContent.GetContent()
118+
if err != nil {
119+
return nil, err
83120
}
84-
return resources, nil
85-
86-
} else if fileContent != nil {
87-
// Process the file content and return it as a binary resource
88-
89-
if fileContent.Content != nil {
90-
decodedContent, err := fileContent.GetContent()
91-
if err != nil {
92-
return nil, err
93-
}
94-
95-
mimeType := mime.TypeByExtension(filepath.Ext(fileContent.GetName()))
96-
97-
// Check if the file is text-based
98-
if strings.HasPrefix(mimeType, "text") {
99-
// Return as TextResourceContents
100-
return []mcp.ResourceContents{
101-
mcp.TextResourceContents{
102-
URI: request.Params.URI,
103-
MIMEType: mimeType,
104-
Text: decodedContent,
105-
},
106-
}, nil
107-
}
108-
109-
// Otherwise, return as BlobResourceContents
121+
122+
mimeType := mime.TypeByExtension(filepath.Ext(fileContent.GetName()))
123+
124+
// Check if the file is text-based
125+
if strings.HasPrefix(mimeType, "text") {
126+
// Return as TextResourceContents
110127
return []mcp.ResourceContents{
111-
mcp.BlobResourceContents{
128+
mcp.TextResourceContents{
112129
URI: request.Params.URI,
113130
MIMEType: mimeType,
114-
Blob: base64.StdEncoding.EncodeToString([]byte(decodedContent)), // Encode content as Base64
131+
Text: decodedContent,
115132
},
116133
}, nil
117134
}
118-
}
119135

120-
return nil, nil
136+
// Otherwise, return as BlobResourceContents
137+
return []mcp.ResourceContents{
138+
mcp.BlobResourceContents{
139+
URI: request.Params.URI,
140+
MIMEType: mimeType,
141+
Blob: base64.StdEncoding.EncodeToString([]byte(decodedContent)), // Encode content as Base64
142+
},
143+
}, nil
144+
}
121145
}
146+
147+
return nil, nil
148+
}
122149
}

pkg/github/server.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ func NewServer(client *github.Client, readOnly bool, t translations.TranslationH
2525
server.WithLogging())
2626

2727
// Add GitHub Resources
28-
defaultTemplate, branchTemplate, tagTemplate, shaTemplate, prTemplate, handler := getRepositoryContent(client, t)
29-
30-
s.AddResourceTemplate(defaultTemplate, handler)
31-
s.AddResourceTemplate(branchTemplate, handler)
32-
s.AddResourceTemplate(tagTemplate, handler)
33-
s.AddResourceTemplate(shaTemplate, handler)
34-
s.AddResourceTemplate(prTemplate, handler)
28+
s.AddResourceTemplate(getRepositoryContent(client, t))
29+
s.AddResourceTemplate(getRepositoryBranchContent(client, t))
30+
s.AddResourceTemplate(getRepositoryCommitContent(client, t))
31+
s.AddResourceTemplate(getRepositoryTagContent(client, t))
32+
s.AddResourceTemplate(getRepositoryPrContent(client, t))
3533

3634
// Add GitHub tools - Issues
3735
s.AddTool(getIssue(client, t))

0 commit comments

Comments
 (0)