enable_pull_request.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package environment
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "github.com/google/go-github/v41/github"
  9. "github.com/porter-dev/porter/api/server/authz"
  10. "github.com/porter-dev/porter/api/server/handlers"
  11. "github.com/porter-dev/porter/api/server/shared"
  12. "github.com/porter-dev/porter/api/server/shared/apierrors"
  13. "github.com/porter-dev/porter/api/server/shared/config"
  14. "github.com/porter-dev/porter/api/types"
  15. "github.com/porter-dev/porter/internal/models"
  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. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  41. return
  42. }
  43. client, err := getGithubClientFromEnvironment(c.Config(), env)
  44. if err != nil {
  45. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  46. return
  47. }
  48. // add an extra check that the installation has permission to read this pull request
  49. pr, ghResp, err := client.PullRequests.Get(r.Context(), env.GitRepoOwner, env.GitRepoName, int(request.Number))
  50. if err != nil {
  51. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  52. return
  53. }
  54. ghResp, err = client.Actions.CreateWorkflowDispatchEventByFileName(
  55. r.Context(), env.GitRepoOwner, env.GitRepoName, fmt.Sprintf("porter_%s_env.yml", env.Name),
  56. github.CreateWorkflowDispatchEventRequest{
  57. Ref: request.BranchFrom,
  58. Inputs: map[string]interface{}{
  59. "pr_number": strconv.FormatUint(uint64(request.Number), 10),
  60. "pr_title": *pr.Title,
  61. "pr_branch_from": request.BranchFrom,
  62. "pr_branch_into": request.BranchInto,
  63. },
  64. },
  65. )
  66. buf := new(bytes.Buffer)
  67. buf.ReadFrom(ghResp.Body)
  68. if ghResp != nil && ghResp.StatusCode == 404 {
  69. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("workflow file not found"), 404))
  70. return
  71. }
  72. if err != nil {
  73. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  74. return
  75. }
  76. namespace := fmt.Sprintf("pr-%d-%s", request.Number, strings.ReplaceAll(env.GitRepoName, "_", "-"))
  77. // create the deployment
  78. depl, err := c.Repo().Environment().CreateDeployment(&models.Deployment{
  79. EnvironmentID: env.ID,
  80. Namespace: namespace,
  81. Status: types.DeploymentStatusCreating,
  82. PullRequestID: request.Number,
  83. RepoOwner: request.RepoOwner,
  84. RepoName: request.RepoName,
  85. PRName: request.Title,
  86. PRBranchFrom: request.BranchFrom,
  87. PRBranchInto: request.BranchInto,
  88. })
  89. if err != nil {
  90. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  91. return
  92. }
  93. // create the backing namespace
  94. agent, err := c.GetAgent(r, cluster, "")
  95. if err != nil {
  96. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  97. return
  98. }
  99. _, err = agent.CreateNamespace(depl.Namespace)
  100. if err != nil {
  101. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  102. return
  103. }
  104. }