enable_pull_request.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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, ghResp, 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. ghResp, err = client.Actions.CreateWorkflowDispatchEventByFileName(
  54. r.Context(), env.GitRepoOwner, env.GitRepoName, fmt.Sprintf("porter_%s_env.yml", env.Name),
  55. github.CreateWorkflowDispatchEventRequest{
  56. Ref: request.BranchFrom,
  57. Inputs: map[string]interface{}{
  58. "pr_number": strconv.FormatUint(uint64(request.Number), 10),
  59. "pr_title": *pr.Title,
  60. "pr_branch_from": request.BranchFrom,
  61. "pr_branch_into": request.BranchInto,
  62. },
  63. },
  64. )
  65. if ghResp != nil {
  66. if ghResp.StatusCode == 404 {
  67. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  68. fmt.Errorf(
  69. "Please make sure the preview environment workflow files are present in PR branch %s and are up to"+
  70. " date with the default branch", request.BranchFrom,
  71. ), 404),
  72. )
  73. return
  74. } else if ghResp.StatusCode == 422 {
  75. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  76. fmt.Errorf(
  77. "Please make sure the workflow files in PR branch %s are up to date with the default branch",
  78. request.BranchFrom,
  79. ), 422),
  80. )
  81. return
  82. }
  83. }
  84. if err != nil {
  85. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  86. return
  87. }
  88. namespace := fmt.Sprintf("pr-%d-%s", request.Number, strings.ReplaceAll(env.GitRepoName, "_", "-"))
  89. // create the deployment
  90. depl, err := c.Repo().Environment().CreateDeployment(&models.Deployment{
  91. EnvironmentID: env.ID,
  92. Namespace: namespace,
  93. Status: types.DeploymentStatusCreating,
  94. PullRequestID: request.Number,
  95. RepoOwner: request.RepoOwner,
  96. RepoName: request.RepoName,
  97. PRName: request.Title,
  98. PRBranchFrom: request.BranchFrom,
  99. PRBranchInto: request.BranchInto,
  100. })
  101. if err != nil {
  102. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  103. return
  104. }
  105. // create the backing namespace
  106. agent, err := c.GetAgent(r, cluster, "")
  107. if err != nil {
  108. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  109. return
  110. }
  111. _, err = agent.CreateNamespace(depl.Namespace)
  112. if err != nil {
  113. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  114. return
  115. }
  116. }