environment.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. return &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. GitRepoBranches: getGitRepoBranches(e.GitRepoBranches),
  46. NewCommentsDisabled: e.NewCommentsDisabled,
  47. Name: e.Name,
  48. Mode: e.Mode,
  49. }
  50. }
  51. type Deployment struct {
  52. gorm.Model
  53. EnvironmentID uint
  54. Namespace string
  55. Status types.DeploymentStatus
  56. Subdomain string
  57. PullRequestID uint
  58. GHDeploymentID int64
  59. GHPRCommentID int64
  60. PRName string
  61. RepoName string
  62. RepoOwner string
  63. CommitSHA string
  64. PRBranchFrom string
  65. PRBranchInto string
  66. }
  67. func (d *Deployment) ToDeploymentType() *types.Deployment {
  68. ghMetadata := &types.GitHubMetadata{
  69. DeploymentID: d.GHDeploymentID,
  70. PRName: d.PRName,
  71. RepoName: d.RepoName,
  72. RepoOwner: d.RepoOwner,
  73. CommitSHA: d.CommitSHA,
  74. PRBranchFrom: d.PRBranchFrom,
  75. PRBranchInto: d.PRBranchInto,
  76. }
  77. return &types.Deployment{
  78. CreatedAt: d.CreatedAt,
  79. UpdatedAt: d.UpdatedAt,
  80. ID: d.Model.ID,
  81. EnvironmentID: d.EnvironmentID,
  82. Namespace: d.Namespace,
  83. Status: d.Status,
  84. Subdomain: d.Subdomain,
  85. PullRequestID: d.PullRequestID,
  86. GitHubMetadata: ghMetadata,
  87. }
  88. }