stack.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package actions
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "github.com/google/go-github/v41/github"
  7. "gopkg.in/yaml.v2"
  8. )
  9. type GithubPROpts struct {
  10. Client *github.Client
  11. GitRepoOwner, GitRepoName string
  12. ApplyWorkflowYAML string
  13. StackName string
  14. ProjectID, ClusterID uint
  15. ServerURL string
  16. DefaultBranch string
  17. SecretName string
  18. PorterYamlPath string
  19. Body string
  20. DeleteWorkflowFilename string
  21. }
  22. type GetStackApplyActionYAMLOpts struct {
  23. ServerURL string
  24. StackName string
  25. ProjectID, ClusterID uint
  26. DefaultBranch string
  27. SecretName string
  28. PorterYamlPath string
  29. }
  30. func OpenGithubPR(opts *GithubPROpts) (*github.PullRequest, error) {
  31. var pr *github.PullRequest
  32. var prBranchName string
  33. if opts.DeleteWorkflowFilename != "" {
  34. prBranchName = "porter-stack-delete"
  35. } else {
  36. prBranchName = "porter-stack"
  37. }
  38. err := createNewBranch(opts.Client,
  39. opts.GitRepoOwner,
  40. opts.GitRepoName,
  41. opts.DefaultBranch,
  42. prBranchName,
  43. )
  44. if err != nil {
  45. return pr, fmt.Errorf(
  46. "error creating branch: %w",
  47. err,
  48. )
  49. }
  50. if opts.DeleteWorkflowFilename == "" {
  51. applyWorkflowYAML, err := getStackApplyActionYAML(&GetStackApplyActionYAMLOpts{
  52. ServerURL: opts.ServerURL,
  53. ClusterID: opts.ClusterID,
  54. ProjectID: opts.ProjectID,
  55. StackName: opts.StackName,
  56. DefaultBranch: opts.DefaultBranch,
  57. SecretName: opts.SecretName,
  58. PorterYamlPath: opts.PorterYamlPath,
  59. })
  60. if err != nil {
  61. return pr, err
  62. }
  63. _, err = commitWorkflowFile(
  64. opts.Client,
  65. fmt.Sprintf("porter_stack_%s.yml", strings.ToLower(opts.StackName)),
  66. applyWorkflowYAML, opts.GitRepoOwner,
  67. opts.GitRepoName, prBranchName, false,
  68. )
  69. if err != nil {
  70. return pr, fmt.Errorf(
  71. "error committing file: %w",
  72. err,
  73. )
  74. }
  75. } else {
  76. err = deleteGithubFile(
  77. opts.Client,
  78. opts.DeleteWorkflowFilename,
  79. opts.GitRepoOwner,
  80. opts.GitRepoName,
  81. prBranchName,
  82. false,
  83. )
  84. if err != nil {
  85. return pr, fmt.Errorf(
  86. "error committing deletion: %w",
  87. err,
  88. )
  89. }
  90. }
  91. var prTitle string
  92. if opts.DeleteWorkflowFilename != "" {
  93. prTitle = fmt.Sprintf("Delete Porter Application %s", opts.StackName)
  94. } else {
  95. prTitle = fmt.Sprintf("Enable Porter Application %s", opts.StackName)
  96. }
  97. pr, _, err = opts.Client.PullRequests.Create(
  98. context.Background(), opts.GitRepoOwner, opts.GitRepoName, &github.NewPullRequest{
  99. Title: github.String(prTitle),
  100. Base: github.String(opts.DefaultBranch),
  101. Head: github.String(prBranchName),
  102. Body: github.String(opts.Body),
  103. },
  104. )
  105. if err != nil {
  106. return pr, fmt.Errorf(
  107. "error creating PR: %w",
  108. err,
  109. )
  110. }
  111. return pr, nil
  112. }
  113. func getStackApplyActionYAML(opts *GetStackApplyActionYAMLOpts) ([]byte, error) {
  114. gaSteps := []GithubActionYAMLStep{
  115. getCheckoutCodeStep(),
  116. getSetTagStep(),
  117. getDeployStackStep(
  118. opts.ServerURL,
  119. opts.SecretName,
  120. opts.StackName,
  121. "v0.1.0",
  122. opts.PorterYamlPath,
  123. opts.ProjectID,
  124. opts.ClusterID,
  125. ),
  126. }
  127. actionYAML := GithubActionYAML{
  128. On: GithubActionYAMLOnPush{
  129. Push: GithubActionYAMLOnPushBranches{
  130. Branches: []string{
  131. opts.DefaultBranch,
  132. },
  133. },
  134. },
  135. Name: fmt.Sprintf("Deploy to %s", opts.StackName),
  136. Jobs: map[string]GithubActionYAMLJob{
  137. "porter-deploy": {
  138. RunsOn: "ubuntu-latest",
  139. Steps: gaSteps,
  140. },
  141. },
  142. }
  143. return yaml.Marshal(actionYAML)
  144. }