environment.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package models
  2. import (
  3. "strings"
  4. "github.com/porter-dev/porter/api/types"
  5. "gorm.io/gorm"
  6. )
  7. type EnvironmentMode uint
  8. type Environment struct {
  9. gorm.Model
  10. ProjectID uint
  11. ClusterID uint
  12. GitInstallationID uint
  13. GitRepoOwner string
  14. GitRepoName string
  15. Name string
  16. Mode string
  17. NewCommentsDisabled bool
  18. NamespaceAnnotations []byte
  19. // WebhookID uniquely identifies the environment when other fields (project, cluster)
  20. // aren't present
  21. WebhookID string `gorm:"unique"`
  22. GithubWebhookID int64
  23. }
  24. func (e *Environment) ToEnvironmentType() *types.Environment {
  25. env := &types.Environment{
  26. ID: e.Model.ID,
  27. ProjectID: e.ProjectID,
  28. ClusterID: e.ClusterID,
  29. GitInstallationID: e.GitInstallationID,
  30. GitRepoOwner: e.GitRepoOwner,
  31. GitRepoName: e.GitRepoName,
  32. NewCommentsDisabled: e.NewCommentsDisabled,
  33. NamespaceAnnotations: make(map[string]string),
  34. Name: e.Name,
  35. Mode: e.Mode,
  36. }
  37. if len(e.NamespaceAnnotations) > 0 {
  38. env.NamespaceAnnotations = make(map[string]string)
  39. annotations := string(e.NamespaceAnnotations)
  40. for _, a := range strings.Split(annotations, ",") {
  41. k, v, found := strings.Cut(a, "=")
  42. if found {
  43. env.NamespaceAnnotations[k] = v
  44. }
  45. }
  46. }
  47. return env
  48. }
  49. type Deployment struct {
  50. gorm.Model
  51. EnvironmentID uint
  52. Namespace string
  53. Status types.DeploymentStatus
  54. Subdomain string
  55. PullRequestID uint
  56. GHDeploymentID int64
  57. GHPRCommentID int64
  58. PRName string
  59. RepoName string
  60. RepoOwner string
  61. CommitSHA string
  62. PRBranchFrom string
  63. PRBranchInto string
  64. }
  65. func (d *Deployment) ToDeploymentType() *types.Deployment {
  66. ghMetadata := &types.GitHubMetadata{
  67. DeploymentID: d.GHDeploymentID,
  68. PRName: d.PRName,
  69. RepoName: d.RepoName,
  70. RepoOwner: d.RepoOwner,
  71. CommitSHA: d.CommitSHA,
  72. PRBranchFrom: d.PRBranchFrom,
  73. PRBranchInto: d.PRBranchInto,
  74. }
  75. return &types.Deployment{
  76. CreatedAt: d.CreatedAt,
  77. UpdatedAt: d.UpdatedAt,
  78. ID: d.Model.ID,
  79. EnvironmentID: d.EnvironmentID,
  80. Namespace: d.Namespace,
  81. Status: d.Status,
  82. Subdomain: d.Subdomain,
  83. PullRequestID: d.PullRequestID,
  84. GitHubMetadata: ghMetadata,
  85. }
  86. }