environment.go 2.4 KB

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