preview.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package actions
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "github.com/google/go-github/v41/github"
  8. "gopkg.in/yaml.v2"
  9. )
  10. type EnvOpts struct {
  11. Client *github.Client
  12. ServerURL string
  13. PorterToken string
  14. GitRepoOwner, GitRepoName string
  15. EnvironmentName string
  16. ProjectID, ClusterID, GitInstallationID uint
  17. }
  18. func SetupEnv(opts *EnvOpts) error {
  19. // create Github environment if it does not exist
  20. _, resp, err := opts.Client.Repositories.GetEnvironment(
  21. context.Background(),
  22. opts.GitRepoOwner,
  23. opts.GitRepoName,
  24. opts.EnvironmentName,
  25. )
  26. if resp.StatusCode == http.StatusNotFound {
  27. _, _, err := opts.Client.Repositories.CreateUpdateEnvironment(
  28. context.Background(),
  29. opts.GitRepoOwner,
  30. opts.GitRepoName,
  31. opts.EnvironmentName,
  32. nil,
  33. )
  34. if err != nil {
  35. return err
  36. }
  37. } else if err != nil {
  38. return err
  39. }
  40. // create porter token secret
  41. err = createGithubSecret(
  42. opts.Client,
  43. getPorterTokenSecretName(opts.ProjectID),
  44. opts.PorterToken,
  45. opts.GitRepoOwner,
  46. opts.GitRepoName,
  47. )
  48. if err != nil {
  49. return err
  50. }
  51. // get the repository to find the default branch
  52. repo, _, err := opts.Client.Repositories.Get(
  53. context.TODO(),
  54. opts.GitRepoOwner,
  55. opts.GitRepoName,
  56. )
  57. if err != nil {
  58. return err
  59. }
  60. defaultBranch := repo.GetDefaultBranch()
  61. applyWorkflowYAML, err := getPreviewApplyActionYAML(opts)
  62. if err != nil {
  63. return err
  64. }
  65. _, err = commitGithubFile(
  66. opts.Client,
  67. fmt.Sprintf("porter_%s_env.yml", strings.ToLower(opts.EnvironmentName)),
  68. applyWorkflowYAML,
  69. opts.GitRepoOwner,
  70. opts.GitRepoName,
  71. defaultBranch,
  72. false,
  73. )
  74. if err != nil {
  75. return err
  76. }
  77. deleteWorkflowYAML, err := getPreviewDeleteActionYAML(opts)
  78. if err != nil {
  79. return err
  80. }
  81. _, err = commitGithubFile(
  82. opts.Client,
  83. fmt.Sprintf("porter_%s_delete_env.yml", strings.ToLower(opts.EnvironmentName)),
  84. deleteWorkflowYAML,
  85. opts.GitRepoOwner,
  86. opts.GitRepoName,
  87. defaultBranch,
  88. false,
  89. )
  90. if err != nil {
  91. return err
  92. }
  93. return err
  94. }
  95. func DeleteEnv(opts *EnvOpts) error {
  96. // get the repository to find the default branch
  97. repo, _, err := opts.Client.Repositories.Get(
  98. context.TODO(),
  99. opts.GitRepoOwner,
  100. opts.GitRepoName,
  101. )
  102. if err != nil {
  103. return err
  104. }
  105. defaultBranch := repo.GetDefaultBranch()
  106. // delete GitHub Environment
  107. _, err = opts.Client.Repositories.DeleteEnvironment(
  108. context.Background(),
  109. opts.GitRepoOwner,
  110. opts.GitRepoName,
  111. opts.EnvironmentName,
  112. )
  113. if err != nil {
  114. return err
  115. }
  116. err = deleteGithubFile(
  117. opts.Client,
  118. fmt.Sprintf("porter_%s_env.yml", strings.ToLower(opts.EnvironmentName)),
  119. opts.GitRepoOwner,
  120. opts.GitRepoName,
  121. defaultBranch,
  122. false,
  123. )
  124. if err != nil {
  125. return err
  126. }
  127. return deleteGithubFile(
  128. opts.Client,
  129. fmt.Sprintf("porter_%s_delete_env.yml", strings.ToLower(opts.EnvironmentName)),
  130. opts.GitRepoOwner,
  131. opts.GitRepoName,
  132. defaultBranch,
  133. false,
  134. )
  135. }
  136. func getPreviewApplyActionYAML(opts *EnvOpts) ([]byte, error) {
  137. gaSteps := []GithubActionYAMLStep{
  138. getCheckoutCodeStep(),
  139. getCreatePreviewEnvStep(
  140. opts.ServerURL,
  141. getPorterTokenSecretName(opts.ProjectID),
  142. opts.ProjectID,
  143. opts.ClusterID,
  144. opts.GitInstallationID,
  145. opts.GitRepoName,
  146. // TODO: change to actual release version
  147. "master",
  148. ),
  149. }
  150. actionYAML := GithubActionYAML{
  151. On: []string{"pull_request"},
  152. Name: "Porter Preview Environment",
  153. Jobs: map[string]GithubActionYAMLJob{
  154. "porter-preview": {
  155. RunsOn: "ubuntu-latest",
  156. Steps: gaSteps,
  157. },
  158. },
  159. }
  160. return yaml.Marshal(actionYAML)
  161. }
  162. func getPreviewDeleteActionYAML(opts *EnvOpts) ([]byte, error) {
  163. gaSteps := []GithubActionYAMLStep{
  164. getDeletePreviewEnvStep(
  165. opts.ServerURL,
  166. getPorterTokenSecretName(opts.ProjectID),
  167. opts.ProjectID,
  168. opts.ClusterID,
  169. opts.GitInstallationID,
  170. opts.GitRepoName,
  171. // TODO: change to actual release version
  172. "master",
  173. ),
  174. }
  175. actionYAML := GithubActionYAML{
  176. On: map[string]interface{}{
  177. "pull_request": map[string]interface{}{
  178. "types": []string{"closed"},
  179. },
  180. },
  181. Name: "Porter Preview Environment",
  182. Jobs: map[string]GithubActionYAMLJob{
  183. "porter-delete-preview": {
  184. RunsOn: "ubuntu-latest",
  185. Steps: gaSteps,
  186. },
  187. },
  188. }
  189. return yaml.Marshal(actionYAML)
  190. }