fix(action): better logging #10

Merged
jalil merged 1 commit from better-logs into main 2024-12-18 16:08:41 +01:00
4 changed files with 62 additions and 24 deletions

View file

@ -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 <RUN_URL>
# USAGE: has_report <REPORT_URL>
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

View file

@ -2,6 +2,11 @@
set -eu
util_path="${GITHUB_ACTION_PATH-.}/utils.sh"
# shellcheck source=utils.sh
. "${util_path}"
# USAGE: json_to_md_rows <FIELD> [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' \

View file

@ -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 '.')

11
utils.sh Normal file
View file

@ -0,0 +1,11 @@
log() {
echo "$@" >&2
}
group() {
log "::group::$1"
}
endgroup() {
log '::endgroup::'
}