2
0

environment.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 Deployment struct {
  78. gorm.Model
  79. EnvironmentID uint
  80. Namespace string
  81. Status types.DeploymentStatus
  82. Subdomain string
  83. PullRequestID uint
  84. GHDeploymentID int64
  85. GHPRCommentID int64
  86. PRName string
  87. RepoName string
  88. RepoOwner string
  89. CommitSHA string
  90. PRBranchFrom string
  91. PRBranchInto string
  92. LastErrors string
  93. }
  94. func (d *Deployment) ToDeploymentType() *types.Deployment {
  95. ghMetadata := &types.GitHubMetadata{
  96. DeploymentID: d.GHDeploymentID,
  97. PRName: d.PRName,
  98. RepoName: d.RepoName,
  99. RepoOwner: d.RepoOwner,
  100. CommitSHA: d.CommitSHA,
  101. PRBranchFrom: d.PRBranchFrom,
  102. PRBranchInto: d.PRBranchInto,
  103. }
  104. return &types.Deployment{
  105. CreatedAt: d.CreatedAt,
  106. UpdatedAt: d.UpdatedAt,
  107. ID: d.Model.ID,
  108. EnvironmentID: d.EnvironmentID,
  109. Namespace: d.Namespace,
  110. Status: d.Status,
  111. Subdomain: d.Subdomain,
  112. PullRequestID: d.PullRequestID,
  113. GitHubMetadata: ghMetadata,
  114. LastErrors: d.LastErrors,
  115. }
  116. }
  117. func (d *Deployment) IsBranchDeploy() bool {
  118. return d.PullRequestID == 0 && d.PRBranchFrom != "" && d.PRBranchInto != "" && d.PRBranchFrom == d.PRBranchInto
  119. }