enable_pull_request.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. ghResp, err := client.Actions.CreateWorkflowDispatchEventByFileName(
  49. r.Context(), env.GitRepoOwner, env.GitRepoName, fmt.Sprintf("porter_%s_env.yml", env.Name),
  50. github.CreateWorkflowDispatchEventRequest{
  51. Ref: request.BranchFrom,
  52. Inputs: map[string]interface{}{
  53. "pr_number": strconv.FormatUint(uint64(request.Number), 10),
  54. "pr_title": request.Title,
  55. "pr_branch_from": request.BranchFrom,
  56. "pr_branch_into": request.BranchInto,
  57. },
  58. },
  59. )
  60. buf := new(bytes.Buffer)
  61. buf.ReadFrom(ghResp.Body)
  62. if ghResp != nil && ghResp.StatusCode == 404 {
  63. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("workflow file not found"), 404))
  64. return
  65. }
  66. if err != nil {
  67. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  68. return
  69. }
  70. namespace := fmt.Sprintf("pr-%d-%s", request.Number, strings.ReplaceAll(env.GitRepoName, "_", "-"))
  71. // create the deployment
  72. depl, err := c.Repo().Environment().CreateDeployment(&models.Deployment{
  73. EnvironmentID: env.ID,
  74. Namespace: namespace,
  75. Status: types.DeploymentStatusCreating,
  76. PullRequestID: request.Number,
  77. RepoOwner: request.RepoOwner,
  78. RepoName: request.RepoName,
  79. PRName: request.Title,
  80. PRBranchFrom: request.BranchFrom,
  81. PRBranchInto: request.BranchInto,
  82. })
  83. if err != nil {
  84. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  85. return
  86. }
  87. // create the backing namespace
  88. agent, err := c.GetAgent(r, cluster, "")
  89. if err != nil {
  90. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  91. return
  92. }
  93. _, err = agent.CreateNamespace(depl.Namespace)
  94. if err != nil {
  95. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  96. return
  97. }
  98. }