Skip to content

Commit 7357483

Browse files
committed
Handle nils
1 parent 39fecdd commit 7357483

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

pkg/buildkite/pipelines.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ type CreatePipelineArgs struct {
283283
SkipQueuedBranchBuilds bool `json:"skip_queued_branch_builds"`
284284
CancelRunningBranchBuilds bool `json:"cancel_running_branch_builds"`
285285
Tags []string `json:"tags"`
286-
CreateWebhook bool `json:"create_webhook"`
286+
CreateWebhook *bool `json:"create_webhook"`
287287
}
288288

289289
func CreatePipeline(client PipelinesClient) (tool mcp.Tool, handler mcp.TypedToolHandlerFunc[CreatePipelineArgs], scopes []string) {
@@ -389,7 +389,13 @@ func CreatePipeline(client PipelinesClient) (tool mcp.Tool, handler mcp.TypedToo
389389
return mcp.NewToolResultError(err.Error()), nil
390390
}
391391

392-
if args.CreateWebhook {
392+
// create webhooks by default to align with the behavior in the dashboard
393+
createWebhook := true
394+
if args.CreateWebhook != nil {
395+
createWebhook = *args.CreateWebhook
396+
}
397+
398+
if createWebhook {
393399
_, err := client.AddWebhook(ctx, args.OrgSlug, pipeline.Slug)
394400
if err != nil {
395401
result := map[string]any{

pkg/buildkite/pipelines_test.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ steps:
147147
`
148148

149149
ctx := context.Background()
150+
webhookCalled := false
150151
client := &MockPipelinesClient{
151152
CreateFunc: func(ctx context.Context, org string, p buildkite.CreatePipeline) (buildkite.Pipeline, *buildkite.Response, error) {
152153

@@ -170,6 +171,16 @@ steps:
170171
},
171172
}, nil
172173
},
174+
AddWebhookFunc: func(ctx context.Context, org string, slug string) (*buildkite.Response, error) {
175+
assert.Equal("org", org)
176+
assert.Equal("test-pipeline", slug)
177+
webhookCalled = true
178+
return &buildkite.Response{
179+
Response: &http.Response{
180+
StatusCode: 201,
181+
},
182+
}, nil
183+
},
173184
}
174185

175186
tool, handler, _ := CreatePipeline(client)
@@ -186,12 +197,16 @@ steps:
186197
Description: "A test pipeline",
187198
Configuration: testPipelineDefinition,
188199
Tags: []string{"tag1", "tag2"},
200+
CreateWebhook: nil,
189201
}
190202

191203
result, err := handler(ctx, request, args)
192204
assert.NoError(err)
205+
assert.True(webhookCalled, "AddWebhook should have been called when CreateWebhook is nil")
206+
193207
textContent := getTextResult(t, result)
194-
assert.Equal(`{"id":"123","name":"Test Pipeline","slug":"test-pipeline","created_at":"0001-01-01T00:00:00Z","skip_queued_branch_builds":false,"cancel_running_branch_builds":false,"cluster_id":"cluster-123","tags":["tag1","tag2"],"provider":{"id":"","webhook_url":"","settings":null}}`, textContent.Text)
208+
assert.Contains(textContent.Text, `"webhook":{"created":true,"note":"Pipeline and webhook created successfully."}`)
209+
assert.Contains(textContent.Text, `"pipeline":{"id":"123","name":"Test Pipeline","slug":"test-pipeline"`)
195210
}
196211

197212
func TestCreatePipelineWithWebhook(t *testing.T) {
@@ -210,6 +225,7 @@ steps:
210225

211226
ctx := context.Background()
212227
webhookCalled := false
228+
createWebhook := true
213229
client := &MockPipelinesClient{
214230
CreateFunc: func(ctx context.Context, org string, p buildkite.CreatePipeline) (buildkite.Pipeline, *buildkite.Response, error) {
215231

@@ -262,7 +278,7 @@ steps:
262278
Description: "A test pipeline",
263279
Configuration: testPipelineDefinition,
264280
Tags: []string{"tag1", "tag2"},
265-
CreateWebhook: true,
281+
CreateWebhook: &createWebhook,
266282
}
267283

268284
result, err := handler(ctx, request, args)
@@ -290,6 +306,7 @@ steps:
290306

291307
ctx := context.Background()
292308
webhookCalled := false
309+
createWebhook := true
293310
client := &MockPipelinesClient{
294311
CreateFunc: func(ctx context.Context, org string, p buildkite.CreatePipeline) (buildkite.Pipeline, *buildkite.Response, error) {
295312

@@ -333,7 +350,7 @@ steps:
333350
Description: "A test pipeline",
334351
Configuration: testPipelineDefinition,
335352
Tags: []string{"tag1", "tag2"},
336-
CreateWebhook: true,
353+
CreateWebhook: &createWebhook,
337354
}
338355

339356
result, err := handler(ctx, request, args)

0 commit comments

Comments
 (0)