environment.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. GitRepoBranches string
  16. Name string
  17. Mode string
  18. NewCommentsDisabled bool
  19. CustomNamespace bool
  20. NamespaceAnnotations []byte
  21. // WebhookID uniquely identifies the environment when other fields (project, cluster)
  22. // aren't present
  23. WebhookID string `gorm:"unique"`
  24. GithubWebhookID int64
  25. }
  26. func getGitRepoBranches(branches string) []string {
  27. var branchesArr []string
  28. if branches != "" {
  29. supposedBranches := strings.Split(branches, ",")
  30. for _, br := range supposedBranches {
  31. name := strings.TrimSpace(br)
  32. if len(name) > 0 {
  33. branchesArr = append(branchesArr, name)
  34. }
  35. }
  36. }
  37. return branchesArr
  38. }
  39. func (e *Environment) ToEnvironmentType() *types.Environment {
  40. env := &types.Environment{
  41. ID: e.Model.ID,
  42. ProjectID: e.ProjectID,
  43. ClusterID: e.ClusterID,
  44. GitInstallationID: e.GitInstallationID,
  45. GitRepoOwner: e.GitRepoOwner,
  46. GitRepoName: e.GitRepoName,
  47. NewCommentsDisabled: e.NewCommentsDisabled,
  48. CustomNamespace: e.CustomNamespace,
  49. NamespaceAnnotations: make(map[string]string),
  50. Name: e.Name,
  51. Mode: e.Mode,
  52. }
  53. branches := getGitRepoBranches(e.GitRepoBranches)
  54. if len(branches) > 0 {
  55. env.GitRepoBranches = branches
  56. } else {
  57. env.GitRepoBranches = []string{}
  58. }
  59. if len(e.NamespaceAnnotations) > 0 {
  60. env.NamespaceAnnotations = make(map[string]string)
  61. annotations := string(e.NamespaceAnnotations)
  62. for _, a := range strings.Split(annotations, ",") {
  63. k, v, found := strings.Cut(a, "=")
  64. if found {
  65. env.NamespaceAnnotations[k] = v
  66. }
  67. }
  68. }
  69. return env
  70. }
  71. type Deployment struct {
  72. gorm.Model
  73. EnvironmentID uint
  74. Namespace string
  75. Status types.DeploymentStatus
  76. Subdomain string
  77. PullRequestID uint
  78. GHDeploymentID int64
  79. GHPRCommentID int64
  80. PRName string
  81. RepoName string
  82. RepoOwner string
  83. CommitSHA string
  84. PRBranchFrom string
  85. PRBranchInto string
  86. }
  87. func (d *Deployment) ToDeploymentType() *types.Deployment {
  88. ghMetadata := &types.GitHubMetadata{
  89. DeploymentID: d.GHDeploymentID,
  90. PRName: d.PRName,
  91. RepoName: d.RepoName,
  92. RepoOwner: d.RepoOwner,
  93. CommitSHA: d.CommitSHA,
  94. PRBranchFrom: d.PRBranchFrom,
  95. PRBranchInto: d.PRBranchInto,
  96. }
  97. return &types.Deployment{
  98. CreatedAt: d.CreatedAt,
  99. UpdatedAt: d.UpdatedAt,
  100. ID: d.Model.ID,
  101. EnvironmentID: d.EnvironmentID,
  102. Namespace: d.Namespace,
  103. Status: d.Status,
  104. Subdomain: d.Subdomain,
  105. PullRequestID: d.PullRequestID,
  106. GitHubMetadata: ghMetadata,
  107. }
  108. }