github_webhook.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package gorm
  2. import (
  3. "context"
  4. "time"
  5. "github.com/google/uuid"
  6. "github.com/porter-dev/porter/internal/models"
  7. "github.com/porter-dev/porter/internal/repository"
  8. "github.com/porter-dev/porter/internal/telemetry"
  9. "gorm.io/gorm"
  10. )
  11. // GithubWebhookRepository uses gorm.DB for querying the database
  12. type GithubWebhookRepository struct {
  13. db *gorm.DB
  14. }
  15. // NewGithubWebhookRepository returns a GithubWebhookRepository which uses
  16. // gorm.DB for querying the database
  17. func NewGithubWebhookRepository(db *gorm.DB) repository.GithubWebhookRepository {
  18. return &GithubWebhookRepository{db}
  19. }
  20. // Insert inserts a new GithubWebhook into the db
  21. func (repo *GithubWebhookRepository) Insert(ctx context.Context, webhook *models.GithubWebhook) (*models.GithubWebhook, error) {
  22. ctx, span := telemetry.NewSpan(ctx, "gorm-insert-github-webhook")
  23. defer span.End()
  24. if webhook == nil {
  25. return nil, telemetry.Error(ctx, span, nil, "github webhook is nil")
  26. }
  27. if webhook.ClusterID == 0 {
  28. return nil, telemetry.Error(ctx, span, nil, "cluster id is empty")
  29. }
  30. if webhook.ProjectID == 0 {
  31. return nil, telemetry.Error(ctx, span, nil, "project id is empty")
  32. }
  33. if webhook.PorterAppID == 0 {
  34. return nil, telemetry.Error(ctx, span, nil, "porter app id is empty")
  35. }
  36. if webhook.ID == uuid.Nil {
  37. webhook.ID = uuid.New()
  38. }
  39. if webhook.CreatedAt.IsZero() {
  40. webhook.CreatedAt = time.Now().UTC()
  41. }
  42. if webhook.UpdatedAt.IsZero() {
  43. webhook.UpdatedAt = time.Now().UTC()
  44. }
  45. if err := repo.db.Save(webhook).Error; err != nil {
  46. return nil, telemetry.Error(ctx, span, err, "error saving webhook")
  47. }
  48. return webhook, nil
  49. }
  50. // GetByClusterAndAppID finds a GithubWebhook by clusterID and appID
  51. func (repo *GithubWebhookRepository) GetByClusterAndAppID(ctx context.Context, clusterID uint, appID uint) (*models.GithubWebhook, error) {
  52. ctx, span := telemetry.NewSpan(ctx, "gorm-get-github-webhook")
  53. defer span.End()
  54. webhook := &models.GithubWebhook{}
  55. if err := repo.db.Where("cluster_id = ? AND porter_app_id = ?", clusterID, appID).Limit(1).Find(&webhook).Error; err != nil {
  56. return nil, telemetry.Error(ctx, span, err, "error finding webhook")
  57. }
  58. return webhook, nil
  59. }
  60. // Get finds a GithubWebhook by id
  61. func (repo *GithubWebhookRepository) Get(ctx context.Context, id uuid.UUID) (*models.GithubWebhook, error) {
  62. ctx, span := telemetry.NewSpan(ctx, "gorm-get-github-webhook")
  63. defer span.End()
  64. webhook := &models.GithubWebhook{}
  65. if err := repo.db.Where("id = ?", id).Limit(1).Find(&webhook).Error; err != nil {
  66. return nil, telemetry.Error(ctx, span, err, "error finding webhook")
  67. }
  68. return webhook, nil
  69. }