enable_pull_request.go 5.0 KB

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