@@ -135,7 +135,7 @@ commands:
135135 - not :
136136 equal : [ esp-idf, << parameters.toolchain >> ]
137137 - not :
138- equal : [ --one-random , << parameters.build-args >> ]
138+ equal : [ arm-iar , << parameters.toolchain >> ]
139139 steps :
140140 - run :
141141 name : Collect Metrics
@@ -147,6 +147,7 @@ commands:
147147 if [ -f "$f" ]; then
148148 BOARD_DIR=$(dirname "$f" | xargs basename)
149149 cp "$f" "/tmp/metrics/${BOARD_DIR}.json"
150+ cat /tmp/metrics/${BOARD_DIR}.json
150151 fi
151152 done
152153
@@ -228,7 +229,7 @@ jobs:
228229 python tools/get_deps.py
229230 pip install tools/linkermap/
230231 # Combine all metrics files
231- tree /tmp/metrics
232+ ls /tmp/metrics
232233 if ls /tmp/metrics/*.json 1> /dev/null 2>&1; then
233234 mkdir -p cmake-build
234235 for f in /tmp/metrics/*.json; do
@@ -242,13 +243,90 @@ jobs:
242243 exit 1
243244 fi
244245
245- - store_artifacts :
246- path : metrics.json
247- destination : metrics.json
246+ # Store metrics.json artifact on master branch
247+ - when :
248+ condition :
249+ equal : [ master, << pipeline.git.branch >> ]
250+ steps :
251+ - store_artifacts :
252+ path : metrics.json
253+ destination : metrics.json
254+
255+ # Compare with base master metrics on PR branches
256+ - when :
257+ condition :
258+ not :
259+ equal : [ master, << pipeline.git.branch >> ]
260+ steps :
261+ - run :
262+ name : Download Base Branch Metrics
263+ command : |
264+ # Download metrics.json artifact from the latest successful build on master branch
265+ mkdir -p base-metrics
266+ # Use CircleCI API to get the latest artifact
267+ curl -s -L "https://dl.circleci.com/api/v2/project/gh/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/latest/artifacts?branch=master&filter=successful" \
268+ -H "Circle-Token: ${CIRCLE_TOKEN:-}" | \
269+ jq -r '.items[] | select(.path == "metrics.json") | .url' | \
270+ head -1 | xargs -I {} curl -s -L -o base-metrics/metrics.json {} || true
271+
272+ - run :
273+ name : Compare with Base Branch
274+ command : |
275+ if [ -f base-metrics/metrics.json ]; then
276+ python tools/metrics.py compare -f tinyusb/src base-metrics/metrics.json metrics.json
277+ cat metrics_compare.md
278+ else
279+ echo "No base metrics found, skipping comparison"
280+ cp metrics.md metrics_compare.md
281+ fi
282+
283+ - run :
284+ name : Post Metrics Comment to PR
285+ command : |
286+ # Only post if this is a PR (CIRCLE_PULL_REQUEST is set)
287+ if [ -z "$CIRCLE_PULL_REQUEST" ]; then
288+ echo "Not a PR, skipping comment"
289+ exit 0
290+ fi
291+
292+ # Extract PR number from URL
293+ PR_NUMBER=$(echo "$CIRCLE_PULL_REQUEST" | grep -oE '[0-9]+$')
294+ if [ -z "$PR_NUMBER" ]; then
295+ echo "Could not extract PR number"
296+ exit 0
297+ fi
298+
299+ # Comment marker to identify our comments
300+ COMMENT_MARKER="<!-- code-metrics-comment -->"
301+ COMMENT_BODY="${COMMENT_MARKER}
302+ $(cat metrics_compare.md)"
303+
304+ # Find existing comment
305+ EXISTING_COMMENT_ID=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \
306+ "https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/issues/${PR_NUMBER}/comments" | \
307+ jq -r ".[] | select(.body | startswith(\"${COMMENT_MARKER}\")) | .id" | head -1)
308+
309+ if [ -n "$EXISTING_COMMENT_ID" ] && [ "$EXISTING_COMMENT_ID" != "null" ]; then
310+ # Update existing comment
311+ curl -s -X PATCH \
312+ -H "Authorization: token ${GITHUB_TOKEN}" \
313+ -H "Content-Type: application/json" \
314+ -d "$(jq -n --arg body "$COMMENT_BODY" '{body: $body}')" \
315+ "https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/issues/comments/${EXISTING_COMMENT_ID}"
316+ echo "Updated existing comment ${EXISTING_COMMENT_ID}"
317+ else
318+ # Create new comment
319+ curl -s -X POST \
320+ -H "Authorization: token ${GITHUB_TOKEN}" \
321+ -H "Content-Type: application/json" \
322+ -d "$(jq -n --arg body "$COMMENT_BODY" '{body: $body}')" \
323+ "https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/issues/${PR_NUMBER}/comments"
324+ echo "Created new comment"
325+ fi
248326
249- - store_artifacts :
250- path : metrics .md
251- destination : metrics .md
327+ - store_artifacts :
328+ path : metrics_compare .md
329+ destination : metrics_compare .md
252330
253331workflows :
254332 build :
0 commit comments