enable_pull_request.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package environment
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  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. "github.com/porter-dev/porter/internal/telemetry"
  16. "gorm.io/gorm"
  17. )
  18. type EnablePullRequestHandler struct {
  19. handlers.PorterHandlerReadWriter
  20. authz.KubernetesAgentGetter
  21. }
  22. func NewEnablePullRequestHandler(
  23. config *config.Config,
  24. decoderValidator shared.RequestDecoderValidator,
  25. writer shared.ResultWriter,
  26. ) *EnablePullRequestHandler {
  27. return &EnablePullRequestHandler{
  28. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  29. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  30. }
  31. }
  32. func (c *EnablePullRequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  33. ctx, span := telemetry.NewSpan(r.Context(), "serve-enable-pull-request")
  34. defer span.End()
  35. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  36. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  37. telemetry.WithAttributes(span,
  38. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  39. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  40. )
  41. request := &types.PullRequest{}
  42. if ok := c.DecodeAndValidate(w, r, request); !ok {
  43. _ = telemetry.Error(ctx, span, nil, "could not decode and validate request")
  44. return
  45. }
  46. telemetry.WithAttributes(span,
  47. telemetry.AttributeKV{Key: "title", Value: request.Title},
  48. telemetry.AttributeKV{Key: "number", Value: request.Number},
  49. telemetry.AttributeKV{Key: "repo-ower", Value: request.RepoOwner},
  50. telemetry.AttributeKV{Key: "repo-name", Value: request.RepoName},
  51. telemetry.AttributeKV{Key: "branch-from", Value: request.BranchFrom},
  52. telemetry.AttributeKV{Key: "branch-into", Value: request.BranchInto},
  53. telemetry.AttributeKV{Key: "created-at", Value: request.CreatedAt},
  54. telemetry.AttributeKV{Key: "updated-at", Value: request.UpdatedAt},
  55. )
  56. env, err := c.Repo().Environment().ReadEnvironmentByOwnerRepoName(project.ID, cluster.ID, request.RepoOwner, request.RepoName)
  57. if err != nil {
  58. err = telemetry.Error(ctx, span, err, "error reading environment by owner repo name")
  59. if errors.Is(err, gorm.ErrRecordNotFound) {
  60. c.HandleAPIError(w, r, apierrors.NewErrNotFound(fmt.Errorf("environment not found in cluster and project")))
  61. return
  62. }
  63. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  64. return
  65. }
  66. envType := env.ToEnvironmentType()
  67. if len(envType.GitRepoBranches) > 0 {
  68. found := false
  69. for _, branch := range env.ToEnvironmentType().GitRepoBranches {
  70. if branch == request.BranchInto {
  71. found = true
  72. break
  73. }
  74. }
  75. if !found {
  76. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "unsuccessful-exit-reason", Value: "cannot find branch-into in git repo branches"})
  77. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  78. fmt.Errorf("base branch '%s' is not enabled for this preview environment, please enable it "+
  79. "in the settings page to continue", request.BranchInto), http.StatusBadRequest,
  80. ))
  81. return
  82. }
  83. } else if len(envType.GitDeployBranches) > 0 {
  84. found := false
  85. for _, branch := range env.ToEnvironmentType().GitDeployBranches {
  86. if branch == request.BranchFrom {
  87. found = true
  88. break
  89. }
  90. }
  91. if found {
  92. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "unsuccessful-exit-reason", Value: "cannot find branch-from in git deploy branches"})
  93. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  94. fmt.Errorf("head branch '%s' is enabled for branch deploys for this preview environment, "+
  95. "please disable it in the settings page to continue", request.BranchInto), http.StatusBadRequest,
  96. ))
  97. return
  98. }
  99. }
  100. client, err := getGithubClientFromEnvironment(c.Config(), env)
  101. if err != nil {
  102. err = telemetry.Error(ctx, span, err, "error getting github client from environment")
  103. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  104. return
  105. }
  106. // add an extra check that the installation has permission to read this pull request
  107. pr, _, err := client.PullRequests.Get(ctx, env.GitRepoOwner, env.GitRepoName, int(request.Number))
  108. if err != nil {
  109. _ = telemetry.Error(ctx, span, err, "error getting pull request")
  110. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("%v: %w", errGithubAPI, err),
  111. http.StatusConflict))
  112. return
  113. }
  114. if pr.GetState() == "closed" {
  115. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "unsuccessful-exit-reason", Value: "pr is closed"})
  116. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("cannot enable deployment for closed PR"),
  117. http.StatusConflict))
  118. return
  119. }
  120. ghResp, err := client.Actions.CreateWorkflowDispatchEventByFileName(
  121. ctx, env.GitRepoOwner, env.GitRepoName, fmt.Sprintf("porter_%s_env.yml", env.Name),
  122. github.CreateWorkflowDispatchEventRequest{
  123. Ref: request.BranchFrom,
  124. Inputs: map[string]interface{}{
  125. "pr_number": strconv.FormatUint(uint64(request.Number), 10),
  126. "pr_title": pr.GetTitle(),
  127. "pr_branch_from": request.BranchFrom,
  128. "pr_branch_into": request.BranchInto,
  129. },
  130. },
  131. )
  132. if err != nil {
  133. err = telemetry.Error(ctx, span, err, "error creating workflow dispatch event")
  134. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  135. return
  136. }
  137. if ghResp != nil {
  138. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "github-status-code", Value: ghResp.StatusCode})
  139. if ghResp.StatusCode == 404 {
  140. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "unsuccessful-exit-reason", Value: "bad github status code"})
  141. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  142. fmt.Errorf(
  143. "please make sure the preview environment workflow files are present in PR branch %s and are up to"+
  144. " date with the default branch", request.BranchFrom,
  145. ), http.StatusConflict),
  146. )
  147. return
  148. } else if ghResp.StatusCode == 422 {
  149. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "unsuccessful-exit-reason", Value: "bad github status code"})
  150. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  151. fmt.Errorf(
  152. "please make sure the workflow files in PR branch %s are up to date with the default branch",
  153. request.BranchFrom,
  154. ), http.StatusConflict),
  155. )
  156. return
  157. }
  158. }
  159. // create the deployment
  160. depl, err := c.Repo().Environment().CreateDeployment(&models.Deployment{
  161. EnvironmentID: env.ID,
  162. Namespace: "",
  163. Status: types.DeploymentStatusCreating,
  164. PullRequestID: request.Number,
  165. RepoOwner: request.RepoOwner,
  166. RepoName: request.RepoName,
  167. PRName: request.Title,
  168. PRBranchFrom: request.BranchFrom,
  169. PRBranchInto: request.BranchInto,
  170. })
  171. if err != nil {
  172. err = telemetry.Error(ctx, span, err, "error creating deployment in repo")
  173. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  174. return
  175. }
  176. c.WriteResult(w, r, depl.ToDeploymentType())
  177. }