2
0

stack.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. }
  21. type GetStackApplyActionYAMLOpts struct {
  22. ServerURL string
  23. StackName string
  24. ProjectID, ClusterID uint
  25. DefaultBranch string
  26. SecretName string
  27. PorterYamlPath string
  28. }
  29. func OpenGithubPR(opts *GithubPROpts) (*github.PullRequest, error) {
  30. var pr *github.PullRequest
  31. applyWorkflowYAML, err := getStackApplyActionYAML(&GetStackApplyActionYAMLOpts{
  32. ServerURL: opts.ServerURL,
  33. ClusterID: opts.ClusterID,
  34. ProjectID: opts.ProjectID,
  35. StackName: opts.StackName,
  36. DefaultBranch: opts.DefaultBranch,
  37. SecretName: opts.SecretName,
  38. PorterYamlPath: opts.PorterYamlPath,
  39. })
  40. if err != nil {
  41. return pr, err
  42. }
  43. prBranchName := "porter-stack"
  44. err = createNewBranch(opts.Client,
  45. opts.GitRepoOwner,
  46. opts.GitRepoName,
  47. opts.DefaultBranch,
  48. prBranchName)
  49. if err != nil {
  50. return pr, fmt.Errorf(
  51. "error creating branch: %w",
  52. err,
  53. )
  54. }
  55. _, err = commitWorkflowFile(
  56. opts.Client,
  57. fmt.Sprintf("porter_stack_%s.yml", strings.ToLower(opts.StackName)),
  58. applyWorkflowYAML, opts.GitRepoOwner,
  59. opts.GitRepoName, prBranchName, false,
  60. )
  61. if err != nil {
  62. return pr, fmt.Errorf(
  63. "error committing file: %w",
  64. err,
  65. )
  66. }
  67. pr, _, err = opts.Client.PullRequests.Create(
  68. context.Background(), opts.GitRepoOwner, opts.GitRepoName, &github.NewPullRequest{
  69. Title: github.String("Enable Porter Application"),
  70. Base: github.String(opts.DefaultBranch),
  71. Head: github.String(prBranchName),
  72. Body: github.String(opts.Body),
  73. },
  74. )
  75. if err != nil {
  76. return pr, fmt.Errorf(
  77. "error creating PR: %w",
  78. err,
  79. )
  80. }
  81. return pr, nil
  82. }
  83. func getStackApplyActionYAML(opts *GetStackApplyActionYAMLOpts) ([]byte, error) {
  84. gaSteps := []GithubActionYAMLStep{
  85. getCheckoutCodeStep(),
  86. getSetTagStep(),
  87. getDeployStackStep(
  88. opts.ServerURL,
  89. opts.SecretName,
  90. opts.StackName,
  91. "v0.1.0",
  92. opts.PorterYamlPath,
  93. opts.ProjectID,
  94. opts.ClusterID,
  95. ),
  96. }
  97. actionYAML := GithubActionYAML{
  98. On: GithubActionYAMLOnPush{
  99. Push: GithubActionYAMLOnPushBranches{
  100. Branches: []string{
  101. opts.DefaultBranch,
  102. },
  103. },
  104. },
  105. Name: "Deploy to Porter",
  106. Jobs: map[string]GithubActionYAMLJob{
  107. "porter-deploy": {
  108. RunsOn: "ubuntu-latest",
  109. Steps: gaSteps,
  110. },
  111. },
  112. }
  113. return yaml.Marshal(actionYAML)
  114. }