- Notifications
You must be signed in to change notification settings - Fork937
feat: list repositories by org#210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
simondanielsson wants to merge5 commits intogithub:mainChoose a base branch fromsimondanielsson:simondanielsson/add-list-repos-by-org
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
4c405a0
Implement list repositories
danielssonsimonbcg727a4b0
Rename repo_type to repoType
danielssonsimonbcg0b5104b
Switch to getClient idiom
danielssonsimonbcg879b1ab
Add enums for list_repositories parameters
danielssonsimonbcge9bd31f
Add list_repositories to README
danielssonsimonbcgFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package github | ||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"github.com/github/github-mcp-server/pkg/translations" | ||
"github.com/google/go-github/v69/github" | ||
"github.com/mark3labs/mcp-go/mcp" | ||
"github.com/mark3labs/mcp-go/server" | ||
) | ||
// ListCommits creates a tool to get commits of a branch in a repository. | ||
func ListRepositories(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||
return mcp.NewTool("list_repositories", | ||
mcp.WithDescription(t("TOOL_LIST_REPOSITORIES_DESCRIPTION", "Get list of repositories in a GitHub organization")), | ||
mcp.WithString("org", | ||
mcp.Required(), | ||
mcp.Description("Organization name"), | ||
), | ||
mcp.WithString("type", | ||
mcp.Description("Type of repositories to list."), | ||
mcp.Enum("all", "public", "private", "forks", "sources", "member"), | ||
mcp.DefaultString("all"), | ||
), | ||
mcp.WithString("sort", | ||
mcp.Description("How to sort the repository list."), | ||
mcp.Enum("created", "updated", "pushed", "full_name"), | ||
mcp.DefaultString("created"), | ||
), | ||
mcp.WithString("direction", | ||
mcp.Description("Direction in which to sort repositories. Default when using full_name: asc; otherwise desc."), | ||
mcp.Enum("asc", "desc"), | ||
), | ||
WithPagination(), | ||
), | ||
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
org, err := requiredParam[string](request, "org") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
pagination, err := OptionalPaginationParams(request) | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
opts := &github.RepositoryListByOrgOptions{ | ||
ListOptions: github.ListOptions{ | ||
Page: pagination.page, | ||
PerPage: pagination.perPage, | ||
}, | ||
} | ||
repoType, err := OptionalParam[string](request, "type") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
if repoType != "" { | ||
opts.Type = repoType | ||
} | ||
sort, err := OptionalParam[string](request, "sort") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
if sort != "" { | ||
opts.Sort = sort | ||
} | ||
direction, err := OptionalParam[string](request, "direction") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
if direction != "" { | ||
opts.Direction = direction | ||
} | ||
client, err := getClient(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get GitHub client: %w", err) | ||
} | ||
repos, resp, err := client.Repositories.ListByOrg(ctx, org, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to list repositories: %w", err) | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
if resp.StatusCode != 200 { | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read response body: %w", err) | ||
} | ||
return mcp.NewToolResultError(fmt.Sprintf("failed to list repositories: %s", string(body))), nil | ||
} | ||
r, err := json.Marshal(repos) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal response: %w", err) | ||
} | ||
return mcp.NewToolResultText(string(r)), nil | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
package github | ||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"testing" | ||
"github.com/github/github-mcp-server/pkg/translations" | ||
"github.com/google/go-github/v69/github" | ||
"github.com/migueleliasweb/go-github-mock/src/mock" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
func Test_ListRepositories(t *testing.T) { | ||
// Verify tool definition once | ||
mockClient := github.NewClient(nil) | ||
tool, _ := ListRepositories(stubGetClientFn(mockClient), translations.NullTranslationHelper) | ||
assert.Equal(t, "list_repositories", tool.Name) | ||
assert.NotEmpty(t, tool.Description) | ||
assert.Contains(t, tool.InputSchema.Properties, "org") | ||
assert.Contains(t, tool.InputSchema.Properties, "type") | ||
assert.Contains(t, tool.InputSchema.Properties, "sort") | ||
assert.Contains(t, tool.InputSchema.Properties, "direction") | ||
assert.Contains(t, tool.InputSchema.Properties, "perPage") | ||
assert.Contains(t, tool.InputSchema.Properties, "page") | ||
assert.ElementsMatch(t, tool.InputSchema.Required, []string{"org"}) | ||
// Setup mock repos for success case | ||
mockRepos := []*github.Repository{ | ||
{ | ||
ID: github.Ptr(int64(1001)), | ||
Name: github.Ptr("repo1"), | ||
FullName: github.Ptr("testorg/repo1"), | ||
Description: github.Ptr("Test repo 1"), | ||
HTMLURL: github.Ptr("https://github.com/testorg/repo1"), | ||
Private: github.Ptr(false), | ||
Fork: github.Ptr(false), | ||
}, | ||
{ | ||
ID: github.Ptr(int64(1002)), | ||
Name: github.Ptr("repo2"), | ||
FullName: github.Ptr("testorg/repo2"), | ||
Description: github.Ptr("Test repo 2"), | ||
HTMLURL: github.Ptr("https://github.com/testorg/repo2"), | ||
Private: github.Ptr(true), | ||
Fork: github.Ptr(false), | ||
}, | ||
} | ||
tests := []struct { | ||
name string | ||
mockedClient *http.Client | ||
requestArgs map[string]interface{} | ||
expectError bool | ||
expectedRepos []*github.Repository | ||
expectedErrMsg string | ||
}{ | ||
{ | ||
name: "successful repositories listing", | ||
mockedClient: mock.NewMockedHTTPClient( | ||
mock.WithRequestMatchHandler( | ||
mock.GetOrgsReposByOrg, | ||
expectQueryParams(t, map[string]string{ | ||
"type": "all", | ||
"sort": "created", | ||
"direction": "desc", | ||
"per_page": "30", | ||
"page": "1", | ||
}).andThen( | ||
mockResponse(t, http.StatusOK, mockRepos), | ||
), | ||
), | ||
), | ||
requestArgs: map[string]interface{}{ | ||
"org": "testorg", | ||
"type": "all", | ||
"sort": "created", | ||
"direction": "desc", | ||
"perPage": float64(30), | ||
"page": float64(1), | ||
}, | ||
expectError: false, | ||
expectedRepos: mockRepos, | ||
}, | ||
{ | ||
name: "successful repos listing with defaults", | ||
mockedClient: mock.NewMockedHTTPClient( | ||
mock.WithRequestMatchHandler( | ||
mock.GetOrgsReposByOrg, | ||
expectQueryParams(t, map[string]string{ | ||
"per_page": "30", | ||
"page": "1", | ||
}).andThen( | ||
mockResponse(t, http.StatusOK, mockRepos), | ||
), | ||
), | ||
), | ||
requestArgs: map[string]interface{}{ | ||
"org": "testorg", | ||
// Using defaults for other parameters | ||
}, | ||
expectError: false, | ||
expectedRepos: mockRepos, | ||
}, | ||
{ | ||
name: "custom pagination and filtering", | ||
mockedClient: mock.NewMockedHTTPClient( | ||
mock.WithRequestMatchHandler( | ||
mock.GetOrgsReposByOrg, | ||
expectQueryParams(t, map[string]string{ | ||
"type": "public", | ||
"sort": "updated", | ||
"direction": "asc", | ||
"per_page": "10", | ||
"page": "2", | ||
}).andThen( | ||
mockResponse(t, http.StatusOK, mockRepos), | ||
), | ||
), | ||
), | ||
requestArgs: map[string]interface{}{ | ||
"org": "testorg", | ||
"type": "public", | ||
"sort": "updated", | ||
"direction": "asc", | ||
"perPage": float64(10), | ||
"page": float64(2), | ||
}, | ||
expectError: false, | ||
expectedRepos: mockRepos, | ||
}, | ||
{ | ||
name: "API error response", | ||
mockedClient: mock.NewMockedHTTPClient( | ||
mock.WithRequestMatchHandler( | ||
mock.GetOrgsReposByOrg, | ||
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
w.WriteHeader(http.StatusNotFound) | ||
_, _ = w.Write([]byte(`{"message": "Not Found"}`)) | ||
}), | ||
), | ||
), | ||
requestArgs: map[string]interface{}{ | ||
"org": "nonexistentorg", | ||
}, | ||
expectError: true, | ||
expectedErrMsg: "failed to list repositories", | ||
}, | ||
{ | ||
name: "rate limit exceeded", | ||
mockedClient: mock.NewMockedHTTPClient( | ||
mock.WithRequestMatchHandler( | ||
mock.GetOrgsReposByOrg, | ||
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
w.WriteHeader(http.StatusForbidden) | ||
_, _ = w.Write([]byte(`{"message": "API rate limit exceeded"}`)) | ||
}), | ||
), | ||
), | ||
requestArgs: map[string]interface{}{ | ||
"org": "testorg", | ||
}, | ||
expectError: true, | ||
expectedErrMsg: "failed to list repositories", | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
// Setup client with mock | ||
client := github.NewClient(tc.mockedClient) | ||
_, handler := ListRepositories(stubGetClientFn(client), translations.NullTranslationHelper) | ||
// Create call request | ||
request := createMCPRequest(tc.requestArgs) | ||
// Call handler | ||
result, err := handler(context.Background(), request) | ||
// Verify results | ||
if tc.expectError { | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), tc.expectedErrMsg) | ||
return | ||
} | ||
require.NoError(t, err) | ||
// Parse the result and get the text content if no error | ||
textContent := getTextResult(t, result) | ||
// Unmarshal and verify the result | ||
var returnedRepos []*github.Repository | ||
err = json.Unmarshal([]byte(textContent.Text), &returnedRepos) | ||
require.NoError(t, err) | ||
assert.Len(t, returnedRepos, len(tc.expectedRepos)) | ||
for i, repo := range returnedRepos { | ||
assert.Equal(t, *tc.expectedRepos[i].ID, *repo.ID) | ||
assert.Equal(t, *tc.expectedRepos[i].Name, *repo.Name) | ||
assert.Equal(t, *tc.expectedRepos[i].FullName, *repo.FullName) | ||
assert.Equal(t, *tc.expectedRepos[i].Private, *repo.Private) | ||
assert.Equal(t, *tc.expectedRepos[i].HTMLURL, *repo.HTMLURL) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.