environment.go 2.7 KB

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