Untitled

 avatar
unknown
plain_text
7 days ago
4.2 kB
5
Indexable
#!/usr/bin/env bash
set -euo pipefail

GITHUB_API_HEADER="Accept: application/vnd.github.v3+json"

# ... (other functions remain unchanged) ...

github::calculate_total_modifications() {
  # ... existing code ...
}

github::has_label() {
  # ... existing code ...
}

github::add_label_to_pr() {
  local -r pr_number="${1}"
  shift
  # All new labels (size and language) are passed as additional arguments.
  local -a new_labels=("$@")

  # Fetch current labels from the issue (pull request).
  local current_labels
  current_labels=$(curl -sSL \
    -H "Authorization: token $GITHUB_TOKEN" \
    -H "$GITHUB_API_HEADER" \
    "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number" | jq -r '.labels[].name')

  # Remove any existing size labels from current labels.
  local filtered_current_labels
  filtered_current_labels=$(echo "$current_labels" | grep -vwE "^(size/xs|size/s|size/m|size/l|size/xl)$" || true)

  # Combine filtered current labels with the new labels.
  local all_labels
  all_labels=$(printf "%s\n%s" "$filtered_current_labels" "$(printf "%s\n" "${new_labels[@]}")")
  # Remove empty lines.
  all_labels=$(echo "$all_labels" | sed '/^\s*$/d')
  # Remove duplicates.
  local unique_labels
  unique_labels=$(echo "$all_labels" | sort -u)

  local -r comma_separated_labels
  comma_separated_labels=$(github::format_labels "$unique_labels")

  log::message "Final labels: $comma_separated_labels"

  curl -sSL \
    -H "Authorization: token $GITHUB_TOKEN" \
    -H "$GITHUB_API_HEADER" \
    -X PATCH \
    -H "Content-Type: application/json" \
    -d "{\"labels\":[$comma_separated_labels]}" \
    "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number" >/dev/null
}

github::format_labels() {
  SAVEIFS=$IFS
  IFS=$'\n'
  local -r labels=($@)
  IFS=$SAVEIFS
  quoted_labels=()

  for ((i = 0; i < ${#labels[@]}; i++)); do
    label="${labels[$i]}"
    quoted_labels+=("$(str::quote "$label")")
  done

  coll::join_by "," "${quoted_labels[@]/#/}"
}

github::comment() {
  local -r comment="$1"

  curl -sSL \
    -H "Authorization: token $GITHUB_TOKEN" \
    -H "$GITHUB_API_HEADER" \
    -X POST \
    -H "Content-Type: application/json" \
    -d "{\"body\":\"$comment\"}" \
    "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number/comments"
}

# --- New Function for Language Labeling using Linguist ---

labeler::add_language_labels() {
  local pr_number="$1"
  
  # Create a temporary directory for the changed files.
  local tmp_dir
  tmp_dir=$(mktemp -d)
  
  # Determine changed files. You may adjust this diff range as needed.
  # This example uses 'origin/main' as the base.
  local changed_files
  changed_files=$(git diff --name-only origin/main...HEAD)
  
  # Copy each changed file into the temporary directory preserving structure.
  for file in $changed_files; do
    if [ -f "$file" ]; then
      mkdir -p "$tmp_dir/$(dirname "$file")"
      cp "$file" "$tmp_dir/$file"
    fi
  done

  # Run Linguist on the temporary directory. Ensure that Ruby and the github-linguist gem are installed.
  local linguist_output
  linguist_output=$(linguist --breakdown "$tmp_dir")
  
  # Debug: Output the linguist breakdown.
  log::message "Linguist output:"
  log::message "$linguist_output"
  
  # Parse Linguist output to get language names.
  # Expected output lines: "LanguageName  xx.xx%"
  local -a languages=()
  while IFS= read -r line; do
    # Skip empty lines.
    [ -z "$line" ] && continue
    # Extract the language name (first column)
    local lang
    lang=$(echo "$line" | awk '{print $1}')
    # Optionally, skip a generic "Other" language.
    if [ "$lang" = "Other" ]; then
      continue
    fi
    # Build a label and convert to lowercase.
    languages+=("pr: lang/$(echo "$lang" | tr '[:upper:]' '[:lower:]')")
  done <<< "$linguist_output"
  
  # Clean up temporary directory.
  rm -rf "$tmp_dir"
  
  # Add each detected language label to the PR.
  for lang_label in "${languages[@]}"; do
    log::message "Adding language label: ${lang_label}"
    github::add_label "$pr_number" "$lang_label"
  done
}
Editor is loading...
Leave a Comment