|
8 | 8 | "errors"
|
9 | 9 | "fmt"
|
10 | 10 | "github.com/brpaz/echozap"
|
| 11 | + payloadpkg "github.com/cirruslabs/cirrus-webhooks-server/internal/command/datadog/payload" |
11 | 12 | "github.com/cirruslabs/cirrus-webhooks-server/internal/datadogsender"
|
12 | 13 | mapset "github.com/deckarep/golang-set/v2"
|
13 | 14 | "github.com/labstack/echo/v4"
|
|
33 | 34 | ErrSignatureVerificationFailed = errors.New("event signature verification failed")
|
34 | 35 | )
|
35 | 36 |
|
36 |
| -type commonWebhookFields struct { |
37 |
| - Action *string |
38 |
| - Timestamp *int64 |
39 |
| - Actor struct { |
40 |
| - ID *int64 |
41 |
| - } |
42 |
| - Repository struct { |
43 |
| - ID *int64 |
44 |
| - Owner *string |
45 |
| - Name *string |
46 |
| - } |
47 |
| - Build struct { |
48 |
| - ID *int64 |
49 |
| - } |
50 |
| - Task struct { |
51 |
| - ID *int64 |
52 |
| - } |
53 |
| -} |
54 |
| - |
55 | 37 | func NewCommand() *cobra.Command {
|
56 | 38 | cmd := &cobra.Command{
|
57 | 39 | Use: "datadog",
|
@@ -182,7 +164,23 @@ func processWebhookEvent(
|
182 | 164 | }
|
183 | 165 |
|
184 | 166 | // Enrich the event with tags
|
185 |
| - enrichEventWithTags(body, evt, logger) |
| 167 | + var payload payloadpkg.Payload |
| 168 | + |
| 169 | + switch presentedEventType { |
| 170 | + case "audit_event": |
| 171 | + payload = &payloadpkg.AuditEvent{} |
| 172 | + case "build", "task": |
| 173 | + payload = &payloadpkg.BuildOrTask{} |
| 174 | + } |
| 175 | + |
| 176 | + if payload != nil { |
| 177 | + if err = json.Unmarshal(body, payload); err != nil { |
| 178 | + logger.Warnf("failed to enrich Datadog event with tags: "+ |
| 179 | + "failed to parse the webhook event of type %q as JSON: %v", presentedEventType, err) |
| 180 | + } else { |
| 181 | + payload.Enrich(ctx.Request().Header, evt, logger) |
| 182 | + } |
| 183 | + } |
186 | 184 |
|
187 | 185 | // Datadog silently discards log events submitted with a
|
188 | 186 | // timestamp that is more than 18 hours in the past, sigh.
|
@@ -227,44 +225,3 @@ func verifyEvent(ctx echo.Context, body []byte) error {
|
227 | 225 |
|
228 | 226 | return nil
|
229 | 227 | }
|
230 |
| - |
231 |
| -func enrichEventWithTags(body []byte, evt *datadogsender.Event, logger *zap.SugaredLogger) { |
232 |
| - var commonWebhookFields commonWebhookFields |
233 |
| - |
234 |
| - if err := json.Unmarshal(body, &commonWebhookFields); err != nil { |
235 |
| - logger.Warnf("failed to enrich Datadog event with tags: "+ |
236 |
| - "failed to parse the webhook event as JSON: %v", err) |
237 |
| - |
238 |
| - return |
239 |
| - } |
240 |
| - |
241 |
| - if value := commonWebhookFields.Action; value != nil { |
242 |
| - evt.Tags = append(evt.Tags, fmt.Sprintf("action:%s", *value)) |
243 |
| - } |
244 |
| - |
245 |
| - if timestamp := commonWebhookFields.Timestamp; timestamp != nil { |
246 |
| - evt.Timestamp = time.UnixMilli(*timestamp).UTC() |
247 |
| - } |
248 |
| - |
249 |
| - if value := commonWebhookFields.Actor.ID; value != nil { |
250 |
| - evt.Tags = append(evt.Tags, fmt.Sprintf("actor_id:%d", *value)) |
251 |
| - } |
252 |
| - |
253 |
| - if value := commonWebhookFields.Repository.ID; value != nil { |
254 |
| - evt.Tags = append(evt.Tags, fmt.Sprintf("repository_id:%d", *value)) |
255 |
| - } |
256 |
| - if value := commonWebhookFields.Repository.Owner; value != nil { |
257 |
| - evt.Tags = append(evt.Tags, fmt.Sprintf("repository_owner:%s", *value)) |
258 |
| - } |
259 |
| - if value := commonWebhookFields.Repository.Name; value != nil { |
260 |
| - evt.Tags = append(evt.Tags, fmt.Sprintf("repository_name:%s", *value)) |
261 |
| - } |
262 |
| - |
263 |
| - if value := commonWebhookFields.Build.ID; value != nil { |
264 |
| - evt.Tags = append(evt.Tags, fmt.Sprintf("build_id:%d", *value)) |
265 |
| - } |
266 |
| - |
267 |
| - if value := commonWebhookFields.Task.ID; value != nil { |
268 |
| - evt.Tags = append(evt.Tags, fmt.Sprintf("task_id:%d", *value)) |
269 |
| - } |
270 |
| -} |
0 commit comments