From 07e91526f1b012c1f04a34d2a016749fe759529d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Thu, 10 Jul 2025 20:26:19 +0200 Subject: [PATCH 1/2] refactor: move scripts to scripts folder This is a bit tidier. --- .gitignore | 1 + action.yml | 12 +-- create-report.sh | 86 ------------------- comment_on_pr.sh => scripts/comment_on_pr.sh | 4 +- scripts/create-report.sh | 81 +++++++++++++++++ .../retrieve-old-report.sh | 2 +- utils.sh => scripts/utils.sh | 4 +- 7 files changed, 93 insertions(+), 97 deletions(-) create mode 100644 .gitignore delete mode 100755 create-report.sh rename comment_on_pr.sh => scripts/comment_on_pr.sh (98%) create mode 100755 scripts/create-report.sh rename retrieve-old-report.sh => scripts/retrieve-old-report.sh (98%) rename utils.sh => scripts/utils.sh (79%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fcfc4a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +result* diff --git a/action.yml b/action.yml index 46431b7..c2cf28e 100644 --- a/action.yml +++ b/action.yml @@ -71,7 +71,7 @@ runs: id: pr-number if: inputs.comment-on-pr == 'true' run: | - . "$GITHUB_ACTION_PATH/utils.sh" + . "$GITHUB_ACTION_PATH/scripts/utils.sh" log 'Determine head_ref' # For push & tag events it'll bet GITHUB_REF_NAME, for pull_request events it'll be GITHUB_HEAD_REF @@ -121,7 +121,7 @@ runs: BASE_BRANCH: ${{ inputs.base-branch }} JOB_NAME: ${{ inputs.job-name }} run: | - . "$GITHUB_ACTION_PATH/utils.sh" + . "$GITHUB_ACTION_PATH/scripts/utils.sh" # Input validation if [ "$DO_COMPARISON" = 'true' ] && [ -z "$JOB_NAME" ]; then @@ -130,17 +130,17 @@ runs: fi # Create Size Report - "$GITHUB_ACTION_PATH/create-report.sh" > report.json + "$GITHUB_ACTION_PATH/scripts/create-report.sh" > report.json # Nothing else to do if [ "$COMMENT" != 'true' ]; then exit 0; fi # Try to do a comparison report if [ "$DO_COMPARISON" = 'true' ]; then - if "$GITHUB_ACTION_PATH/retrieve-old-report.sh" && [ -f old-report.json ]; then + if "$GITHUB_ACTION_PATH/scripts/retrieve-old-report.sh" && [ -f old-report.json ]; then log "Reporting on sizes and comparing to sizes in $HEAD_BRANCH" - "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json old-report.json + "$GITHUB_ACTION_PATH/scripts/comment_on_pr.sh" report.json old-report.json exit 0 else error 'Failed to do comparison, fallback to posting the report without them' @@ -149,7 +149,7 @@ runs: # Just report values log 'Reporting on sizes' - "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json + "$GITHUB_ACTION_PATH/scripts/comment_on_pr.sh" report.json - name: Upload Artifact uses: https://git.salame.cl/actions/upload-artifact@v4 if: inputs.generate-artifact == 'true' diff --git a/create-report.sh b/create-report.sh deleted file mode 100755 index a463eb7..0000000 --- a/create-report.sh +++ /dev/null @@ -1,86 +0,0 @@ -#!/usr/bin/env bash - -set -eu - -util_path="${GITHUB_ACTION_PATH:-.}/utils.sh" - -# shellcheck source=utils.sh -. "${util_path}" - -group 'Retrieving Flake information' -flake_info=$(nix flake show --json) -endgroup - -group 'Show Packages' -packages=$( - jq --raw-output '.packages."x86_64-linux" | select(. != null) | keys[]' <<-EOF - $flake_info - EOF -) -log "$packages" -endgroup - -group 'Show Home Manager Configurations' -hmConfigs=$( - jq --raw-output '.homeConfigurations | select(. != null) | keys[]' <<-EOF - $flake_info - EOF -) -log "$hmConfigs" -endgroup - -group 'Show NixOS Configurations' -nixosConfigs=$( - jq --raw-output '.nixosConfigurations | select(. != null) | keys[]' <<-EOF - $flake_info - EOF -) -log "$nixosConfigs" -endgroup - -pkgs_json() { - group 'Building packages' - trap endgroup RETURN - for package in $packages; do - log "Building $package" - path=$(nix build --print-out-paths ".#$package") - log "Calculating size of $package" - nix path-info --closure-size --json "$path" | - jq --compact-output --arg pkg "$package" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}' - done - endgroup -} - -hm_configs_json() { - group 'Building Home Manager configurations' - trap endgroup RETURN - for config in $hmConfigs; do - log "Building $config" - path=$(nix build --print-out-paths ".#homeConfigurations.$config.activationPackages") - log "Calculating size of $config" - nix path-info --closure-size --json "$path" | - jq --compact-output --arg pkg "$config" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}' - done -} - -nixos_configs_json() { - group 'Building NixOS configurations' - trap endgroup RETURN - for config in $nixosConfigs; do - log "Building $config" - path=$(nix build --print-out-paths ".#nixosConfigurations.$config.config.system.build.toplevel") - log "Calculating size of $config" - nix path-info --closure-size --json "$path" | - jq --compact-output --arg pkg "$config" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}' - done -} - -pkgs=$(pkgs_json | jq --slurp '.') -hmConfigs=$(hm_configs_json | jq --slurp '.') -nixosConfigs=$(nixos_configs_json | jq --slurp '.') - -echo "{}" | jq \ - --argjson pkgs "$pkgs" \ - --argjson nixosConfigs "$nixosConfigs" \ - --argjson hmConfigs "$hmConfigs" \ - '{"packages": $pkgs, "nixosConfigurations": $nixosConfigs, "homeConfigurations": $hmConfigs}' diff --git a/comment_on_pr.sh b/scripts/comment_on_pr.sh similarity index 98% rename from comment_on_pr.sh rename to scripts/comment_on_pr.sh index 9122a22..b927400 100755 --- a/comment_on_pr.sh +++ b/scripts/comment_on_pr.sh @@ -2,9 +2,9 @@ set -eu -util_path="${GITHUB_ACTION_PATH:-.}/utils.sh" +util_path="${GITHUB_ACTION_PATH:-.}/scripts/utils.sh" -# shellcheck source=utils.sh +# shellcheck source=scripts/utils.sh . "${util_path}" # USAGE: json_to_md_rows [JSON_FILE] diff --git a/scripts/create-report.sh b/scripts/create-report.sh new file mode 100755 index 0000000..26fd732 --- /dev/null +++ b/scripts/create-report.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash + +set -eu + +util_path="${GITHUB_ACTION_PATH:-.}/scripts/utils.sh" + +# shellcheck source=scripts/utils.sh +. "${util_path}" + +group 'Retrieving Flake information' +flake_info=$(nix flake show --json --quiet --quiet) +endgroup + +system=$(nix eval --impure --json --expr 'builtins.currentSystem') + +group 'Show Packages' +packages=$(echo "$flake_info" | jq --raw-output --argjson system "$system" 'getpath(["packages", $system]) | select(. != null) | keys[]') +[ -z "$packages" ] || log "$packages" +endgroup + +group 'Show Home Manager Configurations' +hmConfigs=$(echo "$flake_info" | jq --raw-output '.homeConfigurations | select(. != null) | keys[]') +[ -z "$hmConfigs" ] || log "$hmConfigs" +endgroup + +group 'Show NixOS Configurations' +nixosConfigs=$(echo "$flake_info" | jq --raw-output '.nixosConfigurations | select(. != null) | keys[]') +[ -z "$nixosConfigs" ] || log "$nixosConfigs" +endgroup + +closure_size() { + name=$1 + path=$2 + log "Calculating size of $name at $path" + path_info=$(nix path-info --closure-size --json "$path") + echo "$path_info" | + jq --compact-output \ + --arg pkg "$name" \ + --arg path "$path" \ + '.[] | {"name": $pkg, "path": $path, "size": .closureSize, "narSize": .narSize, "raw": .}' +} + +pkgs_json() { + for package in $packages; do + log "Building $package" + path=$(nix build --print-out-paths ".#$package") + closure_size "$package" "$path" + done +} + +hm_configs_json() { + for config in $hmConfigs; do + log "Building $config" + path=$(nix build --print-out-paths ".#homeConfigurations.$config.activationPackages") + closure_size "$config" "$path" + done +} + +nixos_configs_json() { + for config in $nixosConfigs; do + log "Building $config" + path=$(nix build --print-out-paths ".#nixosConfigurations.$config.config.system.build.toplevel") + closure_size "$config" "$path" + done +} + +group 'Building packages' +pkgs=$(pkgs_json | jq --slurp .) +endgroup +group 'Building Home Manager configurations' +hmConfigs=$(hm_configs_json | jq --slurp .) +endgroup +group 'Building NixOS configurations' +nixosConfigs=$(nixos_configs_json | jq --slurp .) +endgroup + +echo "{}" | jq \ + --argjson pkgs "$pkgs" \ + --argjson hmConfigs "$hmConfigs" \ + --argjson nixosConfigs "$nixosConfigs" \ + '{"packages": $pkgs, "nixosConfigurations": $nixosConfigs, "homeConfigurations": $hmConfigs}' diff --git a/retrieve-old-report.sh b/scripts/retrieve-old-report.sh similarity index 98% rename from retrieve-old-report.sh rename to scripts/retrieve-old-report.sh index 14a48db..d1ab0f2 100755 --- a/retrieve-old-report.sh +++ b/scripts/retrieve-old-report.sh @@ -1,6 +1,6 @@ #!/bin/sh -. "${GITHUB_ACTION_PATH}/utils.sh" +. "${GITHUB_ACTION_PATH}/scripts/utils.sh" repo_info() { curl -X GET \ diff --git a/utils.sh b/scripts/utils.sh similarity index 79% rename from utils.sh rename to scripts/utils.sh index e61c2a3..74c31a3 100755 --- a/utils.sh +++ b/scripts/utils.sh @@ -13,9 +13,9 @@ error() { } group() { - log "::group::$1" + echo "::group::$1" } endgroup() { - log '::endgroup::' + echo '::endgroup::' } From 9364b5d35273788b61c056b2286ac43386abb3fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Thu, 10 Jul 2025 20:26:19 +0200 Subject: [PATCH 2/2] refactor: move scripts to scripts folder This is a bit tidier. --- .gitignore | 1 + action.yml | 12 +-- create-report.sh | 86 ------------------- comment_on_pr.sh => scripts/comment_on_pr.sh | 4 +- scripts/create-report.sh | 81 +++++++++++++++++ .../retrieve-old-report.sh | 4 +- utils.sh => scripts/utils.sh | 4 +- 7 files changed, 94 insertions(+), 98 deletions(-) create mode 100644 .gitignore delete mode 100755 create-report.sh rename comment_on_pr.sh => scripts/comment_on_pr.sh (98%) create mode 100755 scripts/create-report.sh rename retrieve-old-report.sh => scripts/retrieve-old-report.sh (95%) rename utils.sh => scripts/utils.sh (79%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fcfc4a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +result* diff --git a/action.yml b/action.yml index 46431b7..8efa9a5 100644 --- a/action.yml +++ b/action.yml @@ -71,7 +71,7 @@ runs: id: pr-number if: inputs.comment-on-pr == 'true' run: | - . "$GITHUB_ACTION_PATH/utils.sh" + . "$GITHUB_ACTION_PATH/scripts/utils.sh" log 'Determine head_ref' # For push & tag events it'll bet GITHUB_REF_NAME, for pull_request events it'll be GITHUB_HEAD_REF @@ -121,7 +121,7 @@ runs: BASE_BRANCH: ${{ inputs.base-branch }} JOB_NAME: ${{ inputs.job-name }} run: | - . "$GITHUB_ACTION_PATH/utils.sh" + . "$GITHUB_ACTION_PATH/scripts/utils.sh" # Input validation if [ "$DO_COMPARISON" = 'true' ] && [ -z "$JOB_NAME" ]; then @@ -130,17 +130,17 @@ runs: fi # Create Size Report - "$GITHUB_ACTION_PATH/create-report.sh" > report.json + "$GITHUB_ACTION_PATH/scripts/create-report.sh" report.json # Nothing else to do if [ "$COMMENT" != 'true' ]; then exit 0; fi # Try to do a comparison report if [ "$DO_COMPARISON" = 'true' ]; then - if "$GITHUB_ACTION_PATH/retrieve-old-report.sh" && [ -f old-report.json ]; then + if "$GITHUB_ACTION_PATH/scripts/retrieve-old-report.sh" && [ -f old-report.json ]; then log "Reporting on sizes and comparing to sizes in $HEAD_BRANCH" - "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json old-report.json + "$GITHUB_ACTION_PATH/scripts/comment_on_pr.sh" report.json old-report.json exit 0 else error 'Failed to do comparison, fallback to posting the report without them' @@ -149,7 +149,7 @@ runs: # Just report values log 'Reporting on sizes' - "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json + "$GITHUB_ACTION_PATH/scripts/comment_on_pr.sh" report.json - name: Upload Artifact uses: https://git.salame.cl/actions/upload-artifact@v4 if: inputs.generate-artifact == 'true' diff --git a/create-report.sh b/create-report.sh deleted file mode 100755 index a463eb7..0000000 --- a/create-report.sh +++ /dev/null @@ -1,86 +0,0 @@ -#!/usr/bin/env bash - -set -eu - -util_path="${GITHUB_ACTION_PATH:-.}/utils.sh" - -# shellcheck source=utils.sh -. "${util_path}" - -group 'Retrieving Flake information' -flake_info=$(nix flake show --json) -endgroup - -group 'Show Packages' -packages=$( - jq --raw-output '.packages."x86_64-linux" | select(. != null) | keys[]' <<-EOF - $flake_info - EOF -) -log "$packages" -endgroup - -group 'Show Home Manager Configurations' -hmConfigs=$( - jq --raw-output '.homeConfigurations | select(. != null) | keys[]' <<-EOF - $flake_info - EOF -) -log "$hmConfigs" -endgroup - -group 'Show NixOS Configurations' -nixosConfigs=$( - jq --raw-output '.nixosConfigurations | select(. != null) | keys[]' <<-EOF - $flake_info - EOF -) -log "$nixosConfigs" -endgroup - -pkgs_json() { - group 'Building packages' - trap endgroup RETURN - for package in $packages; do - log "Building $package" - path=$(nix build --print-out-paths ".#$package") - log "Calculating size of $package" - nix path-info --closure-size --json "$path" | - jq --compact-output --arg pkg "$package" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}' - done - endgroup -} - -hm_configs_json() { - group 'Building Home Manager configurations' - trap endgroup RETURN - for config in $hmConfigs; do - log "Building $config" - path=$(nix build --print-out-paths ".#homeConfigurations.$config.activationPackages") - log "Calculating size of $config" - nix path-info --closure-size --json "$path" | - jq --compact-output --arg pkg "$config" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}' - done -} - -nixos_configs_json() { - group 'Building NixOS configurations' - trap endgroup RETURN - for config in $nixosConfigs; do - log "Building $config" - path=$(nix build --print-out-paths ".#nixosConfigurations.$config.config.system.build.toplevel") - log "Calculating size of $config" - nix path-info --closure-size --json "$path" | - jq --compact-output --arg pkg "$config" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}' - done -} - -pkgs=$(pkgs_json | jq --slurp '.') -hmConfigs=$(hm_configs_json | jq --slurp '.') -nixosConfigs=$(nixos_configs_json | jq --slurp '.') - -echo "{}" | jq \ - --argjson pkgs "$pkgs" \ - --argjson nixosConfigs "$nixosConfigs" \ - --argjson hmConfigs "$hmConfigs" \ - '{"packages": $pkgs, "nixosConfigurations": $nixosConfigs, "homeConfigurations": $hmConfigs}' diff --git a/comment_on_pr.sh b/scripts/comment_on_pr.sh similarity index 98% rename from comment_on_pr.sh rename to scripts/comment_on_pr.sh index 9122a22..b927400 100755 --- a/comment_on_pr.sh +++ b/scripts/comment_on_pr.sh @@ -2,9 +2,9 @@ set -eu -util_path="${GITHUB_ACTION_PATH:-.}/utils.sh" +util_path="${GITHUB_ACTION_PATH:-.}/scripts/utils.sh" -# shellcheck source=utils.sh +# shellcheck source=scripts/utils.sh . "${util_path}" # USAGE: json_to_md_rows [JSON_FILE] diff --git a/scripts/create-report.sh b/scripts/create-report.sh new file mode 100755 index 0000000..cfe4209 --- /dev/null +++ b/scripts/create-report.sh @@ -0,0 +1,81 @@ +#!/bin/sh + +set -eu + +util_path="${GITHUB_ACTION_PATH:-.}/scripts/utils.sh" + +# shellcheck source=scripts/utils.sh +. "${util_path}" + +group 'Retrieving Flake information' +flake_info=$(nix flake show --json --quiet --quiet) +endgroup + +system=$(nix eval --impure --json --expr 'builtins.currentSystem') + +group 'Show Packages' +packages=$(echo "$flake_info" | jq --raw-output --argjson system "$system" 'getpath(["packages", $system]) | select(. != null) | keys[]') +[ -z "$packages" ] || log "$packages" +endgroup + +group 'Show Home Manager Configurations' +hmConfigs=$(echo "$flake_info" | jq --raw-output '.homeConfigurations | select(. != null) | keys[]') +[ -z "$hmConfigs" ] || log "$hmConfigs" +endgroup + +group 'Show NixOS Configurations' +nixosConfigs=$(echo "$flake_info" | jq --raw-output '.nixosConfigurations | select(. != null) | keys[]') +[ -z "$nixosConfigs" ] || log "$nixosConfigs" +endgroup + +closure_size() { + name=$1 + path=$2 + log "Calculating size of $name at $path" + path_info=$(nix path-info --closure-size --json "$path") + echo "$path_info" | + jq --compact-output \ + --arg pkg "$name" \ + --arg path "$path" \ + '.[] | {"name": $pkg, "path": $path, "size": .closureSize, "narSize": .narSize, "raw": .}' +} + +pkgs_json() { + for package in $packages; do + log "Building $package" + path=$(nix build --print-out-paths ".#$package") + closure_size "$package" "$path" + done +} + +hm_configs_json() { + for config in $hmConfigs; do + log "Building $config" + path=$(nix build --print-out-paths ".#homeConfigurations.$config.activationPackages") + closure_size "$config" "$path" + done +} + +nixos_configs_json() { + for config in $nixosConfigs; do + log "Building $config" + path=$(nix build --print-out-paths ".#nixosConfigurations.$config.config.system.build.toplevel") + closure_size "$config" "$path" + done +} + +group 'Building packages' +pkgs=$(pkgs_json | jq --slurp .) +endgroup +group 'Building Home Manager configurations' +hmConfigs=$(hm_configs_json | jq --slurp .) +endgroup +group 'Building NixOS configurations' +nixosConfigs=$(nixos_configs_json | jq --slurp .) +endgroup + +echo "{}" | jq \ + --argjson pkgs "$pkgs" \ + --argjson hmConfigs "$hmConfigs" \ + --argjson nixosConfigs "$nixosConfigs" \ + '{"packages": $pkgs, "nixosConfigurations": $nixosConfigs, "homeConfigurations": $hmConfigs}' >"${1:-/dev/stdout}" diff --git a/retrieve-old-report.sh b/scripts/retrieve-old-report.sh similarity index 95% rename from retrieve-old-report.sh rename to scripts/retrieve-old-report.sh index 14a48db..9c50afe 100755 --- a/retrieve-old-report.sh +++ b/scripts/retrieve-old-report.sh @@ -1,6 +1,6 @@ #!/bin/sh -. "${GITHUB_ACTION_PATH}/utils.sh" +. "${GITHUB_ACTION_PATH}/scripts/utils.sh" repo_info() { curl -X GET \ @@ -74,6 +74,6 @@ curl -X 'GET' \ tar -zvx --strip-components=1 -C "$old" endgroup -(cd "$old" && "$GITHUB_ACTION_PATH/create-report.sh") >old-report.json +(cd "$old" && "$GITHUB_ACTION_PATH/create-report.sh" old-report.json) exit 0 diff --git a/utils.sh b/scripts/utils.sh similarity index 79% rename from utils.sh rename to scripts/utils.sh index e61c2a3..74c31a3 100755 --- a/utils.sh +++ b/scripts/utils.sh @@ -13,9 +13,9 @@ error() { } group() { - log "::group::$1" + echo "::group::$1" } endgroup() { - log '::endgroup::' + echo '::endgroup::' }