Repository navigation
fix: follow GHCR pagination when fetching tags, not just the first page #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cfad806
fix: follow GHCR pagination when fetching tags, not just the first page
stigdreyer 850340e
Merge branch 'main' into fix/ghcr-tag-pagination
mairas 08215f7
fix: skip images whose tag fetch fails partway
mairas 4200918
test: cover check-image-updates tag fetching
mairas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #!/usr/bin/env bash | ||
| # Tests for scripts/check-image-updates.sh against a fake registry. | ||
| set -euo pipefail | ||
|
|
||
| # shellcheck source=tests/lib/step.sh | ||
| source "$(dirname "$0")/lib/step.sh" | ||
| SCRIPT="$ROOT/scripts/check-image-updates.sh" | ||
|
|
||
| TAB=$'\t' | ||
| GHCR_TOKEN="https://ghcr.io/token?scope=repository:acme/app:pull${TAB}0${TAB}{\"token\":\"t\"}${TAB}" | ||
| GHCR_PAGE1="https://ghcr.io/v2/acme/app/tags/list?n=10000" | ||
| GHCR_PAGE2="https://ghcr.io/v2/acme/app/tags/list?last=2023.2.0&n=10000" | ||
| GHCR_NEXT="</v2/acme/app/tags/list?last=2023.2.0&n=10000>; rel=\"next\"" | ||
| HUB_PAGE1="https://hub.docker.com/v2/repositories/acme/tool/tags?page_size=100&ordering=last_updated" | ||
|
|
||
| # check_image <image:tag> <routes...> — runs the script on a compose file | ||
| # holding that image; leaves the resulting image line in IMAGE_LINE. | ||
| check_image() { | ||
| in_temp_dir | ||
| printf 'services:\n app:\n image: %s\n' "$1" > docker-compose.yml | ||
| shift | ||
| CURL_ROUTES=$(mktemp) | ||
| printf '%s\n' "$@" > "$CURL_ROUTES" | ||
| export CURL_ROUTES | ||
| COMPOSE_PATTERN=docker-compose.yml run_step "$SCRIPT" || { | ||
| cat "$STDOUT_FILE" | ||
| FAILURES=$((FAILURES + 1)) | ||
| } | ||
| IMAGE_LINE=$(grep 'image:' docker-compose.yml | sed 's/ *image: //') | ||
| } | ||
|
|
||
| test_ghcr_follows_link_to_second_page() { | ||
| check_image ghcr.io/acme/app:2023.1.0 "$GHCR_TOKEN" \ | ||
| "$GHCR_PAGE1${TAB}0${TAB}{\"tags\":[\"2023.1.0\",\"2023.2.0\"]}${TAB}$GHCR_NEXT" \ | ||
| "$GHCR_PAGE2${TAB}0${TAB}{\"tags\":[\"2026.1.0\"]}${TAB}" | ||
| check "tag from the second GHCR page wins" ghcr.io/acme/app:2026.1.0 "$IMAGE_LINE" | ||
| } | ||
|
|
||
| test_ghcr_single_page() { | ||
| check_image ghcr.io/acme/app:2023.1.0 "$GHCR_TOKEN" \ | ||
| "$GHCR_PAGE1${TAB}0${TAB}{\"tags\":[\"2023.1.0\",\"2023.2.0\"]}${TAB}" | ||
| check "single GHCR page without Link" ghcr.io/acme/app:2023.2.0 "$IMAGE_LINE" | ||
| } | ||
|
|
||
| test_ghcr_failed_page_skips_image() { | ||
| check_image ghcr.io/acme/app:2023.1.0 "$GHCR_TOKEN" \ | ||
| "$GHCR_PAGE1${TAB}0${TAB}{\"tags\":[\"2023.1.0\",\"2023.2.0\"]}${TAB}$GHCR_NEXT" \ | ||
| "$GHCR_PAGE2${TAB}22${TAB}${TAB}" | ||
| check "failed second page leaves the image alone" ghcr.io/acme/app:2023.1.0 "$IMAGE_LINE" | ||
| check "failed second page is reported" yes \ | ||
| "$(grep -q 'Failed to fetch tags' "$STDOUT_FILE" && echo yes || echo no)" | ||
| } | ||
|
|
||
| test_ghcr_failed_token_is_reported() { | ||
| check_image ghcr.io/acme/app:2023.1.0 | ||
| check "failed token request is reported" yes \ | ||
| "$(grep -q 'Failed to fetch tags' "$STDOUT_FILE" && echo yes || echo no)" | ||
| } | ||
|
|
||
| test_never_proposes_an_older_tag() { | ||
| check_image ghcr.io/acme/app:2026.1.0 "$GHCR_TOKEN" \ | ||
| "$GHCR_PAGE1${TAB}0${TAB}{\"tags\":[\"2023.1.0\",\"2023.2.0\"]}${TAB}" | ||
| check "older tags only leave the image alone" ghcr.io/acme/app:2026.1.0 "$IMAGE_LINE" | ||
| } | ||
|
|
||
| test_docker_hub_reads_newest_page_only() { | ||
| check_image acme/tool:1.0.0 \ | ||
| "$HUB_PAGE1${TAB}0${TAB}{\"results\":[{\"name\":\"1.2.0\"},{\"name\":\"1.0.0\"}],\"next\":\"$HUB_PAGE1&page=2\"}${TAB}" | ||
| check "Docker Hub newest tag from the first page" acme/tool:1.2.0 "$IMAGE_LINE" | ||
| } | ||
|
|
||
| FAKEBIN=$(mktemp -d) | ||
| ln -s "$ROOT/tests/lib/fake-curl" "$FAKEBIN/curl" | ||
| PATH="$FAKEBIN:$PATH" | ||
| for t in $(declare -F | awk '{print $3}' | grep '^test_'); do | ||
| "$t" | ||
| done | ||
|
|
||
| [ "$FAILURES" -eq 0 ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #!/usr/bin/env bash | ||
| # Fake curl for tests. Answers URLs listed in $CURL_ROUTES, one per line as | ||
| # <url> TAB <exit code> TAB <body> TAB <Link header value or empty>. | ||
| # Writes the response headers to the -D file; an unlisted URL fails like a 404. | ||
| headers="" | ||
| url="" | ||
| while [ $# -gt 0 ]; do | ||
| case $1 in | ||
| -D) headers=$2; shift 2 ;; | ||
| -H) shift 2 ;; | ||
| -*) shift ;; | ||
| *) url=$1; shift ;; | ||
| esac | ||
| done | ||
|
|
||
| while IFS=$'\t' read -r route code body link; do | ||
| [ "$route" = "$url" ] || continue | ||
| if [ -n "$headers" ]; then | ||
| { | ||
| printf 'HTTP/2 200\r\n' | ||
| [ -n "$link" ] && printf 'link: %s\r\n' "$link" | ||
| printf '\r\n' | ||
| } > "$headers" | ||
| fi | ||
| [ "$code" = 0 ] || exit "$code" | ||
| printf '%s\n' "$body" | ||
| exit 0 | ||
| done < "$CURL_ROUTES" | ||
| exit 22 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.