فهرست منبع

set pr meta on preview target creation (#4603)

ianedwards 2 سال پیش
والد
کامیت
5000af484a

+ 12 - 0
api/server/handlers/deployment_target/create.go

@@ -71,12 +71,24 @@ func (c *CreateDeploymentTargetHandler) ServeHTTP(w http.ResponseWriter, r *http
 		name = request.Selector
 	}
 
+	var metadata *porterv1.DeploymentTargetMeta
+	if request.Metadata.PullRequest.Repository != "" {
+		metadata = &porterv1.DeploymentTargetMeta{
+			PullRequest: &porterv1.PullRequest{
+				Number:     int64(request.Metadata.PullRequest.Number),
+				Repository: request.Metadata.PullRequest.Repository,
+				HeadRef:    request.Metadata.PullRequest.HeadRef,
+			},
+		}
+	}
+
 	createReq := connect.NewRequest(&porterv1.CreateDeploymentTargetRequest{
 		ProjectId: int64(project.ID),
 		ClusterId: int64(clusterId),
 		Name:      name,
 		Namespace: name,
 		IsPreview: request.Preview,
+		Metadata:  metadata,
 	})
 
 	ccpResp, err := c.Config().ClusterControlPlaneClient.CreateDeploymentTarget(ctx, createReq)

+ 15 - 0
api/types/deployment_target.go

@@ -28,6 +28,21 @@ type CreateDeploymentTargetRequest struct {
 	Preview  bool   `json:"preview"`
 	// required if using the project-scoped endpoint
 	ClusterId uint `json:"cluster_id"`
+	// optional metadata field
+	Metadata Metadata `json:"metadata,omitempty"`
+}
+
+// Metadata is a DB-level representation of the metadata field in a deployment target
+type Metadata struct {
+	PullRequest TargetPullRequest `json:"pull_request"`
+}
+
+// TargetPullRequest represents a pull request in the metadata field of a deployment target
+type TargetPullRequest struct {
+	Repository string `json:"repository"`
+	Number     int    `json:"number"`
+	Title      string `json:"title"`
+	HeadRef    string `json:"head_ref"`
 }
 
 // CreateDeploymentTargetResponse is the response object for the /deployment-targets POST endpoint

+ 22 - 7
cli/cmd/v2/apply.go

@@ -81,18 +81,19 @@ func Apply(ctx context.Context, inp ApplyInput) error {
 		return errors.New("cluster must be set")
 	}
 
-	deploymentTargetID, err := deploymentTargetFromConfig(ctx, client, cliConf.Project, cliConf.Cluster, inp.PreviewApply)
-	if err != nil {
-		return fmt.Errorf("error getting deployment target from config: %w", err)
-	}
-
 	var prNumber int
 	prNumberEnv := os.Getenv("PORTER_PR_NUMBER")
 	if prNumberEnv != "" {
-		prNumber, err = strconv.Atoi(prNumberEnv)
+		parsedPRInt, err := strconv.Atoi(prNumberEnv)
 		if err != nil {
 			return fmt.Errorf("error parsing PORTER_PR_NUMBER to int: %w", err)
 		}
+		prNumber = parsedPRInt
+	}
+
+	deploymentTargetID, err := deploymentTargetFromConfig(ctx, client, cliConf.Project, cliConf.Cluster, inp.PreviewApply, prNumber)
+	if err != nil {
+		return fmt.Errorf("error getting deployment target from config: %w", err)
 	}
 
 	porterYamlExists := len(inp.PorterYamlPath) != 0
@@ -343,7 +344,7 @@ func commitSHAFromEnv() string {
 	return commitSHA
 }
 
-func deploymentTargetFromConfig(ctx context.Context, client api.Client, projectID, clusterID uint, previewApply bool) (string, error) {
+func deploymentTargetFromConfig(ctx context.Context, client api.Client, projectID, clusterID uint, previewApply bool, prNumber int) (string, error) {
 	var deploymentTargetID string
 
 	if os.Getenv("PORTER_DEPLOYMENT_TARGET_ID") != "" {
@@ -375,11 +376,25 @@ func deploymentTargetFromConfig(ctx context.Context, client api.Client, projectI
 			return deploymentTargetID, errors.New("branch name is empty. Please run apply in a git repository with access to the git CLI")
 		}
 
+		var repository string
+		if os.Getenv("PORTER_REPO_NAME") != "" {
+			repository = os.Getenv("PORTER_REPO_NAME")
+		} else if os.Getenv("GITHUB_REPOSITORY") != "" {
+			repository = os.Getenv("GITHUB_REPOSITORY")
+		}
+
 		targetResp, err := client.CreateDeploymentTarget(ctx, projectID, &types.CreateDeploymentTargetRequest{
 			Selector:  "",
 			Name:      branchName,
 			Preview:   true,
 			ClusterId: clusterID,
+			Metadata: types.Metadata{
+				PullRequest: types.TargetPullRequest{
+					Repository: repository,
+					Number:     prNumber,
+					HeadRef:    branchName,
+				},
+			},
 		})
 		if err != nil {
 			return deploymentTargetID, fmt.Errorf("error calling create deployment target endpoint: %w", err)

+ 1 - 0
dashboard/src/assets/git-compare.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-git-compare-arrows"><circle cx="5" cy="6" r="3"/><path d="M12 6h5a2 2 0 0 1 2 2v7"/><path d="m15 9-3-3 3-3"/><circle cx="19" cy="18" r="3"/><path d="M12 18H7a2 2 0 0 1-2-2V9"/><path d="m9 15 3 3-3 3"/></svg>

+ 1 - 1
dashboard/src/lib/hooks/useAddon.ts

@@ -139,7 +139,7 @@ export const useAddonList = ({
               "monitoring",
               "porter-agent-system",
               "external-secrets",
-              "infisical-operator"
+              "infisical"
             ].includes(a.namespace ?? "");
           });
       },

+ 1 - 1
dashboard/src/main/home/add-on-dashboard/legacy_AddOnDashboard.tsx

@@ -50,7 +50,7 @@ export const RestrictedNamespaces = [
   "monitoring",
   "porter-agent-system",
   "external-secrets",
-  "infisical-operator"
+  "infisical",
 ];
 
 const templateBlacklist = ["web", "worker", "job", "umbrella"];

+ 9 - 4
dashboard/src/main/home/app-dashboard/new-app-flow/utils.ts

@@ -24,7 +24,7 @@ export const getGithubAction = (
   stackName: string,
   branchName: string,
   porterYamlPath: string = "porter.yaml",
-  deploymentTargetId: string = "",
+  deploymentTargetId: string = ""
 ): string => {
   return `on:
   push:
@@ -52,7 +52,11 @@ jobs:
         PORTER_STACK_NAME: ${stackName}
         PORTER_TAG: \${{ steps.vars.outputs.sha_short }}
         PORTER_TOKEN: \${{ secrets.PORTER_STACK_${projectID}_${clusterId} }}
-        ${deploymentTargetId ? `PORTER_DEPLOYMENT_TARGET_ID: ${deploymentTargetId}` : ""}`;
+        ${
+          deploymentTargetId
+            ? `PORTER_DEPLOYMENT_TARGET_ID: ${deploymentTargetId}`
+            : ""
+        }`;
 };
 
 export const getPreviewGithubAction = ({
@@ -67,7 +71,7 @@ export const getPreviewGithubAction = ({
   appName: string;
   branch: string;
   porterYamlPath?: string;
-}) => {
+}): string => {
   return `"on":
   pull_request:
     branches:
@@ -101,5 +105,6 @@ jobs:
         PORTER_STACK_NAME: ${appName}
         PORTER_TAG: \${{ steps.vars.outputs.sha_short }}
         PORTER_TOKEN: \${{ secrets.PORTER_STACK_${projectId}_${clusterId} }}
-        PORTER_PR_NUMBER: \${{ github.event.number }}`;
+        PORTER_PR_NUMBER: \${{ github.event.number }}
+        PORTER_REPO_NAME: \${{ github.event.repository.name }}`;
 };

+ 0 - 4
dashboard/src/main/home/cluster-dashboard/preview-environments/v2/ConfigurableAppList.tsx

@@ -24,10 +24,6 @@ export const ConfigurableAppList: React.FC = () => {
     clusterId: currentCluster?.id ?? 0,
   });
 
-  if (status === "loading") {
-    return <Loading offset="-150px" />;
-  }
-
   if (apps.length === 0) {
     return (
       <Fieldset>

+ 1 - 0
dashboard/src/main/home/cluster-dashboard/preview-environments/v2/setup-app/PreviewAppDataContainer.tsx

@@ -168,6 +168,7 @@ export const PreviewAppDataContainer: React.FC<Props> = ({
           }))
           .otherwise(() => ({}));
         const secrets = match(addon.config)
+          .returnType<Record<string, string>>()
           .with({ type: "postgres" }, (conf) => ({
             POSTGRESQL_PASSWORD: conf.password,
           }))

+ 1 - 1
go.mod

@@ -87,7 +87,7 @@ require (
 	github.com/nats-io/nats.go v1.24.0
 	github.com/open-policy-agent/opa v0.44.0
 	github.com/ory/client-go v1.9.0
-	github.com/porter-dev/api-contracts v0.2.157
+	github.com/porter-dev/api-contracts v0.2.158
 	github.com/riandyrn/otelchi v0.5.1
 	github.com/santhosh-tekuri/jsonschema/v5 v5.0.1
 	github.com/stefanmcshane/helm v0.0.0-20221213002717-88a4a2c6e77d

+ 2 - 2
go.sum

@@ -1563,8 +1563,8 @@ github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/polyfloyd/go-errorlint v0.0.0-20210722154253-910bb7978349/go.mod h1:wi9BfjxjF/bwiZ701TzmfKu6UKC357IOAtNr0Td0Lvw=
-github.com/porter-dev/api-contracts v0.2.157 h1:xjC1q4/8ZUl5QLVyCkTfIiMZn+k8h0c9AO9nrCFcZ1Y=
-github.com/porter-dev/api-contracts v0.2.157/go.mod h1:VV5BzXd02ZdbWIPLVP+PX3GKawJSGQnxorVT2sUZALU=
+github.com/porter-dev/api-contracts v0.2.158 h1:928I9vELiqntau4Yp8cVuX7FcLgo95Lv2uBVYj84is8=
+github.com/porter-dev/api-contracts v0.2.158/go.mod h1:VV5BzXd02ZdbWIPLVP+PX3GKawJSGQnxorVT2sUZALU=
 github.com/porter-dev/switchboard v0.0.3 h1:dBuYkiVLa5Ce7059d6qTe9a1C2XEORFEanhbtV92R+M=
 github.com/porter-dev/switchboard v0.0.3/go.mod h1:xSPzqSFMQ6OSbp42fhCi4AbGbQbsm6nRvOkrblFeXU4=
 github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=

+ 1 - 0
internal/integrations/ci/actions/steps.go

@@ -106,6 +106,7 @@ func getDeployStackStep(
 		"PORTER_TAG":        "${{ steps.vars.outputs.sha_short }}",
 		"PORTER_STACK_NAME": stackName,
 		"PORTER_PR_NUMBER":  "${{ github.event.number }}",
+		"PORTER_REPO_NAME":  "${{ github.event.repository.name }}",
 	}
 
 	if deploymentTargetId != "" {