ソースを参照

format namespace with pr id

Ian Edwards 2 年 前
コミット
1927f8db62
2 ファイル変更24 行追加4 行削除
  1. 14 0
      add_default_envs.sql
  2. 10 4
      cli/cmd/stack/environment.go

+ 14 - 0
add_default_envs.sql

@@ -0,0 +1,14 @@
+with ecc as (
+	insert into environment_configs (
+		"created_at",
+		"updated_at",
+		"project_id",
+		"cluster_id",
+		"name",
+		"auto",
+		"is_default"
+	) select now(), now(), p.id as project_id, c.id as cluster_id, c."name", false, true from projects p join clusters c on c.project_id = p.id where p.simplified_view_enabled = true
+	returning id, cluster_id
+) update porter_apps pa set environment_config_id = ecc.id
+from ecc 
+where pa.cluster_id = ecc.cluster_id

+ 10 - 4
cli/cmd/stack/environment.go

@@ -11,7 +11,7 @@ import (
 )
 
 type GitHubMetadata struct {
-	Repo, RepoOwner, BranchFrom, PRName string
+	Repo, RepoOwner, BranchFrom, PRName, PullRequestID string
 }
 
 type EnvironmentMeta struct {
@@ -46,7 +46,7 @@ func HandleEnvironmentConfiguration(
 			return "", envMeta, fmt.Errorf("unable to read environment config from DB: %w", err)
 		}
 
-		namespace = formatNamespaceForEnvironment(envConf.Name, ghMeta.RepoOwner, ghMeta.Repo, ghMeta.BranchFrom)
+		namespace = formatNamespaceForEnvironment(envConf.Name, ghMeta.RepoOwner, ghMeta.Repo, ghMeta.PullRequestID)
 
 		envMeta.EnvironmentConfigID = uint(eci)
 		envMeta.Namespace = namespace
@@ -58,8 +58,8 @@ func HandleEnvironmentConfiguration(
 	return namespace, envMeta, nil
 }
 
-func formatNamespaceForEnvironment(envName, repoOwner, repo, branch string) string {
-	return fmt.Sprintf("porter-env-%s-%s-%s-%s", envName, repoOwner, repo, branch)
+func formatNamespaceForEnvironment(envName, repoOwner, repo, pullRequestID string) string {
+	return fmt.Sprintf("porter-env-%s-%s-%s-%s", envName, repoOwner, repo, pullRequestID)
 }
 
 func getGitDeployMeta() (GitHubMetadata, error) {
@@ -89,5 +89,11 @@ func getGitDeployMeta() (GitHubMetadata, error) {
 	}
 	ghMeta.PRName = prName
 
+	prIDStr := os.Getenv("PORTER_PULL_REQUEST_ID")
+	if prIDStr == "" {
+		return ghMeta, fmt.Errorf("PORTER_PULL_REQUEST_ID not set")
+	}
+	ghMeta.PullRequestID = prIDStr
+
 	return ghMeta, nil
 }