fantonangeli commented on code in PR #2608:
URL: 
https://github.com/apache/incubator-kie-tools/pull/2608#discussion_r1768865631


##########
packages/serverless-logic-web-tools/src/settings/runtimeTools/RuntimeToolsSettings.tsx:
##########
@@ -43,14 +43,28 @@ import {
   saveConfigCookie,
 } from "./RuntimeToolsConfig";
 import { removeTrailingSlashFromUrl } from "../../url";
+import { validDataIndexUrl } from "../../url";
+import { Alert } from "@patternfly/react-core/dist/js";
+import { useAppI18n } from "../../i18n";
 
 const PAGE_TITLE = "Runtime Tools";
 
+enum DataIndexValidation {

Review Comment:
   XXS suggestion. On some pages we call this `FormValiationOptions`, we can 
make this name consistent with those pages:
   
packages/serverless-logic-web-tools/src/settings/openshift/OpenShiftSettingsSimpleConfig.tsx
   packages/online-editor/src/accounts/kubernetes/ConnectToKubernetesSimple.tsx
   packages/online-editor/src/accounts/openshift/ConnecToOpenShiftSimple.tsx
   
   ```suggestion
   enum FormValiationOptions {
   ```



##########
packages/serverless-logic-web-tools/src/url/index.ts:
##########
@@ -26,3 +26,12 @@
 export function removeTrailingSlashFromUrl(url: string): string {
   return url.replace(/\/$/, "");
 }
+
+export function validDataIndexUrl(url: string): boolean {
+  try {
+    new URL(url);
+    return true;

Review Comment:
   I found a bug here, we are accepting every protocol in this function.
   
   @tiagobento it's out of scope, but do you think we should also reproduce 
this suggestion here?
   
https://github.com/kumaradityaraj/kie-tools/blob/fc27f22fa01303a39132ae27494b43a41bc7101f/packages/kubernetes-bridge/src/service/KubernetesConnection.ts#L51
   
   ```suggestion
       const parsedUrl = new URL(url);
       return parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:";
   ```



##########
packages/serverless-logic-web-tools/src/url/index.ts:
##########
@@ -26,3 +26,12 @@
 export function removeTrailingSlashFromUrl(url: string): string {
   return url.replace(/\/$/, "");
 }
+
+export function validDataIndexUrl(url: string): boolean {

Review Comment:
   We can make this name consistent with the other kie-tools validation 
functions.
   ```suggestion
   export function isDataIndexUrlValid(url: string): boolean {
   ```



##########
packages/serverless-logic-web-tools/tests/url/dataIndexValidUrl.test.ts:
##########
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { validDataIndexUrl } from "../../src/url";
+
+describe("removeTrailingSlash", () => {

Review Comment:
   Leftover?



##########
packages/serverless-logic-web-tools/tests/url/dataIndexValidUrl.test.ts:
##########
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { validDataIndexUrl } from "../../src/url";
+
+describe("removeTrailingSlash", () => {
+  it.each([
+    ["http://example.com/";, true],
+    ["https://example.com/";, true],

Review Comment:
   Let's ensure we accept only http/https protocol.
   ```suggestion
       ["http://example.com";, true],
       ["http://example.com/";, true],
       ["https://example.com/";, true],
       ["ftps://example.com/", false],
   ```



##########
packages/serverless-logic-web-tools/src/settings/runtimeTools/RuntimeToolsSettings.tsx:
##########
@@ -43,14 +43,28 @@ import {
   saveConfigCookie,
 } from "./RuntimeToolsConfig";
 import { removeTrailingSlashFromUrl } from "../../url";
+import { validDataIndexUrl } from "../../url";
+import { Alert } from "@patternfly/react-core/dist/js";
+import { useAppI18n } from "../../i18n";
 
 const PAGE_TITLE = "Runtime Tools";
 
+enum DataIndexValidation {
+  INITIAL = "INITIAL",
+  INVALID = "INVALID",
+}
+
 export function RuntimeToolsSettings(props: SettingsPageProps) {
+  const { i18n } = useAppI18n();
   const settings = useSettings();
   const settingsDispatch = useSettingsDispatch();
   const [config, setConfig] = useState(settings.runtimeTools.config);
   const [isModalOpen, setIsModalOpen] = useState(false);
+  const [isDataIndexUrlValidated, setDataIndexUrlValidated] = 
useState(DataIndexValidation.INVALID);

Review Comment:
   ```suggestion
     const [isDataIndexUrlValidated, setDataIndexUrlValidated] = 
useState(DataIndexValidation.INITIAL);
   ```



##########
packages/serverless-logic-web-tools/src/i18n/AppI18n.ts:
##########
@@ -71,6 +71,7 @@ interface AppDictionary extends ReferenceDictionary {
       validationError: string;
       connectionError: string;
       configExpiredWarning: string;
+      validDataIndexURLError: string;

Review Comment:
   Thank you for taking care of the translations.
   This section is related to the OpenShift settings page.
   I suggest creating a section for RuntimeToolsSettings page to store this 
property.
   ie: `RuntimeToolsSettings.configModal.validDataIndexURLError`



##########
packages/serverless-logic-web-tools/src/settings/runtimeTools/RuntimeToolsSettings.tsx:
##########
@@ -80,6 +94,11 @@ export function RuntimeToolsSettings(props: 
SettingsPageProps) {
     const newConfig: RuntimeToolsSettingsConfig = {
       dataIndexUrl: removeTrailingSlashFromUrl(config.dataIndexUrl),
     };
+    if (!validDataIndexUrl(config.dataIndexUrl)) {
+      setDataIndexUrlValidated(DataIndexValidation.INVALID);
+      return;
+    }
+

Review Comment:
   After checking if it's valid it would be great to really test the url 
answers to a simple query.
   You can use:
   
https://github.com/apache/incubator-kie-tools/blob/fc27f22fa01303a39132ae27494b43a41bc7101f/packages/sonataflow-deployment-webapp/src/data/index.ts#L33
   Maybe we can move `verifyDataIndex()` inside the package 
`runtime-tools-swf-gateway-api` to avoid code duplication.



##########
packages/serverless-logic-web-tools/tests/url/dataIndexValidUrl.test.ts:
##########
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { validDataIndexUrl } from "../../src/url";
+
+describe("removeTrailingSlash", () => {
+  it.each([
+    ["http://example.com/";, true],
+    ["https://example.com/";, true],
+    ["loremIpsum", false],
+    ["google.com", false],
+  ])("should validate the data index URL", (inputUrl, isValidUrl) => {

Review Comment:
   Let's improve the log messages.
   ```suggestion
     ])("the data index URL %s validation should be %s", (inputUrl, isValidUrl) 
=> {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to