enable_pull_request.go 3.3 KB

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