2024-12-17 23:15:39 +01:00
#!/bin/sh
set -eu
2024-12-19 23:12:40 +01:00
util_path = " ${ GITHUB_ACTION_PATH :- . } /utils.sh "
2024-12-18 15:51:07 +01:00
# shellcheck source=utils.sh
. " ${ util_path } "
2024-12-17 23:15:39 +01:00
# USAGE: json_to_md_rows <FIELD> [JSON_FILE]
#
# JSON_FILE can be piped from stdin
json_to_md_rows( ) {
jq --raw-output \
" . $1 [] " ' | "| `\(.name)` | \(.size) | \(.narSize) |"' " $2 " |
2024-12-22 13:55:44 +01:00
numfmt --suffix= B --to= iec-i --field= 4,6
2024-12-17 23:15:39 +01:00
}
# USAGE: json_to_md_rows <FIELD> [JSON_FILE]
#
# JSON_FILE can be piped from stdin
json_to_md_rows_and_change( ) {
jq --raw-output \
" . $1 [] " ' | "| `\(.name)` | \(.size) | \(.sizeChange) | \(.narSize) | \(.narSizeChange) |"' |
2024-12-22 13:55:44 +01:00
numfmt --suffix= B --to= iec-i --field= 4,6,8,10
2024-12-17 23:15:39 +01:00
}
# USAGE: has_elements <FIELD> <JSON_FILE>
has_elements( ) {
if [ " ${ 2 +set } " = 'set' ] ; then
[ " $( jq " . $1 != [] " " $2 " ) " = 'true' ]
else
[ " $( jq " . $1 != [] " ) " = 'true' ]
fi
}
# USAGE: markdown_from_report <REPORT> [BASE_REPORT]
#
# If BASE_REPORT is provided, a comparison will be made
markdown_from_report( ) {
cat <<-"EOF"
# Flake output sizes
2024-12-19 23:52:27 +01:00
**Definitions:**
2024-12-17 23:15:39 +01:00
2024-12-19 23:52:27 +01:00
- ` Name` : the name of the package/configuration.
- ` Size` : the closure size ( size on disk/NAR size + all transitive dependencies) .
- ` NAR Size` : the size of the build output ( package without the dependencies) .
EOF
2024-12-17 23:15:39 +01:00
if [ " ${ 2 +set } " = "set" ] ; then
cat <<-"EOF"
2024-12-19 23:52:27 +01:00
- ` [ NAR] Size Change` : the amount changed compared to the main branch.
2024-12-17 23:15:39 +01:00
EOF
2024-12-19 23:52:27 +01:00
fi
cat <<-"EOF"
**Tips on reading this data:**
- For NixOS configurations you generally care only about the ` Size` ( closure size/size on disk) .
- Reduce the ` Size` by disabling unneeded services/default packages.
- For Packages you care about both the ` Size` and the ` NAR Size` .
- Reduce the ` NAR Size` by reducing the size of the build outputs, e.g. don' t copy unnecessary data to the $out dir, optimize binaries for size, etc.
- Reduce the ` Size` by reducing the dependencies ( e.g. ` buildInputs` ) .
- Don' t worry too much about size, some dependencies are deduplicated, e.g. ` glibc` adds ~40MiB to the ` Size` , but is generally shared by ~every binary on the system, so, chances are, you are already including it from somewhere else and statically linking with e.g. ` musl` is not gonna improve things.
EOF
if [ " ${ 2 +set } " = "set" ] ; then
2024-12-19 23:12:40 +01:00
compare = $( jq --slurp --from-file " ${ GITHUB_ACTION_PATH :- . } /compare.jq " " $1 " " $2 " )
2024-12-17 23:15:39 +01:00
if echo " $compare " | has_elements 'nixosConfigurations' ; then
cat <<-"EOF"
# NixOS Configurations
| Name | Size | Size Change | NAR Size | NAR Size Change |
| ------| -----:| ------------:| ---------:| ----------------:|
EOF
echo " $compare " | json_to_md_rows_and_change "nixosConfigurations"
echo
fi
if echo " $compare " | has_elements 'packages' ; then
cat <<-"EOF"
# Packages
| Name | Size | Size Change | NAR Size | NAR Size Change |
| ------| -----:| ------------:| ---------:| ----------------:|
EOF
echo " $compare " | json_to_md_rows_and_change "packages"
echo
fi
else
if has_elements 'nixosConfigurations' " $1 " ; then
cat <<-"EOF"
# NixOS Configurations
| Name | Size | NAR Size |
| ------| -----:| ---------:|
EOF
json_to_md_rows "nixosConfigurations" " $1 "
echo
fi
if has_elements 'packages' " $1 " ; then
cat <<-"EOF"
# Packages
| Name | Size | NAR Size |
| ------| -----:| ---------:|
EOF
json_to_md_rows "packages" " $1 "
echo
fi
fi
}
# Test outside CI
2024-12-19 23:12:40 +01:00
if [ " ${ CI :- false } " != 'true' ] ; then
2024-12-17 23:15:39 +01:00
markdown_from_report " $@ "
exit 0
fi
2024-12-18 15:51:07 +01:00
log 'Determine head_ref'
2024-12-17 23:15:39 +01:00
# For push & tag events it'll bet GITHUB_REF_NAME, for pull_request events it'll be GITHUB_HEAD_REF
2024-12-19 23:12:40 +01:00
head_ref = ${ GITHUB_REF_NAME :- $GITHUB_HEAD_REF }
2024-12-17 23:15:39 +01:00
2024-12-18 15:51:07 +01:00
log " Get PR number for $head_ref "
2024-12-17 23:15:39 +01:00
prs = $( curl -X 'GET' \
" $GITHUB_API_URL /repos/ $GITHUB_REPOSITORY /pulls?state=open&sort=recentupdate " \
-H " Authorization: token $GITHUB_TOKEN " \
-H 'accept: application/json' )
pr_number = $( echo " $prs " |
jq --arg head_ref " $head_ref " '.[] | select(.head.ref == $head_ref) | .number' )
# Protect against running before a PR is made or if it is triggered on the main branch
if [ -z " $pr_number " ] ; then
2024-12-19 21:56:16 +01:00
warn "No PR created for this commit"
2024-12-17 23:15:39 +01:00
exit 0
fi
2024-12-18 15:51:07 +01:00
log " Retrieved index: $pr_number "
log " Expected PR URL: $GITHUB_SERVER_URL / $GITHUB_REPOSITORY /pulls/ $pr_number "
2024-12-17 23:15:39 +01:00
2024-12-18 15:51:07 +01:00
log 'Generating comment body'
2024-12-17 23:15:39 +01:00
comment = $( markdown_from_report " $@ " )
2024-12-18 16:18:13 +01:00
group 'Comment Data'
2024-12-18 15:51:07 +01:00
log " $comment "
endgroup
2024-12-17 23:15:39 +01:00
data = $( echo '{}' | jq --arg comment " $comment " '.body=$comment' )
2024-12-18 16:18:13 +01:00
group 'Request data'
2024-12-18 15:51:07 +01:00
log " $data "
endgroup
2024-12-17 23:15:39 +01:00
curl -o - -X 'POST' \
" $GITHUB_API_URL /repos/ $GITHUB_REPOSITORY /issues/ $pr_number /comments " \
-H 'accept: application/json' \
-H " Authorization: token $GITHUB_TOKEN " \
-H 'Content-Type: application/json' \
-d " $data "