|
|
@@ -1,14 +1,16 @@
|
|
|
+import { useCallback, useContext } from "react";
|
|
|
import { PorterApp } from "@porter-dev/api-contracts";
|
|
|
+import { match } from "ts-pattern";
|
|
|
+import { z } from "zod";
|
|
|
+
|
|
|
import {
|
|
|
- PorterAppFormData,
|
|
|
- SourceOptions,
|
|
|
clientAppToProto,
|
|
|
+ type PorterAppFormData,
|
|
|
+ type SourceOptions,
|
|
|
} from "lib/porter-apps";
|
|
|
-import { useCallback, useContext } from "react";
|
|
|
-import { Context } from "shared/Context";
|
|
|
+
|
|
|
import api from "shared/api";
|
|
|
-import { match } from "ts-pattern";
|
|
|
-import { z } from "zod";
|
|
|
+import { Context } from "shared/Context";
|
|
|
|
|
|
export type AppValidationResult = {
|
|
|
validatedAppProto: PorterApp;
|
|
|
@@ -16,13 +18,20 @@ export type AppValidationResult = {
|
|
|
secrets: Record<string, string>;
|
|
|
};
|
|
|
|
|
|
+type AppValidationHook = {
|
|
|
+ validateApp: (
|
|
|
+ data: PorterAppFormData,
|
|
|
+ runValidation?: boolean
|
|
|
+ ) => Promise<AppValidationResult>;
|
|
|
+};
|
|
|
+
|
|
|
export const useAppValidation = ({
|
|
|
deploymentTargetID,
|
|
|
creating = false,
|
|
|
}: {
|
|
|
deploymentTargetID?: string;
|
|
|
creating?: boolean;
|
|
|
-}) => {
|
|
|
+}): AppValidationHook => {
|
|
|
const { currentProject, currentCluster } = useContext(Context);
|
|
|
|
|
|
const getBranchHead = async ({
|
|
|
@@ -33,8 +42,8 @@ export const useAppValidation = ({
|
|
|
source: SourceOptions & {
|
|
|
type: "github";
|
|
|
};
|
|
|
- }) => {
|
|
|
- const [owner, repo_name] = await z
|
|
|
+ }): Promise<string> => {
|
|
|
+ const [owner, repoName] = await z
|
|
|
.tuple([z.string(), z.string()])
|
|
|
.parseAsync(source.git_repo_name?.split("/"));
|
|
|
|
|
|
@@ -46,7 +55,7 @@ export const useAppValidation = ({
|
|
|
project_id: projectID,
|
|
|
kind: "github",
|
|
|
owner,
|
|
|
- name: repo_name,
|
|
|
+ name: repoName,
|
|
|
branch: source.git_branch,
|
|
|
}
|
|
|
);
|
|
|
@@ -57,13 +66,13 @@ export const useAppValidation = ({
|
|
|
})
|
|
|
.parseAsync(res.data);
|
|
|
|
|
|
- return commitData;
|
|
|
+ return commitData.commit_sha;
|
|
|
};
|
|
|
|
|
|
const validateApp = useCallback(
|
|
|
async (
|
|
|
data: PorterAppFormData,
|
|
|
- prevRevision?: PorterApp
|
|
|
+ runValidation = true
|
|
|
): Promise<AppValidationResult> => {
|
|
|
if (!currentProject || !currentCluster) {
|
|
|
throw new Error("No project or cluster selected");
|
|
|
@@ -93,17 +102,23 @@ export const useAppValidation = ({
|
|
|
}, {});
|
|
|
|
|
|
const proto = clientAppToProto(data);
|
|
|
- const commit_sha = await match(data.source)
|
|
|
+
|
|
|
+ if (!runValidation) {
|
|
|
+ return { validatedAppProto: proto, variables, secrets };
|
|
|
+ }
|
|
|
+
|
|
|
+ const commitSha = await match(data.source)
|
|
|
.with({ type: "github" }, async (src) => {
|
|
|
if (!creating) {
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
- const { commit_sha } = await getBranchHead({
|
|
|
+ const commitSha = await getBranchHead({
|
|
|
projectID: currentProject.id,
|
|
|
source: src,
|
|
|
});
|
|
|
- return commit_sha;
|
|
|
+
|
|
|
+ return commitSha;
|
|
|
})
|
|
|
.with({ type: "docker-registry" }, () => {
|
|
|
return "";
|
|
|
@@ -139,7 +154,7 @@ export const useAppValidation = ({
|
|
|
})
|
|
|
),
|
|
|
deployment_target_id: deploymentTargetID,
|
|
|
- commit_sha,
|
|
|
+ commit_sha: commitSha,
|
|
|
deletions: {
|
|
|
service_names: data.deletions.serviceNames.map((s) => s.name),
|
|
|
predeploy: data.deletions.predeploy.map((s) => s.name),
|
|
|
@@ -167,7 +182,7 @@ export const useAppValidation = ({
|
|
|
}
|
|
|
);
|
|
|
|
|
|
- return { validatedAppProto: validatedAppProto, variables, secrets };
|
|
|
+ return { validatedAppProto, variables, secrets };
|
|
|
},
|
|
|
[deploymentTargetID, currentProject, currentCluster]
|
|
|
);
|