enable_pull_request.go 4.1 KB

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