update_deployment_status.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package environment
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "github.com/porter-dev/porter/api/server/authz"
  7. "github.com/porter-dev/porter/api/server/handlers"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/porter-dev/porter/api/server/shared/commonutils"
  11. "github.com/porter-dev/porter/api/server/shared/config"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/porter-dev/porter/internal/models/integrations"
  15. "gorm.io/gorm"
  16. )
  17. type UpdateDeploymentStatusHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. authz.KubernetesAgentGetter
  20. }
  21. func NewUpdateDeploymentStatusHandler(
  22. config *config.Config,
  23. decoderValidator shared.RequestDecoderValidator,
  24. writer shared.ResultWriter,
  25. ) *UpdateDeploymentStatusHandler {
  26. return &UpdateDeploymentStatusHandler{
  27. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  28. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  29. }
  30. }
  31. func (c *UpdateDeploymentStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  32. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  33. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  34. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  35. owner, name, ok := commonutils.GetOwnerAndNameParams(c, w, r)
  36. if !ok {
  37. return
  38. }
  39. request := &types.UpdateDeploymentStatusRequest{}
  40. if ok := c.DecodeAndValidate(w, r, request); !ok {
  41. return
  42. }
  43. if request.Namespace == "" && request.PRNumber == 0 {
  44. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  45. fmt.Errorf("either namespace or pr_number must be present in request body"), http.StatusBadRequest,
  46. ))
  47. return
  48. }
  49. var err error
  50. // read the environment to get the environment id
  51. env, err := c.Repo().Environment().ReadEnvironment(project.ID, cluster.ID, uint(ga.InstallationID), owner, name)
  52. if err != nil {
  53. if errors.Is(err, gorm.ErrRecordNotFound) {
  54. c.HandleAPIError(w, r, apierrors.NewErrNotFound(errEnvironmentNotFound))
  55. return
  56. }
  57. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  58. return
  59. }
  60. depl, err := c.Repo().Environment().ReadDeploymentForBranch(env.ID, owner, name, request.PRBranchFrom)
  61. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  62. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  63. return
  64. }
  65. if depl == nil {
  66. if request.PRNumber != 0 {
  67. depl, err = c.Repo().Environment().ReadDeploymentByGitDetails(env.ID, owner, name, request.PRNumber)
  68. if err != nil {
  69. if errors.Is(err, gorm.ErrRecordNotFound) {
  70. c.HandleAPIError(w, r, apierrors.NewErrNotFound(errDeploymentNotFound))
  71. return
  72. }
  73. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  74. return
  75. }
  76. } else if request.Namespace != "" {
  77. depl, err = c.Repo().Environment().ReadDeployment(env.ID, request.Namespace)
  78. if err != nil {
  79. if errors.Is(err, gorm.ErrRecordNotFound) {
  80. c.HandleAPIError(w, r, apierrors.NewErrNotFound(errDeploymentNotFound))
  81. return
  82. }
  83. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  84. return
  85. }
  86. }
  87. }
  88. if depl == nil {
  89. c.HandleAPIError(w, r, apierrors.NewErrNotFound(errDeploymentNotFound))
  90. return
  91. }
  92. client, err := getGithubClientFromEnvironment(c.Config(), env)
  93. if err != nil {
  94. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  95. fmt.Errorf("unable to get github client: %w", err), http.StatusConflict,
  96. ))
  97. return
  98. }
  99. if !depl.IsBranchDeploy() {
  100. // add a check for the PR to be open before creating a comment
  101. prClosed, err := isGithubPRClosed(client, depl.RepoOwner, depl.RepoName, int(depl.PullRequestID))
  102. if err != nil {
  103. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  104. fmt.Errorf("error fetching details of github PR for deployment ID: %d. Error: %w",
  105. depl.ID, err), http.StatusConflict,
  106. ))
  107. return
  108. }
  109. if prClosed {
  110. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("Github PR has been closed"),
  111. http.StatusConflict))
  112. return
  113. }
  114. }
  115. if depl.Status == types.DeploymentStatusInactive && request.Status != string(types.DeploymentStatusCreating) {
  116. // a deployment from "inactive" state can only transition to "creating"
  117. c.WriteResult(w, r, depl.ToDeploymentType())
  118. return
  119. }
  120. depl.Status = types.DeploymentStatus(request.Status)
  121. // create the deployment
  122. depl, err = c.Repo().Environment().UpdateDeployment(depl)
  123. if err != nil {
  124. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  125. return
  126. }
  127. c.WriteResult(w, r, depl.ToDeploymentType())
  128. }