enable_pull_request.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 in the settings page",
  59. request.BranchInto), http.StatusBadRequest,
  60. ))
  61. return
  62. }
  63. }
  64. client, err := getGithubClientFromEnvironment(c.Config(), env)
  65. if err != nil {
  66. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  67. return
  68. }
  69. // add an extra check that the installation has permission to read this pull request
  70. pr, _, err := client.PullRequests.Get(r.Context(), env.GitRepoOwner, env.GitRepoName, int(request.Number))
  71. if err != nil {
  72. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("%v: %w", errGithubAPI, err),
  73. http.StatusConflict))
  74. return
  75. }
  76. if pr.GetState() == "closed" {
  77. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("cannot enable deployment for closed PR"),
  78. http.StatusConflict))
  79. return
  80. }
  81. ghResp, err := client.Actions.CreateWorkflowDispatchEventByFileName(
  82. r.Context(), env.GitRepoOwner, env.GitRepoName, fmt.Sprintf("porter_%s_env.yml", env.Name),
  83. github.CreateWorkflowDispatchEventRequest{
  84. Ref: request.BranchFrom,
  85. Inputs: map[string]interface{}{
  86. "pr_number": strconv.FormatUint(uint64(request.Number), 10),
  87. "pr_title": *pr.Title,
  88. "pr_branch_from": request.BranchFrom,
  89. "pr_branch_into": request.BranchInto,
  90. },
  91. },
  92. )
  93. if ghResp != nil {
  94. if ghResp.StatusCode == 404 {
  95. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  96. fmt.Errorf(
  97. "please make sure the preview environment workflow files are present in PR branch %s and are up to"+
  98. " date with the default branch", request.BranchFrom,
  99. ), http.StatusConflict),
  100. )
  101. return
  102. } else if ghResp.StatusCode == 422 {
  103. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  104. fmt.Errorf(
  105. "please make sure the workflow files in PR branch %s are up to date with the default branch",
  106. request.BranchFrom,
  107. ), http.StatusConflict),
  108. )
  109. return
  110. }
  111. }
  112. if err != nil {
  113. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  114. return
  115. }
  116. // create the deployment
  117. depl, err := c.Repo().Environment().CreateDeployment(&models.Deployment{
  118. EnvironmentID: env.ID,
  119. Namespace: "",
  120. Status: types.DeploymentStatusCreating,
  121. PullRequestID: request.Number,
  122. RepoOwner: request.RepoOwner,
  123. RepoName: request.RepoName,
  124. PRName: request.Title,
  125. PRBranchFrom: request.BranchFrom,
  126. PRBranchInto: request.BranchInto,
  127. })
  128. if err != nil {
  129. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  130. return
  131. }
  132. c.WriteResult(w, r, depl.ToDeploymentType())
  133. }