You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by kl...@apache.org on 2024/05/15 07:50:14 UTC

(incubator-devlake) branch main updated: fix: related projects error when updating scope config (#7472)

This is an automated email from the ASF dual-hosted git repository.

klesh pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 3dd13fad3 fix: related projects error when updating scope config (#7472)
3dd13fad3 is described below

commit 3dd13fad3469ac88f1c6bf1bfbe082815a1b61ff
Author: 青湛 <0x...@gmail.com>
AuthorDate: Wed May 15 19:50:09 2024 +1200

    fix: related projects error when updating scope config (#7472)
---
 config-ui/src/api/scope/index.ts                   |   6 +-
 .../src/plugins/components/scope-config/index.tsx  | 150 +++++++++++++++++----
 .../routes/blueprint/connection-detail/index.tsx   |   8 +-
 .../routes/blueprint/connection-detail/table.tsx   |  87 +++++-------
 config-ui/src/routes/connection/connection.tsx     |  68 +---------
 5 files changed, 164 insertions(+), 155 deletions(-)

diff --git a/config-ui/src/api/scope/index.ts b/config-ui/src/api/scope/index.ts
index b4331cd50..7007314f6 100644
--- a/config-ui/src/api/scope/index.ts
+++ b/config-ui/src/api/scope/index.ts
@@ -37,8 +37,10 @@ export const list = (
     data,
   });
 
-export const get = (plugin: string, connectionId: ID, scopeId: ID) =>
-  request(`/plugins/${plugin}/connections/${connectionId}/scopes/${scopeId}`);
+export const get = (plugin: string, connectionId: ID, scopeId: ID, payload?: { blueprints: boolean }) =>
+  request(`/plugins/${plugin}/connections/${connectionId}/scopes/${scopeId}`, {
+    data: payload,
+  });
 
 export const remove = (plugin: string, connectionId: ID, scopeId: ID, onlyData: boolean) =>
   request(`/plugins/${plugin}/connections/${connectionId}/scopes/${scopeId}?delete_data_only=${onlyData}`, {
diff --git a/config-ui/src/plugins/components/scope-config/index.tsx b/config-ui/src/plugins/components/scope-config/index.tsx
index 303191aa5..8c3c6fe71 100644
--- a/config-ui/src/plugins/components/scope-config/index.tsx
+++ b/config-ui/src/plugins/components/scope-config/index.tsx
@@ -23,6 +23,7 @@ import styled from 'styled-components';
 
 import API from '@/api';
 import { IconButton, Message } from '@/components';
+import { PATHS } from '@/config';
 import { operator } from '@/utils';
 
 import { PluginName } from '../plugin-name';
@@ -31,31 +32,42 @@ import { ScopeConfigForm } from '../scope-config-form';
 
 const Wrapper = styled.div``;
 
+type RelatedProjects = Array<{ name: string; blueprintId: ID; scopes: Array<{ scopeName: string }> }>;
+
 interface Props {
   plugin: string;
   connectionId: ID;
   scopeId: ID;
   scopeName: string;
-  id?: ID;
-  name?: string;
-  onSuccess?: (id?: ID) => void;
+  scopeConfigId?: ID;
+  scopeConfigName?: string;
+  projects?: Array<{ name: string; blueprintId: ID }>;
+  onSuccess: (id?: ID) => void;
 }
 
-export const ScopeConfig = ({ plugin, connectionId, scopeId, scopeName, id, name, onSuccess }: Props) => {
+export const ScopeConfig = ({
+  plugin,
+  connectionId,
+  scopeId,
+  scopeName,
+  scopeConfigId,
+  scopeConfigName,
+  onSuccess,
+}: Props) => {
   const [type, setType] = useState<'associate' | 'update' | 'relatedProjects' | 'duplicate'>();
-  const [relatedProjects, setRelatedProjects] = useState<
-    Array<{ name: string; scopes: Array<{ scopeId: ID; scopeName: string }> }>
-  >([]);
+  const [relatedProjects, setRelatedProjects] = useState<RelatedProjects>([]);
+
+  const [operating, setOperating] = useState(false);
 
   const {
     token: { colorPrimary },
   } = theme.useToken();
 
-  const handleHideDialog = () => setType(undefined);
+  const [modal, contextHolder] = Modal.useModal();
 
-  const handleCheckScopeConfig = async () => {
-    if (!id) return;
+  const handleHideDialog = () => setType(undefined);
 
+  const handleCheckScopeConfig = async (id: ID) => {
     const [success, res] = await operator(() => API.scopeConfig.check(plugin, id), { hideToast: true });
 
     if (success) {
@@ -64,7 +76,7 @@ export const ScopeConfig = ({ plugin, connectionId, scopeId, scopeName, id, name
         scopes: it.scopes,
       }));
 
-      if (projects.length > 0) {
+      if (projects.length > 1) {
         setRelatedProjects(projects);
         setType('relatedProjects');
       } else {
@@ -73,9 +85,82 @@ export const ScopeConfig = ({ plugin, connectionId, scopeId, scopeName, id, name
     }
   };
 
+  const handleRun = async (pname: string, blueprintId: ID, data?: { skipCollectors?: boolean; fullSync?: boolean }) => {
+    const [success] = await operator(() => API.blueprint.trigger(blueprintId, data), {
+      setOperating,
+    });
+
+    if (success) {
+      window.open(PATHS.PROJECT(pname, { tab: 'status' }));
+    }
+  };
+
+  const handleShowProjectsModal = (projects: RelatedProjects) => {
+    if (!projects || !projects.length) {
+      onSuccess();
+    } else if (projects.length === 1) {
+      const [{ name, blueprintId }] = projects;
+      modal.success({
+        closable: true,
+        centered: true,
+        width: 550,
+        title: 'Scope Config Saved',
+        content: 'Please re-transform data to apply the updated scope config.',
+        footer: (
+          <div style={{ marginTop: 20, textAlign: 'center' }}>
+            <Button
+              type="primary"
+              loading={operating}
+              onClick={() => handleRun(name, blueprintId, { skipCollectors: true })}
+            >
+              Re-transform now
+            </Button>
+          </div>
+        ),
+        onCancel: onSuccess,
+      });
+    } else {
+      modal.success({
+        closable: true,
+        centered: true,
+        width: 830,
+        title: 'Scope Config Saved',
+        content: (
+          <>
+            <div style={{ marginBottom: 16 }}>
+              The listed projects are impacted. Please re-transform the data to apply the updated scope config.
+            </div>
+            <ul>
+              {projects.map(({ name, blueprintId }: { name: string; blueprintId: ID }) => (
+                <li key={name} style={{ marginBottom: 10 }}>
+                  <Space>
+                    <span>{name}</span>
+                    <Button
+                      size="small"
+                      type="link"
+                      loading={operating}
+                      onClick={() => handleRun(name, blueprintId, { skipCollectors: true })}
+                    >
+                      Re-transform Data
+                    </Button>
+                  </Space>
+                </li>
+              ))}
+            </ul>
+          </>
+        ),
+        footer: null,
+        onCancel: onSuccess,
+      });
+    }
+  };
+
   const handleAssociate = async (trId: ID) => {
-    const [success] = await operator(
-      () => API.scope.update(plugin, connectionId, scopeId, { scopeConfigId: trId !== 'None' ? +trId : null }),
+    const [success, res] = await operator(
+      async () => {
+        await API.scope.update(plugin, connectionId, scopeId, { scopeConfigId: trId === 'None' ? null : +trId });
+        return API.scope.get(plugin, connectionId, scopeId, { blueprints: true });
+      },
       {
         hideToast: true,
       },
@@ -83,18 +168,35 @@ export const ScopeConfig = ({ plugin, connectionId, scopeId, scopeName, id, name
 
     if (success) {
       handleHideDialog();
-      onSuccess?.(trId);
+      handleShowProjectsModal(
+        (res.blueprints ?? []).map((it: any) => ({
+          name: it.projectName,
+          blueprintId: it.id,
+          scopes: [
+            {
+              scopeId,
+              scopeName,
+            },
+          ],
+        })),
+      );
     }
   };
 
-  const handleUpdate = (trId: ID) => {
+  const handleUpdate = async (trId: ID) => {
     handleHideDialog();
-    onSuccess?.(trId);
+
+    const [success, res] = await operator(() => API.scopeConfig.check(plugin, trId), { hideToast: true });
+
+    if (success) {
+      handleShowProjectsModal(res.projects ?? []);
+    }
   };
 
   return (
     <Wrapper>
-      <span>{id ? name : 'N/A'}</span>
+      {contextHolder}
+      <span>{scopeConfigId ? scopeConfigName : 'N/A'}</span>
       <IconButton
         icon={<LinkOutlined />}
         helptip="Associate Scope Config"
@@ -104,13 +206,13 @@ export const ScopeConfig = ({ plugin, connectionId, scopeId, scopeName, id, name
           setType('associate');
         }}
       />
-      {id && (
+      {scopeConfigId && (
         <IconButton
           icon={<EditOutlined />}
           helptip=" Edit Scope Config"
           type="link"
           size="small"
-          onClick={handleCheckScopeConfig}
+          onClick={() => handleCheckScopeConfig(scopeConfigId)}
         />
       )}
       {type === 'associate' && (
@@ -126,7 +228,7 @@ export const ScopeConfig = ({ plugin, connectionId, scopeId, scopeName, id, name
             <ScopeConfigForm
               plugin={plugin}
               connectionId={connectionId}
-              scopeConfigId={id}
+              scopeConfigId={scopeConfigId}
               scopeId={scopeId}
               onCancel={handleHideDialog}
               onSubmit={handleAssociate}
@@ -135,7 +237,7 @@ export const ScopeConfig = ({ plugin, connectionId, scopeId, scopeName, id, name
             <ScopeConfigSelect
               plugin={plugin}
               connectionId={connectionId}
-              scopeConfigId={id}
+              scopeConfigId={scopeConfigId}
               onCancel={handleHideDialog}
               onSubmit={handleAssociate}
             />
@@ -148,7 +250,7 @@ export const ScopeConfig = ({ plugin, connectionId, scopeId, scopeName, id, name
             plugin={plugin}
             connectionId={connectionId}
             showWarning
-            scopeConfigId={id}
+            scopeConfigId={scopeConfigId}
             scopeId={scopeId}
             onCancel={handleHideDialog}
             onSubmit={handleUpdate}
@@ -162,7 +264,7 @@ export const ScopeConfig = ({ plugin, connectionId, scopeId, scopeName, id, name
             connectionId={connectionId}
             showWarning
             forceCreate
-            scopeConfigId={id}
+            scopeConfigId={scopeConfigId}
             scopeId={scopeId}
             onCancel={handleHideDialog}
             onSubmit={handleAssociate}
@@ -175,7 +277,7 @@ export const ScopeConfig = ({ plugin, connectionId, scopeId, scopeName, id, name
           width={830}
           centered
           footer={null}
-          title={`Edit '${name}' for '${scopeName}'`}
+          title={`Edit '${scopeConfigName}' for '${scopeName}'`}
           onCancel={handleHideDialog}
         >
           <Message content="The change will apply to all following projects:" />
diff --git a/config-ui/src/routes/blueprint/connection-detail/index.tsx b/config-ui/src/routes/blueprint/connection-detail/index.tsx
index d0c66d4bc..665d0ca1f 100644
--- a/config-ui/src/routes/blueprint/connection-detail/index.tsx
+++ b/config-ui/src/routes/blueprint/connection-detail/index.tsx
@@ -223,13 +223,7 @@ export const BlueprintConnectionDetailPage = () => {
             Manage Data Scope
           </Button>
         </Flex>
-        <BlueprintConnectionDetailTable
-          plugin={plugin}
-          connectionId={connectionId}
-          scopeIds={scopeIds}
-          operating={operating}
-          onRun={handleRun}
-        />
+        <BlueprintConnectionDetailTable plugin={plugin} connectionId={connectionId} scopeIds={scopeIds} />
       </Flex>
       <Modal open={open} width={820} centered title="Manage Data Scope" footer={null} onCancel={handleHideDataScope}>
         <DataScopeSelect
diff --git a/config-ui/src/routes/blueprint/connection-detail/table.tsx b/config-ui/src/routes/blueprint/connection-detail/table.tsx
index 35de59ad1..7036d59a2 100644
--- a/config-ui/src/routes/blueprint/connection-detail/table.tsx
+++ b/config-ui/src/routes/blueprint/connection-detail/table.tsx
@@ -17,7 +17,7 @@
  */
 
 import { useState } from 'react';
-import { Table, Modal, Button } from 'antd';
+import { Table } from 'antd';
 
 import API from '@/api';
 import { useRefreshData } from '@/hooks';
@@ -27,11 +27,9 @@ interface Props {
   plugin: string;
   connectionId: ID;
   scopeIds: ID[];
-  operating: boolean;
-  onRun: (params: { skipCollectors: boolean }) => void;
 }
 
-export const BlueprintConnectionDetailTable = ({ plugin, connectionId, scopeIds, operating, onRun }: Props) => {
+export const BlueprintConnectionDetailTable = ({ plugin, connectionId, scopeIds }: Props) => {
   const [version, setVersion] = useState(1);
 
   const { ready, data } = useRefreshData(async () => {
@@ -44,59 +42,34 @@ export const BlueprintConnectionDetailTable = ({ plugin, connectionId, scopeIds,
     }));
   }, [version]);
 
-  const [modal, contextHolder] = Modal.useModal();
-
-  const handleChangeScopeConfig = () => {
-    modal.success({
-      closable: true,
-      centered: true,
-      width: 550,
-      title: 'Scope Config Saved',
-      content: 'Please re-transform data to apply the updated scope config.',
-      footer: (
-        <div style={{ marginTop: 20, textAlign: 'center' }}>
-          <Button type="primary" loading={operating} onClick={() => onRun({ skipCollectors: true })}>
-            Re-transform now
-          </Button>
-        </div>
-      ),
-      onCancel: () => {
-        setVersion(version + 1);
-      },
-    });
-  };
-
   return (
-    <>
-      <Table
-        loading={!ready}
-        rowKey="id"
-        size="middle"
-        columns={[
-          {
-            title: 'Data Scope',
-            dataIndex: 'name',
-            key: 'name',
-          },
-          {
-            title: 'Scope Config',
-            key: 'scopeConfig',
-            render: (_, { id, name, scopeConfigId, scopeConfigName }) => (
-              <ScopeConfig
-                plugin={plugin}
-                connectionId={connectionId}
-                scopeId={id}
-                scopeName={name}
-                id={scopeConfigId}
-                name={scopeConfigName}
-                onSuccess={handleChangeScopeConfig}
-              />
-            ),
-          },
-        ]}
-        dataSource={data ?? []}
-      />
-      {contextHolder}
-    </>
+    <Table
+      loading={!ready}
+      rowKey="id"
+      size="middle"
+      columns={[
+        {
+          title: 'Data Scope',
+          dataIndex: 'name',
+          key: 'name',
+        },
+        {
+          title: 'Scope Config',
+          key: 'scopeConfig',
+          render: (_, { id, name, scopeConfigId, scopeConfigName }) => (
+            <ScopeConfig
+              plugin={plugin}
+              connectionId={connectionId}
+              scopeId={id}
+              scopeName={name}
+              scopeConfigId={scopeConfigId}
+              scopeConfigName={scopeConfigName}
+              onSuccess={() => setVersion(version + 1)}
+            />
+          ),
+        },
+      ]}
+      dataSource={data ?? []}
+    />
   );
 };
diff --git a/config-ui/src/routes/connection/connection.tsx b/config-ui/src/routes/connection/connection.tsx
index 74533aa54..d2d81e623 100644
--- a/config-ui/src/routes/connection/connection.tsx
+++ b/config-ui/src/routes/connection/connection.tsx
@@ -69,8 +69,6 @@ export const Connection = () => {
     token: { colorPrimary },
   } = theme.useToken();
 
-  const [modal, contextHolder] = Modal.useModal();
-
   const dispatch = useAppDispatch();
   const connection = useAppSelector((state) => selectConnection(state, `${plugin}-${connectionId}`)) as IConnection;
 
@@ -230,65 +228,6 @@ export const Connection = () => {
     }
   };
 
-  const handleRun = async (pname: string, blueprintId: ID, data?: { skipCollectors?: boolean; fullSync?: boolean }) => {
-    const [success] = await operator(() => API.blueprint.trigger(blueprintId, data), {
-      setOperating,
-      hideToast: true,
-    });
-
-    if (success) {
-      window.open(PATHS.PROJECT(pname, { tab: 'status' }));
-    }
-  };
-
-  const handleScopeConfigChange = async (scopeConfigId?: ID) => {
-    if (!scopeConfigId) {
-      setVersion(version + 1);
-      return;
-    }
-
-    const [success, res] = await operator(() => API.scopeConfig.check(plugin, scopeConfigId), { hideToast: true });
-
-    if (success) {
-      if (!res.projects) {
-        setVersion(version + 1);
-        return;
-      }
-
-      modal.success({
-        closable: true,
-        centered: true,
-        width: 830,
-        title: 'Scope Config Saved',
-        content: (
-          <>
-            <div style={{ marginBottom: 16 }}>
-              The listed projects are impacted. Please re-transform the data to apply the updated scope config.
-            </div>
-            <ul>
-              {res.projects.map(({ name, blueprintId }: { name: string; blueprintId: ID }) => (
-                <li key={name} style={{ marginBottom: 10 }}>
-                  <Space>
-                    <span>{name}</span>
-                    <Button
-                      size="small"
-                      type="link"
-                      onClick={() => handleRun(name, blueprintId, { skipCollectors: true })}
-                    >
-                      Re-transform Data
-                    </Button>
-                  </Space>
-                </li>
-              ))}
-            </ul>
-          </>
-        ),
-        footer: null,
-        onCancel: () => setVersion(version + 1),
-      });
-    }
-  };
-
   return (
     <PageHeader
       breadcrumbs={[
@@ -368,9 +307,9 @@ export const Connection = () => {
                   connectionId={connectionId}
                   scopeId={id}
                   scopeName={name}
-                  id={configId}
-                  name={configName}
-                  onSuccess={handleScopeConfigChange}
+                  scopeConfigId={configId}
+                  scopeConfigName={configName}
+                  onSuccess={() => setVersion(version + 1)}
                 />
               ),
             },
@@ -579,7 +518,6 @@ export const Connection = () => {
           )}
         </Modal>
       )}
-      {contextHolder}
     </PageHeader>
   );
 };