Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
28 changes: 14 additions & 14 deletions pkg/github/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func ListWorkflows(getClient GetClientFn, t translations.TranslationHelperFunc)

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

// Set up list options
Expand Down Expand Up @@ -208,7 +208,7 @@ func ListWorkflowRuns(getClient GetClientFn, t translations.TranslationHelperFun

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

// Set up list options
Expand Down Expand Up @@ -294,7 +294,7 @@ func RunWorkflow(getClient GetClientFn, t translations.TranslationHelperFunc) (t

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

event := github.CreateWorkflowDispatchEventRequest{
Expand Down Expand Up @@ -375,7 +375,7 @@ func GetWorkflowRun(getClient GetClientFn, t translations.TranslationHelperFunc)

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

workflowRun, resp, err := client.Actions.GetWorkflowRunByID(ctx, owner, repo, runID)
Expand Down Expand Up @@ -431,7 +431,7 @@ func GetWorkflowRunLogs(getClient GetClientFn, t translations.TranslationHelperF

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

// Get the download URL for the logs
Expand Down Expand Up @@ -523,7 +523,7 @@ func ListWorkflowJobs(getClient GetClientFn, t translations.TranslationHelperFun

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

// Set up list options
Expand Down Expand Up @@ -627,7 +627,7 @@ func GetJobLogs(getClient GetClientFn, t translations.TranslationHelperFunc) (to

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

// Validate parameters
Expand Down Expand Up @@ -851,7 +851,7 @@ func RerunWorkflowRun(getClient GetClientFn, t translations.TranslationHelperFun

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

resp, err := client.Actions.RerunWorkflowByID(ctx, owner, repo, runID)
Expand Down Expand Up @@ -914,7 +914,7 @@ func RerunFailedJobs(getClient GetClientFn, t translations.TranslationHelperFunc

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

resp, err := client.Actions.RerunFailedJobsByID(ctx, owner, repo, runID)
Expand Down Expand Up @@ -977,7 +977,7 @@ func CancelWorkflowRun(getClient GetClientFn, t translations.TranslationHelperFu

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

resp, err := client.Actions.CancelWorkflowRunByID(ctx, owner, repo, runID)
Expand Down Expand Up @@ -1056,7 +1056,7 @@ func ListWorkflowRunArtifacts(getClient GetClientFn, t translations.TranslationH

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

// Set up list options
Expand Down Expand Up @@ -1118,7 +1118,7 @@ func DownloadWorkflowRunArtifact(getClient GetClientFn, t translations.Translati

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

// Get the download URL for the artifact
Expand Down Expand Up @@ -1184,7 +1184,7 @@ func DeleteWorkflowRunLogs(getClient GetClientFn, t translations.TranslationHelp

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

resp, err := client.Actions.DeleteWorkflowRunLogs(ctx, owner, repo, runID)
Expand Down Expand Up @@ -1247,7 +1247,7 @@ func GetWorkflowRunUsage(getClient GetClientFn, t translations.TranslationHelper

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

usage, resp, err := client.Actions.GetWorkflowRunUsageByID(ctx, owner, repo, runID)
Expand Down
4 changes: 2 additions & 2 deletions pkg/github/code_scanning.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func GetCodeScanningAlert(getClient GetClientFn, t translations.TranslationHelpe

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

alert, resp, err := client.CodeScanning.GetAlert(ctx, owner, repo, int64(alertNumber))
Expand Down Expand Up @@ -139,7 +139,7 @@ func ListCodeScanningAlerts(getClient GetClientFn, t translations.TranslationHel

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}
alerts, resp, err := client.CodeScanning.ListAlertsForRepo(ctx, owner, repo, &github.AlertListOptions{Ref: ref, State: state, Severity: severity, ToolName: toolName})
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions pkg/github/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func GetIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (tool

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}
issue, resp, err := client.Issues.Get(ctx, owner, repo, issueNumber)
if err != nil {
Expand Down Expand Up @@ -128,7 +128,7 @@ func AddIssueComment(getClient GetClientFn, t translations.TranslationHelperFunc

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}
createdComment, resp, err := client.Issues.CreateComment(ctx, owner, repo, issueNumber, comment)
if err != nil {
Expand Down Expand Up @@ -295,7 +295,7 @@ func CreateIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (t

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}
issue, resp, err := client.Issues.Create(ctx, owner, repo, issueRequest)
if err != nil {
Expand Down Expand Up @@ -417,7 +417,7 @@ func ListIssues(getClient GetClientFn, t translations.TranslationHelperFunc) (to

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}
issues, resp, err := client.Issues.ListByRepo(ctx, owner, repo, opts)
if err != nil {
Expand Down Expand Up @@ -563,7 +563,7 @@ func UpdateIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (t

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}
updatedIssue, resp, err := client.Issues.Edit(ctx, owner, repo, issueNumber, issueRequest)
if err != nil {
Expand Down Expand Up @@ -646,7 +646,7 @@ func GetIssueComments(getClient GetClientFn, t translations.TranslationHelperFun

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}
comments, resp, err := client.Issues.ListComments(ctx, owner, repo, issueNumber, opts)
if err != nil {
Expand Down Expand Up @@ -744,7 +744,7 @@ func AssignCopilotToIssue(getGQLClient GetGQLClientFn, t translations.Translatio

client, err := getGQLClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

// Firstly, we try to find the copilot bot in the suggested actors for the repository.
Expand Down
52 changes: 26 additions & 26 deletions pkg/github/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func ListNotifications(getClient GetClientFn, t translations.TranslationHelperFu
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

filter, err := OptionalParam[string](request, "filter")
Expand Down Expand Up @@ -162,7 +162,7 @@ func DismissNotification(getclient GetClientFn, t translations.TranslationHelper
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
client, err := getclient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

threadID, err := RequiredParam[string](request, "threadID")
Expand Down Expand Up @@ -233,7 +233,7 @@ func MarkAllNotificationsRead(getClient GetClientFn, t translations.TranslationH
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

lastReadAt, err := OptionalParam[string](request, "lastReadAt")
Expand Down Expand Up @@ -307,7 +307,7 @@ func GetNotificationDetails(getClient GetClientFn, t translations.TranslationHel
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

notificationID, err := RequiredParam[string](request, "notificationID")
Expand Down Expand Up @@ -350,6 +350,22 @@ const (
)

// ManageNotificationSubscription creates a tool to manage a notification subscription (ignore, watch, delete)
func executeNotificationSubscriptionAction(ctx context.Context, client *github.Client, notificationID, action string) (result any, resp *github.Response, err error) {
switch action {
case NotificationActionIgnore:
sub := &github.Subscription{Ignored: ToBoolPtr(true)}
return client.Activity.SetThreadSubscription(ctx, notificationID, sub)
case NotificationActionWatch:
sub := &github.Subscription{Ignored: ToBoolPtr(false), Subscribed: ToBoolPtr(true)}
return client.Activity.SetThreadSubscription(ctx, notificationID, sub)
case NotificationActionDelete:
resp, err := client.Activity.DeleteThreadSubscription(ctx, notificationID)
return nil, resp, err
default:
return nil, nil, fmt.Errorf("invalid action: %s", action)
}
}

func ManageNotificationSubscription(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("manage_notification_subscription",
mcp.WithDescription(t("TOOL_MANAGE_NOTIFICATION_SUBSCRIPTION_DESCRIPTION", "Manage a notification subscription: ignore, watch, or delete a notification thread subscription.")),
Expand All @@ -370,7 +386,7 @@ func ManageNotificationSubscription(getClient GetClientFn, t translations.Transl
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

notificationID, err := RequiredParam[string](request, "notificationID")
Expand All @@ -382,26 +398,11 @@ func ManageNotificationSubscription(getClient GetClientFn, t translations.Transl
return mcp.NewToolResultError(err.Error()), nil
}

var (
resp *github.Response
result any
apiErr error
)

switch action {
case NotificationActionIgnore:
sub := &github.Subscription{Ignored: ToBoolPtr(true)}
result, resp, apiErr = client.Activity.SetThreadSubscription(ctx, notificationID, sub)
case NotificationActionWatch:
sub := &github.Subscription{Ignored: ToBoolPtr(false), Subscribed: ToBoolPtr(true)}
result, resp, apiErr = client.Activity.SetThreadSubscription(ctx, notificationID, sub)
case NotificationActionDelete:
resp, apiErr = client.Activity.DeleteThreadSubscription(ctx, notificationID)
default:
return mcp.NewToolResultError("Invalid action. Must be one of: ignore, watch, delete."), nil
}

result, resp, apiErr := executeNotificationSubscriptionAction(ctx, client, notificationID, action)
if apiErr != nil {
if apiErr.Error() == fmt.Sprintf("invalid action: %s", action) {
return mcp.NewToolResultError("Invalid action. Must be one of: ignore, watch, delete."), nil
}
return ghErrors.NewGitHubAPIErrorResponse(ctx,
fmt.Sprintf("failed to %s notification subscription", action),
resp,
Expand All @@ -416,7 +417,6 @@ func ManageNotificationSubscription(getClient GetClientFn, t translations.Transl
}

if action == NotificationActionDelete {
// Special case for delete as there is no response body
return mcp.NewToolResultText("Notification subscription deleted"), nil
}

Expand Down Expand Up @@ -459,7 +459,7 @@ func ManageRepositoryNotificationSubscription(getClient GetClientFn, t translati
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
return nil, fmt.Errorf(ErrFailedToGetGitHubClient, err)
}

owner, err := RequiredParam[string](request, "owner")
Expand Down
Loading