enable_pull_request.go 4.0 KB

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