Mohammed Nafees 3 лет назад
Родитель
Сommit
f22bab84d3

+ 17 - 15
api/server/handlers/environment/reenable_deployment.go

@@ -71,21 +71,23 @@ func (c *ReenableDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 		return
 	}
 
-	// add a check for the PR to be open before creating a comment
-	prClosed, err := isGithubPRClosed(client, depl.RepoOwner, depl.RepoName, int(depl.PullRequestID))
-
-	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
-			fmt.Errorf("error fetching details of github PR for deployment ID: %d. Error: %w",
-				depl.ID, err), http.StatusConflict,
-		))
-		return
-	}
-
-	if prClosed {
-		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("Github PR has been closed"),
-			http.StatusConflict))
-		return
+	if !depl.IsBranchDeploy() {
+		// add a check for the PR to be open before creating a comment
+		prClosed, err := isGithubPRClosed(client, depl.RepoOwner, depl.RepoName, int(depl.PullRequestID))
+
+		if err != nil {
+			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
+				fmt.Errorf("error fetching details of github PR for deployment ID: %d. Error: %w",
+					depl.ID, err), http.StatusConflict,
+			))
+			return
+		}
+
+		if prClosed {
+			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("Github PR has been closed"),
+				http.StatusConflict))
+			return
+		}
 	}
 
 	depl.Status = types.DeploymentStatusCreating

+ 24 - 16
api/server/handlers/environment/trigger_deployment_workflow.go

@@ -72,21 +72,23 @@ func (c *TriggerDeploymentWorkflowHandler) ServeHTTP(w http.ResponseWriter, r *h
 		return
 	}
 
-	// add a check for the PR to be open before creating a comment
-	prClosed, err := isGithubPRClosed(client, depl.RepoOwner, depl.RepoName, int(depl.PullRequestID))
-
-	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
-			fmt.Errorf("error fetching details of github PR for deployment ID: %d. Error: %w",
-				depl.ID, err), http.StatusConflict,
-		))
-		return
-	}
-
-	if prClosed {
-		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("Github PR has been closed"),
-			http.StatusConflict))
-		return
+	if !depl.IsBranchDeploy() {
+		// add a check for the PR to be open before creating a comment
+		prClosed, err := isGithubPRClosed(client, depl.RepoOwner, depl.RepoName, int(depl.PullRequestID))
+
+		if err != nil {
+			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
+				fmt.Errorf("error fetching details of github PR for deployment ID: %d. Error: %w",
+					depl.ID, err), http.StatusConflict,
+			))
+			return
+		}
+
+		if prClosed {
+			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("Github PR has been closed"),
+				http.StatusConflict))
+			return
+		}
 	}
 
 	latestWorkflowRun, err := commonutils.GetLatestWorkflowRun(client, env.GitRepoOwner, env.GitRepoName,
@@ -105,12 +107,18 @@ func (c *TriggerDeploymentWorkflowHandler) ServeHTTP(w http.ResponseWriter, r *h
 		return
 	}
 
+	prNumber := depl.PullRequestID
+
+	if depl.IsBranchDeploy() {
+		prNumber = depl.ID
+	}
+
 	ghResp, err := client.Actions.CreateWorkflowDispatchEventByFileName(
 		r.Context(), env.GitRepoOwner, env.GitRepoName, fmt.Sprintf("porter_%s_env.yml", env.Name),
 		github.CreateWorkflowDispatchEventRequest{
 			Ref: depl.PRBranchFrom,
 			Inputs: map[string]interface{}{
-				"pr_number":      strconv.FormatUint(uint64(depl.PullRequestID), 10),
+				"pr_number":      strconv.FormatUint(uint64(prNumber), 10),
 				"pr_title":       depl.PRName,
 				"pr_branch_from": depl.PRBranchFrom,
 				"pr_branch_into": depl.PRBranchInto,

+ 19 - 14
api/server/handlers/environment/update_deployment.go

@@ -109,21 +109,23 @@ func (c *UpdateDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
 		return
 	}
 
-	// add a check for the PR to be open before creating a comment
-	prClosed, err := isGithubPRClosed(client, owner, name, int(depl.PullRequestID))
+	if !depl.IsBranchDeploy() {
+		// add a check for the PR to be open before creating a comment
+		prClosed, err := isGithubPRClosed(client, owner, name, int(depl.PullRequestID))
 
-	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
-			fmt.Errorf("error fetching details of github PR for deployment ID: %d. Error: %w",
-				depl.ID, err), http.StatusConflict,
-		))
-		return
-	}
+		if err != nil {
+			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
+				fmt.Errorf("error fetching details of github PR for deployment ID: %d. Error: %w",
+					depl.ID, err), http.StatusConflict,
+			))
+			return
+		}
 
-	if prClosed {
-		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("Github PR has been closed"),
-			http.StatusConflict))
-		return
+		if prClosed {
+			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("Github PR has been closed"),
+				http.StatusConflict))
+			return
+		}
 	}
 
 	ghDeployment, err := createGithubDeployment(client, env, request.PRBranchFrom, request.ActionID)
@@ -133,7 +135,10 @@ func (c *UpdateDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
 		return
 	}
 
-	depl.Namespace = request.Namespace
+	if !depl.IsBranchDeploy() {
+		depl.Namespace = request.Namespace
+	}
+
 	depl.GHDeploymentID = ghDeployment.GetID()
 	depl.CommitSHA = request.CommitSHA
 

+ 15 - 13
api/server/handlers/environment/update_deployment_status.go

@@ -115,21 +115,23 @@ func (c *UpdateDeploymentStatusHandler) ServeHTTP(w http.ResponseWriter, r *http
 		return
 	}
 
-	// add a check for the PR to be open before creating a comment
-	prClosed, err := isGithubPRClosed(client, depl.RepoOwner, depl.RepoName, int(depl.PullRequestID))
+	if !depl.IsBranchDeploy() {
+		// add a check for the PR to be open before creating a comment
+		prClosed, err := isGithubPRClosed(client, depl.RepoOwner, depl.RepoName, int(depl.PullRequestID))
 
-	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
-			fmt.Errorf("error fetching details of github PR for deployment ID: %d. Error: %w",
-				depl.ID, err), http.StatusConflict,
-		))
-		return
-	}
+		if err != nil {
+			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
+				fmt.Errorf("error fetching details of github PR for deployment ID: %d. Error: %w",
+					depl.ID, err), http.StatusConflict,
+			))
+			return
+		}
 
-	if prClosed {
-		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("Github PR has been closed"),
-			http.StatusConflict))
-		return
+		if prClosed {
+			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("Github PR has been closed"),
+				http.StatusConflict))
+			return
+		}
 	}
 
 	if depl.Status == types.DeploymentStatusInactive && request.Status != string(types.DeploymentStatusCreating) {

+ 1 - 7
cli/cmd/apply.go

@@ -789,13 +789,7 @@ func (t *DeploymentHook) PreApply() error {
 	}
 
 	if t.isBranchDeploy() {
-		t.namespace = fmt.Sprintf("previewbranch-%s-%s-%s", t.branchFrom,
-			strings.ReplaceAll(strings.ToLower(t.repoOwner), "_", "-"),
-			strings.ReplaceAll(strings.ToLower(t.repoName), "_", "-"))
-
-		if len(t.namespace) > 63 {
-			t.namespace = t.namespace[:63] // Kubernetes' DNS 1123 label requirement
-		}
+		t.namespace = preview.GetNamespaceForBranchDeploy(t.branchFrom, t.repoOwner, t.repoName)
 	}
 
 	nsList, err := t.client.GetK8sNamespaces(

+ 30 - 1
cli/cmd/preview/utils.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"os"
 	"strconv"
+	"strings"
 
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/cli/cmd/config"
@@ -119,7 +120,7 @@ func GetTarget(resourceName string, input map[string]interface{}) (*preview.Targ
 		output.Cluster = uint(cluster)
 	}
 
-	output.Namespace = os.Getenv("PORTER_NAMESPACE")
+	output.Namespace = getNamespace()
 
 	// next, check for values in the YAML file
 	if output.Project == 0 {
@@ -175,6 +176,34 @@ func GetTarget(resourceName string, input map[string]interface{}) (*preview.Targ
 	return output, nil
 }
 
+func GetNamespaceForBranchDeploy(branch, owner, name string) string {
+	namespace := fmt.Sprintf("previewbranch-%s-%s-%s", branch,
+		strings.ReplaceAll(strings.ToLower(owner), "_", "-"),
+		strings.ReplaceAll(strings.ToLower(name), "_", "-"))
+
+	if len(namespace) > 63 {
+		namespace = namespace[:63] // Kubernetes' DNS 1123 label requirement
+	}
+
+	return namespace
+}
+
+func getNamespace() string {
+	if owner, ok := os.LookupEnv("PORTER_REPO_OWNER"); ok {
+		if repo, ok := os.LookupEnv("PORTER_REPO_NAME"); ok {
+			if branchFrom, ok := os.LookupEnv("PORTER_BRANCH_FROM"); ok {
+				if branchInto, ok := os.LookupEnv("PORTER_BRANCH_INTO"); ok {
+					if branchInto == branchFrom { // branch deploy
+						return GetNamespaceForBranchDeploy(branchInto, owner, repo)
+					}
+				}
+			}
+		}
+	}
+
+	return os.Getenv("PORTER_NAMESPACE")
+}
+
 func existsInRepo(projectID uint, name, version, url string) (map[string]interface{}, error) {
 	chart, err := config.GetAPIClient().GetTemplate(
 		context.Background(),