Skip to content

Commit 30e1910

Browse files
committed
decode base64 from get_file_contents response content
1 parent c423a52 commit 30e1910

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

pkg/github/repositories.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,14 @@ func GetFileContents(getClient GetClientFn, t translations.TranslationHelperFunc
454454
if err != nil {
455455
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
456456
}
457-
opts := &github.RepositoryContentGetOptions{Ref: branch}
458-
fileContent, dirContent, resp, err := client.Repositories.GetContents(ctx, owner, repo, path, opts)
457+
458+
// Use the go-github client to get file/directory contents
459+
opts := &github.RepositoryContentGetOptions{}
460+
if branch != "" {
461+
opts.Ref = branch
462+
}
463+
464+
fileContent, directoryContent, resp, err := client.Repositories.GetContents(ctx, owner, repo, path, opts)
459465
if err != nil {
460466
return nil, fmt.Errorf("failed to get file contents: %w", err)
461467
}
@@ -469,19 +475,23 @@ func GetFileContents(getClient GetClientFn, t translations.TranslationHelperFunc
469475
return mcp.NewToolResultError(fmt.Sprintf("failed to get file contents: %s", string(body))), nil
470476
}
471477

472-
var result interface{}
473478
if fileContent != nil {
474-
result = fileContent
479+
// It's a file - return the raw decoded content
480+
content, err := fileContent.GetContent()
481+
if err != nil {
482+
return nil, fmt.Errorf("failed to decode file content: %w", err)
483+
}
484+
return mcp.NewToolResultText(content), nil
485+
} else if directoryContent != nil {
486+
// It's a directory - return JSON array of contents
487+
r, err := json.Marshal(directoryContent)
488+
if err != nil {
489+
return nil, fmt.Errorf("failed to marshal directory contents: %w", err)
490+
}
491+
return mcp.NewToolResultText(string(r)), nil
475492
} else {
476-
result = dirContent
493+
return mcp.NewToolResultError("unexpected response: neither file nor directory content returned"), nil
477494
}
478-
479-
r, err := json.Marshal(result)
480-
if err != nil {
481-
return nil, fmt.Errorf("failed to marshal response: %w", err)
482-
}
483-
484-
return mcp.NewToolResultText(string(r)), nil
485495
}
486496
}
487497

0 commit comments

Comments
 (0)