environment.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. NamespaceLabels []byte
  20. NamespaceAnnotations []byte
  21. GitDeployBranches string
  22. // WebhookID uniquely identifies the environment when other fields (project, cluster)
  23. // aren't present
  24. WebhookID string `gorm:"unique"`
  25. GithubWebhookID int64
  26. }
  27. func getGitRepoBranches(branches string) []string {
  28. var branchesArr []string
  29. if branches != "" {
  30. supposedBranches := strings.Split(branches, ",")
  31. for _, br := range supposedBranches {
  32. name := strings.TrimSpace(br)
  33. if len(name) > 0 {
  34. branchesArr = append(branchesArr, name)
  35. }
  36. }
  37. }
  38. return branchesArr
  39. }
  40. func (e *Environment) ToEnvironmentType() *types.Environment {
  41. env := &types.Environment{
  42. ID: e.Model.ID,
  43. ProjectID: e.ProjectID,
  44. ClusterID: e.ClusterID,
  45. GitInstallationID: e.GitInstallationID,
  46. GitRepoOwner: e.GitRepoOwner,
  47. GitRepoName: e.GitRepoName,
  48. NewCommentsDisabled: e.NewCommentsDisabled,
  49. NamespaceLabels: 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. branches = getGitRepoBranches(e.GitDeployBranches)
  60. if len(branches) > 0 {
  61. env.GitDeployBranches = branches
  62. } else {
  63. env.GitDeployBranches = []string{}
  64. }
  65. if len(e.NamespaceLabels) > 0 {
  66. env.NamespaceLabels = make(map[string]string)
  67. labels := string(e.NamespaceLabels)
  68. for _, a := range strings.Split(labels, ",") {
  69. k, v, found := strings.Cut(a, "=")
  70. if found {
  71. env.NamespaceLabels[k] = v
  72. }
  73. }
  74. }
  75. return env
  76. }
  77. type DeploymentRevision struct {
  78. gorm.Model
  79. DeploymentID uint
  80. RevisionNumber uint
  81. Status types.DeploymentStatus
  82. Resources []DeploymentRevisionResource
  83. }
  84. func (d *DeploymentRevision) ToDeploymentRevisionType() *types.DeploymentRevision {
  85. resources := []types.DeploymentRevisionResource{}
  86. for _, res := range d.Resources {
  87. resources = append(resources, res.ToDeploymentResourceType())
  88. }
  89. return &types.DeploymentRevision{
  90. RevisionNumber: d.RevisionNumber,
  91. DeploymentID: d.DeploymentID,
  92. Status: d.Status,
  93. Resources: resources,
  94. }
  95. }
  96. type DeploymentRevisionResource struct {
  97. gorm.Model
  98. DeploymentRevisionID uint
  99. Name string
  100. Type string
  101. Status types.DeploymentStatus
  102. Errors string
  103. }
  104. func (d *DeploymentRevisionResource) ToDeploymentResourceType() types.DeploymentRevisionResource {
  105. res := types.DeploymentRevisionResource{
  106. Name: d.Name,
  107. Type: d.Type,
  108. Status: d.Status,
  109. Errors: d.Errors,
  110. }
  111. return res
  112. }
  113. type Deployment struct {
  114. gorm.Model
  115. EnvironmentID uint
  116. Namespace string
  117. Status types.DeploymentStatus
  118. Subdomain string
  119. PullRequestID uint
  120. GHDeploymentID int64
  121. GHPRCommentID int64
  122. PRName string
  123. RepoName string
  124. RepoOwner string
  125. CommitSHA string
  126. PRBranchFrom string
  127. PRBranchInto string
  128. Revisions []DeploymentRevision
  129. }
  130. func (d *Deployment) ToDeploymentType() *types.Deployment {
  131. ghMetadata := &types.GitHubMetadata{
  132. DeploymentID: d.GHDeploymentID,
  133. PRName: d.PRName,
  134. RepoName: d.RepoName,
  135. RepoOwner: d.RepoOwner,
  136. CommitSHA: d.CommitSHA,
  137. PRBranchFrom: d.PRBranchFrom,
  138. PRBranchInto: d.PRBranchInto,
  139. }
  140. return &types.Deployment{
  141. CreatedAt: d.CreatedAt,
  142. UpdatedAt: d.UpdatedAt,
  143. ID: d.Model.ID,
  144. EnvironmentID: d.EnvironmentID,
  145. Namespace: d.Namespace,
  146. Status: d.Status,
  147. Subdomain: d.Subdomain,
  148. PullRequestID: d.PullRequestID,
  149. GitHubMetadata: ghMetadata,
  150. }
  151. }
  152. func (d *Deployment) IsBranchDeploy() bool {
  153. return d.PullRequestID == 0 && d.PRBranchFrom != "" && d.PRBranchInto != "" && d.PRBranchFrom == d.PRBranchInto
  154. }