Quellcode durchsuchen

Merge branch 'nafees/preview-env-new-endpoints' of https://github.com/porter-dev/porter into nafees/preview-env-new-endpoints

portersupport vor 4 Jahren
Ursprung
Commit
ecbda1fc00

+ 16 - 8
api/server/handlers/environment/delete_deployment.go

@@ -2,6 +2,8 @@ package environment
 
 import (
 	"context"
+	"errors"
+	"fmt"
 	"net/http"
 	"strings"
 
@@ -15,6 +17,7 @@ import (
 	"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 DeleteDeploymentHandler struct {
@@ -44,6 +47,19 @@ func (c *DeleteDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
 		return
 	}
 
+	// check that the environment belongs to the project and cluster IDs
+	env, err := c.Repo().Environment().ReadEnvironmentByID(project.ID, cluster.ID, envID)
+
+	if err != nil {
+		if errors.Is(err, gorm.ErrRecordNotFound) {
+			c.HandleAPIError(w, r, apierrors.NewErrForbidden(fmt.Errorf("environment id not found in cluster and project")))
+			return
+		}
+
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(reqErr))
+		return
+	}
+
 	owner, name, ok := gitinstallation.GetOwnerAndNameParams(c, w, r)
 
 	if !ok {
@@ -65,14 +81,6 @@ func (c *DeleteDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
 		return
 	}
 
-	// read the environment to get the environment id
-	env, err := c.Repo().Environment().ReadEnvironmentByID(project.ID, cluster.ID, depl.EnvironmentID)
-
-	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-		return
-	}
-
 	// delete corresponding namespace
 	agent, err := c.GetAgent(r, cluster, "")
 

+ 10 - 2
api/server/handlers/environment/enable_pull_request.go

@@ -56,13 +56,21 @@ func (c *EnablePullRequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 		return
 	}
 
-	ghResp, err := client.Actions.CreateWorkflowDispatchEventByFileName(
+	// add an extra check that the installation has permission to read this pull request
+	pr, ghResp, err := client.PullRequests.Get(r.Context(), env.GitRepoOwner, env.GitRepoName, int(request.Number))
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	ghResp, err = client.Actions.CreateWorkflowDispatchEventByFileName(
 		r.Context(), env.GitRepoOwner, env.GitRepoName, fmt.Sprintf("porter_%s_env.yml", env.Name),
 		github.CreateWorkflowDispatchEventRequest{
 			Ref: request.BranchFrom,
 			Inputs: map[string]interface{}{
 				"pr_number":      strconv.FormatUint(uint64(request.Number), 10),
-				"pr_title":       request.Title,
+				"pr_title":       *pr.Title,
 				"pr_branch_from": request.BranchFrom,
 				"pr_branch_into": request.BranchInto,
 			},

+ 1 - 1
api/server/handlers/environment/reenable_deployment.go

@@ -43,7 +43,7 @@ func (c *ReenableDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 		return
 	}
 
-	depl, err := c.Repo().Environment().ReadDeploymentByID(deplID)
+	depl, err := c.Repo().Environment().ReadDeploymentByID(project.ID, cluster.ID, deplID)
 
 	if err != nil {
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))

+ 1 - 1
api/server/router/base.go

@@ -540,7 +540,7 @@ func GetBaseRoutes(
 	})
 
 	if config.ServerConf.GithubIncomingWebhookSecret != "" {
-		// POST /api/github/incoming_webhook -> webhook.NewGithubIncomingWebhook
+		// POST /api/github/incoming_webhook/{webhook_id} -> webhook.NewGithubIncomingWebhook
 		githubIncomingWebhookEndpoint := factory.NewAPIEndpoint(
 			&types.APIRequestMetadata{
 				Verb:   types.APIVerbCreate,

+ 1 - 1
internal/models/environment.go

@@ -21,7 +21,7 @@ type Environment struct {
 
 	// WebhookID uniquely identifies the environment when other fields (project, cluster)
 	// aren't present
-	WebhookID string
+	WebhookID string `gorm:"unique"`
 }
 
 func (e *Environment) ToEnvironmentType() *types.Environment {

+ 1 - 1
internal/repository/environment.go

@@ -12,7 +12,7 @@ type EnvironmentRepository interface {
 	DeleteEnvironment(env *models.Environment) (*models.Environment, error)
 	CreateDeployment(deployment *models.Deployment) (*models.Deployment, error)
 	ReadDeployment(environmentID uint, namespace string) (*models.Deployment, error)
-	ReadDeploymentByID(id uint) (*models.Deployment, error)
+	ReadDeploymentByID(projectID, clusterID, id uint) (*models.Deployment, error)
 	ReadDeploymentByCluster(projectID, clusterID uint, namespace string) (*models.Deployment, error)
 	ReadDeploymentByGitDetails(environmentID uint, owner, repo string, prNumber uint) (*models.Deployment, error)
 	ListDeploymentsByCluster(projectID, clusterID uint, states ...string) ([]*models.Deployment, error)

+ 7 - 2
internal/repository/gorm/environment.go

@@ -116,11 +116,16 @@ func (repo *EnvironmentRepository) ReadDeployment(environmentID uint, namespace
 	return depl, nil
 }
 
-func (repo *EnvironmentRepository) ReadDeploymentByID(id uint) (*models.Deployment, error) {
+func (repo *EnvironmentRepository) ReadDeploymentByID(projectID, clusterID, id uint) (*models.Deployment, error) {
 	depl := &models.Deployment{}
-	if err := repo.db.Where("id = ?", id).First(&depl).Error; err != nil {
+
+	if err := repo.db.
+		Order("deployments.updated_at desc").
+		Joins("INNER JOIN environments ON environments.id = deployments.environment_id").
+		Where("environments.project_id = ? AND environments.cluster_id = ? AND id = ?", projectID, clusterID, id).First(&depl).Error; err != nil {
 		return nil, err
 	}
+
 	return depl, nil
 }
 

+ 1 - 1
internal/repository/test/environment.go

@@ -58,7 +58,7 @@ func (repo *EnvironmentRepository) ReadDeployment(environmentID uint, namespace
 	panic("unimplemented")
 }
 
-func (repo *EnvironmentRepository) ReadDeploymentByID(id uint) (*models.Deployment, error) {
+func (repo *EnvironmentRepository) ReadDeploymentByID(projectID, clusterID, id uint) (*models.Deployment, error) {
 	panic("unimplemented")
 }