Skip to content
45 changes: 23 additions & 22 deletions dev_tools/ci/size-labeler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ PR_NUMBER, GITHUB_REPOSITORY, GITHUB_TOKEN. The script is intended
for automated execution from GitHub Actions workflow."

declare -ar LABELS=(
"Size: XS"
"size: XS"
"size: S"
"size: M"
"size: L"
Expand Down Expand Up @@ -110,30 +110,31 @@ function api_call() {

function compute_changes() {
local -r pr="$1"
local page=1
local total_changes=0
while true; do
local response
response="$(api_call "pulls/${pr}/files?per_page=100&page=${page}")"

if [[ "$(jq_stdin '. | length' <<<"${response}")" -eq 0 ]]; then
break
fi
Comment on lines +108 to +113
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to replace all the pagination logic with gh api --paginate and let it do the next-page calls for us. We can also let the gh sum up change counts in a jq expression, for example,

gh api --paginate /repos/quantumlib/qsim/pulls/965/files \
   --jq="[0, .[].changes] | add"

(the leading 0 in the array is for getting an empty array to sum to 0 rather than null)

Extending the expression with some filter for an IGNORED regex pattern would be straightforward, but we should leave it for later when (if ever) it becomes necessary.

Copy link
Copy Markdown
Collaborator Author

@mhucka mhucka Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a clever and nicely simple solution, but note that the current code does not use gh. This change would introduce a dependency on gh. If we do that, it would be better to rewrite the script to remove the api_call() function and replace it with direct uses of gh everywhere. But I'm not sure it's a good idea to depend on gh for this script.

In the latest push, I simplified the compute_changes() function by taking advantage of the add trick above, and also removed IGNORED. Let me know what you think.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gh is included in the ubuntu-slim runner so there is nothing extra to do to use it on GHA.

AFAICT the pagination should work as is. That said we should eventually transition to the gh here and in Cirq to save ourselves trouble with low-level API calls.


local response
local change_info
local -r keys_filter='with_entries(select([.key] | inside(["changes", "filename"])))'
response="$(api_call "pulls/${pr}/files")"
change_info="$(jq_stdin "map(${keys_filter})" <<<"${response}")"

local files total_changes
readarray -t files < <(jq_stdin -c '.[]' <<<"${change_info}")
total_changes=0
for file in "${files[@]}"; do
local name changes
name="$(jq_stdin -r '.filename' <<<"${file}")"
for pattern in "${IGNORED[@]}"; do
if [[ "$name" =~ ${pattern} ]]; then
info "File $name ignored"
continue 2
fi
done
changes="$(jq_stdin -r '.changes' <<<"${file}")"
info "File $name +-$changes"
total_changes="$((total_changes + changes))"
while IFS= read -r name && IFS= read -r changes; do
for pattern in "${IGNORED[@]}"; do
# shellcheck disable=SC2053 # Need leave the pattern unquoted.
if [[ "${name}" == ${pattern} ]]; then
info "File ${name} ignored"
continue 2
fi
done
info "File ${name} +-${changes}"
total_changes="$((total_changes + changes))"
done < <(jq_stdin -r '.[] | .filename, .changes' <<<"${response}")
((page++))
done
echo "$total_changes"
echo "${total_changes}"
}

function get_size_label() {
Expand Down
Loading