Skip to content

Commit

Permalink
set pr meta on preview target creation (#4603)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianedwards authored May 7, 2024
1 parent d4a4bfe commit 5000af4
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 20 deletions.
12 changes: 12 additions & 0 deletions api/server/handlers/deployment_target/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions api/types/deployment_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 22 additions & 7 deletions cli/cmd/v2/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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") != "" {
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions dashboard/src/assets/git-compare.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion dashboard/src/lib/hooks/useAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export const useAddonList = ({
"monitoring",
"porter-agent-system",
"external-secrets",
"infisical-operator"
"infisical"
].includes(a.namespace ?? "");
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const RestrictedNamespaces = [
"monitoring",
"porter-agent-system",
"external-secrets",
"infisical-operator"
"infisical",
];

const templateBlacklist = ["web", "worker", "job", "umbrella"];
Expand Down
13 changes: 9 additions & 4 deletions dashboard/src/main/home/app-dashboard/new-app-flow/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const getGithubAction = (
stackName: string,
branchName: string,
porterYamlPath: string = "porter.yaml",
deploymentTargetId: string = "",
deploymentTargetId: string = ""
): string => {
return `on:
push:
Expand Down Expand Up @@ -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 = ({
Expand All @@ -67,7 +71,7 @@ export const getPreviewGithubAction = ({
appName: string;
branch: string;
porterYamlPath?: string;
}) => {
}): string => {
return `"on":
pull_request:
branches:
Expand Down Expand Up @@ -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 }}`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions internal/integrations/ci/actions/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down

0 comments on commit 5000af4

Please sign in to comment.