Просмотр исходного кода

Implemented api endpoint for manual creation of prev envs

jnfrati 4 лет назад
Родитель
Сommit
3b24ecf7a3

+ 20 - 1
dashboard/src/main/home/cluster-dashboard/preview-environments/deployments/DeploymentList.tsx

@@ -126,6 +126,19 @@ const DeploymentList = ({ environments }: { environments: Environment[] }) => {
       .finally(() => setIsLoading(false));
   };
 
+  const handlePreviewEnvironmentManualCreation = (pullRequest: PullRequest) => {
+    setPullRequests((prev) => {
+      return prev.filter((pr) => {
+        return (
+          pr.pr_title === pullRequest.pr_title &&
+          `${pr.repo_owner}/${pr.repo_name}` ===
+            `${pullRequest.repo_owner}/${pullRequest.repo_name}`
+        );
+      });
+    });
+    handleRefresh();
+  };
+
   const filteredDeployments = useMemo(() => {
     if (statusSelectorVal === "not_deployed") {
       return [];
@@ -198,7 +211,13 @@ const DeploymentList = ({ environments }: { environments: Environment[] }) => {
     return (
       <>
         {filteredPullRequests.map((pr) => {
-          return <PullRequestCard key={pr.pr_title} pullRequest={pr} />;
+          return (
+            <PullRequestCard
+              key={pr.pr_title}
+              pullRequest={pr}
+              onCreation={handlePreviewEnvironmentManualCreation}
+            />
+          );
         })}
         {filteredDeployments.map((d) => {
           return (

+ 25 - 4
dashboard/src/main/home/cluster-dashboard/preview-environments/deployments/PullRequestCard.tsx

@@ -1,15 +1,36 @@
-import React, { useState } from "react";
+import React, { useState, useContext } from "react";
 import styled from "styled-components";
 import pr_icon from "assets/pull_request_icon.svg";
 import { PullRequest } from "../types";
 import { integrationList } from "shared/common";
+import api from "shared/api";
+import { Context } from "shared/Context";
 
-const PullRequestCard = ({ pullRequest }: { pullRequest: PullRequest }) => {
+const PullRequestCard = ({
+  pullRequest,
+  onCreation,
+}: {
+  pullRequest: PullRequest;
+  onCreation: (pullRequest: PullRequest) => void;
+}) => {
+  const { currentProject, currentCluster, setCurrentError } = useContext(
+    Context
+  );
   const [showRepoTooltip, setShowRepoTooltip] = useState(false);
 
   const repository = `${pullRequest.repo_owner}/${pullRequest.repo_name}`;
 
-  const createPreviewEnvironment = () => {};
+  const createPreviewEnvironment = async () => {
+    try {
+      await api.createPreviewEnvironmentDeployment("<token>", pullRequest, {
+        cluster_id: currentCluster?.id,
+        project_id: currentProject?.id,
+      });
+      onCreation(pullRequest);
+    } catch (error) {
+      setCurrentError(error);
+    }
+  };
 
   return (
     <DeploymentCardWrapper>
@@ -48,7 +69,7 @@ const PullRequestCard = ({ pullRequest }: { pullRequest: PullRequest }) => {
         </Flex>
       </DataContainer>
       <Flex>
-        <CreatePreviewEnvironmentButton>
+        <CreatePreviewEnvironmentButton onClick={createPreviewEnvironment}>
           <i className="material-icons">add</i>
           Create Preview environment
         </CreatePreviewEnvironmentButton>

+ 25 - 0
dashboard/src/shared/api.tsx

@@ -1,3 +1,4 @@
+import { PullRequest } from "main/home/cluster-dashboard/preview-environments/types";
 import { release } from "process";
 import { baseApi } from "./baseApi";
 
@@ -130,6 +131,28 @@ const deleteEnvironment = baseApi<
   return `/api/projects/${project_id}/gitrepos/${git_installation_id}/${git_repo_owner}/${git_repo_name}/clusters/${cluster_id}/environment`;
 });
 
+const createPreviewEnvironmentDeployment = baseApi<
+  PullRequest,
+  { project_id: number; cluster_id: number }
+>(
+  "POST",
+  ({ project_id, cluster_id }) =>
+    `/api/projects/${project_id}/clusters/${cluster_id}/deployments/pull_request`
+);
+
+const reenablePreviewEnvironmentDeployment = baseApi<
+  {},
+  {
+    project_id: number;
+    cluster_id: number;
+    deployment_id: number;
+  }
+>(
+  "PATCH",
+  ({ project_id, cluster_id, deployment_id }) =>
+    `/api/projects/${project_id}/clusters/${cluster_id}/deployments/${deployment_id}/reenable`
+);
+
 const listEnvironments = baseApi<
   {},
   {
@@ -1679,6 +1702,8 @@ export default {
   createEmailVerification,
   createEnvironment,
   deleteEnvironment,
+  createPreviewEnvironmentDeployment,
+  reenablePreviewEnvironmentDeployment,
   listEnvironments,
   createGCPIntegration,
   createInvite,