|
|
@@ -9,8 +9,12 @@ import (
|
|
|
|
|
|
"github.com/bradleyfalzon/ghinstallation/v2"
|
|
|
"github.com/google/go-github/v41/github"
|
|
|
+ "github.com/porter-dev/porter/api/server/shared/apierrors"
|
|
|
"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/repository"
|
|
|
+ "gorm.io/gorm"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
@@ -65,3 +69,58 @@ func isGithubPRClosed(
|
|
|
|
|
|
return ghPR.GetState() == "closed", nil
|
|
|
}
|
|
|
+
|
|
|
+func validateGetDeploymentRequest(
|
|
|
+ projectID, clusterID, envID uint,
|
|
|
+ owner, name string,
|
|
|
+ request *types.GetDeploymentRequest,
|
|
|
+ repo repository.Repository,
|
|
|
+) (*models.Deployment, apierrors.RequestError) {
|
|
|
+ if request.PRNumber == 0 && request.DeploymentID == 0 && request.Namespace == "" {
|
|
|
+ return nil, apierrors.NewErrPassThroughToClient(
|
|
|
+ fmt.Errorf("one of id, pr_number or namespace must be present in request body"), http.StatusBadRequest,
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ var depl *models.Deployment
|
|
|
+ var err error
|
|
|
+
|
|
|
+ // read the deployment
|
|
|
+ if request.DeploymentID != 0 {
|
|
|
+ depl, err = repo.Environment().ReadDeploymentByID(projectID, clusterID, request.DeploymentID)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return nil, apierrors.NewErrNotFound(errDeploymentNotFound)
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil, apierrors.NewErrInternal(err)
|
|
|
+ }
|
|
|
+ } else if request.PRNumber != 0 {
|
|
|
+ depl, err = repo.Environment().ReadDeploymentByGitDetails(envID, owner, name, request.PRNumber)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return nil, apierrors.NewErrNotFound(errDeploymentNotFound)
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil, apierrors.NewErrInternal(err)
|
|
|
+ }
|
|
|
+ } else if request.Namespace != "" {
|
|
|
+ depl, err = repo.Environment().ReadDeployment(envID, request.Namespace)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return nil, apierrors.NewErrNotFound(errDeploymentNotFound)
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil, apierrors.NewErrInternal(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if depl == nil {
|
|
|
+ return nil, apierrors.NewErrNotFound(errDeploymentNotFound)
|
|
|
+ }
|
|
|
+
|
|
|
+ return depl, nil
|
|
|
+}
|