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

mintsweet pushed a commit to branch fix-webhook-apiKey
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git

commit 3449e38102d9acd3d60c9a45d20a71d629a281ef
Author: mintsweet <[email protected]>
AuthorDate: Wed Nov 8 20:41:02 2023 +1300

    fix(config-ui): handle webhook apiKey error
---
 config-ui/src/features/connections/slice.ts            | 12 +++++-------
 config-ui/src/features/connections/utils.ts            |  3 +--
 .../register/webhook/components/create-dialog.tsx      |  7 ++++---
 .../register/webhook/components/view-dialog.tsx        | 18 ++++++++++--------
 config-ui/src/types/webhook.ts                         |  1 -
 5 files changed, 20 insertions(+), 21 deletions(-)

diff --git a/config-ui/src/features/connections/slice.ts 
b/config-ui/src/features/connections/slice.ts
index 2908df406..c22e91b96 100644
--- a/config-ui/src/features/connections/slice.ts
+++ b/config-ui/src/features/connections/slice.ts
@@ -107,7 +107,10 @@ export const testConnection = createAsyncThunk(
 
 export const addWebhook = createAsyncThunk('connections/addWebhook', async 
(payload: any) => {
   const webhook = await API.plugin.webhook.create(payload);
-  return transformWebhook(webhook);
+  return {
+    webhook: transformWebhook(webhook),
+    apiKey: webhook.apiKey.apiKey,
+  };
 });
 
 export const removeWebhook = createAsyncThunk('connections/removeWebhook', 
async (id: ID) => {
@@ -171,7 +174,7 @@ export const connectionsSlice = createSlice({
         }
       })
       .addCase(addWebhook.fulfilled, (state, action) => {
-        state.webhooks.push(action.payload);
+        state.webhooks.push(action.payload.webhook);
       })
       .addCase(removeWebhook.fulfilled, (state, action) => {
         state.webhooks = state.webhooks.filter((wh) => wh.id !== 
action.payload);
@@ -180,11 +183,6 @@ export const connectionsSlice = createSlice({
         state.webhooks = state.webhooks.map((wh) =>
           wh.id === action.payload.id ? { ...wh, name: action.payload.name } : 
wh,
         );
-      })
-      .addCase(renewWebhookApiKey.fulfilled, (state, action) => {
-        state.webhooks = state.webhooks.map((wh) =>
-          wh.id === action.payload.id ? { ...wh, apiKey: action.payload.apiKey 
} : wh,
-        );
       });
   },
 });
diff --git a/config-ui/src/features/connections/utils.ts 
b/config-ui/src/features/connections/utils.ts
index 2a383fd77..8ad5be5bb 100644
--- a/config-ui/src/features/connections/utils.ts
+++ b/config-ui/src/features/connections/utils.ts
@@ -39,7 +39,7 @@ export const transformConnection = (plugin: string, 
connection: IConnectionAPI):
     password: connection.password,
     appId: connection.appId,
     secretKey: connection.secretKey,
-    enableGraphql:connection.enableGraphql,
+    enableGraphql: connection.enableGraphql,
   };
 };
 
@@ -50,7 +50,6 @@ export const transformWebhook = (connection: IWebhookAPI): 
IWebhook => {
     postIssuesEndpoint: connection.postIssuesEndpoint,
     closeIssuesEndpoint: connection.closeIssuesEndpoint,
     postPipelineDeployTaskEndpoint: connection.postPipelineDeployTaskEndpoint,
-    apiKey: connection.apiKey.apiKey,
     apiKeyId: connection.apiKey.id,
   };
 };
diff --git 
a/config-ui/src/plugins/register/webhook/components/create-dialog.tsx 
b/config-ui/src/plugins/register/webhook/components/create-dialog.tsx
index 6f45f6abf..b148f9267 100644
--- a/config-ui/src/plugins/register/webhook/components/create-dialog.tsx
+++ b/config-ui/src/plugins/register/webhook/components/create-dialog.tsx
@@ -51,9 +51,10 @@ export const CreateDialog = ({ isOpen, onCancel, 
onSubmitAfter }: Props) => {
   const handleSubmit = async () => {
     const [success, res] = await operator(
       async () => {
-        const { id, apiKey, postIssuesEndpoint, closeIssuesEndpoint, 
postPipelineDeployTaskEndpoint } = await dispatch(
-          addWebhook({ name }),
-        ).unwrap();
+        const {
+          webhook: { id, postIssuesEndpoint, closeIssuesEndpoint, 
postPipelineDeployTaskEndpoint },
+          apiKey,
+        } = await dispatch(addWebhook({ name })).unwrap();
 
         return {
           id,
diff --git a/config-ui/src/plugins/register/webhook/components/view-dialog.tsx 
b/config-ui/src/plugins/register/webhook/components/view-dialog.tsx
index e3c89a36e..10c4604bc 100644
--- a/config-ui/src/plugins/register/webhook/components/view-dialog.tsx
+++ b/config-ui/src/plugins/register/webhook/components/view-dialog.tsx
@@ -32,10 +32,10 @@ interface Props {
   onCancel: () => void;
 }
 
-const transformURI = (prefix: string, webhook: IWebhook) => {
+const transformURI = (prefix: string, webhook: IWebhook, apiKey: string) => {
   return {
     postIssuesEndpoint: `curl ${prefix}${webhook.postIssuesEndpoint} -X 'POST' 
-H 'Authorization: Bearer ${
-      webhook.apiKey ?? '{API_KEY}'
+      apiKey ?? '{API_KEY}'
     }' -d '{
         "issue_key":"DLK-1234",
         "title":"a feature from DLK",
@@ -46,10 +46,10 @@ const transformURI = (prefix: string, webhook: IWebhook) => 
{
         "updated_date":"2020-01-01T12:00:00+00:00"
      }'`,
     closeIssuesEndpoint: `curl ${prefix}${webhook.closeIssuesEndpoint} -X 
'POST' -H 'Authorization: Bearer ${
-      webhook.apiKey ?? '{API_KEY}'
+      apiKey ?? '{API_KEY}'
     }'`,
     postDeploymentsCurl: `curl 
${prefix}${webhook.postPipelineDeployTaskEndpoint} -X 'POST' -H 'Authorization: 
Bearer ${
-      webhook.apiKey ?? '{API_KEY}'
+      apiKey ?? '{API_KEY}'
     }' -d '{
          "commit_sha":"the sha of deployment commit",
          "repo_url":"the repo URL of the deployment commit",
@@ -61,19 +61,21 @@ const transformURI = (prefix: string, webhook: IWebhook) => 
{
 export const ViewDialog = ({ initialId, onCancel }: Props) => {
   const [isOpen, setIsOpen] = useState(false);
   const [operating, setOperating] = useState(false);
+  const [apiKey, setApiKey] = useState('');
 
   const dispatch = useAppDispatch();
   const webhook = useAppSelector((state) => selectWebhook(state, initialId)) 
as IWebhook;
   const prefix = useMemo(() => `${window.location.origin}/api`, []);
 
-  const URI = transformURI(prefix, webhook);
+  const URI = transformURI(prefix, webhook, apiKey);
 
   const handleGenerateNewKey = async () => {
-    const [success] = await operator(() => 
dispatch(renewWebhookApiKey(initialId)), {
+    const [success, res] = await operator(async () => await 
dispatch(renewWebhookApiKey(initialId)).unwrap(), {
       setOperating,
     });
 
     if (success) {
+      setApiKey(res.apiKey);
       setIsOpen(false);
     }
   };
@@ -120,12 +122,12 @@ export const ViewDialog = ({ initialId, onCancel }: 
Props) => {
           label="API Key"
           subLabel="If you have forgotten your API key, you can revoke the 
previous key and generate a new one as a replacement."
         >
-          {!webhook.apiKey ? (
+          {!apiKey ? (
             <Button intent={Intent.PRIMARY} text="Revoke and generate a new 
key" onClick={() => setIsOpen(true)} />
           ) : (
             <>
               <S.ApiKey>
-                <CopyText content={webhook.apiKey} />
+                <CopyText content={apiKey} />
                 <span>No Expiration</span>
               </S.ApiKey>
               <S.Tips>
diff --git a/config-ui/src/types/webhook.ts b/config-ui/src/types/webhook.ts
index 71b510ee6..2035bf675 100644
--- a/config-ui/src/types/webhook.ts
+++ b/config-ui/src/types/webhook.ts
@@ -34,6 +34,5 @@ export interface IWebhook {
   postIssuesEndpoint: string;
   closeIssuesEndpoint: string;
   postPipelineDeployTaskEndpoint: string;
-  apiKey: string;
   apiKeyId: number;
 }

Reply via email to