Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit383c2d2

Browse files
committed
refactor to make testing easier
1 parentb3a3d15 commit383c2d2

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-
funcgetRepositoryContent(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+
funcgetRepositoryContent(client*github.Client,t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
1918
returnmcp.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+
funcgetRepositoryBranchContent(client*github.Client,t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
27+
returnmcp.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+
funcgetRepositoryCommitContent(client*github.Client,t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
36+
returnmcp.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+
funcgetRepositoryTagContent(client*github.Client,t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
45+
returnmcp.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+
funcgetRepositoryPrContent(client*github.Client,t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
54+
returnmcp.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+
funchandlerFunc(client*github.Client,_ translations.TranslationHelperFunc)func(ctx context.Context,request mcp.ReadResourceRequest) ([]mcp.ResourceContents,error) {
62+
returnfunc(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-
ifok {
46-
opts.Ref=sha[0]
47-
}
69+
opts:=&github.RepositoryContentGetOptions{}
4870

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

54-
tag,ok:=request.Params.Arguments["tag"].([]string)
55-
ifok {
56-
opts.Ref="refs/tags/"+tag[0]
57-
}
58-
prNumber,ok:=request.Params.Arguments["pr_number"].([]string)
59-
ifok {
60-
opts.Ref="refs/pull/"+prNumber[0]+"/head"
61-
}
76+
branch,ok:=request.Params.Arguments["branch"].([]string)
77+
ifok {
78+
opts.Ref="refs/heads/"+branch[0]
79+
}
80+
81+
tag,ok:=request.Params.Arguments["tag"].([]string)
82+
ifok {
83+
opts.Ref="refs/tags/"+tag[0]
84+
}
85+
prNumber,ok:=request.Params.Arguments["pr_number"].([]string)
86+
ifok {
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+
iferr!=nil {
93+
returnnil,err
94+
}
95+
96+
ifdirectoryContent!=nil {
97+
// Process the directory content and return it as resource contents
98+
varresources []mcp.ResourceContents
99+
for_,entry:=rangedirectoryContent {
100+
mimeType:="text/directory"
101+
ifentry.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-
iferr!=nil {
66-
returnnil,err
67110
}
111+
returnresources,nil
68112

69-
ifdirectoryContent!=nil {
70-
// Process the directory content and return it as resource contents
71-
varresources []mcp.ResourceContents
72-
for_,entry:=rangedirectoryContent {
73-
mimeType:="text/directory"
74-
ifentry.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+
}elseiffileContent!=nil {
114+
// Process the file content and return it as a binary resource
82115

116+
iffileContent.Content!=nil {
117+
decodedContent,err:=fileContent.GetContent()
118+
iferr!=nil {
119+
returnnil,err
83120
}
84-
returnresources,nil
85-
86-
}elseiffileContent!=nil {
87-
// Process the file content and return it as a binary resource
88-
89-
iffileContent.Content!=nil {
90-
decodedContent,err:=fileContent.GetContent()
91-
iferr!=nil {
92-
returnnil,err
93-
}
94-
95-
mimeType:=mime.TypeByExtension(filepath.Ext(fileContent.GetName()))
96-
97-
// Check if the file is text-based
98-
ifstrings.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+
ifstrings.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-
returnnil,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+
returnnil,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)

[8]ページ先頭

©2009-2025 Movatter.jp