enable_pull_request.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package environment
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "github.com/google/go-github/v41/github"
  9. "github.com/porter-dev/porter/api/server/authz"
  10. "github.com/porter-dev/porter/api/server/handlers"
  11. "github.com/porter-dev/porter/api/server/shared"
  12. "github.com/porter-dev/porter/api/server/shared/apierrors"
  13. "github.com/porter-dev/porter/api/server/shared/config"
  14. "github.com/porter-dev/porter/api/types"
  15. "github.com/porter-dev/porter/internal/models"
  16. "gorm.io/gorm"
  17. )
  18. type EnablePullRequestHandler struct {
  19. handlers.PorterHandlerReadWriter
  20. authz.KubernetesAgentGetter
  21. }
  22. func NewEnablePullRequestHandler(
  23. config *config.Config,
  24. decoderValidator shared.RequestDecoderValidator,
  25. writer shared.ResultWriter,
  26. ) *EnablePullRequestHandler {
  27. return &EnablePullRequestHandler{
  28. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  29. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  30. }
  31. }
  32. func (c *EnablePullRequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  33. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  34. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  35. request := &types.PullRequest{}
  36. if ok := c.DecodeAndValidate(w, r, request); !ok {
  37. return
  38. }
  39. env, err := c.Repo().Environment().ReadEnvironmentByOwnerRepoName(project.ID, cluster.ID, request.RepoOwner, request.RepoName)
  40. if err != nil {
  41. if errors.Is(err, gorm.ErrRecordNotFound) {
  42. c.HandleAPIError(w, r, apierrors.NewErrNotFound(fmt.Errorf("environment not found in cluster and project")))
  43. return
  44. }
  45. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  46. return
  47. }
  48. client, err := getGithubClientFromEnvironment(c.Config(), env)
  49. if err != nil {
  50. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  51. return
  52. }
  53. // add an extra check that the installation has permission to read this pull request
  54. pr, _, err := client.PullRequests.Get(r.Context(), env.GitRepoOwner, env.GitRepoName, int(request.Number))
  55. if err != nil {
  56. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("%v: %w", errGithubAPI, err),
  57. http.StatusConflict))
  58. return
  59. }
  60. if pr.GetState() == "closed" {
  61. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("cannot enable deployment for closed PR"),
  62. http.StatusConflict))
  63. return
  64. }
  65. ghResp, err := client.Actions.CreateWorkflowDispatchEventByFileName(
  66. r.Context(), env.GitRepoOwner, env.GitRepoName, fmt.Sprintf("porter_%s_env.yml", env.Name),
  67. github.CreateWorkflowDispatchEventRequest{
  68. Ref: request.BranchFrom,
  69. Inputs: map[string]interface{}{
  70. "pr_number": strconv.FormatUint(uint64(request.Number), 10),
  71. "pr_title": *pr.Title,
  72. "pr_branch_from": request.BranchFrom,
  73. "pr_branch_into": request.BranchInto,
  74. },
  75. },
  76. )
  77. if ghResp != nil {
  78. if ghResp.StatusCode == 404 {
  79. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  80. fmt.Errorf(
  81. "please make sure the preview environment workflow files are present in PR branch %s and are up to"+
  82. " date with the default branch", request.BranchFrom,
  83. ), http.StatusConflict),
  84. )
  85. return
  86. } else if ghResp.StatusCode == 422 {
  87. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  88. fmt.Errorf(
  89. "please make sure the workflow files in PR branch %s are up to date with the default branch",
  90. request.BranchFrom,
  91. ), http.StatusConflict),
  92. )
  93. return
  94. }
  95. }
  96. if err != nil {
  97. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  98. return
  99. }
  100. namespace := fmt.Sprintf("pr-%d-%s", request.Number, strings.ToLower(strings.ReplaceAll(env.GitRepoName, "_", "-")))
  101. // create the deployment
  102. depl, err := c.Repo().Environment().CreateDeployment(&models.Deployment{
  103. EnvironmentID: env.ID,
  104. Namespace: namespace,
  105. Status: types.DeploymentStatusCreating,
  106. PullRequestID: request.Number,
  107. RepoOwner: request.RepoOwner,
  108. RepoName: request.RepoName,
  109. PRName: request.Title,
  110. PRBranchFrom: request.BranchFrom,
  111. PRBranchInto: request.BranchInto,
  112. })
  113. if err != nil {
  114. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  115. return
  116. }
  117. // create the backing namespace
  118. agent, err := c.GetAgent(r, cluster, "")
  119. if err != nil {
  120. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  121. return
  122. }
  123. _, err = agent.CreateNamespace(depl.Namespace)
  124. if err != nil {
  125. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  126. return
  127. }
  128. }