Skip to content
44 changes: 13 additions & 31 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 All @@ -39,14 +39,6 @@ declare -A LIMITS=(
["${LABELS[4]}"]="$((2 ** 63 - 1))"
)

declare -ar IGNORED=(
"*_pb2.py"
"*_pb2.pyi"
"*_pb2_grpc.py"
".*.lock"
"*.bundle.js"
)

function info() {
echo >&2 "INFO: ${*}"
}
Expand Down Expand Up @@ -110,30 +102,20 @@ function api_call() {

function compute_changes() {
local -r pr="$1"
local page=1
local 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

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 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.

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.


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))"
changes=$((changes + $(jq_stdin '[0, .[].changes] | add' <<<"${response}")))
((page++))
done
echo "$total_changes"
echo "${changes}"
}

function get_size_label() {
Expand Down
Loading