Skip to content

Commit e4d90e9

Browse files
authored
Merge pull request #271 from buildkite/refactor/annotation-scope-validation
refactor: simplify annotation scope validation and improve schema descriptions
2 parents b113bc6 + 7130e16 commit e4d90e9

1 file changed

Lines changed: 15 additions & 18 deletions

File tree

pkg/buildkite/annotations.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package buildkite
22

33
import (
44
"context"
5+
"errors"
56

67
"github.com/buildkite/buildkite-mcp-server/pkg/trace"
78
"github.com/buildkite/buildkite-mcp-server/pkg/utils"
@@ -27,7 +28,7 @@ type ListAnnotationsArgs struct {
2728
OrgSlug string `json:"org_slug"`
2829
PipelineSlug string `json:"pipeline_slug"`
2930
BuildNumber string `json:"build_number"`
30-
Scope string `json:"scope,omitempty" jsonschema:"Annotation scope: build or job (defaults to build)"`
31+
Scope string `json:"scope,omitempty" jsonschema:"Annotation scope: 'build' (default) or 'job'. When 'job', job_id is required."`
3132
JobID string `json:"job_id,omitempty" jsonschema:"Job ID required when scope is job"`
3233
Page int `json:"page,omitempty" jsonschema:"Page number for pagination (min 1)"`
3334
PerPage int `json:"per_page,omitempty" jsonschema:"Results per page for pagination (min 1\\, max 100)"`
@@ -37,7 +38,7 @@ type CreateAnnotationArgs struct {
3738
OrgSlug string `json:"org_slug"`
3839
PipelineSlug string `json:"pipeline_slug"`
3940
BuildNumber string `json:"build_number"`
40-
Scope string `json:"scope,omitempty" jsonschema:"Annotation scope: build or job (defaults to build)"`
41+
Scope string `json:"scope,omitempty" jsonschema:"Annotation scope: 'build' (default) or 'job'. When 'job', job_id is required."`
4142
JobID string `json:"job_id,omitempty" jsonschema:"Job ID required when scope is job"`
4243
Body string `json:"body" jsonschema:"The annotation body as HTML or Markdown"`
4344
Style string `json:"style,omitempty" jsonschema:"Optional annotation style: success, info, warning, or error"`
@@ -46,21 +47,17 @@ type CreateAnnotationArgs struct {
4647
Append bool `json:"append,omitempty" jsonschema:"Append the body to an existing annotation with the same context"`
4748
}
4849

49-
func normalizeAnnotationScope(scope, jobID string) (string, string) {
50-
if scope == "" {
51-
scope = annotationScopeBuild
52-
}
53-
50+
func normalizeAnnotationScope(scope, jobID string) (string, error) {
5451
switch scope {
55-
case annotationScopeBuild:
56-
return scope, ""
52+
case "", annotationScopeBuild:
53+
return annotationScopeBuild, nil
5754
case annotationScopeJob:
5855
if jobID == "" {
59-
return "", "job_id is required when scope is 'job'"
56+
return "", errors.New("job_id is required when scope is 'job'")
6057
}
61-
return scope, ""
58+
return annotationScopeJob, nil
6259
default:
63-
return "", "scope must be 'build' or 'job'"
60+
return "", errors.New("scope must be 'build' or 'job'")
6461
}
6562
}
6663

@@ -77,9 +74,9 @@ func ListAnnotations() (mcp.Tool, mcp.ToolHandlerFor[ListAnnotationsArgs, any],
7774
ctx, span := trace.Start(ctx, "buildkite.ListAnnotations")
7875
defer span.End()
7976

80-
scope, validationErr := normalizeAnnotationScope(args.Scope, args.JobID)
81-
if validationErr != "" {
82-
return utils.NewToolResultError(validationErr), nil, nil
77+
scope, scopeErr := normalizeAnnotationScope(args.Scope, args.JobID)
78+
if scopeErr != nil {
79+
return utils.NewToolResultError(scopeErr.Error()), nil, nil
8380
}
8481

8582
paginationParams := paginationFromArgs(args.Page, args.PerPage)
@@ -142,9 +139,9 @@ func CreateAnnotation() (mcp.Tool, mcp.ToolHandlerFor[CreateAnnotationArgs, any]
142139
ctx, span := trace.Start(ctx, "buildkite.CreateAnnotation")
143140
defer span.End()
144141

145-
scope, validationErr := normalizeAnnotationScope(args.Scope, args.JobID)
146-
if validationErr != "" {
147-
return utils.NewToolResultError(validationErr), nil, nil
142+
scope, scopeErr := normalizeAnnotationScope(args.Scope, args.JobID)
143+
if scopeErr != nil {
144+
return utils.NewToolResultError(scopeErr.Error()), nil, nil
148145
}
149146

150147
span.SetAttributes(

0 commit comments

Comments
 (0)