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

Refactoring to match bash standards #45

Merged
merged 2 commits into from
Jul 13, 2024
Merged

Refactoring to match bash standards #45

merged 2 commits into from
Jul 13, 2024

Conversation

johnlk
Copy link
Contributor

@johnlk johnlk commented Jul 13, 2024

Description

This pull request refactors the existing bash scripts to improve code quality, readability, and adherence to modern bash conventions and best practices.

Changes Made

  • Implemented consistent use of [[ for conditionals instead of [
  • Replaced echo -e with printf for more portable and predictable output
  • Introduced readonly variables for color codes in utils.sh
  • Simplified the utils::verify_required_env_vars function using an array and loop
  • Added set -euo pipefail to main.sh for improved error handling
  • Replaced $HOME_DIR/src/main.sh with $0 in docpars command for better portability
  • Extracted file ignoring logic into a separate function in github.sh
  • Simplified JSON processing in github.sh by removing base64 encoding/decoding
  • Improved variable naming and scoping across all files
  • Removed unnecessary semicolons and local -r declarations where appropriate
  • Added explicit error handling and exit codes

Screenshots

N/A

@johnlk johnlk self-assigned this Jul 13, 2024
Copy link

Score: 85

Improvements:

  • Ensure consistency in quoting style for readability.
  • Refactor github::get_commit_diff to streamline and separate responsibilities more clearly.
  • Consider extracting the API request logic to a separate utility function to avoid repetition.
  • Add proper comments or documentation headers for functions to improve maintainability.
  • Check for potential error handling where appropriate, such as checking the success of curl requests.
  • Improve the overall script to handle edge cases such as empty or invalid API responses.
github::get_commit_diff() {
  local -r pr_number="$1"
  local -r files_to_ignore="$2"
  local -r api_url="$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$pr_number"
  
  local body=""
  body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" "$api_url/files?per_page=100")
  
  if [[ -z "$files_to_ignore" ]]; then
    echo "$body"
    return
  fi

  local diffs=""
  while read -r file; do
    local filename status
    filename=$(jq -r '.filename' <<< "$file")
    status=$(jq -r '.status' <<< "$file")
    
    [[ "$status" == "removed" ]] && continue
    
    if ! github::should_ignore_file "$filename" "$files_to_ignore"; then
      diffs+=$(jq -r '.patch' <<< "$file")
      diffs+=$'\n\n'
    fi
  done < <(jq -c '.[]' <<< "$body")
  
  echo "$diffs"
}

Temp PR change

Put back test change
@johnlk johnlk force-pushed the john/refactoring branch from 63c6c79 to 1544cb6 Compare July 13, 2024 18:04
@johnlk johnlk merged commit 7182ae9 into main Jul 13, 2024
1 check passed
@johnlk johnlk deleted the john/refactoring branch July 13, 2024 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant