Procházet zdrojové kódy

include commit sha on create (#3474)

ianedwards před 2 roky
rodič
revize
0ac7a13e7c

+ 60 - 2
dashboard/src/lib/hooks/useAppValidation.ts

@@ -1,17 +1,59 @@
 import { PorterApp } from "@porter-dev/api-contracts";
-import { PorterAppFormData, clientAppToProto } from "lib/porter-apps";
+import {
+  PorterAppFormData,
+  SourceOptions,
+  clientAppToProto,
+} 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";
 
 export const useAppValidation = ({
   deploymentTargetID,
+  creating = false,
 }: {
   deploymentTargetID?: string;
+  creating?: boolean;
 }) => {
   const { currentProject, currentCluster } = useContext(Context);
 
+  const getBranchHead = async ({
+    projectID,
+    source,
+  }: {
+    projectID: number;
+    source: SourceOptions & {
+      type: "github";
+    };
+  }) => {
+    const [owner, repo_name] = await z
+      .tuple([z.string(), z.string()])
+      .parseAsync(source.git_repo_name?.split("/"));
+
+    const res = await api.getBranchHead(
+      "<token>",
+      {},
+      {
+        ...source,
+        project_id: projectID,
+        kind: "github",
+        owner,
+        name: repo_name,
+        branch: source.git_branch,
+      }
+    );
+
+    const commitData = await z
+      .object({
+        commit_sha: z.string(),
+      })
+      .parseAsync(res.data);
+
+    return commitData;
+  };
+
   const validateApp = useCallback(
     async (data: PorterAppFormData) => {
       if (!currentProject || !currentCluster) {
@@ -23,6 +65,22 @@ export const useAppValidation = ({
       }
 
       const proto = clientAppToProto(data);
+      const commit_sha = await match(data.source)
+        .with({ type: "github" }, async (src) => {
+          if (!creating) {
+            return "";
+          }
+
+          const { commit_sha } = await getBranchHead({
+            projectID: currentProject.id,
+            source: src,
+          });
+          return commit_sha;
+        })
+        .with({ type: "docker-registry" }, () => {
+          return "";
+        })
+        .exhaustive();
 
       const res = await api.validatePorterApp(
         "<token>",
@@ -33,7 +91,7 @@ export const useAppValidation = ({
             })
           ),
           deployment_target_id: deploymentTargetID,
-          commit_sha: "", // not sending a commit sha since the CLI will handle this
+          commit_sha,
         },
         {
           project_id: currentProject.id,

+ 1 - 0
dashboard/src/main/home/app-dashboard/create-app/CreateApp.tsx

@@ -131,6 +131,7 @@ const CreateApp: React.FC<CreateAppProps> = ({ history }) => {
   const { updateAppStep } = useAppAnalytics(name);
   const { validateApp } = useAppValidation({
     deploymentTargetID: deploymentTarget?.deployment_target_id,
+    creating: true,
   });
 
   const onSubmit = handleSubmit(async (data) => {