forgejo-mcp/operation/user/user.go
Christoph Görn dd9116fbca
fix: 🚨 clear golangci-lint v2 backlog (closes bead forgejo-mcp-hc9)
- errorlint: fmt.Errorf %v→%w across all handlers (errors now wrap the
  underlying SDK/HTTP error for errors.Is/As); errors.Is for
  context.Canceled in cmd
- staticcheck quickfixes: Fprintf over WriteString(Sprintf), tagged
  switches, omit inferred 'any'; QF1001 resolved by hoisting hex checks
  into isHex predicates (normalizeColor, validateSHA)
- govet: reflect.Ptr → reflect.Pointer in pkg/ptr
- errcheck: explicit '_ =' for fs.Parse (ExitOnError) and deferred
  zap Sync
- repair four test assertions the autofix broke (it dropped the
  're.Code != X' half of combined type-assert checks, inverting the
  test); all errors.As sites use 'var re *resource.ResourceError'

golangci-lint: 0 issues. staticcheck, vet, tests: clean.
2026-06-11 00:03:36 +02:00

65 lines
1.8 KiB
Go

package user
import (
"context"
"fmt"
"time"
"codeberg.org/goern/forgejo-mcp/v2/pkg/forgejo"
"codeberg.org/goern/forgejo-mcp/v2/pkg/log"
"codeberg.org/goern/forgejo-mcp/v2/pkg/to"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
const (
GetMyUserInfoToolName = "get_my_user_info"
)
var (
GetMyUserInfoTool = mcp.NewTool(
GetMyUserInfoToolName,
mcp.WithDescription("Get user info"),
)
)
func RegisterTool(s *server.MCPServer) {
s.AddTool(GetMyUserInfoTool, GetUserInfoFn)
s.AddTool(CheckNotificationsTool, CheckNotificationsFn)
s.AddTool(GetNotificationThreadTool, GetNotificationThreadFn)
s.AddTool(MarkNotificationReadTool, MarkNotificationReadFn)
s.AddTool(MarkAllNotificationsReadTool, MarkAllNotificationsReadFn)
s.AddTool(ListRepoNotificationsTool, ListRepoNotificationsFn)
s.AddTool(MarkRepoNotificationsReadTool, MarkRepoNotificationsReadFn)
}
func GetUserInfoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
ctx, _ = log.WithMCPContext(ctx, GetMyUserInfoToolName)
start := time.Now()
log.LogMCPToolStart(ctx, GetMyUserInfoToolName, map[string]interface{}{})
client, err := forgejo.Client(ctx)
if err != nil {
return to.ErrorResult(err)
}
user, resp, err := client.GetMyUserInfo()
duration := time.Since(start)
// Log API call details — guard resp against nil (e.g. network error)
statusCode := 0
if resp != nil {
statusCode = resp.StatusCode
}
forgejo.LogAPICall(ctx, "GET", "/user", duration, statusCode, err)
if err != nil {
log.LogMCPToolError(ctx, GetMyUserInfoToolName, duration, err)
return to.ErrorResult(fmt.Errorf("get user info err: %w", err))
}
log.LogMCPToolComplete(ctx, GetMyUserInfoToolName, duration, fmt.Sprintf("Retrieved info for user: %s", user.UserName))
return to.TextResult(user)
}