environment.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. NamespaceAnnotations []byte
  20. GitDeployBranches string
  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. NamespaceAnnotations: make(map[string]string),
  49. Name: e.Name,
  50. Mode: e.Mode,
  51. }
  52. branches := getGitRepoBranches(e.GitRepoBranches)
  53. if len(branches) > 0 {
  54. env.GitRepoBranches = branches
  55. } else {
  56. env.GitRepoBranches = []string{}
  57. }
  58. branches = getGitRepoBranches(e.GitDeployBranches)
  59. if len(branches) > 0 {
  60. env.GitDeployBranches = branches
  61. } else {
  62. env.GitDeployBranches = []string{}
  63. }
  64. if len(e.NamespaceAnnotations) > 0 {
  65. env.NamespaceAnnotations = make(map[string]string)
  66. annotations := string(e.NamespaceAnnotations)
  67. for _, a := range strings.Split(annotations, ",") {
  68. k, v, found := strings.Cut(a, "=")
  69. if found {
  70. env.NamespaceAnnotations[k] = v
  71. }
  72. }
  73. }
  74. return env
  75. }
  76. type Deployment struct {
  77. gorm.Model
  78. EnvironmentID uint
  79. Namespace string
  80. Status types.DeploymentStatus
  81. Subdomain string
  82. PullRequestID uint
  83. GHDeploymentID int64
  84. GHPRCommentID int64
  85. PRName string
  86. RepoName string
  87. RepoOwner string
  88. CommitSHA string
  89. PRBranchFrom string
  90. PRBranchInto string
  91. }
  92. func (d *Deployment) ToDeploymentType() *types.Deployment {
  93. ghMetadata := &types.GitHubMetadata{
  94. DeploymentID: d.GHDeploymentID,
  95. PRName: d.PRName,
  96. RepoName: d.RepoName,
  97. RepoOwner: d.RepoOwner,
  98. CommitSHA: d.CommitSHA,
  99. PRBranchFrom: d.PRBranchFrom,
  100. PRBranchInto: d.PRBranchInto,
  101. }
  102. return &types.Deployment{
  103. CreatedAt: d.CreatedAt,
  104. UpdatedAt: d.UpdatedAt,
  105. ID: d.Model.ID,
  106. EnvironmentID: d.EnvironmentID,
  107. Namespace: d.Namespace,
  108. Status: d.Status,
  109. Subdomain: d.Subdomain,
  110. PullRequestID: d.PullRequestID,
  111. GitHubMetadata: ghMetadata,
  112. }
  113. }
  114. func (d *Deployment) IsBranchDeploy() bool {
  115. return d.PullRequestID == 0 && d.PRBranchFrom != "" && d.PRBranchInto != "" && d.PRBranchFrom == d.PRBranchInto
  116. }