enable_pull_request.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. if !project.PreviewEnvsEnabled {
  35. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(errPreviewProjectDisabled, http.StatusForbidden))
  36. return
  37. } else if !cluster.PreviewEnvsEnabled {
  38. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(errPreviewClusterDisabled, http.StatusForbidden))
  39. return
  40. }
  41. request := &types.PullRequest{}
  42. if ok := c.DecodeAndValidate(w, r, request); !ok {
  43. return
  44. }
  45. env, err := c.Repo().Environment().ReadEnvironmentByOwnerRepoName(project.ID, cluster.ID, request.RepoOwner, request.RepoName)
  46. if err != nil {
  47. if errors.Is(err, gorm.ErrRecordNotFound) {
  48. c.HandleAPIError(w, r, apierrors.NewErrNotFound(fmt.Errorf("environment not found in cluster and project")))
  49. return
  50. }
  51. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  52. return
  53. }
  54. client, err := getGithubClientFromEnvironment(c.Config(), env)
  55. if err != nil {
  56. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  57. return
  58. }
  59. // add an extra check that the installation has permission to read this pull request
  60. pr, _, err := client.PullRequests.Get(r.Context(), env.GitRepoOwner, env.GitRepoName, int(request.Number))
  61. if err != nil {
  62. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("%v: %w", errGithubAPI, err),
  63. http.StatusConflict))
  64. return
  65. }
  66. if pr.GetState() == "closed" {
  67. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("cannot enable deployment for closed PR"),
  68. http.StatusConflict))
  69. return
  70. }
  71. ghResp, err := client.Actions.CreateWorkflowDispatchEventByFileName(
  72. r.Context(), env.GitRepoOwner, env.GitRepoName, fmt.Sprintf("porter_%s_env.yml", env.Name),
  73. github.CreateWorkflowDispatchEventRequest{
  74. Ref: request.BranchFrom,
  75. Inputs: map[string]interface{}{
  76. "pr_number": strconv.FormatUint(uint64(request.Number), 10),
  77. "pr_title": *pr.Title,
  78. "pr_branch_from": request.BranchFrom,
  79. "pr_branch_into": request.BranchInto,
  80. },
  81. },
  82. )
  83. if ghResp != nil {
  84. if ghResp.StatusCode == 404 {
  85. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  86. fmt.Errorf(
  87. "please make sure the preview environment workflow files are present in PR branch %s and are up to"+
  88. " date with the default branch", request.BranchFrom,
  89. ), http.StatusConflict),
  90. )
  91. return
  92. } else if ghResp.StatusCode == 422 {
  93. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  94. fmt.Errorf(
  95. "please make sure the workflow files in PR branch %s are up to date with the default branch",
  96. request.BranchFrom,
  97. ), http.StatusConflict),
  98. )
  99. return
  100. }
  101. }
  102. if err != nil {
  103. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  104. return
  105. }
  106. }