| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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/commonutils"
- "github.com/porter-dev/porter/api/server/shared/config"
- "github.com/porter-dev/porter/api/types"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/models/integrations"
- "gorm.io/gorm"
- )
- type GetDeploymentHandler struct {
- handlers.PorterHandlerReadWriter
- }
- func NewGetDeploymentHandler(
- config *config.Config,
- decoderValidator shared.RequestDecoderValidator,
- writer shared.ResultWriter,
- ) *GetDeploymentHandler {
- return &GetDeploymentHandler{
- PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
- }
- }
- func (c *GetDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
- project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
- cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
- owner, name, ok := commonutils.GetOwnerAndNameParams(c, w, r)
- if !ok {
- return
- }
- request := &types.GetDeploymentRequest{}
- if ok := c.DecodeAndValidate(w, r, request); !ok {
- return
- }
- // read the environment to get the environment id
- env, err := c.Repo().Environment().ReadEnvironment(project.ID, cluster.ID, uint(ga.InstallationID), owner, name)
- if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
- c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
- fmt.Errorf("environment not found: is the environment enabled for this git installation?"),
- http.StatusNotFound,
- ))
- return
- } else if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- depl, err := c.Repo().Environment().ReadDeployment(env.ID, request.Namespace)
- if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
- c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
- fmt.Errorf("deployment not found"),
- http.StatusNotFound,
- ))
- return
- } else if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- c.WriteResult(w, r, depl.ToDeploymentType())
- }
|