Skip to content

add support for list_issues #26

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

Merged
merged 4 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,34 @@ and set it as the GITHUB_PERSONAL_ACCESS_TOKEN environment variable.
- `repo`: Repository name (string, required)
- `issue_number`: Issue number (number, required)

- **create_issue** - Create a new issue in a GitHub repository

- `owner`: Repository owner (string, required)
- `repo`: Repository name (string, required)
- `title`: Issue title (string, required)
- `body`: Issue body content (string, optional)
- `assignees`: Comma-separated list of usernames to assign to this issue (string, optional)
- `labels`: Comma-separated list of labels to apply to this issue (string, optional)

- **add_issue_comment** - Add a comment to an issue

- `owner`: Repository owner (string, required)
- `repo`: Repository name (string, required)
- `issue_number`: Issue number (number, required)
- `body`: Comment text (string, required)

- **list_issues** - List and filter repository issues

- `owner`: Repository owner (string, required)
- `repo`: Repository name (string, required)
- `state`: Filter by state ('open', 'closed', 'all') (string, optional)
- `labels`: Comma-separated list of labels to filter by (string, optional)
- `sort`: Sort by ('created', 'updated', 'comments') (string, optional)
- `direction`: Sort direction ('asc', 'desc') (string, optional)
- `since`: Filter by date (ISO 8601 timestamp) (string, optional)
- `page`: Page number (number, optional)
- `per_page`: Results per page (number, optional)

- **search_issues** - Search for issues and pull requests
- `query`: Search query (string, required)
- `sort`: Sort field (string, optional)
Expand Down Expand Up @@ -313,16 +334,14 @@ Lots of things!
Missing tools:

- push_files (files array)
- create_issue (assignees and labels arrays)
- list_issues (labels array)
- update_issue (labels and assignees arrays)
- create_pull_request_review (comments array)

Testing

- Unit tests
- Integration tests
- Blackbox testing: ideally comparing output to Anthromorphic's server to make sure that this is a fully compatible drop-in replacement.
- Blackbox testing: ideally comparing output to Anthropic's server to make sure that this is a fully compatible drop-in replacement.

And some other stuff:

Expand Down
201 changes: 201 additions & 0 deletions pkg/github/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"time"

"github.com/google/go-github/v69/github"
"github.com/mark3labs/mcp-go/mcp"
Expand Down Expand Up @@ -182,3 +183,203 @@ func searchIssues(client *github.Client) (tool mcp.Tool, handler server.ToolHand
return mcp.NewToolResultText(string(r)), nil
}
}

// createIssue creates a tool to create a new issue in a GitHub repository.
func createIssue(client *github.Client) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("create_issue",
mcp.WithDescription("Create a new issue in a GitHub repository"),
mcp.WithString("owner",
mcp.Required(),
mcp.Description("Repository owner"),
),
mcp.WithString("repo",
mcp.Required(),
mcp.Description("Repository name"),
),
mcp.WithString("title",
mcp.Required(),
mcp.Description("Issue title"),
),
mcp.WithString("body",
mcp.Description("Issue body content"),
),
mcp.WithString("assignees",
mcp.Description("Comma-separate list of usernames to assign to this issue"),
),
mcp.WithString("labels",
mcp.Description("Comma-separate list of labels to apply to this issue"),
),
),
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
owner := request.Params.Arguments["owner"].(string)
repo := request.Params.Arguments["repo"].(string)
title := request.Params.Arguments["title"].(string)

// Optional parameters
var body string
if b, ok := request.Params.Arguments["body"].(string); ok {
body = b
}

// Parse assignees if present
assignees := []string{} // default to empty slice, can't be nil
if a, ok := request.Params.Arguments["assignees"].(string); ok && a != "" {
assignees = parseCommaSeparatedList(a)
}

// Parse labels if present
labels := []string{} // default to empty slice, can't be nil
if l, ok := request.Params.Arguments["labels"].(string); ok && l != "" {
labels = parseCommaSeparatedList(l)
}

// Create the issue request
issueRequest := &github.IssueRequest{
Title: github.Ptr(title),
Body: github.Ptr(body),
Assignees: &assignees,
Labels: &labels,
}

issue, resp, err := client.Issues.Create(ctx, owner, repo, issueRequest)
if err != nil {
return nil, fmt.Errorf("failed to create issue: %w", err)
}
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusCreated {
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 create issue: %s", string(body))), nil
}

r, err := json.Marshal(issue)
if err != nil {
return nil, fmt.Errorf("failed to marshal response: %w", err)
}

return mcp.NewToolResultText(string(r)), nil
}
}

// listIssues creates a tool to list and filter repository issues
func listIssues(client *github.Client) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("list_issues",
mcp.WithDescription("List issues in a GitHub repository with filtering options"),
mcp.WithString("owner",
mcp.Required(),
mcp.Description("Repository owner"),
),
mcp.WithString("repo",
mcp.Required(),
mcp.Description("Repository name"),
),
mcp.WithString("state",
mcp.Description("Filter by state ('open', 'closed', 'all')"),
),
mcp.WithString("labels",
mcp.Description("Comma-separated list of labels to filter by"),
),
mcp.WithString("sort",
mcp.Description("Sort by ('created', 'updated', 'comments')"),
),
mcp.WithString("direction",
mcp.Description("Sort direction ('asc', 'desc')"),
),
mcp.WithString("since",
mcp.Description("Filter by date (ISO 8601 timestamp)"),
),
mcp.WithNumber("page",
mcp.Description("Page number"),
),
mcp.WithNumber("per_page",
mcp.Description("Results per page"),
),
),
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
owner := request.Params.Arguments["owner"].(string)
repo := request.Params.Arguments["repo"].(string)

opts := &github.IssueListByRepoOptions{}

// Set optional parameters if provided
if state, ok := request.Params.Arguments["state"].(string); ok && state != "" {
opts.State = state
}

if labels, ok := request.Params.Arguments["labels"].(string); ok && labels != "" {
opts.Labels = parseCommaSeparatedList(labels)
}

if sort, ok := request.Params.Arguments["sort"].(string); ok && sort != "" {
opts.Sort = sort
}

if direction, ok := request.Params.Arguments["direction"].(string); ok && direction != "" {
opts.Direction = direction
}

if since, ok := request.Params.Arguments["since"].(string); ok && since != "" {
timestamp, err := parseISOTimestamp(since)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("failed to list issues: %s", err.Error())), nil
}
opts.Since = timestamp
}

if page, ok := request.Params.Arguments["page"].(float64); ok {
opts.Page = int(page)
}

if perPage, ok := request.Params.Arguments["per_page"].(float64); ok {
opts.PerPage = int(perPage)
}

issues, resp, err := client.Issues.ListByRepo(ctx, owner, repo, opts)
if err != nil {
return nil, fmt.Errorf("failed to list issues: %w", err)
}
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
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 issues: %s", string(body))), nil
}

r, err := json.Marshal(issues)
if err != nil {
return nil, fmt.Errorf("failed to marshal issues: %w", err)
}

return mcp.NewToolResultText(string(r)), nil
}
}

// parseISOTimestamp parses an ISO 8601 timestamp string into a time.Time object.
// Returns the parsed time or an error if parsing fails.
// Example formats supported: "2023-01-15T14:30:00Z", "2023-01-15"
func parseISOTimestamp(timestamp string) (time.Time, error) {
if timestamp == "" {
return time.Time{}, fmt.Errorf("empty timestamp")
}

// Try RFC3339 format (standard ISO 8601 with time)
t, err := time.Parse(time.RFC3339, timestamp)
if err == nil {
return t, nil
}

// Try simple date format (YYYY-MM-DD)
t, err = time.Parse("2006-01-02", timestamp)
if err == nil {
return t, nil
}

// Return error with supported formats
return time.Time{}, fmt.Errorf("invalid ISO 8601 timestamp: %s (supported formats: YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DD)", timestamp)
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy