stack.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. }
  15. func OpenGithubPR(opts GithubPROpts) error {
  16. // get the repository to find the default branch
  17. repo, _, err := opts.Client.Repositories.Get(
  18. context.TODO(),
  19. opts.GitRepoOwner,
  20. opts.GitRepoName,
  21. )
  22. if err != nil {
  23. return err
  24. }
  25. defaultBranch := repo.GetDefaultBranch()
  26. err = createNewBranch(opts.Client,
  27. opts.GitRepoOwner,
  28. opts.GitRepoName,
  29. defaultBranch,
  30. "porter-stack")
  31. if err != nil {
  32. return fmt.Errorf(
  33. "Unable to create PR to merge workflow files into protected branch: %s.\n"+
  34. "To enable Porter Preview Environment deployments, please create Github workflow "+
  35. "files in this branch with the following contents:\n"+
  36. "--------\n%s--------\nERROR: %w",
  37. defaultBranch, string(applyWorkflowYAML), ErrCreatePRForProtectedBranch,
  38. )
  39. }
  40. _, err = commitWorkflowFile(
  41. opts.Client,
  42. fmt.Sprintf("porter_%s_env.yml", strings.ToLower(opts.EnvironmentName)),
  43. applyWorkflowYAML, opts.GitRepoOwner,
  44. opts.GitRepoName, "porter-preview", false,
  45. )
  46. if err != nil {
  47. return fmt.Errorf(
  48. "Unable to create PR to merge workflow files into protected branch: %s.\n"+
  49. "To enable Porter Preview Environment deployments, please create Github workflow "+
  50. "files in this branch with the following contents:\n"+
  51. "--------\n%s--------\nERROR: %w",
  52. defaultBranch, string(applyWorkflowYAML), ErrCreatePRForProtectedBranch,
  53. )
  54. }
  55. pr, _, err := opts.Client.PullRequests.Create(
  56. context.Background(), opts.GitRepoOwner, opts.GitRepoName, &github.NewPullRequest{
  57. Title: github.String("Enable Porter Preview Environment deployments"),
  58. Base: github.String(defaultBranch),
  59. Head: github.String("porter-preview"),
  60. },
  61. )
  62. if err != nil {
  63. return err
  64. }
  65. return nil
  66. }
  67. func getStackApplyActionYAML(opts *EnvOpts) ([]byte, error) {
  68. gaSteps := []GithubActionYAMLStep{
  69. getCheckoutCodeStep(),
  70. getCreatePreviewEnvStep(
  71. opts.ServerURL,
  72. getPreviewEnvSecretName(opts.ProjectID, opts.ClusterID, opts.InstanceName),
  73. opts.ProjectID,
  74. opts.ClusterID,
  75. opts.GitInstallationID,
  76. opts.GitRepoOwner,
  77. opts.GitRepoName,
  78. "v0.2.1",
  79. ),
  80. }
  81. actionYAML := GithubActionYAML{
  82. On: map[string]interface{}{
  83. "workflow_dispatch": map[string]interface{}{
  84. "inputs": map[string]interface{}{
  85. "pr_number": map[string]interface{}{
  86. "description": "Pull request number",
  87. "type": "string",
  88. "required": true,
  89. },
  90. "pr_title": map[string]interface{}{
  91. "description": "Pull request title",
  92. "type": "string",
  93. "required": true,
  94. },
  95. "pr_branch_from": map[string]interface{}{
  96. "description": "Pull request head branch",
  97. "type": "string",
  98. "required": true,
  99. },
  100. "pr_branch_into": map[string]interface{}{
  101. "description": "Pull request base branch",
  102. "type": "string",
  103. "required": true,
  104. },
  105. },
  106. },
  107. },
  108. Name: "Porter Preview Environment",
  109. Jobs: map[string]GithubActionYAMLJob{
  110. "porter-preview": {
  111. RunsOn: "ubuntu-latest",
  112. Concurrency: map[string]string{
  113. "group": "${{ github.workflow }}-${{ github.event.inputs.pr_number }}",
  114. },
  115. Steps: gaSteps,
  116. },
  117. },
  118. }
  119. return yaml.Marshal(actionYAML)
  120. }