-
Notifications
You must be signed in to change notification settings - Fork 596
feat: implement get_repository_discussions tool with GraphQL support #261
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sridharavinash
wants to merge
3
commits into
main
Choose a base branch
from
discussions-tooling
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.
+311
−3
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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,260 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/github/github-mcp-server/pkg/translations" | ||
"github.com/mark3labs/mcp-go/mcp" | ||
"github.com/mark3labs/mcp-go/server" | ||
"github.com/shurcooL/githubv4" | ||
) | ||
|
||
// Comment represents a comment on a GitHub Discussion | ||
type Comment struct { | ||
ID string `json:"id"` | ||
Body string `json:"body"` | ||
CreatedAt string `json:"createdAt"` | ||
Author string `json:"author"` | ||
} | ||
|
||
// Discussion represents a GitHub Discussion with its essential fields | ||
type Discussion struct { | ||
ID string `json:"id"` | ||
Number int `json:"number"` | ||
Title string `json:"title"` | ||
Body string `json:"body"` | ||
CreatedAt string `json:"createdAt"` | ||
UpdatedAt string `json:"updatedAt"` | ||
URL string `json:"url"` | ||
Category string `json:"category"` | ||
Author string `json:"author"` | ||
Locked bool `json:"locked"` | ||
UpvoteCount int `json:"upvoteCount"` | ||
CommentCount int `json:"commentCount"` | ||
Comments []Comment `json:"comments,omitempty"` | ||
} | ||
|
||
// GetRepositoryDiscussions creates a tool to fetch discussions from a specific repository. | ||
func GetRepositoryDiscussions(getGraphQLClient GetGraphQLClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||
return mcp.NewTool("get_repository_discussions", | ||
mcp.WithDescription(t("TOOL_GET_REPOSITORY_DISCUSSIONS_DESCRIPTION", "Get discussions from a specific GitHub repository")), | ||
mcp.WithString("owner", | ||
mcp.Required(), | ||
mcp.Description("Repository owner"), | ||
), | ||
mcp.WithString("repo", | ||
mcp.Required(), | ||
mcp.Description("Repository name"), | ||
), | ||
), | ||
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
owner, err := requiredParam[string](request, "owner") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
|
||
repo, err := requiredParam[string](request, "repo") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
|
||
categoryId, err := OptionalParam[string](request, "categoryId") | ||
Check failure on line 63 in pkg/github/discussions.go
|
||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
|
||
pagination, err := OptionalPaginationParams(request) | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
|
||
// Get GraphQL client | ||
client, err := getGraphQLClient(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get GitHub GraphQL client: %w", err) | ||
} | ||
|
||
// Define GraphQL query variables | ||
variables := map[string]interface{}{ | ||
"owner": githubv4.String(owner), | ||
"name": githubv4.String(repo), | ||
"first": githubv4.Int(pagination.perPage), | ||
Check failure on line 83 in pkg/github/discussions.go
|
||
"after": (*githubv4.String)(nil), // For pagination - null means first page | ||
} | ||
|
||
// For pagination beyond the first page | ||
// TODO Fix | ||
if pagination.page > 1 { | ||
// We'd need an actual cursor here, but for simplicity we'll compute a rough offset | ||
// In real implementation, you should store and use actual cursor values | ||
cursorStr := githubv4.String(fmt.Sprintf("%d", (pagination.page-1)*pagination.perPage)) | ||
variables["after"] = &cursorStr | ||
} | ||
|
||
// Define the GraphQL query structure and query string based on whether categoryId is provided | ||
var query struct { | ||
Repository struct { | ||
Discussions struct { | ||
TotalCount int | ||
Nodes []struct { | ||
ID githubv4.ID | ||
Number int | ||
Title string | ||
Body string | ||
CreatedAt githubv4.DateTime | ||
UpdatedAt githubv4.DateTime | ||
URL githubv4.URI | ||
Category struct { | ||
Name string | ||
} | ||
Author struct { | ||
Login string | ||
} | ||
Locked bool | ||
UpvoteCount int | ||
Comments struct { | ||
TotalCount int | ||
Nodes []struct { | ||
ID githubv4.ID | ||
Body string | ||
CreatedAt githubv4.DateTime | ||
Author struct { | ||
Login string | ||
} | ||
} | ||
} `graphql:"comments(first: 10)"` | ||
} | ||
PageInfo struct { | ||
EndCursor githubv4.String | ||
HasNextPage bool | ||
} | ||
} `graphql:"discussions(first: $first, after: $after)"` | ||
} `graphql:"repository(owner: $owner, name: $name)"` | ||
} | ||
|
||
// Define a type for the Discussions GraphQL query to avoid duplication | ||
type discussionQueryType struct { | ||
TotalCount int | ||
Nodes []struct { | ||
ID githubv4.ID | ||
Number int | ||
Title string | ||
Body string | ||
CreatedAt githubv4.DateTime | ||
UpdatedAt githubv4.DateTime | ||
URL githubv4.URI | ||
Category struct { | ||
Name string | ||
} | ||
Author struct { | ||
Login string | ||
} | ||
Locked bool | ||
UpvoteCount int | ||
Comments struct { | ||
TotalCount int | ||
Nodes []struct { | ||
ID githubv4.ID | ||
Body string | ||
CreatedAt githubv4.DateTime | ||
Author struct { | ||
Login string | ||
} | ||
} | ||
} `graphql:"comments(first: 10)"` | ||
} | ||
PageInfo struct { | ||
EndCursor githubv4.String | ||
HasNextPage bool | ||
} | ||
} | ||
|
||
// Add categoryId to query if it was provided | ||
if categoryId != "" { | ||
variables["categoryId"] = githubv4.ID(categoryId) | ||
// Use a separate query structure that includes the categoryId parameter | ||
var queryWithCategory struct { | ||
Repository struct { | ||
Discussions discussionQueryType `graphql:"discussions(first: $first, after: $after, categoryId: $categoryId)"` | ||
} `graphql:"repository(owner: $owner, name: $name)"` | ||
} | ||
|
||
// Execute the query with categoryId | ||
err = client.Query(ctx, &queryWithCategory, variables) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to query discussions with category: %w", err) | ||
} | ||
|
||
// Copy the results to our main query structure | ||
query.Repository.Discussions = queryWithCategory.Repository.Discussions | ||
} else { | ||
// Execute the original query without categoryId | ||
err = client.Query(ctx, &query, variables) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to query discussions: %w", err) | ||
} | ||
} | ||
|
||
// Execute the GraphQL query | ||
err = client.Query(ctx, &query, variables) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to query discussions: %w", err) | ||
} | ||
|
||
// Convert the GraphQL response to our Discussion type | ||
discussions := make([]Discussion, 0, len(query.Repository.Discussions.Nodes)) | ||
for _, node := range query.Repository.Discussions.Nodes { | ||
// Process comments for this discussion | ||
comments := make([]Comment, 0, len(node.Comments.Nodes)) | ||
for _, commentNode := range node.Comments.Nodes { | ||
comment := Comment{ | ||
ID: fmt.Sprintf("%v", commentNode.ID), | ||
Body: commentNode.Body, | ||
CreatedAt: commentNode.CreatedAt.String(), | ||
Author: commentNode.Author.Login, | ||
} | ||
comments = append(comments, comment) | ||
} | ||
|
||
discussion := Discussion{ | ||
ID: fmt.Sprintf("%v", node.ID), | ||
Number: node.Number, | ||
Title: node.Title, | ||
Body: node.Body, | ||
CreatedAt: node.CreatedAt.String(), | ||
UpdatedAt: node.UpdatedAt.String(), | ||
URL: node.URL.String(), | ||
Category: node.Category.Name, | ||
Author: node.Author.Login, | ||
Locked: node.Locked, | ||
UpvoteCount: node.UpvoteCount, | ||
CommentCount: node.Comments.TotalCount, | ||
Comments: comments, | ||
} | ||
discussions = append(discussions, discussion) | ||
} | ||
|
||
// Create the response | ||
result := struct { | ||
TotalCount int `json:"totalCount"` | ||
Discussions []Discussion `json:"discussions"` | ||
HasNextPage bool `json:"hasNextPage"` | ||
EndCursor string `json:"endCursor"` | ||
}{ | ||
TotalCount: query.Repository.Discussions.TotalCount, | ||
Discussions: discussions, | ||
HasNextPage: query.Repository.Discussions.PageInfo.HasNextPage, | ||
EndCursor: string(query.Repository.Discussions.PageInfo.EndCursor), | ||
} | ||
|
||
// Marshal the result to JSON | ||
r, err := json.Marshal(result) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal discussions result: %w", err) | ||
} | ||
|
||
return mcp.NewToolResultText(string(r)), nil | ||
} | ||
} |
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really glad we would be able to also have access to the comments