stack.go 2.6 KB

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