Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions .github/workflows/gw_comment_trigger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
outputs:
repo_url: ${{ steps.parse.outputs.repo_url }}
limit: ${{ steps.parse.outputs.limit }}
branch: ${{ steps.parse.outputs.branch }}

steps:
- name: Parse comment
Expand All @@ -24,6 +25,7 @@ jobs:
run: |
echo "Comment body: $COMMENT_BODY"

# Extract repository URL
# Extract repository URL using environment variable
repo_url=$(echo "$COMMENT_BODY" | grep -oP 'gw --repo \K[^\s]+' || echo "")
if [ -z "$repo_url" ]; then
Expand All @@ -37,7 +39,7 @@ jobs:
exit 1
fi

# Extract limit with improved regex and validation
# Extract limit
limit_raw=$(echo "$COMMENT_BODY" | grep -oP -- '--limit\s+\K\d+' || echo "")

# Validate and set limit (default to 10 if not specified or invalid)
Expand All @@ -50,9 +52,18 @@ jobs:
fi
fi

# Extract branch
branch_raw=$(echo "$COMMENT_BODY" | grep -oP -- '--branch\s+\K[^\s]+' || echo "")
if [ -n "$branch_raw" ]; then
branch="$branch_raw"
else
branch=""
fi

echo "repo_url=$repo_url" >> $GITHUB_OUTPUT
echo "limit=$limit" >> $GITHUB_OUTPUT
echo "Parsed repo: $repo_url, limit: $limit"
echo "branch=$branch" >> $GITHUB_OUTPUT
echo "Parsed repo: $repo_url, limit: $limit, branch: $branch"

run-bugspots:
needs: parse-comment
Expand All @@ -79,15 +90,16 @@ jobs:
env:
REPO_URL: ${{ needs.parse-comment.outputs.repo_url }}
LIMIT: ${{ needs.parse-comment.outputs.limit }}
BRANCH: ${{ needs.parse-comment.outputs.branch }}
run: |
chmod +x BugPredict/gw.sh
# Run the script with environment variables to prevent injection
BugPredict/gw.sh "$REPO_URL" "$LIMIT"
BugPredict/gw.sh "$REPO_URL" "$LIMIT" "$BRANCH"

- name: Check for repository errors
id: check-errors
env:
REPO_URL: ${{ needs.parse-comment.outputs.repo_url }}
BRANCH: ${{ needs.parse-comment.outputs.branch }}
run: |
repo_name=$(basename "$REPO_URL" .git)

Expand All @@ -104,6 +116,8 @@ jobs:
echo "error_type=invalid_repo" >> $GITHUB_OUTPUT
elif [[ "$error_content" == *"No commits found"* ]]; then
echo "error_type=no_commits" >> $GITHUB_OUTPUT
elif [[ "$error_content" == *"no such branch"* ]]; then
echo "error_type=invalid_branch" >> $GITHUB_OUTPUT
else
echo "error_type=unknown" >> $GITHUB_OUTPUT
fi
Expand Down Expand Up @@ -150,6 +164,13 @@ jobs:
echo "" >> comment.md
echo "Bugspots analysis requires commit history to analyze bug patterns." >> comment.md
;;
"invalid_branch")
echo "### 🚫 Invalid Branch" >> comment.md
echo "" >> comment.md
echo "**Error:** The specified branch does not exist in the repository." >> comment.md
echo "" >> comment.md
echo "Please specify a valid branch or omit the --branch parameter to use the default branch." >> comment.md
;;
*)
echo "### ⚠️ Analysis Error" >> comment.md
echo "" >> comment.md
Expand All @@ -170,6 +191,7 @@ jobs:
echo '```' >> comment.md
echo "gw --repo https://github.com/username/repository" >> comment.md
echo "gw --repo https://github.com/username/repository --limit 15" >> comment.md
echo "gw --repo https://github.com/username/repository --branch main" >> comment.md
echo '```' >> comment.md

- name: Prepare success comment
Expand Down
90 changes: 55 additions & 35 deletions BugPredict/gw.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ set -e
# Parse command line arguments
REPO_URL="$1"
LIMIT="${2:-10}"
BRANCH="$3"

if [ -z "$REPO_URL" ]; then
echo "Usage: $0 <repository_url> [limit]"
echo "Example: $0 https://github.com/user/repo.git 15"
echo "Usage: $0 <repository_url> [limit] [branch]"
echo "Example: $0 https://github.com/user/repo.git 15 main"
exit 1
fi

Expand All @@ -19,9 +20,10 @@ OUTPUT_DIR="bugspots-results"
echo "🚀 Bugspots Comment Analyzer starting at $(date '+%Y-%m-%d %H:%M:%S %Z')"
echo "Repository: $REPO_URL"
echo "File limit: $LIMIT"
[ -n "$BRANCH" ] && echo "Branch: $BRANCH"

# Check if bugspots is installed
if ! gem list bugspots -i > /dev/null; then
# Check if bugspots gem is installed
if ! gem list bugspots -i > /dev/null 2>&1; then
echo "Installing bugspots gem..."
gem install bugspots
fi
Expand All @@ -32,32 +34,27 @@ mkdir -p "$OUTPUT_DIR"
echo "🔄 Cloning $REPO_URL ..."
repo_name=$(basename "$REPO_URL" .git)

# Try different branch names in order of preference
branches=("main" "master" "develop" "dev")
clone_success=false
# Initialize selected_branch
selected_branch=""

for branch in "${branches[@]}"; do
echo "Trying to clone branch: $branch"
if git clone --branch "$branch" --depth 1000 "$REPO_URL" "$WORKDIR/$repo_name" 2>/dev/null; then
clone_success=true
echo "✅ Successfully cloned branch: $branch"
break
# Clone repository with defaut branch or specified branch
echo "Cloning repository..."
if [ -n "$BRANCH" ]; then
if ! git clone --branch "$BRANCH" --depth 1000 "$REPO_URL" "$WORKDIR/$repo_name" 2>/dev/null; then
echo "❌ Error: Failed to clone specified branch: $BRANCH" >&2
echo "Clone failed for $repo_name branch $BRANCH at $(date '+%Y-%m-%d %H:%M:%S %Z')" > "$OUTPUT_DIR/bugspots-${repo_name}.err"
exit 1
fi
done

# If named branches fail, try default clone
if [ "$clone_success" = false ]; then
echo "Named branches failed, trying default clone..."
if git clone --depth 1000 "$REPO_URL" "$WORKDIR/$repo_name"; then
clone_success=true
echo "✅ Successfully cloned with default branch"
selected_branch="$BRANCH"
echo "✅ Successfully cloned branch: $BRANCH"
else
if ! git clone --depth 1000 "$REPO_URL" "$WORKDIR/$repo_name" 2>/dev/null; then
echo "Error: Failed to clone $REPO_URL" >&2
echo "Clone failed for $repo_name at $(date '+%Y-%m-%d %H:%M:%S %Z')" > "$OUTPUT_DIR/bugspots-${repo_name}.err"
exit 1
fi
fi

if [ "$clone_success" = false ]; then
echo "❌ Error: Failed to clone $REPO_URL" >&2
echo "Clone failed for $repo_name at $(date '+%Y-%m-%d %H:%M:%S %Z')" > "$OUTPUT_DIR/bugspots-${repo_name}.err"
exit 1
selected_branch=$(git -C "$WORKDIR/$repo_name" rev-parse --abbrev-ref HEAD)
echo "✅ Successfully cloned with default branch: $selected_branch"
fi

# Verify repository
Expand All @@ -84,9 +81,23 @@ total_commits=$(git rev-list --count HEAD 2>/dev/null || echo "unknown")
echo "📈 Repository has $total_commits commits in current branch"

# Run bugspots with simplified word pattern
echo "📊 Running Bugspots for $repo_name ..."
echo "Executing: git bugspots -w fix" >&2
if ! git bugspots -w fix > "../../$OUTPUT_DIR/bugspots-${repo_name}.log" 2> "../../$OUTPUT_DIR/bugspots-${repo_name}.err"; then
echo "📊 Running Bugspots for $repo_name on branch $selected_branch..."

# Build the bugspots command
bugspots_cmd="bugspots ."

# Add branch parameter if specified or detected
if [ -n "$selected_branch" ]; then
bugspots_cmd="$bugspots_cmd --branch $selected_branch"
fi

# Add regex pattern for bug-fix commits
bugspots_cmd="$bugspots_cmd --regex 'fix(es|ed)?|close(s|d)?'"

echo "Executing: $bugspots_cmd" >&2

# Execute bugspots command
if ! eval "$bugspots_cmd" > "../../$OUTPUT_DIR/bugspots-${repo_name}.log" 2> "../../$OUTPUT_DIR/bugspots-${repo_name}.err"; then
echo "❌ Error: Bugspots failed for $repo_name. Check $OUTPUT_DIR/bugspots-${repo_name}.err" >&2
if [ -s "../../$OUTPUT_DIR/bugspots-${repo_name}.err" ]; then
echo "Error details:"
Expand All @@ -103,24 +114,33 @@ else
if [ -s "../../$OUTPUT_DIR/bugspots-${repo_name}.log" ]; then
if grep -q "Hotspots:" "../../$OUTPUT_DIR/bugspots-${repo_name}.log"; then
# Extract and count hotspots
hotspot_lines=$(sed -n '/Hotspots:/,$p' "../../$OUTPUT_DIR/bugspots-${repo_name}.log" | tail -n +2 | grep -E '^\s*[0-9]+\.[0-9]+.*' | wc -l)
hotspot_lines=$(sed -n '/Hotspots:/,$p' "../../$OUTPUT_DIR/bugspots-${repo_name}.log" | tail -n +2 | grep -E '^\s*[0-9]+\.[0-9]+.*' 2>/dev/null | wc -l)
echo "📋 Found $hotspot_lines hotspot files"

# Show summary if available - look for bug fix commits count
if grep -qE "Found \d+ bugfix commits|Found \d+ fix commits" "../../$OUTPUT_DIR/bugspots-${repo_name}.log"; then
bugfix_info=$(grep -oE "Found \d+ (bugfix|fix) commits" "../../$OUTPUT_DIR/bugspots-${repo_name}.log" | head -1)
echo "🐛 $bugfix_info"
fi

# Extract top N hotspots after "Hotspots:" line
echo ""
echo "🎯 Top $LIMIT hotspots found:"
echo "================================"
sed -n '/Hotspots:/,$p' "../../$OUTPUT_DIR/bugspots-${repo_name}.log" | \
tail -n +2 | \
grep -E '^\s*[0-9]+\.[0-9]+.*' | \
grep -E '^\s*[0-9]+\.[0-9]+.*' 2>/dev/null | \
head -n "$LIMIT" | \
sed 's/^\s*//'
sed 's/^\s*//' | \
while IFS= read -r line; do
echo " $line"
done
echo "================================"

# Create a clean output file with only the top N hotspots for the GitHub Actions
sed -n '/Hotspots:/,$p' "../../$OUTPUT_DIR/bugspots-${repo_name}.log" | \
tail -n +2 | \
grep -E '^\s*[0-9]+\.[0-9]+.*' | \
grep -E '^\s*[0-9]+\.[0-9]+.*' 2>/dev/null | \
head -n "$LIMIT" | \
sed 's/^\s*//' > "../../$OUTPUT_DIR/bugspots-${repo_name}-top.log"

Expand All @@ -139,4 +159,4 @@ cd - > /dev/null
# Clean up temporary directories
rm -rf "$WORKDIR"

echo "🏁 Bugspots Comment Analyzer completed at $(date '+%Y-%m-%d %H:%M:%S %Z')"
echo "🏁 Bugspots Comment Analyzer completed at $(date '+%Y-%m-%d %H:%M:%S %Z')"
Loading