Skip to content

CCache Stats Comment #950

CCache Stats Comment

CCache Stats Comment #950

name: CCache Stats Comment
on:
workflow_run:
workflows: [Validate]
types:
- completed
permissions:
contents: read
pull-requests: write
actions: read
jobs:
comment:
name: Post CCache Stats
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request'
steps:
- name: Download artifacts
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
mkdir -p artifacts && cd artifacts
artifacts_url="${{ github.event.workflow_run.artifacts_url }}"
# Track processed artifact names to skip duplicates (API returns newest first)
seen_file=$(mktemp)
gh api --paginate "$artifacts_url" -q '.artifacts[] | [.name, .archive_download_url] | @tsv' | while read artifact
do
IFS=$'\t' read name url <<< "$artifact"
case "$name" in
ccache-stats-*|event-file)
# Skip if we've already processed an artifact with this name
if grep -qxF "$name" "$seen_file" 2>/dev/null; then
echo "Skipping duplicate artifact: $name"
continue
fi
echo "$name" >> "$seen_file"
gh api $url > "$name.zip"
unzip -d "$name" "$name.zip"
;;
esac
done
rm -f "$seen_file"
- name: Check for artifacts
id: artifact_check
run: |
if [ -d artifacts/event-file ]; then
echo "artifacts_found=true" >> $GITHUB_OUTPUT
else
echo "artifacts_found=false" >> $GITHUB_OUTPUT
fi
- name: Generate combined stats comment
if: steps.artifact_check.outputs.artifacts_found == 'true'
run: |
# Function to read a value from data.txt
get_value() {
local file="$1"
local key="$2"
if [ -f "$file" ]; then
grep "^${key}=" "$file" 2>/dev/null | cut -d= -f2- || echo "N/A"
else
echo "-"
fi
}
# Function to format hit rate with emoji
format_hit_rate() {
local rate="$1"
if [ "$rate" = "N/A" ] || [ "$rate" = "-" ] || [ -z "$rate" ]; then
echo "-"
else
# Extract numeric value
local num=$(echo "$rate" | grep -oE '[0-9.]+' | head -1)
if [ -n "$num" ]; then
# Compare using bc or awk
local pct=$(echo "$num" | awk '{printf "%.0f", $1}')
if [ "$pct" -ge 50 ]; then
echo "🟒 **${rate}**"
elif [ "$pct" -ge 20 ]; then
echo "🟑 ${rate}"
else
echo "πŸ”΄ ${rate}"
fi
else
echo "${rate}"
fi
fi
}
# Start building the comment
echo '## πŸš€ CCache Statistics' > artifacts/comment.md
echo '' >> artifacts/comment.md
# Build the main table
# Check if LEAP artifacts exist
has_leap=false
for dir in artifacts/ccache-stats-leap-*/; do
if [ -d "$dir" ]; then
has_leap=true
break
fi
done
if [ "$has_leap" = true ]; then
echo "| Configuration | 🐧 RHEL | 🦊 openEuler | 🦎 LEAP |" >> artifacts/comment.md
echo "|:--------------|:-------:|:------------:|:-------:|" >> artifacts/comment.md
else
echo "| Configuration | 🐧 RHEL | 🦊 openEuler |" >> artifacts/comment.md
echo "|:--------------|:-------:|:------------:|" >> artifacts/comment.md
fi
# x86_64/gnu15
rhel_rate=$(get_value "artifacts/ccache-stats-rhel-x86_64-gnu15/data.txt" "hit_rate")
euler_rate=$(get_value "artifacts/ccache-stats-openeuler-x86_64/data.txt" "hit_rate")
leap_rate=$(get_value "artifacts/ccache-stats-leap-x86_64-gnu15/data.txt" "hit_rate")
if [ "$has_leap" = true ]; then
echo "| πŸ–₯️ x86_64 / gnu15 | $(format_hit_rate "$rhel_rate") | $(format_hit_rate "$euler_rate") | $(format_hit_rate "$leap_rate") |" >> artifacts/comment.md
else
echo "| πŸ–₯️ x86_64 / gnu15 | $(format_hit_rate "$rhel_rate") | $(format_hit_rate "$euler_rate") |" >> artifacts/comment.md
fi
# x86_64/intel
rhel_intel_rate=$(get_value "artifacts/ccache-stats-rhel-x86_64-intel/data.txt" "hit_rate")
if [ "$has_leap" = true ]; then
echo "| πŸ–₯️ x86_64 / intel | $(format_hit_rate "$rhel_intel_rate") | - | - |" >> artifacts/comment.md
else
echo "| πŸ–₯️ x86_64 / intel | $(format_hit_rate "$rhel_intel_rate") | - |" >> artifacts/comment.md
fi
# aarch64/gnu15
rhel_arm_rate=$(get_value "artifacts/ccache-stats-rhel-aarch64-gnu15/data.txt" "hit_rate")
euler_arm_rate=$(get_value "artifacts/ccache-stats-openeuler-aarch64/data.txt" "hit_rate")
leap_arm_rate=$(get_value "artifacts/ccache-stats-leap-aarch64-gnu15/data.txt" "hit_rate")
if [ "$has_leap" = true ]; then
echo "| πŸ’ͺ aarch64 / gnu15 | $(format_hit_rate "$rhel_arm_rate") | $(format_hit_rate "$euler_arm_rate") | $(format_hit_rate "$leap_arm_rate") |" >> artifacts/comment.md
else
echo "| πŸ’ͺ aarch64 / gnu15 | $(format_hit_rate "$rhel_arm_rate") | $(format_hit_rate "$euler_arm_rate") |" >> artifacts/comment.md
fi
echo "" >> artifacts/comment.md
# Add collapsible raw output section
echo "<details>" >> artifacts/comment.md
echo "<summary>πŸ“Š Detailed Statistics</summary>" >> artifacts/comment.md
echo "" >> artifacts/comment.md
for dir in artifacts/ccache-stats-*/; do
if [ -d "$dir" ]; then
job_name=$(get_value "${dir}data.txt" "job_name")
if [ -f "${dir}raw.txt" ]; then
echo "### ${job_name}" >> artifacts/comment.md
echo '```' >> artifacts/comment.md
cat "${dir}raw.txt" >> artifacts/comment.md
echo '```' >> artifacts/comment.md
echo "" >> artifacts/comment.md
fi
fi
done
echo "</details>" >> artifacts/comment.md
echo "" >> artifacts/comment.md
# Add metadata footer
echo "---" >> artifacts/comment.md
echo "*πŸ€– Generated from workflow run [\`${{ github.event.workflow_run.id }}\`](${{ github.event.workflow_run.html_url }})*" >> artifacts/comment.md
- name: Get PR number
if: steps.artifact_check.outputs.artifacts_found == 'true'
id: pr
run: |
event_file=$(find artifacts/event-file -type f | head -1)
if [ -n "$event_file" ]; then
pr_number=$(jq -r '.pull_request.number // .number // empty' "$event_file" 2>/dev/null || echo "")
if [ -n "$pr_number" ]; then
echo "number=$pr_number" >> $GITHUB_OUTPUT
fi
fi
- name: Post comment to PR
if: steps.artifact_check.outputs.artifacts_found == 'true' && steps.pr.outputs.number != ''
uses: actions/github-script@v9
with:
script: |
const fs = require('fs');
const commentBody = fs.readFileSync('artifacts/comment.md', 'utf8');
const prNumber = ${{ steps.pr.outputs.number }};
console.log(`Posting comment to PR #${prNumber}`);
const { owner, repo } = context.repo;
// Look for existing ccache stats comment
const comments = await github.rest.issues.listComments({
owner,
repo,
issue_number: prNumber,
});
const existingComment = comments.data.find(comment =>
comment.body.includes('## πŸš€ CCache Statistics') &&
comment.user.type === 'Bot'
);
if (existingComment) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existingComment.id,
body: commentBody
});
console.log(`Updated existing comment ${existingComment.id}`);
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number: prNumber,
body: commentBody
});
console.log('Created new comment');
}