update_deployment_status.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package environment
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/porter-dev/porter/api/server/authz"
  6. "github.com/porter-dev/porter/api/server/handlers"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/apierrors"
  9. "github.com/porter-dev/porter/api/server/shared/commonutils"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. "github.com/porter-dev/porter/api/types"
  12. "github.com/porter-dev/porter/internal/models"
  13. "github.com/porter-dev/porter/internal/models/integrations"
  14. )
  15. type UpdateDeploymentStatusHandler struct {
  16. handlers.PorterHandlerReadWriter
  17. authz.KubernetesAgentGetter
  18. }
  19. func NewUpdateDeploymentStatusHandler(
  20. config *config.Config,
  21. decoderValidator shared.RequestDecoderValidator,
  22. writer shared.ResultWriter,
  23. ) *UpdateDeploymentStatusHandler {
  24. return &UpdateDeploymentStatusHandler{
  25. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  26. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  27. }
  28. }
  29. func (c *UpdateDeploymentStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  30. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  31. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  32. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  33. owner, name, ok := commonutils.GetOwnerAndNameParams(c, w, r)
  34. if !ok {
  35. return
  36. }
  37. request := &types.UpdateDeploymentStatusRequest{}
  38. if ok := c.DecodeAndValidate(w, r, request); !ok {
  39. return
  40. }
  41. // read the environment to get the environment id
  42. env, err := c.Repo().Environment().ReadEnvironment(project.ID, cluster.ID, uint(ga.InstallationID), owner, name)
  43. if err != nil {
  44. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  45. return
  46. }
  47. // read the deployment
  48. depl, err := c.Repo().Environment().ReadDeployment(env.ID, request.Namespace)
  49. if err != nil {
  50. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  51. return
  52. }
  53. client, err := getGithubClientFromEnvironment(c.Config(), env)
  54. if err != nil {
  55. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  56. fmt.Errorf("unable to get github client: %w", err), http.StatusConflict,
  57. ))
  58. return
  59. }
  60. // add a check for the PR to be open before creating a comment
  61. prClosed, err := isGithubPRClosed(client, depl.RepoOwner, depl.RepoName, int(depl.PullRequestID))
  62. if err != nil {
  63. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  64. fmt.Errorf("error fetching details of github PR for deployment ID: %d. Error: %w",
  65. depl.ID, err), http.StatusConflict,
  66. ))
  67. return
  68. }
  69. if prClosed {
  70. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("Github PR has been closed"),
  71. http.StatusConflict))
  72. return
  73. }
  74. if depl.Status == types.DeploymentStatusInactive && request.Status != string(types.DeploymentStatusCreating) {
  75. // a deployment from "inactive" state can only transition to "creating"
  76. c.WriteResult(w, r, depl.ToDeploymentType())
  77. return
  78. }
  79. depl.Status = types.DeploymentStatus(request.Status)
  80. // create the deployment
  81. depl, err = c.Repo().Environment().UpdateDeployment(depl)
  82. if err != nil {
  83. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  84. return
  85. }
  86. c.WriteResult(w, r, depl.ToDeploymentType())
  87. }