Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added preinstall script #955

Merged
merged 7 commits into from
Jan 29, 2025
Merged

Added preinstall script #955

merged 7 commits into from
Jan 29, 2025

Conversation

mirooon
Copy link
Contributor

@mirooon mirooon commented Jan 27, 2025

Which Jira task belongs to this PR?

Why did I implement it this way?

The scriptMaster script relies on system-installed packages (bc and jq) for its functionality. Without these dependencies being explicitly installed, users running scriptMaster are likely to encounter various issues beacause both bc and jq are used extensively in the script. This change ensures that the required dependencies are installed on initial yarn install.

Checklist before requesting a review

Checklist for reviewer (DO NOT DEPLOY and contracts BEFORE CHECKING THIS!!!)

  • I have checked that any arbitrary calls to external contracts are validated and or restricted
  • I have checked that any privileged calls (i.e. storage modifications) are validated and or restricted
  • I have ensured that any new contracts have had AT A MINIMUM 1 preliminary audit conducted on by <company/auditor>

Copy link
Contributor

coderabbitai bot commented Jan 27, 2025

Walkthrough

A new shell script named preinstall.sh has been added to the project, which automates the installation of necessary packages based on the detected operating system (Linux or macOS). The script checks for the presence of required packages (jq and bc) and installs them using the appropriate package manager. Additionally, a preinstall script entry has been introduced in the package.json file to execute this shell script before the installation of package dependencies.

Changes

File Change Summary
package.json Added "preinstall": "bash preinstall.sh" script entry
preinstall.sh New shell script to install jq and bc packages on Linux and macOS, with OS detection and package manager handling

Suggested reviewers

  • ezynda3

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0a52203 and 369d813.

📒 Files selected for processing (1)
  • preinstall.sh (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • preinstall.sh
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: enforce-min-test-coverage
  • GitHub Check: run-unit-tests
  • GitHub Check: generate-tag

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (2)
preinstall.sh (2)

1-2: Add error handling and cleanup.

The script would benefit from proper error handling and cleanup of temporary files.

Add these at the beginning of the script:

 #!/bin/bash
+set -e  # Exit on error
+
+# Cleanup function
+cleanup() {
+  # Add cleanup tasks here
+  rm -f "$BREW_SCRIPT" 2>/dev/null || true
+}
+
+# Set up trap
+trap cleanup EXIT

1-53: Improve documentation and user feedback.

The script would benefit from better documentation and more informative messages.

Add a header comment block and improve messaging:

 #!/bin/bash
+
+# preinstall.sh
+#
+# This script ensures that required system packages (jq and bc) are installed
+# before running the scriptMaster script. It supports Linux (with apt) and
+# macOS (with Homebrew) systems.
+#
+# Required packages:
+# - jq: Command-line JSON processor
+# - bc: Basic calculator
 
-echo "Setting up the development environment..."
+echo "🔧 Setting up the development environment..."
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 42-42: Quote this to prevent word splitting.

(SC2046)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 663278d and 6897acc.

📒 Files selected for processing (2)
  • package.json (1 hunks)
  • preinstall.sh (1 hunks)
🧰 Additional context used
🪛 Shellcheck (0.10.0)
preinstall.sh

[warning] 42-42: Quote this to prevent word splitting.

(SC2046)

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: enforce-min-test-coverage
  • GitHub Check: run-unit-tests
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (1)
package.json (1)

56-56: Consider adding a postinstall verification step.

While the preinstall script is correctly added, consider adding a verification step to ensure the required packages are available after installation.

Add a verification script:

     "preinstall": "bash preinstall.sh",
+    "postinstall": "command -v jq >/dev/null && command -v bc >/dev/null || (echo 'Required packages not found after installation' && exit 1)",

Also, verify that this change doesn't conflict with the existing postinstall script:

preinstall.sh Outdated Show resolved Hide resolved
preinstall.sh Outdated Show resolved Hide resolved
preinstall.sh Outdated Show resolved Hide resolved
@lifi-action-bot
Copy link
Collaborator

Test Coverage Report

Line Coverage: 78.66% (2238 / 2845 lines)
Function Coverage: 84.71% ( 388 / 458 functions)
Branch Coverage: 39.60% ( 221 / 558 branches)
Test coverage (78.66%) is above min threshold (76%). Check passed.

@mirooon mirooon marked this pull request as ready for review January 27, 2025 22:22
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (3)
preinstall.sh (3)

6-15: ⚠️ Potential issue

Improve security and robustness of Linux package installation.

The current implementation has several security and robustness concerns that need to be addressed.

Apply this diff to improve security and robustness:

+readonly VALID_PACKAGES=("jq" "bc")
+
 install_package_linux() {
   PACKAGE=$1
+  
+  # Validate package is in allowed list
+  if [[ ! " ${VALID_PACKAGES[@]} " =~ " ${PACKAGE} " ]]; then
+    echo "Invalid package name: $PACKAGE"
+    exit "$E_PACKAGE"
+  fi
+
+  # Check for apt-get availability
+  if ! command -v apt-get &> /dev/null; then
+    echo "apt-get not found. This script requires apt-based Linux distribution."
+    exit "$E_GENERAL"
+  fi
+
   if ! command -v "$PACKAGE" &> /dev/null; then
     echo "Installing $PACKAGE on Linux..."
-    sudo apt-get update -y
-    sudo apt-get install -y $PACKAGE
+    if ! sudo apt-get update -y; then
+      echo "Failed to update package list"
+      exit "$E_PACKAGE"
+    fi
+    if ! sudo apt-get install -y "$PACKAGE"; then
+      echo "Failed to install $PACKAGE"
+      exit "$E_PACKAGE"
+    fi
   else
     echo "$PACKAGE is already installed."
   fi
 }

30-34: 🛠️ Refactor suggestion

Add sudo availability check for Linux.

The script should verify sudo availability before proceeding with Linux installation.

 if [[ "$OS" == "Linux" ]]; then
   echo "Detected Linux. Using apt package manager."
+  # Check for sudo availability
+  if ! command -v sudo &> /dev/null; then
+    echo "sudo is required but not available"
+    exit "$E_GENERAL"
+  fi
   install_package_linux jq
   install_package_linux bc

37-56: ⚠️ Potential issue

Improve Homebrew installation security.

The Homebrew installation process needs additional security measures.

  1. Verify the downloaded script's checksum
  2. Use a specific version of Homebrew instead of HEAD
  3. Add timeout for curl download

Apply this diff:

   # Check if Homebrew is installed
   if ! command -v brew &> /dev/null; then
     echo "Homebrew is not installed. Installing Homebrew..."
+    
+    # Known good version and checksum
+    readonly BREW_VERSION="4.2.4"
+    readonly BREW_CHECKSUM="valid_sha256_checksum_here"  # Replace with actual checksum
+    
     # Download script first to allow inspection
     BREW_SCRIPT=$(mktemp)
-    if ! curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh -o "$BREW_SCRIPT"; then
+    if ! curl --max-time 30 -fsSL "https://raw.githubusercontent.com/Homebrew/install/${BREW_VERSION}/install.sh" -o "$BREW_SCRIPT"; then
       echo "Failed to download Homebrew install script"
       rm -f "$BREW_SCRIPT"
-      exit 1
+      exit "$E_DOWNLOAD"
     fi
+    
+    # Verify checksum
+    if ! echo "$BREW_CHECKSUM $BREW_SCRIPT" | sha256sum -c --status; then
+      echo "Homebrew install script checksum verification failed"
+      rm -f "$BREW_SCRIPT"
+      exit "$E_DOWNLOAD"
+    fi
+    
     # Execute the downloaded script
     if ! /bin/bash "$BREW_SCRIPT"; then
       echo "Failed to install Homebrew"
       rm -f "$BREW_SCRIPT"
-      exit 1
+      exit "$E_GENERAL"
     fi
     rm -f "$BREW_SCRIPT"
+    
     # Add Homebrew to PATH for immediate use
-    eval "$("$(brew --prefix)/bin/brew" shellenv)"
+    if ! BREW_PATH="$(brew --prefix)"; then
+      echo "Failed to get Homebrew prefix"
+      exit "$E_GENERAL"
+    fi
+    if ! eval "$("${BREW_PATH}/bin/brew" shellenv)"; then
+      echo "Failed to update shell environment"
+      exit "$E_GENERAL"
+    fi
   fi
🧹 Nitpick comments (2)
preinstall.sh (2)

27-29: Improve OS detection robustness.

The OS detection could be more robust by handling edge cases.

-# Detect the operating system
-OS=$(uname -s)
+# Detect the operating system
+readonly OS=$(uname -s)
+
+# Verify supported OS
+if [[ ! "$OS" =~ ^(Linux|Darwin)$ ]]; then
+  echo "Unsupported operating system: $OS"
+  echo "This script supports only Linux and macOS."
+  exit "$E_GENERAL"
+fi

66-66: Improve final confirmation message.

The final message could be more informative by listing the installed packages and their versions.

-echo "All necessary packages are installed."
+echo "Installation complete! Installed packages:"
+for package in "${VALID_PACKAGES[@]}"; do
+  if command -v "$package" &> /dev/null; then
+    version=$("$package" --version 2>/dev/null || echo "version not available")
+    echo "- $package: $version"
+  fi
+done
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6897acc and 6bba4b1.

📒 Files selected for processing (1)
  • preinstall.sh (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: enforce-min-test-coverage
  • GitHub Check: run-unit-tests
  • GitHub Check: analyze
  • GitHub Check: generate-tag
🔇 Additional comments (1)
preinstall.sh (1)

17-25: ⚠️ Potential issue

Improve security and robustness of macOS package installation.

The macOS installation function has similar security and robustness concerns.

Apply this diff to improve security and robustness:

 install_package_mac() {
   PACKAGE=$1
+  
+  # Validate package is in allowed list
+  if [[ ! " ${VALID_PACKAGES[@]} " =~ " ${PACKAGE} " ]]; then
+    echo "Invalid package name: $PACKAGE"
+    exit "$E_PACKAGE"
+  fi
+
   if ! command -v "$PACKAGE" &> /dev/null; then
     echo "Installing $PACKAGE on macOS..."
-    brew install $PACKAGE
+    if ! brew install "$PACKAGE"; then
+      echo "Failed to install $PACKAGE"
+      exit "$E_PACKAGE"
+    fi
   else
     echo "$PACKAGE is already installed."
   fi
 }

Likely invalid or redundant comment.

preinstall.sh Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
preinstall.sh (1)

22-33: Quote variable to prevent word splitting.

Add quotes around $PACKAGE in the command check for consistency with error handling.

   PACKAGE=$1
-  if ! command -v $PACKAGE &> /dev/null; then
+  if ! command -v "$PACKAGE" &> /dev/null; then
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6bba4b1 and 8714dfd.

📒 Files selected for processing (1)
  • preinstall.sh (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: enforce-min-test-coverage
  • GitHub Check: run-unit-tests
  • GitHub Check: generate-tag
🔇 Additional comments (6)
preinstall.sh (6)

1-4: LGTM!

The script initialization is clean and straightforward.


35-41: LGTM!

Good practice to check for sudo availability on Linux systems before proceeding.


43-77: LGTM!

The implementation follows security best practices:

  • Downloads Homebrew script for inspection before execution
  • Proper error handling and cleanup
  • Clear user messaging for unsupported systems

79-79: LGTM!

Clear completion message.


6-20: 🛠️ Refactor suggestion

Add error handling for package installation.

While the apt-get availability check is good, the package installation step needs error handling.

   if ! command -v $PACKAGE &> /dev/null; then
     echo "Installing $PACKAGE on Linux..."
     sudo apt-get update -y
-    sudo apt-get install -y $PACKAGE
+    if ! sudo apt-get install -y "$PACKAGE"; then
+      echo "Failed to install $PACKAGE"
+      exit 1
+    fi

Likely invalid or redundant comment.


45-46: Verify package version compatibility.

The script installs the latest versions of jq and bc without version constraints. Please verify that the latest versions are compatible with your project.

Also applies to: 71-72

preinstall.sh Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
preinstall.sh (3)

55-57: Update Linux detection message to reflect multiple package manager support.

The message incorrectly states it's using apt package manager, but the script supports both apt-get and dnf.

-  echo "Detected Linux. Using apt package manager."
+  echo "Detected Linux. Checking for supported package managers..."

35-35: Declare PACKAGE as local variable.

For consistency with the Linux function and to prevent variable leakage, declare PACKAGE as local.

-  PACKAGE=$1
+  local PACKAGE=$1

91-91: Enhance completion message with package details.

Consider making the success message more informative by listing the installed packages.

-echo "All necessary packages are installed."
+echo "Successfully installed required packages: jq, bc"
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8714dfd and 0a52203.

📒 Files selected for processing (1)
  • preinstall.sh (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: enforce-min-test-coverage
  • GitHub Check: run-unit-tests
  • GitHub Check: generate-tag
🔇 Additional comments (1)
preinstall.sh (1)

50-89: LGTM! Robust OS detection and package management.

The implementation includes:

  • Proper sudo availability check for Linux
  • Secure Homebrew installation with error handling
  • Clear error messages for unsupported OS

preinstall.sh Show resolved Hide resolved
ezynda3
ezynda3 previously approved these changes Jan 28, 2025
0xDEnYO
0xDEnYO previously approved these changes Jan 28, 2025
preinstall.sh Outdated Show resolved Hide resolved
preinstall.sh Outdated Show resolved Hide resolved
@mirooon mirooon dismissed stale reviews from 0xDEnYO and ezynda3 via 369d813 January 29, 2025 08:21
@0xDEnYO 0xDEnYO enabled auto-merge (squash) January 29, 2025 10:06
@0xDEnYO 0xDEnYO merged commit 6c3f04f into main Jan 29, 2025
16 checks passed
@0xDEnYO 0xDEnYO deleted the add-preinstall-setup-bash-script branch January 29, 2025 10:06
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.

4 participants