Skip to content

Commit 37a6088

Browse files
committed
fix: handling of directories
1 parent be2f36f commit 37a6088

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

pkg/github/repositories.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -570,24 +570,14 @@ func GetFileContents(getClient GetClientFn, getRawClient raw.GetRawClientFn, t t
570570
if strings.HasSuffix(path, "/") {
571571
opts := &github.RepositoryContentGetOptions{Ref: ref}
572572
_, dirContent, resp, err := client.Repositories.GetContents(ctx, owner, repo, path, opts)
573-
if err != nil {
574-
return mcp.NewToolResultError("failed to get file contents"), nil
575-
}
576-
defer func() { _ = resp.Body.Close() }()
577-
578-
if resp.StatusCode != 200 {
579-
body, err := io.ReadAll(resp.Body)
573+
if err == nil && resp.StatusCode == http.StatusOK {
574+
defer func() { _ = resp.Body.Close() }()
575+
r, err := json.Marshal(dirContent)
580576
if err != nil {
581-
return mcp.NewToolResultError("failed to read response body"), nil
577+
return mcp.NewToolResultError("failed to marshal response"), nil
582578
}
583-
return mcp.NewToolResultError(fmt.Sprintf("failed to get file contents: %s", string(body))), nil
579+
return mcp.NewToolResultText(string(r)), nil
584580
}
585-
586-
r, err := json.Marshal(dirContent)
587-
if err != nil {
588-
return mcp.NewToolResultError("failed to marshal response"), nil
589-
}
590-
return mcp.NewToolResultText(string(r)), nil
591581
}
592582

593583
// The path does not point to a file or directory.
@@ -1301,6 +1291,8 @@ func GetTag(getClient GetClientFn, t translations.TranslationHelperFunc) (tool m
13011291
// filterPaths filters the entries in a GitHub tree to find paths that
13021292
// match the given suffix.
13031293
func filterPaths(entries []*github.TreeEntry, path string, maxResults int) []string {
1294+
path = strings.TrimSuffix(path, "/") // Normalize path to avoid double slashes
1295+
13041296
matchedPaths := []string{}
13051297
for _, entry := range entries {
13061298
if len(matchedPaths) == maxResults {
@@ -1311,6 +1303,9 @@ func filterPaths(entries []*github.TreeEntry, path string, maxResults int) []str
13111303
continue // Skip empty paths
13121304
}
13131305
if strings.HasSuffix(entryPath, path) {
1306+
if entry.GetType() == "tree" {
1307+
entryPath += "/" // show directories with a trailing slash
1308+
}
13141309
matchedPaths = append(matchedPaths, entryPath)
13151310
}
13161311
}

0 commit comments

Comments
 (0)