1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
package api
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/thepeterstone/claudomator/internal/config"
)
// signBody computes the HMAC-SHA256 signature for a webhook payload.
func signBody(t *testing.T, secret, body string) string {
t.Helper()
mac := hmac.New(sha256.New, []byte(secret))
mac.Write([]byte(body))
return "sha256=" + hex.EncodeToString(mac.Sum(nil))
}
func webhookPost(t *testing.T, srv *Server, eventType, payload, sig string) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequest("POST", "/api/webhooks/github", bytes.NewBufferString(payload))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-GitHub-Event", eventType)
if sig != "" {
req.Header.Set("X-Hub-Signature-256", sig)
}
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
return w
}
const checkRunFailurePayload = `{
"action": "completed",
"check_run": {
"name": "CI Build",
"conclusion": "failure",
"html_url": "https://github.com/owner/myrepo/runs/123",
"head_sha": "abc123def",
"check_suite": {
"head_branch": "main"
}
},
"repository": {
"name": "myrepo",
"full_name": "owner/myrepo"
}
}`
const checkRunSuccessPayload = `{
"action": "completed",
"check_run": {
"name": "CI Build",
"conclusion": "success",
"html_url": "https://github.com/owner/myrepo/runs/123",
"head_sha": "abc123def",
"check_suite": {
"head_branch": "main"
}
},
"repository": {
"name": "myrepo",
"full_name": "owner/myrepo"
}
}`
const workflowRunFailurePayload = `{
"action": "completed",
"workflow_run": {
"name": "CI Pipeline",
"conclusion": "failure",
"html_url": "https://github.com/owner/myrepo/actions/runs/789",
"head_sha": "def456abc",
"head_branch": "feature-branch"
},
"repository": {
"name": "myrepo",
"full_name": "owner/myrepo"
}
}`
const workflowRunTimedOutPayload = `{
"action": "completed",
"workflow_run": {
"name": "CI Pipeline",
"conclusion": "timed_out",
"html_url": "https://github.com/owner/myrepo/actions/runs/789",
"head_sha": "def456abc",
"head_branch": "main"
},
"repository": {
"name": "myrepo",
"full_name": "owner/myrepo"
}
}`
func TestGitHubWebhook_CheckRunFailure_CreatesTask(t *testing.T) {
srv, store := testServer(t)
srv.projects = []config.Project{{Name: "myrepo", Dir: "/workspace/myrepo"}}
w := webhookPost(t, srv, "check_run", checkRunFailurePayload, "")
if w.Code != http.StatusOK {
t.Fatalf("want 200, got %d; body: %s", w.Code, w.Body.String())
}
var resp map[string]string
json.NewDecoder(w.Body).Decode(&resp)
taskID, ok := resp["task_id"]
if !ok || taskID == "" {
t.Fatal("expected task_id in response")
}
tk, err := store.GetTask(taskID)
if err != nil {
t.Fatalf("task not found in store: %v", err)
}
if !strings.Contains(tk.Name, "CI Build") {
t.Errorf("task name %q does not contain check name", tk.Name)
}
if !strings.Contains(tk.Name, "main") {
t.Errorf("task name %q does not contain branch", tk.Name)
}
if tk.Agent.ProjectDir != "/workspace/myrepo" {
t.Errorf("task project dir = %q, want /workspace/myrepo", tk.Agent.ProjectDir)
}
if !contains(tk.Tags, "ci") || !contains(tk.Tags, "auto") {
t.Errorf("task tags %v missing expected ci/auto tags", tk.Tags)
}
}
func TestGitHubWebhook_WorkflowRunFailure_CreatesTask(t *testing.T) {
srv, store := testServer(t)
srv.projects = []config.Project{{Name: "myrepo", Dir: "/workspace/myrepo"}}
w := webhookPost(t, srv, "workflow_run", workflowRunFailurePayload, "")
if w.Code != http.StatusOK {
t.Fatalf("want 200, got %d; body: %s", w.Code, w.Body.String())
}
var resp map[string]string
json.NewDecoder(w.Body).Decode(&resp)
taskID, ok := resp["task_id"]
if !ok || taskID == "" {
t.Fatal("expected task_id in response")
}
tk, err := store.GetTask(taskID)
if err != nil {
t.Fatalf("task not found in store: %v", err)
}
if !strings.Contains(tk.Name, "CI Pipeline") {
t.Errorf("task name %q does not contain workflow name", tk.Name)
}
if !strings.Contains(tk.Name, "feature-branch") {
t.Errorf("task name %q does not contain branch", tk.Name)
}
}
func TestGitHubWebhook_WorkflowRunTimedOut_CreatesTask(t *testing.T) {
srv, store := testServer(t)
srv.projects = []config.Project{{Name: "myrepo", Dir: "/workspace/myrepo"}}
w := webhookPost(t, srv, "workflow_run", workflowRunTimedOutPayload, "")
if w.Code != http.StatusOK {
t.Fatalf("want 200, got %d; body: %s", w.Code, w.Body.String())
}
var resp map[string]string
json.NewDecoder(w.Body).Decode(&resp)
taskID := resp["task_id"]
if taskID == "" {
t.Fatal("expected task_id in response")
}
if _, err := store.GetTask(taskID); err != nil {
t.Fatalf("task not found in store: %v", err)
}
}
func TestGitHubWebhook_CheckRunSuccess_Returns204(t *testing.T) {
srv, _ := testServer(t)
srv.projects = []config.Project{{Name: "myrepo", Dir: "/workspace/myrepo"}}
w := webhookPost(t, srv, "check_run", checkRunSuccessPayload, "")
if w.Code != http.StatusNoContent {
t.Fatalf("want 204, got %d; body: %s", w.Code, w.Body.String())
}
}
func TestGitHubWebhook_InvalidHMAC_Returns401(t *testing.T) {
srv, _ := testServer(t)
srv.webhookSecret = "correct-secret"
srv.projects = []config.Project{{Name: "myrepo", Dir: "/workspace/myrepo"}}
w := webhookPost(t, srv, "check_run", checkRunFailurePayload, "sha256=badhash")
if w.Code != http.StatusUnauthorized {
t.Fatalf("want 401, got %d; body: %s", w.Code, w.Body.String())
}
}
func TestGitHubWebhook_ValidHMAC_AcceptsRequest(t *testing.T) {
srv, store := testServer(t)
srv.webhookSecret = "my-secret"
srv.projects = []config.Project{{Name: "myrepo", Dir: "/workspace/myrepo"}}
sig := signBody(t, "my-secret", checkRunFailurePayload)
w := webhookPost(t, srv, "check_run", checkRunFailurePayload, sig)
if w.Code != http.StatusOK {
t.Fatalf("want 200, got %d; body: %s", w.Code, w.Body.String())
}
var resp map[string]string
json.NewDecoder(w.Body).Decode(&resp)
if _, err := store.GetTask(resp["task_id"]); err != nil {
t.Fatal("task not found after valid HMAC request")
}
}
func TestGitHubWebhook_NoSecretConfigured_SkipsHMACCheck(t *testing.T) {
srv, store := testServer(t)
// No webhook secret configured — even a missing signature should pass.
srv.projects = []config.Project{{Name: "myrepo", Dir: "/workspace/myrepo"}}
w := webhookPost(t, srv, "check_run", checkRunFailurePayload, "")
if w.Code != http.StatusOK {
t.Fatalf("want 200, got %d; body: %s", w.Code, w.Body.String())
}
var resp map[string]string
json.NewDecoder(w.Body).Decode(&resp)
if _, err := store.GetTask(resp["task_id"]); err != nil {
t.Fatal("task not found")
}
}
func TestGitHubWebhook_UnknownEvent_Returns204(t *testing.T) {
srv, _ := testServer(t)
w := webhookPost(t, srv, "push", `{"ref":"refs/heads/main"}`, "")
if w.Code != http.StatusNoContent {
t.Fatalf("want 204, got %d; body: %s", w.Code, w.Body.String())
}
}
func TestGitHubWebhook_MissingEvent_Returns204(t *testing.T) {
srv, _ := testServer(t)
req := httptest.NewRequest("POST", "/api/webhooks/github", bytes.NewBufferString("{}"))
req.Header.Set("Content-Type", "application/json")
// No X-GitHub-Event header
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusNoContent {
t.Fatalf("want 204, got %d; body: %s", w.Code, w.Body.String())
}
}
func TestGitHubWebhook_FallbackToSingleProject(t *testing.T) {
srv, store := testServer(t)
// Project name doesn't match repo name, but there's only one project → fall back.
srv.projects = []config.Project{{Name: "different-name", Dir: "/workspace/someapp"}}
w := webhookPost(t, srv, "check_run", checkRunFailurePayload, "")
if w.Code != http.StatusOK {
t.Fatalf("want 200, got %d; body: %s", w.Code, w.Body.String())
}
var resp map[string]string
json.NewDecoder(w.Body).Decode(&resp)
tk, err := store.GetTask(resp["task_id"])
if err != nil {
t.Fatalf("task not found: %v", err)
}
if tk.Agent.ProjectDir != "/workspace/someapp" {
t.Errorf("expected fallback to /workspace/someapp, got %q", tk.Agent.ProjectDir)
}
}
func TestGitHubWebhook_NoProjectsConfigured_CreatesTaskWithoutProjectDir(t *testing.T) {
srv, store := testServer(t)
// No projects configured — task should still be created, just no project dir set.
w := webhookPost(t, srv, "check_run", checkRunFailurePayload, "")
if w.Code != http.StatusOK {
t.Fatalf("want 200, got %d; body: %s", w.Code, w.Body.String())
}
var resp map[string]string
json.NewDecoder(w.Body).Decode(&resp)
tk, err := store.GetTask(resp["task_id"])
if err != nil {
t.Fatalf("task not found: %v", err)
}
if tk.Agent.ProjectDir != "" {
t.Errorf("expected empty project dir, got %q", tk.Agent.ProjectDir)
}
}
// contains checks if a string slice contains a value.
func contains(slice []string, val string) bool {
for _, s := range slice {
if s == val {
return true
}
}
return false
}
|