webhook.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package gitinstallation
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/internal/telemetry"
  5. "github.com/google/go-github/v41/github"
  6. "github.com/porter-dev/porter/api/server/authz"
  7. "github.com/porter-dev/porter/api/server/handlers"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. "gorm.io/gorm"
  12. ints "github.com/porter-dev/porter/internal/models/integrations"
  13. )
  14. type GithubAppWebhookHandler struct {
  15. handlers.PorterHandlerReadWriter
  16. authz.KubernetesAgentGetter
  17. }
  18. func NewGithubAppWebhookHandler(
  19. config *config.Config,
  20. decoderValidator shared.RequestDecoderValidator,
  21. writer shared.ResultWriter,
  22. ) *GithubAppWebhookHandler {
  23. return &GithubAppWebhookHandler{
  24. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  25. }
  26. }
  27. func (c *GithubAppWebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. ctx, span := telemetry.NewSpan(r.Context(), "serve-new-github-app-webhook")
  29. defer span.End()
  30. r = r.Clone(ctx)
  31. payload, err := github.ValidatePayload(r, []byte(c.Config().GithubAppConf.WebhookSecret))
  32. if err != nil {
  33. err = telemetry.Error(ctx, span, err, "error validating payload")
  34. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  35. return
  36. }
  37. event, err := github.ParseWebHook(github.WebHookType(r), payload)
  38. if err != nil {
  39. err = telemetry.Error(ctx, span, err, "error parsing webhook")
  40. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  41. return
  42. }
  43. switch e := event.(type) {
  44. case *github.InstallationEvent:
  45. if *e.Action == "created" {
  46. _, err := c.Repo().GithubAppInstallation().ReadGithubAppInstallationByAccountID(*e.Installation.Account.ID)
  47. if err != nil && err == gorm.ErrRecordNotFound {
  48. // insert account/installation pair into database
  49. _, err := c.Repo().GithubAppInstallation().CreateGithubAppInstallation(&ints.GithubAppInstallation{
  50. AccountID: *e.Installation.Account.ID,
  51. InstallationID: *e.Installation.ID,
  52. })
  53. if err != nil {
  54. err = telemetry.Error(ctx, span, err, "error creating github app installation")
  55. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  56. return
  57. }
  58. return
  59. } else if err != nil {
  60. err = telemetry.Error(ctx, span, err, "error reading github app installation")
  61. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  62. return
  63. }
  64. }
  65. if *e.Action == "deleted" {
  66. err := c.Repo().GithubAppInstallation().DeleteGithubAppInstallationByAccountID(*e.Installation.Account.ID)
  67. if err != nil {
  68. err = telemetry.Error(ctx, span, err, "error deleting github app installation")
  69. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  70. return
  71. }
  72. }
  73. }
  74. }