|
|
@@ -0,0 +1,81 @@
|
|
|
+package environment
|
|
|
+
|
|
|
+import (
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "net/http"
|
|
|
+
|
|
|
+ "github.com/porter-dev/porter/api/server/handlers"
|
|
|
+ "github.com/porter-dev/porter/api/server/shared"
|
|
|
+ "github.com/porter-dev/porter/api/server/shared/apierrors"
|
|
|
+ "github.com/porter-dev/porter/api/server/shared/config"
|
|
|
+ "github.com/porter-dev/porter/api/server/shared/requestutils"
|
|
|
+ "github.com/porter-dev/porter/api/types"
|
|
|
+ "github.com/porter-dev/porter/internal/models"
|
|
|
+ "gorm.io/gorm"
|
|
|
+)
|
|
|
+
|
|
|
+type ListDeploymentRevisionsHandler struct {
|
|
|
+ handlers.PorterHandlerReadWriter
|
|
|
+}
|
|
|
+
|
|
|
+func NewListDeploymentRevisionsHandler(
|
|
|
+ config *config.Config,
|
|
|
+ decoderValidator shared.RequestDecoderValidator,
|
|
|
+ writer shared.ResultWriter,
|
|
|
+) *ListDeploymentRevisionsHandler {
|
|
|
+ return &ListDeploymentRevisionsHandler{
|
|
|
+ PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (c *ListDeploymentRevisionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
+ project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
|
|
|
+ cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
|
|
|
+
|
|
|
+ envID, reqErr := requestutils.GetURLParamUint(r, "environment_id")
|
|
|
+
|
|
|
+ if reqErr != nil {
|
|
|
+ c.HandleAPIError(w, r, reqErr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ deplID, reqErr := requestutils.GetURLParamUint(r, "deployment_id")
|
|
|
+
|
|
|
+ if reqErr != nil {
|
|
|
+ c.HandleAPIError(w, r, reqErr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ depl, err := c.Repo().Environment().ReadDeploymentByID(project.ID, cluster.ID, deplID)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ c.HandleAPIError(w, r, apierrors.NewErrNotFound(errDeploymentNotFound))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if depl.EnvironmentID != envID {
|
|
|
+ c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("deployment does not exist in environment"), http.StatusBadRequest))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ revisions, err := c.Repo().Environment().ListDeploymentRevisions(deplID)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var res []*types.DeploymentRevision
|
|
|
+
|
|
|
+ for _, rev := range revisions {
|
|
|
+ res = append(res, rev.ToDeploymentRevisionType())
|
|
|
+ }
|
|
|
+
|
|
|
+ c.WriteResult(w, r, res)
|
|
|
+}
|