Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set pr meta on preview target creation #4603

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
name = request.Selector
}

var metadata *porterv1.DeploymentTargetMeta

Check failure on line 74 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Go Linter

undefined: porterv1.DeploymentTargetMeta

Check failure on line 74 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Go Linter

undefined: porterv1.DeploymentTargetMeta

Check failure on line 74 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Running Go Tests (ubuntu-latest, api)

undefined: porterv1.DeploymentTargetMeta

Check failure on line 74 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Running Go Tests (ubuntu-latest, cmd)

undefined: porterv1.DeploymentTargetMeta
if request.Metadata.PullRequest.Repository != "" {
metadata = &porterv1.DeploymentTargetMeta{

Check failure on line 76 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Go Linter

undefined: porterv1.DeploymentTargetMeta

Check failure on line 76 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Go Linter

undefined: porterv1.DeploymentTargetMeta

Check failure on line 76 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Running Go Tests (ubuntu-latest, api)

undefined: porterv1.DeploymentTargetMeta

Check failure on line 76 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Running Go Tests (ubuntu-latest, cmd)

undefined: porterv1.DeploymentTargetMeta
PullRequest: &porterv1.PullRequest{

Check failure on line 77 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Go Linter

undefined: porterv1.PullRequest

Check failure on line 77 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Go Linter

undefined: porterv1.PullRequest

Check failure on line 77 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Running Go Tests (ubuntu-latest, api)

undefined: porterv1.PullRequest

Check failure on line 77 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Running Go Tests (ubuntu-latest, cmd)

undefined: 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,

Check failure on line 91 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Go Linter

unknown field Metadata in struct literal of type porterv1.CreateDeploymentTargetRequest (typecheck)

Check failure on line 91 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Go Linter

unknown field Metadata in struct literal of type porterv1.CreateDeploymentTargetRequest) (typecheck)

Check failure on line 91 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Running Go Tests (ubuntu-latest, api)

unknown field Metadata in struct literal of type porterv1.CreateDeploymentTargetRequest

Check failure on line 91 in api/server/handlers/deployment_target/create.go

View workflow job for this annotation

GitHub Actions / Running Go Tests (ubuntu-latest, cmd)

unknown field Metadata in struct literal of type porterv1.CreateDeploymentTargetRequest
})

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
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
Loading