Files
nolimit-discounts/ci/publish-release.sh
T
kristianwindandClaude Opus 5 8d0fa8c9d2 Build the download address from the plugin header, not from the forge
The v1.4.0 release went out and the feed came back readable, with this in it:

  "browser_download_url":
    "http://57.129.89.117:25001/kw/woocommerce-email-discount-manager/..."

That field is filled from the forge's own configured root URL, which is not
necessarily the address the outside world reaches it on. A shop handed that
would be downloading plugin code over plain HTTP from a raw IP on a
non-standard port, bypassing the Cloudflare tunnel in front of gitea.nolimit.dk
and breaking the day that address changes.

parse_release() now rebuilds both the package and the release-page address from
the repository address in the plugin header, which is the one place that
already had to be right for any of this to work. tests/smoke.php pins it with
the exact response the live feed returned.

The release check had the same bug from the other end, and a worse version of
it: it verified the address the API reported rather than the one the plugin
builds, so it was a green check on a URL no shop ever uses. It now downloads
the package the plugin would download and opens the zip to confirm it is the
plugin and not an error page with a 200 on it.

And it only runs on the forge named in "Update URI". The other one is a mirror;
requiring a mirror to be anonymously readable failed the GitHub half of the
v1.4.0 release for a repository that is private on purpose.

ci/update-smoke.sh is the check none of the above can stand in for: install the
previous version into a throwaway shop and let WordPress find, download and
install the new one by itself. Every piece can be individually correct and the
update still not arrive.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-16 10:49:52 +02:00

179 lines
7.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Publish a release with the plugin zip attached, on whichever forge is running
# this, and then verify the result the way a shop will actually see it.
#
# Needs: TAG, GITHUB_REPOSITORY, GITHUB_API_URL, GITHUB_SERVER_URL, GH_TOKEN.
#
# The verification at the end is the part worth keeping. The update feed is
# read anonymously by every shop, and the two ways it breaks — a private
# repository and a release with no zip attached — both leave a release page
# that looks perfectly fine to whoever is logged in. Nobody would notice for
# months; what they would notice is that updates stopped arriving.
set -euo pipefail
cd "$(dirname "$0")/.."
. ci/lib.sh
ci_load_conf
: "${TAG:?TAG is not set}"
: "${GH_TOKEN:?GH_TOKEN is not set}"
: "${GITHUB_REPOSITORY:?}"
: "${GITHUB_API_URL:?}"
zip="build/$PLUGIN_SLUG.zip"
[ -f "$zip" ] || { echo "$zip does not exist — run ci/build-zip.sh first." >&2; exit 1; }
owner="${GITHUB_REPOSITORY%%/*}"
repo="${GITHUB_REPOSITORY#*/}"
# The tag message is the announcement, so it has to survive the checkout.
#
# actions/checkout fetches every tag correctly and then overwrites this one ref
# with the commit SHA — `fetch-depth: 0` does not save you. The annotated tag
# becomes a lightweight one pointing at the commit, and reading its message
# returns the COMMIT message instead: on a merge commit, "Merge pull request
# #12 from ...". Nothing fails. The release is simply published with the wrong
# text, and only the page shows it.
git fetch --force origin "refs/tags/$TAG:refs/tags/$TAG"
[ "$(git cat-file -t "$TAG")" = tag ] || {
echo "$TAG is not an annotated tag. Its message is what gets published, so create it with 'git tag -a'." >&2
exit 1
}
notes=$(git tag -l --format='%(contents)' "$TAG")
first_line=$(printf '%s\n' "$notes" | head -1)
case "$first_line" in
'Merge pull request'*|'Merge branch'*|'')
echo "The release notes read '$first_line'." >&2
echo "That is a commit message, not the tag message — the annotated tag did not survive the checkout." >&2
exit 1
;;
esac
ci_step "release notes"
printf '%s\n' "$notes" | sed 's/^/ /'
# The updater reads this line out of the release body, so wp-admin can show
# what this version was tested against rather than what the last one claimed.
readme=
for f in readme.txt README.txt; do [ -f "$f" ] && readme=$f && break; done
tested_wp=$([ -n "$readme" ] && sed -n -E 's/^Tested up to:[[:space:]]*([^[:space:]]+).*/\1/p' "$readme" | head -1)
tested_wc=$(ci_plugin_header 'WC tested up to')
body=$(printf '%s\n\n---\n\nTested up to: %s\nWC tested up to: %s\n' \
"$notes" "${tested_wp:-unknown}" "${tested_wc:-unknown}")
api() { # method path [data]
curl -fsS -X "$1" \
-H "Authorization: token $GH_TOKEN" \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
${3:+-d "$3"} \
"$2"
}
if [ "${GITHUB_SERVER_URL:-}" = 'https://github.com' ]; then
ci_step "publishing to GitHub"
gh release create "$TAG" "$zip" --title "$TAG" --notes "$body" 2>/dev/null \
|| gh release upload "$TAG" "$zip" --clobber
release_api="$GITHUB_API_URL/repos/$owner/$repo/releases/tags/$TAG"
else
ci_step "publishing to $GITHUB_SERVER_URL"
payload=$(printf '%s' "$body" | python3 -c '
import json, sys, os
print(json.dumps({
"tag_name": os.environ["TAG"],
"name": os.environ["TAG"],
"body": sys.stdin.read(),
"draft": False,
"prerelease": False,
}))')
# A re-run of a release must not fail on "already exists".
existing=$(curl -fsS -H "Authorization: token $GH_TOKEN" \
"$GITHUB_API_URL/repos/$owner/$repo/releases/tags/$TAG" 2>/dev/null || true)
id=$(printf '%s' "$existing" | sed -n -E 's/.*"id":[[:space:]]*([0-9]+).*/\1/p' | head -1)
if [ -z "$id" ]; then
created=$(api POST "$GITHUB_API_URL/repos/$owner/$repo/releases" "$payload")
id=$(printf '%s' "$created" | sed -n -E 's/.*"id":[[:space:]]*([0-9]+).*/\1/p' | head -1)
fi
[ -n "$id" ] || { echo "Could not create or find the release for $TAG." >&2; exit 1; }
echo " release id $id"
curl -fsS -X POST \
-H "Authorization: token $GH_TOKEN" \
-F "attachment=@$zip" \
"$GITHUB_API_URL/repos/$owner/$repo/releases/$id/assets?name=$PLUGIN_SLUG.zip" >/dev/null
echo " attached $PLUGIN_SLUG.zip"
release_api="$GITHUB_API_URL/repos/$owner/$repo/releases/latest"
fi
ci_step "the feed, read the way a shop reads it"
# Only the forge named in "Update URI" actually serves shops. This script runs
# on both, and the other one is a mirror — requiring a mirror to be anonymously
# readable would fail every release for a repository that is private on purpose.
# (It did, on the first v1.4.0 run: GitHub is private and is not the feed.)
update_uri=$(sed -n -E 's/^[[:space:]]*\*[[:space:]]*Update URI:[[:space:]]*(.*)/\1/p' "$PLUGIN_MAIN" | head -1 | tr -d '[:space:]')
feed_host=$(printf '%s' "$update_uri" | sed -E 's|^[a-z]+://([^/]+).*|\1|')
this_host=$(printf '%s' "${GITHUB_SERVER_URL:-}" | sed -E 's|^[a-z]+://([^/]+).*|\1|')
if [ -z "$update_uri" ]; then
echo "The plugin header has no Update URI, so there is no feed to check."
exit 0
fi
if [ "$feed_host" != "$this_host" ]; then
echo " $this_host is a mirror; shops read $feed_host. Nothing to verify here."
exit 0
fi
feed=$(curl -fsS --max-time 30 "$release_api" 2>/dev/null || true)
if [ -z "$feed" ]; then
echo
ci_fail "$release_api cannot be read without credentials, so no shop will ever be offered this update."
echo "Either the repository is still private, or the release was not created."
echo "For Gitea: Settings → make the repository public. The update feed is a public read by design;"
echo "that is the whole mechanism, and there is nothing secret in a GPL plugin zip anyway."
exit 1
fi
printf '%s' "$feed" | grep -q '"browser_download_url"' || {
echo
ci_fail "The release is readable, but it has no zip attached. WordPress would be offered an update it cannot download."
exit 1
}
# Checked at the address the PLUGIN builds, not the one the API reports.
#
# A forge fills browser_download_url from its own configured root URL, which is
# not necessarily the address the outside world reaches it on: gitea.nolimit.dk
# returns http://57.129.89.117:25001/... there, a plain-HTTP raw IP that
# bypasses the Cloudflare tunnel. Verifying that address would be verifying a
# URL no shop ever uses — a green check on the wrong thing, which is worse than
# no check.
package="$update_uri/releases/download/$TAG/$PLUGIN_SLUG.zip"
code=$(curl -s -o /tmp/wpci-feed.zip -w '%{http_code}' -L --max-time 120 "$package")
echo " $package -> HTTP $code"
[ "$code" = 200 ] || { ci_fail "The package URL the plugin will use is not anonymously downloadable."; exit 1; }
# And it has to be the plugin, not an error page with a 200 on it.
python3 -c '
import sys, zipfile
names = zipfile.ZipFile("/tmp/wpci-feed.zip").namelist()
main = sys.argv[1] + "/" + sys.argv[2]
if main not in names:
sys.exit("The downloaded zip does not contain " + main)
print(" it is the plugin: %d entries, %s present" % (len(names), main))
' "$PLUGIN_SLUG" "$PLUGIN_MAIN"
rm -f /tmp/wpci-feed.zip
echo
echo "Published $TAG. A shop running this plugin will be offered it within twelve hours,"
echo "or immediately on Dashboard → Updates → Check again."