environment.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. // 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. NamespaceLabels: 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. if len(e.NamespaceLabels) > 0 {
  59. env.NamespaceLabels = make(map[string]string)
  60. labels := string(e.NamespaceLabels)
  61. for _, a := range strings.Split(labels, ",") {
  62. k, v, found := strings.Cut(a, "=")
  63. if found {
  64. env.NamespaceLabels[k] = v
  65. }
  66. }
  67. }
  68. return env
  69. }
  70. type Deployment struct {
  71. gorm.Model
  72. EnvironmentID uint
  73. Namespace string
  74. Status types.DeploymentStatus
  75. Subdomain string
  76. PullRequestID uint
  77. GHDeploymentID int64
  78. GHPRCommentID int64
  79. PRName string
  80. RepoName string
  81. RepoOwner string
  82. CommitSHA string
  83. PRBranchFrom string
  84. PRBranchInto string
  85. }
  86. func (d *Deployment) ToDeploymentType() *types.Deployment {
  87. ghMetadata := &types.GitHubMetadata{
  88. DeploymentID: d.GHDeploymentID,
  89. PRName: d.PRName,
  90. RepoName: d.RepoName,
  91. RepoOwner: d.RepoOwner,
  92. CommitSHA: d.CommitSHA,
  93. PRBranchFrom: d.PRBranchFrom,
  94. PRBranchInto: d.PRBranchInto,
  95. }
  96. return &types.Deployment{
  97. CreatedAt: d.CreatedAt,
  98. UpdatedAt: d.UpdatedAt,
  99. ID: d.Model.ID,
  100. EnvironmentID: d.EnvironmentID,
  101. Namespace: d.Namespace,
  102. Status: d.Status,
  103. Subdomain: d.Subdomain,
  104. PullRequestID: d.PullRequestID,
  105. GitHubMetadata: ghMetadata,
  106. }
  107. }