Skip to content

fix: Optimize script library scripts and execution methods#8262

Merged
f2c-ci-robot[bot] merged 1 commit intodev-v2from
pr@dev-v2@fix_script_library
Mar 26, 2025
Merged

fix: Optimize script library scripts and execution methods#8262
f2c-ci-robot[bot] merged 1 commit intodev-v2from
pr@dev-v2@fix_script_library

Conversation

@ssongliu
Copy link
Copy Markdown
Member

No description provided.

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot Bot commented Mar 26, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

});
} else {
done();
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided JavaScript code for handling closing operations includes several changes and optimizations:

Changes Made

  1. Querying Elements:

    • Removed logging statements that query elements within drawerContent.value.
    • Simplified the conditional logic to check for .terminal existence before asking for confirmation.
  2. Handling Form Check:

    • Combined hasForm and !hasTerminal checks into a single condition, simplifying the logic.
    • Returned early if no form or terminal is present, effectively skipping the modal prompt when unnecessary.
  3. Removed Unnecessary Code Block:

    • The original block handling both forms and terminals was redundant with the current setup, which only handles non-terminal cases.

Optimization Suggestions/Improvements

  1. Code Readability:

    • Added comments to clarify the purpose of key lines and decisions made during the function execution.
    • Maintaining consistent indentation throughout reduces cognitive load when reviewing the code.
  2. Consistency with Logic:

    • Ensured all conditions and actions are handled consistently across similar scenarios (i.e., checking for presence of .el-form vs .terminal).

These modifications make the code more efficient, easier to understand, and maintain while addressing potential edge cases where there might be empty drawers without either a form or a terminal element.

});
},
},
{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code has two changes. The first change removes the runRef.value!.acceptParams call and instead replaces it with an ElMessageBox confirmation dialog prompting the user to confirm if they want to execute the script. This adds a layer of safety and prevents accidental execution of scripts without proper confirmation.

Potential Improvements:

  1. Consistent Usage: Ensure that all button configurations follow a consistent structure for better readability.
  2. Error Handling/Feedback: Consider adding handling for exceptions or errors during the execution step using something like try-catch blocks to improve robustness.

Here's the modified section of the code with improved formatting:

@@ -198,7 +198,32 @@ const buttons = [
     {
         label: i18n.global.t('commons.button.handle'),
         click: (row: Cronjob.ScriptInfo) => {
-            runRef.value!.acceptParams({ scriptID: row.id, scriptName: row.name });
+            ElMessageBox.confirm(
+                i18n.global.t('cronjob.library.handleHelper', [
+                    globalStore.currentNode === 'local'
+                        ? i18n.global.t('xpack.node.master')
+                        : globalStore.currentNode,
+                    row.name,
+                ]),
+                i18n.global.t('commons.button.handle'),
+                {
+                    confirmButtonText: i18n.global.t('commons.button.confirm'),
+                    cancelButtonText: i18n.global.t('commons.button.cancel'),
+                    type: 'info',
+                },
+            )
+                .then(() => {
+                    // Assuming runRef.value is defined elsewhere and acceptsParams method exists
+                    try {
+                        runRef.value.acceptParams({
+                            scriptID: row.id,
+                            scriptName: row.name,
+                        });
+                    } catch (error) {
+                        console.error(`Failed to execute script '${row.name}':`, error);
+                        ElMessage.error(i18n.global.t('cronjob.errorMessage'));
+                    }
+                })
+                .catch(() => {
+                    // User canceled operation
+                    ElMessage.info(i18n.global.t('cronjob.cancelledOperation'));
+                });
         },
     },
 };

This version includes basic exception handling and uses a more detailed message when the operation fails.

_, _ = pty.Write([]byte("bash -c \"$INIT_SCRIPT\"\n"))
}

lcmd := &LocalCommand{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main issues with the code is:

  1. The comment in initCmd should be updated to mention that it's an INIT_SCRIPT.
  2. There should be a space added after "bash" in "bash -c "$INIT_SCRIPT\"", as per Unix shell syntax.

Suggestions for improvement:

  • Use a function name like CreateCommand. It better describes what the function does and aligns with Go naming conventions.
  • Consider using filepath.Join() instead of manual concatenation when creating full paths if applicable, which would enhance safety and make the code more robust for different environments.

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Member

@wanghe-fit2cloud wanghe-fit2cloud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@wanghe-fit2cloud
Copy link
Copy Markdown
Member

/approve

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot Bot commented Mar 26, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: wanghe-fit2cloud

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@f2c-ci-robot f2c-ci-robot Bot merged commit d4213da into dev-v2 Mar 26, 2025
6 checks passed
@f2c-ci-robot f2c-ci-robot Bot deleted the pr@dev-v2@fix_script_library branch March 26, 2025 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants