From 08d9598c2661dcd76f07cc65b03c128d131bb68f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Wed, 18 Dec 2024 15:51:07 +0100 Subject: [PATCH] fix(action): better logging Be more clear about why it failed --- action.yml | 18 +++++++++++++----- comment_on_pr.sh | 28 ++++++++++++++++++---------- create-report.sh | 29 ++++++++++++++++++++--------- utils.sh | 11 +++++++++++ 4 files changed, 62 insertions(+), 24 deletions(-) create mode 100644 utils.sh diff --git a/action.yml b/action.yml index 6ca1dca..7fba491 100644 --- a/action.yml +++ b/action.yml @@ -66,6 +66,8 @@ runs: HEAD_BRANCH: ${{ inputs.base-branch }} JOB_NAME: ${{ inputs.job-name }} run: | + . "${GITHUB_ACTION_PATH}/utils.sh" + # USAGE: base_report_url base_report_url() { curl -X 'GET' \ @@ -77,27 +79,33 @@ runs: --arg head_branch "$HEAD_BRANCH" \ '[.workflow_runs[] | select(.name == $name and .head_branch == $head_branch)] | first | .url' } - # USAGE: has_report + # USAGE: has_report has_report() { http_code=$(curl -X 'GET' -o /dev/null --silent -Iw '%{http_code}' \ -H "Authorization: token $GITHUB_TOKEN" \ - "$1/artifacts/$ARTIFACT_NAME") + "$1") + log "Got code $http_code for $url" test "$http_code" = '200' } # If we have a previous report compare against it if [ "$JOB_NAME" ] && [ "$HEAD_BRANCH" ]; then url=$(base_report_url) - echo "Found previous run at: $url" - if has_report "$url"; then + log "Found previous run at: $url" + report_url="$url/artifacts/$ARTIFACT_NAME" + if has_report "$report_url"; then + log 'Found previous report, downloading...' curl -X 'GET' \ -H "Authorization: token $GITHUB_TOKEN" \ "$url/artifacts/$ARTIFACT_NAME" | gunzip > old-report.json - echo 'Found previous report' + log "Reporting on sizes and comparing to sizes in $HEAD_BRANCH" "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json old-report.json exit 0 + else + log "Failed to find previous report, expected at: $report_url" fi fi + log 'Reporting on sizes' "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json diff --git a/comment_on_pr.sh b/comment_on_pr.sh index 8f99989..fee22a9 100755 --- a/comment_on_pr.sh +++ b/comment_on_pr.sh @@ -2,6 +2,11 @@ set -eu +util_path="${GITHUB_ACTION_PATH-.}/utils.sh" + +# shellcheck source=utils.sh +. "${util_path}" + # USAGE: json_to_md_rows [JSON_FILE] # # JSON_FILE can be piped from stdin @@ -96,11 +101,11 @@ if [ "${CI-false}" != 'true' ]; then exit 0 fi -echo 'Determine head_ref' +log 'Determine head_ref' # For push & tag events it'll bet GITHUB_REF_NAME, for pull_request events it'll be GITHUB_HEAD_REF head_ref=${GITHUB_REF_NAME-$GITHUB_HEAD_REF} -echo "Get PR number for $head_ref" +log "Get PR number for $head_ref" prs=$(curl -X 'GET' \ "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls?state=open&sort=recentupdate" \ -H "Authorization: token $GITHUB_TOKEN" \ @@ -111,22 +116,25 @@ pr_number=$(echo "$prs" | # Protect against running before a PR is made or if it is triggered on the main branch if [ -z "$pr_number" ]; then - echo "No PR created for this commit" + log "No PR created for this commit" exit 0 fi -echo "Retrieved index: $pr_number" >&2 -echo "Expected PR URL: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pulls/$pr_number" >&2 +log "Retrieved index: $pr_number" +log "Expected PR URL: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pulls/$pr_number" -echo 'Generating comment body' >&2 +log 'Generating comment body' comment=$(markdown_from_report "$@") -echo 'Posting comment:' >&2 -echo "$comment" >&2 +group 'Comment Data:' +log "$comment" +endgroup -echo 'Request data:' >&2 data=$(echo '{}' | jq --arg comment "$comment" '.body=$comment') -echo "$data" >&2 +group 'Request data:' +log "$data" +endgroup + curl -o - -X 'POST' \ "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number/comments" \ -H 'accept: application/json' \ diff --git a/create-report.sh b/create-report.sh index d45e73c..b896f5d 100755 --- a/create-report.sh +++ b/create-report.sh @@ -2,41 +2,52 @@ set -eu -echo 'Retrieving Flake information' >&2 +util_path="${GITHUB_ACTION_PATH-.}/utils.sh" + +# shellcheck source=utils.sh +. "${util_path}" + +log 'Retrieving Flake information' flake_info=$(nix flake show --json 2>/dev/null) packages=$( jq --raw-output '.packages."x86_64-linux" | select(. != null) | keys[]' <<-EOF $flake_info EOF ) -echo "Packages:" >&2 -echo "$packages" >&2 +group 'Packages:' +log "$packages" +endgroup configurations=$( jq --raw-output '.nixosConfigurations | select(. != null) | keys[]' <<-EOF $flake_info EOF ) -echo "NixOS Configurations:" >&2 -echo "$configurations" >&2 +group 'NixOS Configurations:' +log "$configurations" +endgroup pkgs_json() { + group 'Building packages' for package in $packages; do - echo "Building $package" >&2 + log "Building $package" path=$(nix build --print-out-paths ".#$package" 2>/dev/null) - echo "Calculating size of $package" >&2 + log "Calculating size of $package" nix path-info --closure-size --json "$path" 2>/dev/null | jq --compact-output --arg pkg "$package" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}' done + endgroup } configs_json() { + group 'Building NixOS configurations' for config in $configurations; do - echo "Building $config" >&2 + log "Building $config" path=$(nix build --print-out-paths ".#nixosConfigurations.$config.config.system.build.toplevel" 2>/dev/null) - echo "Calculating size of $config" >&2 + log "Calculating size of $config" nix path-info --closure-size --json "$path" 2>/dev/null | jq --compact-output --arg pkg "$config" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}' done + endgroup } pkgs=$(pkgs_json | jq --slurp '.') diff --git a/utils.sh b/utils.sh new file mode 100644 index 0000000..25fe287 --- /dev/null +++ b/utils.sh @@ -0,0 +1,11 @@ +log() { + echo "$@" >&2 +} + +group() { + log "::group::$1" +} + +endgroup() { + log '::endgroup::' +}