-
Notifications
You must be signed in to change notification settings - Fork 34
/
handlers_githubwebhooks.go
977 lines (854 loc) · 29.7 KB
/
handlers_githubwebhooks.go
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
//
// Copyright 2023 Stacklok, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package controlplane contains the control plane API for the minder.
package controlplane
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"net/http"
"sort"
"strconv"
"strings"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/google/go-github/v61/github"
"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/stacklok/minder/internal/artifacts"
"github.com/stacklok/minder/internal/config/server"
"github.com/stacklok/minder/internal/controlplane/metrics"
"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/engine/entities"
"github.com/stacklok/minder/internal/events"
"github.com/stacklok/minder/internal/projects/features"
"github.com/stacklok/minder/internal/providers/github/installations"
ghprov "github.com/stacklok/minder/internal/providers/github/service"
"github.com/stacklok/minder/internal/repositories"
"github.com/stacklok/minder/internal/util"
"github.com/stacklok/minder/internal/verifier/verifyif"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provifv1 "github.com/stacklok/minder/pkg/providers/v1"
)
// errRepoNotFound is returned when a repository is not found
var errRepoNotFound = errors.New("repository not found")
// errArtifactNotFound is returned when an artifact is not found
var errArtifactNotFound = errors.New("artifact not found")
// errArtifactVersionSkipped is returned when an artifact is skipped because it has no tags
var errArtifactVersionSkipped = errors.New("artifact version skipped, has no tags")
// errRepoIsPrivate is returned when a repository is private
var errRepoIsPrivate = errors.New("repository is private")
// errNotHandled is returned when a webhook event is not handled
var errNotHandled = errors.New("webhook event not handled")
// newErrNotHandled returns a new errNotHandled error
func newErrNotHandled(smft string, args ...any) error {
msg := fmt.Sprintf(smft, args...)
return fmt.Errorf("%w: %s", errNotHandled, msg)
}
// https://docs.github.com/en/webhooks/webhook-events-and-payloads#about-webhook-events-and-payloads
var repoEvents = []string{
"branch_protection_configuration",
"branch_protection_rule",
"code_scanning_alert",
"create", // a tag or branch is created
"member",
"meta", // webhook itself
"repository_vulnerability_alert",
"org_block",
"organization",
"public",
// listening to push makes sure we evaluate on pushes to branches we need to check, but might be too noisy
// for topic branches
"push",
"repository",
"repository_advisory",
"repository_import",
"repository_ruleset",
"secret_scanning_alert",
"secret_scanning_alert_location",
"security_advisory",
"security_and_analysis",
"team",
"team_add",
}
// WebhookActionEventDeleted is the action for a deleted event
const (
WebhookActionEventDeleted = "deleted"
WebhookActionEventOpened = "opened"
WebhookActionEventClosed = "closed"
WebhookActionEventSynchronize = "synchronize"
WebhookActionEventPublished = "published"
)
func entityFromWebhookEventTypeKey(m *message.Message) pb.Entity {
key := m.Metadata.Get(events.GithubWebhookEventTypeKey)
switch {
case key == "package":
return pb.Entity_ENTITY_ARTIFACTS
case key == "pull_request":
return pb.Entity_ENTITY_PULL_REQUESTS
case slices.Contains(repoEvents, key):
return pb.Entity_ENTITY_REPOSITORIES
}
return pb.Entity_ENTITY_UNSPECIFIED
}
// HandleGitHubAppWebhook handles incoming GitHub App webhooks
func (s *Server) HandleGitHubAppWebhook() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
wes := &metrics.WebhookEventState{
Typ: "unknown",
Accepted: false,
Error: true,
}
defer func() {
s.mt.AddWebhookEventTypeCount(r.Context(), wes)
}()
rawWBPayload, err := s.ghProviders.ValidateGitHubAppWebhookPayload(r)
if err != nil {
log.Printf("Error validating webhook payload: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
wes.Typ = github.WebHookType(r)
if wes.Typ == "ping" {
logPingReceivedEvent(r.Context(), rawWBPayload)
wes.Error = false
return
}
m := message.NewMessage(uuid.New().String(), nil)
m.Metadata.Set(events.ProviderDeliveryIdKey, github.DeliveryID(r))
m.Metadata.Set(events.ProviderSourceKey, "https://api.github.com/") // TODO: handle other sources
m.Metadata.Set(events.GithubWebhookEventTypeKey, wes.Typ)
ctx := r.Context()
l := zerolog.Ctx(ctx).With().
Str("webhook-event-type", m.Metadata[events.GithubWebhookEventTypeKey]).
Str("providertype", m.Metadata[events.ProviderTypeKey]).
Str("upstream-delivery-id", m.Metadata[events.ProviderDeliveryIdKey]).
Logger()
if err := s.parseGithubAppEventForProcessing(rawWBPayload, m); err != nil {
wes = handleParseError(wes.Typ, err)
if wes.Error {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
return
}
wes.Accepted = true
l.Info().Str("message-id", m.UUID).Msg("publishing event for execution")
if err := s.evt.Publish(installations.ProviderInstallationTopic, m); err != nil {
wes.Error = true
log.Printf("Error publishing message: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
wes.Error = false
w.WriteHeader(http.StatusOK)
}
}
// HandleGitHubWebHook handles incoming GitHub webhooks
// See https://docs.github.com/en/developers/webhooks-and-events/webhooks/about-webhooks
// for more information.
func (s *Server) HandleGitHubWebHook() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
wes := &metrics.WebhookEventState{
Typ: "unknown",
Accepted: false,
Error: true,
}
defer func() {
s.mt.AddWebhookEventTypeCount(r.Context(), wes)
}()
// Validate the payload signature. This is required for security reasons.
// See https://docs.github.com/en/developers/webhooks-and-events/webhooks/securing-your-webhooks
// for more information. Note that this is not required for the GitHub App
// webhook secret, but it is required for OAuth2 App.
// it returns a uuid for the webhook, but we are not currently using it
segments := strings.Split(r.URL.Path, "/")
_ = segments[len(segments)-1]
rawWBPayload, err := validatePayloadSignature(r, &s.cfg.WebhookConfig)
if err != nil {
log.Printf("Error validating webhook payload: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
wes.Typ = github.WebHookType(r)
if wes.Typ == "ping" {
logPingReceivedEvent(r.Context(), rawWBPayload)
wes.Error = false
return
}
// TODO: extract sender and event time from payload portably
m := message.NewMessage(uuid.New().String(), nil)
m.Metadata.Set(events.ProviderDeliveryIdKey, github.DeliveryID(r))
m.Metadata.Set(events.ProviderTypeKey, string(db.ProviderTypeGithub))
m.Metadata.Set(events.ProviderSourceKey, "https://api.github.com/") // TODO: handle other sources
m.Metadata.Set(events.GithubWebhookEventTypeKey, wes.Typ)
ctx := r.Context()
l := zerolog.Ctx(ctx).With().
Str("webhook-event-type", m.Metadata[events.GithubWebhookEventTypeKey]).
Str("providertype", m.Metadata[events.ProviderTypeKey]).
Str("upstream-delivery-id", m.Metadata[events.ProviderDeliveryIdKey]).
Logger()
l.Debug().Msg("parsing event")
// Parse the webhook event and construct a message for the event router
if err := s.parseGithubEventForProcessing(rawWBPayload, m); err != nil {
wes = handleParseError(wes.Typ, err)
if wes.Error {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
return
}
wes.Accepted = true
l.Info().Str("message-id", m.UUID).Msg("publishing event for execution")
// Channel the event based on the webhook action
var watermillTopic string
if shouldIssueDeletionEvent(m) {
watermillTopic = events.TopicQueueReconcileEntityDelete
} else {
watermillTopic = events.TopicQueueEntityEvaluate
}
// Publish the message to the event router
if err := s.evt.Publish(watermillTopic, m); err != nil {
wes.Error = true
log.Printf("Error publishing message: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
// We successfully published the message
wes.Error = false
w.WriteHeader(http.StatusOK)
}
}
// logPingReceivedEvent logs the type of token used to authenticate the webhook. The idea is to log a link between the
// repo and the token type. Since this is done only for the ping event, we can assume that the sender is the app that
// installed the webhook on the repository.
func logPingReceivedEvent(ctx context.Context, rawWHPayload []byte) {
l := zerolog.Ctx(ctx).With().Logger()
var payload map[string]any
err := json.Unmarshal(rawWHPayload, &payload)
if err == nil {
repoInfo, ok := payload["repository"].(map[string]any)
if ok {
// Log the repository ID and URL if available
repoID, err := parseRepoID(repoInfo["id"])
if err == nil {
l = l.With().Int64("github-repository-id", repoID).Logger()
}
repoUrl := repoInfo["html_url"].(string)
l = l.With().Str("github-repository-url", repoUrl).Logger()
}
// During the ping event, the sender corresponds to the app that installed the webhook on the repository
if payload["sender"] != nil {
// Log the sender if available
senderLogin, err := util.JQReadFrom[string](ctx, ".sender.login", payload)
if err == nil {
l = l.With().Str("sender-login", senderLogin).Logger()
}
senderHTMLUrl, err := util.JQReadFrom[string](ctx, ".sender.html_url", payload)
if err == nil {
if strings.Contains(senderHTMLUrl, "github.com/apps") {
l = l.With().Str("sender-token-type", "github-app").Logger()
} else {
l = l.With().Str("sender-token-type", "oauth-app").Logger()
}
}
}
}
l.Debug().Msg("ping received")
}
// NoopWebhookHandler is a no-op handler for webhooks
func (s *Server) NoopWebhookHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
wes := &metrics.WebhookEventState{
Typ: "unknown",
Accepted: false,
Error: true,
}
defer func() {
s.mt.AddWebhookEventTypeCount(r.Context(), wes)
}()
wes.Typ = github.WebHookType(r)
wes.Accepted = true
wes.Error = false
w.WriteHeader(http.StatusOK)
}
}
func validatePayloadSignature(r *http.Request, wc *server.WebhookConfig) (payload []byte, err error) {
var br *bytes.Reader
br, err = readerFromRequest(r)
if err != nil {
return
}
signature := r.Header.Get(github.SHA256SignatureHeader)
if signature == "" {
signature = r.Header.Get(github.SHA1SignatureHeader)
}
contentType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
return
}
whSecret, err := wc.GetWebhookSecret()
if err != nil {
return
}
payload, err = github.ValidatePayloadFromBody(contentType, br, signature, []byte(whSecret))
if err == nil {
return
}
payload, err = validatePreviousSecrets(r.Context(), signature, contentType, br, wc)
return
}
func readerFromRequest(r *http.Request) (*bytes.Reader, error) {
b, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
err = r.Body.Close()
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
func validatePreviousSecrets(
ctx context.Context,
signature, contentType string,
br *bytes.Reader,
wc *server.WebhookConfig,
) (payload []byte, err error) {
previousSecrets := []string{}
if wc.PreviousWebhookSecretFile != "" {
previousSecrets, err = wc.GetPreviousWebhookSecrets()
if err != nil {
return
}
}
for _, prevSecret := range previousSecrets {
_, err = br.Seek(0, io.SeekStart)
if err != nil {
return
}
payload, err = github.ValidatePayloadFromBody(contentType, br, signature, []byte(prevSecret))
if err == nil {
zerolog.Ctx(ctx).Warn().Msg("used previous secret to validate payload")
return
}
}
err = fmt.Errorf("failed to validate payload with any fallback secret")
return
}
func handleParseError(typ string, parseErr error) *metrics.WebhookEventState {
state := &metrics.WebhookEventState{Typ: typ, Accepted: false, Error: true}
var logMsg string
switch {
case errors.Is(parseErr, errRepoNotFound):
state.Error = false
logMsg = "repository not found"
case errors.Is(parseErr, errArtifactNotFound):
state.Error = false
logMsg = "artifact not found"
case errors.Is(parseErr, errRepoIsPrivate):
state.Error = false
logMsg = "repository is private"
case errors.Is(parseErr, errNotHandled):
state.Error = false
logMsg = fmt.Sprintf("webhook event not handled (%v)", parseErr)
case errors.Is(parseErr, errArtifactVersionSkipped):
state.Error = false
logMsg = "artifact version skipped, has no tags"
default:
logMsg = fmt.Sprintf("Error parsing github webhook message: %v", parseErr)
}
log.Print(logMsg)
return state
}
func (_ *Server) parseGithubAppEventForProcessing(
rawWHPayload []byte,
msg *message.Message,
) error {
var event github.InstallationEvent
if msg.Metadata.Get(events.GithubWebhookEventTypeKey) != "installation" {
return newErrNotHandled("github app event %s not handled", msg.Metadata.Get(events.GithubWebhookEventTypeKey))
}
err := json.Unmarshal(rawWHPayload, &event)
if err != nil {
return fmt.Errorf("error unmarshalling payload: %w", err)
}
action := event.GetAction()
if action == "" {
return fmt.Errorf("action is empty")
}
if action != WebhookActionEventDeleted {
return newErrNotHandled("event %s with action %s not handled",
msg.Metadata.Get(events.GithubWebhookEventTypeKey), action)
}
installationID := event.GetInstallation().GetID()
if installationID == 0 {
return fmt.Errorf("installation ID is 0")
}
payloadBytes, err := json.Marshal(ghprov.GitHubAppInstallationDeletedPayload{
InstallationID: installationID,
})
if err != nil {
return fmt.Errorf("error marshalling payload: %w", err)
}
installations.ProviderInstanceRemovedMessage(
msg,
db.ProviderClassGithubApp,
payloadBytes)
return nil
}
func (s *Server) parseGithubEventForProcessing(
rawWHPayload []byte,
msg *message.Message,
) error {
ent := entityFromWebhookEventTypeKey(msg)
if ent == pb.Entity_ENTITY_UNSPECIFIED {
return newErrNotHandled("event %s not handled", msg.Metadata.Get(events.GithubWebhookEventTypeKey))
}
ctx := context.Background()
var payload map[string]any
if err := json.Unmarshal(rawWHPayload, &payload); err != nil {
return fmt.Errorf("error unmarshalling payload: %w", err)
}
// get information about the repository from the payload
dbRepo, err := getRepoInformationFromPayload(ctx, s.store, payload)
if err != nil {
return fmt.Errorf("error getting repo information from payload: %w", err)
}
var action string // explicit declaration to use the default value
action, err = util.JQReadFrom[string](ctx, ".action", payload)
if err != nil && !errors.Is(err, util.ErrNoValueFound) {
return fmt.Errorf("error getting action from payload: %w", err)
}
// determine if the payload is an artifact published event
// TODO: this needs to be managed via signals
if ent == pb.Entity_ENTITY_ARTIFACTS && action == WebhookActionEventPublished {
return s.parseArtifactPublishedEvent(ctx, payload, msg, dbRepo, dbRepo.ProviderID, action)
} else if ent == pb.Entity_ENTITY_PULL_REQUESTS {
return s.parsePullRequestModEvent(ctx, payload, msg, dbRepo, dbRepo.ProviderID, action)
} else if ent == pb.Entity_ENTITY_REPOSITORIES {
return parseRepoEvent(payload, msg, dbRepo, action)
}
return newErrNotHandled("event %s with action %s not handled",
msg.Metadata.Get(events.GithubWebhookEventTypeKey), action)
}
func parseRepoEvent(
whPayload map[string]any,
msg *message.Message,
dbrepo db.Repository,
action string,
) error {
if action == WebhookActionEventDeleted {
// Find out what kind of repository event we are dealing with
if whPayload["hook"] != nil || whPayload["hook_id"] != nil {
// Having these means it's a repository event related to the webhook itself, i.e., deleted, created, etc.
// Get the webhook ID from the payload
whID := whPayload["hook_id"].(float64)
if dbrepo.WebhookID.Valid {
// Check if the payload webhook ID matches the one we have stored in the DB for this repository
if int64(whID) != dbrepo.WebhookID.Int64 {
// This means we got a deleted event for a webhook ID that doesn't correspond to the one we have stored in the DB.
return newErrNotHandled("delete event %s with action %s not handled, hook ID %d does not match stored webhook ID %d",
msg.Metadata.Get(events.GithubWebhookEventTypeKey), action, int64(whID), dbrepo.WebhookID.Int64)
}
}
// This means we got a deleted event for a webhook ID that corresponds to the one we have stored in the DB.
// We will remove the repo from the DB, so we can proceed with the deletion event for this entity (repository)
// TODO: perhaps handle this better by trying to re-create the webhook if it was deleted manually
}
// If we don't have hook/hook_id, continue with the deletion event for this entity (repository)
}
// protobufs are our API, so we always execute on these instead of the DB directly.
repo := repositories.PBRepositoryFromDB(dbrepo)
eiw := entities.NewEntityInfoWrapper().
WithProviderID(dbrepo.ProviderID).
WithRepository(repo).
WithProjectID(dbrepo.ProjectID).
WithRepositoryID(dbrepo.ID).
WithActionEvent(action)
return eiw.ToMessage(msg)
}
func (s *Server) parseArtifactPublishedEvent(
ctx context.Context,
whPayload map[string]any,
msg *message.Message,
dbrepo db.Repository,
providerID uuid.UUID,
action string,
) error {
// we need to have information about package and repository
if whPayload["package"] == nil || whPayload["repository"] == nil {
log.Printf("could not determine relevant entity for event. Skipping execution.")
return nil
}
provider, err := s.providerManager.InstantiateFromID(ctx, providerID)
if err != nil {
log.Printf("error instantiating provider: %v", err)
return err
}
cli, err := provifv1.As[provifv1.GitHub](provider)
if err != nil {
log.Printf("error instantiating provider: %v", err)
return err
}
tempArtifact, err := gatherArtifact(ctx, cli, whPayload)
if err != nil {
return fmt.Errorf("error gathering versioned artifact: %w", err)
}
dbArtifact, err := s.store.UpsertArtifact(ctx, db.UpsertArtifactParams{
RepositoryID: uuid.NullUUID{
UUID: dbrepo.ID,
Valid: true,
},
ArtifactName: tempArtifact.GetName(),
ArtifactType: tempArtifact.GetTypeLower(),
ArtifactVisibility: tempArtifact.Visibility,
ProjectID: dbrepo.ProjectID,
ProviderID: dbrepo.ProviderID,
ProviderName: dbrepo.Provider,
})
if err != nil {
return fmt.Errorf("error upserting artifact: %w", err)
}
_, pbArtifact, err := artifacts.GetArtifact(ctx, s.store, dbrepo.ProjectID, dbArtifact.ID)
if err != nil {
return fmt.Errorf("error getting artifact with versions: %w", err)
}
// TODO: wrap in a function
pbArtifact.Versions = tempArtifact.Versions
eiw := entities.NewEntityInfoWrapper().
WithArtifact(pbArtifact).
WithProviderID(dbrepo.ProviderID).
WithProjectID(dbrepo.ProjectID).
WithRepositoryID(dbrepo.ID).
WithArtifactID(dbArtifact.ID).
WithActionEvent(action)
return eiw.ToMessage(msg)
}
func (s *Server) parsePullRequestModEvent(
ctx context.Context,
whPayload map[string]any,
msg *message.Message,
dbrepo db.Repository,
providerID uuid.UUID,
action string,
) error {
provider, err := s.providerManager.InstantiateFromID(ctx, providerID)
if err != nil {
log.Printf("error instantiating provider: %v", err)
return err
}
cli, err := provifv1.As[provifv1.GitHub](provider)
if err != nil {
log.Printf("error instantiating provider: %v", err)
return err
}
prEvalInfo, err := getPullRequestInfoFromPayload(ctx, whPayload)
if err != nil {
return fmt.Errorf("error getting pull request information from payload: %w", err)
}
dbPr, err := s.reconcilePrWithDb(ctx, dbrepo, prEvalInfo)
if errors.Is(err, errNotHandled) {
return err
} else if err != nil {
return fmt.Errorf("error reconciling PR with DB: %w", err)
}
err = updatePullRequestInfoFromProvider(ctx, cli, dbrepo, prEvalInfo)
if err != nil {
return fmt.Errorf("error updating pull request information from provider: %w", err)
}
log.Printf("evaluating PR %+v", prEvalInfo)
eiw := entities.NewEntityInfoWrapper().
WithPullRequest(prEvalInfo).
WithPullRequestID(dbPr.ID).
WithProviderID(dbrepo.ProviderID).
WithProjectID(dbrepo.ProjectID).
WithRepositoryID(dbrepo.ID).
WithActionEvent(action)
return eiw.ToMessage(msg)
}
func extractArtifactFromPayload(ctx context.Context, payload map[string]any) (*pb.Artifact, error) {
artifactName, err := util.JQReadFrom[string](ctx, ".package.name", payload)
if err != nil {
return nil, err
}
artifactType, err := util.JQReadFrom[string](ctx, ".package.package_type", payload)
if err != nil {
return nil, err
}
ownerLogin, err := util.JQReadFrom[string](ctx, ".package.owner.login", payload)
if err != nil {
return nil, err
}
repoName, err := util.JQReadFrom[string](ctx, ".repository.full_name", payload)
if err != nil {
return nil, err
}
artifact := &pb.Artifact{
Owner: ownerLogin,
Name: artifactName,
Type: artifactType,
Repository: repoName,
// visibility and createdAt are not in the payload, we need to get it with a REST call
}
return artifact, nil
}
func extractArtifactVersionFromPayload(ctx context.Context, payload map[string]any) (*pb.ArtifactVersion, error) {
packageVersionId, err := util.JQReadFrom[float64](ctx, ".package.package_version.id", payload)
if err != nil {
return nil, err
}
packageVersionSha, err := util.JQReadFrom[string](ctx, ".package.package_version.version", payload)
if err != nil {
return nil, err
}
tag, err := util.JQReadFrom[string](ctx, ".package.package_version.container_metadata.tag.name", payload)
if err != nil {
return nil, err
}
version := &pb.ArtifactVersion{
VersionId: int64(packageVersionId),
Tags: []string{tag},
Sha: packageVersionSha,
}
return version, nil
}
func gatherArtifactInfo(
ctx context.Context,
client provifv1.GitHub,
payload map[string]any,
) (*pb.Artifact, error) {
artifact, err := extractArtifactFromPayload(ctx, payload)
if err != nil {
return nil, fmt.Errorf("error extracting artifact from payload: %w", err)
}
// we also need to fill in the visibility which is not in the payload
ghArtifact, err := client.GetPackageByName(ctx, artifact.Owner, string(verifyif.ArtifactTypeContainer), artifact.Name)
if err != nil {
return nil, fmt.Errorf("error extracting artifact from repo: %w", err)
}
artifact.Visibility = *ghArtifact.Visibility
return artifact, nil
}
func gatherArtifactVersionInfo(
ctx context.Context,
cli provifv1.GitHub,
payload map[string]any,
artifactOwnerLogin, artifactName string,
) (*pb.ArtifactVersion, error) {
version, err := extractArtifactVersionFromPayload(ctx, payload)
if err != nil {
return nil, fmt.Errorf("error extracting artifact version from payload: %w", err)
}
// not all information is in the payload, we need to get it from the container registry
// and/or GH API
err = updateArtifactVersionFromRegistry(ctx, cli, artifactOwnerLogin, artifactName, version)
if err != nil {
return nil, fmt.Errorf("error getting upstream information for artifact version: %w", err)
}
return version, nil
}
func gatherArtifact(
ctx context.Context,
cli provifv1.GitHub,
payload map[string]any,
) (*pb.Artifact, error) {
artifact, err := gatherArtifactInfo(ctx, cli, payload)
if err != nil {
return nil, fmt.Errorf("error gatherinfo artifact info: %w", err)
}
version, err := gatherArtifactVersionInfo(ctx, cli, payload, artifact.Owner, artifact.Name)
if err != nil {
return nil, fmt.Errorf("error extracting artifact from payload: %w", err)
}
artifact.Versions = []*pb.ArtifactVersion{version}
return artifact, nil
}
func updateArtifactVersionFromRegistry(
ctx context.Context,
client provifv1.GitHub,
artifactOwnerLogin, artifactName string,
version *pb.ArtifactVersion,
) error {
// we'll grab the artifact version from the REST endpoint because we need the visibility
// and createdAt fields which are not in the payload
ghVersion, err := client.GetPackageVersionById(ctx, artifactOwnerLogin, string(verifyif.ArtifactTypeContainer),
artifactName, version.VersionId)
if err != nil {
return fmt.Errorf("error getting package version from repository: %w", err)
}
tags := ghVersion.Metadata.Container.Tags
// if the artifact has no tags, skip it
if len(tags) == 0 {
return errArtifactVersionSkipped
}
sort.Strings(tags)
version.Tags = tags
if ghVersion.CreatedAt != nil {
version.CreatedAt = timestamppb.New(*ghVersion.CreatedAt.GetTime())
}
return nil
}
func getPullRequestInfoFromPayload(
ctx context.Context,
payload map[string]any,
) (*pb.PullRequest, error) {
prUrl, err := util.JQReadFrom[string](ctx, ".pull_request.url", payload)
if err != nil {
return nil, fmt.Errorf("error getting pull request url from payload: %w", err)
}
prNumber, err := util.JQReadFrom[float64](ctx, ".pull_request.number", payload)
if err != nil {
return nil, fmt.Errorf("error getting pull request number from payload: %w", err)
}
prAuthorId, err := util.JQReadFrom[float64](ctx, ".pull_request.user.id", payload)
if err != nil {
return nil, fmt.Errorf("error getting pull request author ID from payload: %w", err)
}
action, err := util.JQReadFrom[string](ctx, ".action", payload)
if err != nil {
return nil, fmt.Errorf("error getting action from payload: %w", err)
}
return &pb.PullRequest{
Url: prUrl,
Number: int64(prNumber),
AuthorId: int64(prAuthorId),
Action: action,
}, nil
}
func (s *Server) reconcilePrWithDb(
ctx context.Context,
dbrepo db.Repository,
prEvalInfo *pb.PullRequest,
) (*db.PullRequest, error) {
var retErr error
var retPr *db.PullRequest
switch prEvalInfo.Action {
case WebhookActionEventOpened, WebhookActionEventSynchronize:
dbPr, err := s.store.UpsertPullRequest(ctx, db.UpsertPullRequestParams{
RepositoryID: dbrepo.ID,
PrNumber: prEvalInfo.Number,
})
if err != nil {
return nil, fmt.Errorf(
"cannot upsert PR %d in repo %s/%s",
prEvalInfo.Number, dbrepo.RepoOwner, dbrepo.RepoName)
}
retPr = &dbPr
retErr = nil
case WebhookActionEventClosed:
err := s.store.DeletePullRequest(ctx, db.DeletePullRequestParams{
RepositoryID: dbrepo.ID,
PrNumber: prEvalInfo.Number,
})
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, fmt.Errorf("cannot delete PR record %d in repo %s/%s",
prEvalInfo.Number, dbrepo.RepoOwner, dbrepo.RepoName)
}
retPr = nil
retErr = errNotHandled
default:
log.Printf("action %s is not handled for pull requests", prEvalInfo.Action)
retPr = nil
retErr = errNotHandled
}
return retPr, retErr
}
func updatePullRequestInfoFromProvider(
ctx context.Context,
cli provifv1.GitHub,
dbrepo db.Repository,
prEvalInfo *pb.PullRequest,
) error {
prReply, err := cli.GetPullRequest(ctx, dbrepo.RepoOwner, dbrepo.RepoName, int(prEvalInfo.Number))
if err != nil {
return fmt.Errorf("error getting pull request: %w", err)
}
prEvalInfo.CommitSha = *prReply.Head.SHA
prEvalInfo.RepoOwner = dbrepo.RepoOwner
prEvalInfo.RepoName = dbrepo.RepoName
return nil
}
func getRepoInformationFromPayload(
ctx context.Context,
store db.Store,
payload map[string]any,
) (db.Repository, error) {
repoInfo, ok := payload["repository"].(map[string]any)
if !ok {
return db.Repository{}, fmt.Errorf("unable to determine repository for event: %w", errRepoNotFound)
}
id, err := parseRepoID(repoInfo["id"])
if err != nil {
return db.Repository{}, fmt.Errorf("error parsing repository ID: %w", err)
}
// At this point, we're unsure what the project ID is, so we need to look it up.
// It's the same case for the provider. We can gather this information from the
// repository ID.
dbrepo, err := store.GetRepositoryByRepoID(ctx, id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
log.Printf("repository %d not found", id)
// no use in continuing if the repository doesn't exist
return db.Repository{}, fmt.Errorf("repository %d not found: %w", id, errRepoNotFound)
}
return db.Repository{}, fmt.Errorf("error getting repository: %w", err)
}
if dbrepo.ProjectID.String() == "" {
return db.Repository{}, fmt.Errorf("no project found for repository %s/%s: %w",
dbrepo.RepoOwner, dbrepo.RepoName, errRepoNotFound)
}
// ignore processing webhooks for private repositories
isPrivate, ok := repoInfo["private"].(bool)
if ok {
if isPrivate && !features.ProjectAllowsPrivateRepos(ctx, store, dbrepo.ProjectID) {
return db.Repository{}, errRepoIsPrivate
}
}
log.Printf("handling event for repository %d", id)
return dbrepo, nil
}
func parseRepoID(repoID any) (int64, error) {
switch v := repoID.(type) {
case int32:
return int64(v), nil
case int64:
return v, nil
case float64:
return int64(v), nil
case string:
// convert string to int
return strconv.ParseInt(v, 10, 64)
default:
return 0, fmt.Errorf("unknown type for repoID: %T", v)
}
}
func shouldIssueDeletionEvent(m *message.Message) bool {
return m.Metadata.Get(entities.ActionEventKey) == WebhookActionEventDeleted &&
deletionOfRelevantType(m)
}
func deletionOfRelevantType(m *message.Message) bool {
return m.Metadata.Get(events.GithubWebhookEventTypeKey) == "repository" ||
m.Metadata.Get(events.GithubWebhookEventTypeKey) == "meta"
}