From a98834701132f510fc6e6759d30e6a0f18b304b3 Mon Sep 17 00:00:00 2001 From: Sorin Marta Date: Mon, 9 Nov 2026 13:40:35 +0200 Subject: [PATCH] Add reusable release workflow for add-ons Add-ons call it to prepare a release PR and publish a GitHub release with the ZIP built by the core Gruntfile. --- .github/scripts/addon-changelog.py | 93 ++++++++ .github/workflows/addon-release.yml | 349 ++++++++++++++++++++++++++++ Gruntfile.js | 15 +- 3 files changed, 455 insertions(+), 2 deletions(-) create mode 100644 .github/scripts/addon-changelog.py create mode 100644 .github/workflows/addon-release.yml diff --git a/.github/scripts/addon-changelog.py b/.github/scripts/addon-changelog.py new file mode 100644 index 000000000..c720d932d --- /dev/null +++ b/.github/scripts/addon-changelog.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 +"""Changelog helpers for the add-on release workflow. + +Add-on READMEs do not share one changelog format, so new entries copy the +heading and bullet style of the newest existing entry. + +Usage: + addon-changelog.py ensure Insert a placeholder entry if has none. + addon-changelog.py notes Print the bullets of the entry. +""" + +import re +import sys +from pathlib import Path + +MARKER = re.compile(r"^==\s*Changelog\s*==[ \t]*\r?\n?", re.MULTILINE) +VERSION = re.compile(r"\d+(?:\.\d+)+[0-9A-Za-z.-]*") + + +def read(readme): + # Bytes keep the original line endings; several add-on READMEs use CRLF. + return readme.read_bytes().decode() + + +def changelog_lines(text): + match = MARKER.search(text) + if not match: + raise SystemExit("README is missing the == Changelog == section") + return match.end(), text[match.end():].splitlines(keepends=True) + + +def is_heading(line, version=None): + stripped = line.strip().strip("=").strip() + if stripped.lower().startswith("version "): + stripped = stripped[8:].strip() + found = VERSION.fullmatch(stripped) + return bool(found) and (version is None or stripped == version) + + +def ensure(readme, version): + text = read(readme) + start, lines = changelog_lines(text) + + if any(is_heading(line, version) for line in lines): + print(f"Changelog entry for {version} already present.") + return + + newline = "\r\n" if "\r\n" in text else "\n" + heading = f"= {version} =" + bullet = "* " + offset = start + position = None + for line in lines: + if position is None and is_heading(line): + heading = VERSION.sub(version, line.strip(), count=1) + position = offset + if line.startswith(("- ", "* ")): + bullet = line[:2] + break + offset += len(line) + + entry = f"{heading}{newline}{bullet}Enhancement: Various improvements and fixes.{newline}{newline}" + if position is None: + # No entries yet: put the first one on the line after the marker. + position = start + readme.write_bytes((text[:position] + entry + text[position:]).encode()) + print(f"Inserted a fallback changelog entry for {version}.") + + +def notes(readme, version): + _, lines = changelog_lines(read(readme)) + out = [] + capturing = False + for line in lines: + if is_heading(line): + if capturing: + break + capturing = is_heading(line, version) + continue + if capturing: + out.append(line.rstrip()) + print("\n".join(out).strip()) + + +def main(): + if len(sys.argv) != 4 or sys.argv[1] not in ("ensure", "notes"): + raise SystemExit(__doc__) + command, readme, version = sys.argv[1], Path(sys.argv[2]), sys.argv[3] + (ensure if command == "ensure" else notes)(readme, version) + + +if __name__ == "__main__": + main() diff --git a/.github/workflows/addon-release.yml b/.github/workflows/addon-release.yml new file mode 100644 index 000000000..a135a7871 --- /dev/null +++ b/.github/workflows/addon-release.yml @@ -0,0 +1,349 @@ +name: Add-on Release + +# Reusable release workflow for the premium add-ons (business-directory-*). +# +# Add-ons have no build tooling of their own: the core Gruntfile registers each +# one by its sibling path (../business-directory-). This workflow +# recreates that layout by cloning core next to the add-on checkout, then runs +# the module's Grunt tasks from core. +# +# prepare: changelog, version bump, minify, i18n, release PR (+ ZIP artifact for testing). +# publish: on release PR merge, build the ZIP and attach it to a GitHub release. + +on: + workflow_call: + inputs: + version: + description: 'Version (e.g. 5.4.4). Read from the release/ branch on merge.' + required: false + type: string + tested_up_to: + description: 'WP "Tested up to" version (blank = keep current)' + required: false + type: string + phase: + description: 'prepare or publish. Always publish on release PR merge.' + required: false + type: string + core_ref: + description: 'Branch of business-directory-plugin to take the Gruntfile from' + required: false + type: string + default: master + secrets: + GH_TOKEN: + required: true + ANTHROPIC_API_KEY: + required: true + +jobs: + release: + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + id-token: write + + steps: + - name: Resolve release + id: release + env: + EVENT: ${{ github.event_name }} + MERGED: ${{ github.event.pull_request.merged }} + HEAD_REF: ${{ github.event.pull_request.head.ref }} + INPUT_VERSION: ${{ inputs.version }} + INPUT_PHASE: ${{ inputs.phase }} + REPO_NAME: ${{ github.event.repository.name }} + run: | + set -euo pipefail + if [ "$EVENT" = "pull_request" ]; then + if [ "$MERGED" != "true" ] || [[ "$HEAD_REF" != release/* ]]; then + echo "Not a merged release PR." + exit 1 + fi + VERSION="${HEAD_REF#release/}" + PHASE=publish + else + VERSION="$INPUT_VERSION" + PHASE="$INPUT_PHASE" + fi + + if ! [[ "$VERSION" =~ ^[0-9]+(\.[0-9]+)+([A-Za-z0-9.-]+)?$ ]]; then + echo "Invalid version: '$VERSION'" + exit 1 + fi + if [ "$PHASE" != "prepare" ] && [ "$PHASE" != "publish" ]; then + echo "Invalid phase: '$PHASE'" + exit 1 + fi + if [[ "$REPO_NAME" != business-directory-* ]]; then + echo "Expected a business-directory-* repository, got '$REPO_NAME'" + exit 1 + fi + + # Same id the core Gruntfile derives from the module path. + MODULE="${REPO_NAME#business-directory-}" + # Grunt treats any version containing "b" as a beta. + PRERELEASE=false + [[ "$VERSION" == *b* ]] && PRERELEASE=true + + { + echo "version=$VERSION" + echo "phase=$PHASE" + echo "module=$MODULE" + echo "prerelease=$PRERELEASE" + echo "core_dir=$RUNNER_TEMP/bd/business-directory-plugin" + echo "zip=$RUNNER_TEMP/bd/$REPO_NAME-$VERSION.zip" + } >> "$GITHUB_OUTPUT" + + - name: Checkout add-on + uses: actions/checkout@v4 + with: + ref: ${{ github.event.repository.default_branch }} + token: ${{ secrets.GH_TOKEN }} + fetch-depth: 0 + + - name: Check tag is free + env: + VERSION: ${{ steps.release.outputs.version }} + run: | + if git rev-parse -q --verify "refs/tags/v$VERSION" >/dev/null; then + echo "Tag v$VERSION already exists." + exit 1 + fi + + - name: Find README + id: readme + run: | + README="" + for f in README.txt README.TXT; do + [ -f "$f" ] && README="$f" && break + done + echo "path=$README" >> "$GITHUB_OUTPUT" + [ -n "$README" ] || echo "::warning::No README.txt found; changelog steps are skipped." + + - name: Place core next to the add-on + env: + CORE_REF: ${{ inputs.core_ref }} + CORE_DIR: ${{ steps.release.outputs.core_dir }} + REPO_NAME: ${{ github.event.repository.name }} + run: | + set -euo pipefail + git clone --depth 1 --branch "$CORE_REF" \ + https://github.com/Strategy11/business-directory-plugin.git "$CORE_DIR" + # The Gruntfile points at ../business-directory-. + ln -s "$GITHUB_WORKSPACE" "$(dirname "$CORE_DIR")/$REPO_NAME" + + - name: Install gettext + if: steps.release.outputs.phase == 'prepare' + run: sudo apt-get update && sudo apt-get install -y gettext + + - name: Setup pnpm + uses: pnpm/action-setup@v4 + with: + version: 10 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Install core dependencies + working-directory: ${{ steps.release.outputs.core_dir }} + run: pnpm install --frozen-lockfile + + # ---- prepare ---- + + - name: Create release branch + if: steps.release.outputs.phase == 'prepare' + run: git checkout -b "release/${{ steps.release.outputs.version }}" + + - name: Collect changelog context + if: steps.release.outputs.phase == 'prepare' && steps.readme.outputs.path != '' + id: changelog-context + env: + VERSION: ${{ steps.release.outputs.version }} + run: | + set -euo pipefail + CONTEXT="${GITHUB_WORKSPACE}/.changelog-context.txt" + LAST_TAG="$(git describe --tags --abbrev=0 2>/dev/null || true)" + { + echo "Release version: ${VERSION}" + echo + if [ -n "$LAST_TAG" ]; then + echo "Last tag: ${LAST_TAG}" + echo + echo "== Commits since ${LAST_TAG} ==" + git log --no-merges --pretty=format:'%s' "${LAST_TAG}..HEAD" + echo + echo + echo "== Changed files ==" + git diff --stat "${LAST_TAG}..HEAD" + echo + echo "== Name status ==" + git diff --name-status "${LAST_TAG}..HEAD" + else + echo "No tags found." + echo + echo "== Last 50 commits ==" + git log --no-merges --pretty=format:'%s' -50 + echo + fi + } > "$CONTEXT" + echo "path=${CONTEXT}" >> "$GITHUB_OUTPUT" + + - name: Generate changelog via Claude Code + if: steps.release.outputs.phase == 'prepare' && steps.readme.outputs.path != '' + continue-on-error: true + uses: anthropics/claude-code-action@v1 + with: + anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} + github_token: ${{ secrets.GH_TOKEN }} + prompt: | + You are generating a changelog entry for a WordPress plugin add-on release. + Version: ${{ steps.release.outputs.version }} + + Use ONLY these sources: + 1. ${{ steps.changelog-context.outputs.path }} (commits and changed-file summary) + 2. The existing changelog in ${{ steps.readme.outputs.path }} (the first 3-4 entries after `== Changelog ==`) + + Do NOT run a full `git diff` of file contents. The context file is enough. + If you need more detail, Read at most a few individual source files named in the context. + Do not read binaries, lockfiles, vendor, dist, or generated assets. + + Write a changelog entry and insert it into ${{ steps.readme.outputs.path }} directly above the newest existing entry + (or right after `== Changelog ==` if there are none). Do not remove or edit older entries. + + Changelog format rules: + - Copy the heading style of the newest existing entry exactly, only changing the version + (e.g. `= Version 5.4.3 =` becomes `= Version ${{ steps.release.outputs.version }} =`; `= 5.4.3 =` becomes `= ${{ steps.release.outputs.version }} =`). + If there is no existing entry, use `= ${{ steps.release.outputs.version }} =`. + - Copy the bullet character of the newest existing entry (`- ` or `* `). + - Each bullet starts with New: , Enhancement: , or Fix: followed by a concise user-facing description + - Group related changes into single entries + - Skip merge commits, CI/tooling changes, dependency updates, and non-user-facing changes + - Omit any security-related fixes entirely (auth bypasses, XSS, CSRF, privilege escalation, sanitization/escaping hardening for vulnerabilities, CVEs, etc.). Do not describe them and do not add vague placeholders like "Fix: Security improvements". If a change mixes security and non-security work, include only the non-security user-facing parts. + - Keep the file's existing line endings. + - End the entry with an empty line before the next entry + + Finish in as few turns as possible. Only modify ${{ steps.readme.outputs.path }}. + claude_args: | + --model claude-haiku-4-5 + --max-turns 10 + --allowedTools "Read,Edit,Write" + + - name: Ensure changelog entry exists + if: steps.release.outputs.phase == 'prepare' && steps.readme.outputs.path != '' + run: | + rm -f .changelog-context.txt + python3 -I "${{ steps.release.outputs.core_dir }}/.github/scripts/addon-changelog.py" \ + ensure "${{ steps.readme.outputs.path }}" "${{ steps.release.outputs.version }}" + + - name: Bump version, minify assets and update translations + if: steps.release.outputs.phase == 'prepare' + working-directory: ${{ steps.release.outputs.core_dir }} + env: + MODULE: ${{ steps.release.outputs.module }} + VERSION: ${{ steps.release.outputs.version }} + run: pnpm exec grunt "setversion:$MODULE:$VERSION" "minify:$MODULE" "i18n:$MODULE" + + - name: Update Tested up to + if: steps.release.outputs.phase == 'prepare' && inputs.tested_up_to != '' && steps.readme.outputs.path != '' + run: | + sed -i "s/^Tested up to: .*/Tested up to: ${{ inputs.tested_up_to }}/" "${{ steps.readme.outputs.path }}" + + # ---- both phases ---- + + - name: Verify plugin header version + env: + VERSION: ${{ steps.release.outputs.version }} + run: | + # Catches modules the core Gruntfile does not register, or whose main file it misses. + if ! grep -lE "^[ *]*Version:[[:space:]]*${VERSION//./\\.}[[:space:]]*$" ./*.php; then + echo "No root PHP file has 'Version: $VERSION'. Check this module's registerModule() entry in the core Gruntfile." + exit 1 + fi + + - name: Build ZIP + working-directory: ${{ steps.release.outputs.core_dir }} + env: + MODULE: ${{ steps.release.outputs.module }} + VERSION: ${{ steps.release.outputs.version }} + run: | + pnpm exec grunt "zip:$MODULE:$VERSION" + test -f "${{ steps.release.outputs.zip }}" + + # ---- prepare ---- + + - name: Upload ZIP for testing + if: steps.release.outputs.phase == 'prepare' + uses: actions/upload-artifact@v4 + with: + name: ${{ github.event.repository.name }}-${{ steps.release.outputs.version }} + path: ${{ steps.release.outputs.zip }} + + - name: Commit and push + if: steps.release.outputs.phase == 'prepare' + env: + GH_TOKEN: ${{ secrets.GH_TOKEN }} + VERSION: ${{ steps.release.outputs.version }} + run: | + git remote set-url origin "https://x-access-token:${GH_TOKEN}@github.com/${{ github.repository }}.git" + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git add -A + git commit -m "Prepare release ${VERSION}" + git push -u origin "release/${VERSION}" + + - name: Create pull request + if: steps.release.outputs.phase == 'prepare' + env: + GH_TOKEN: ${{ secrets.GH_TOKEN }} + VERSION: ${{ steps.release.outputs.version }} + BASE: ${{ github.event.repository.default_branch }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + run: | + gh pr create \ + --title "Release ${VERSION}" \ + --base "$BASE" \ + --body "$(cat < "$NOTES" + fi + ARGS=(--title "v${VERSION}" --target "$(git rev-parse HEAD)") + if [ -s "$NOTES" ]; then + ARGS+=(--notes-file "$NOTES") + else + ARGS+=(--generate-notes) + fi + [ "$PRERELEASE" = "true" ] && ARGS+=(--prerelease) + gh release create "v${VERSION}" "${{ steps.release.outputs.zip }}" "${ARGS[@]}" diff --git a/Gruntfile.js b/Gruntfile.js index 1116701dc..32ade1bc5 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -146,7 +146,7 @@ module.exports = function( grunt ) { grunt.config.set( 'replace.setversion-' + id, { src: [ - basedir + '/business-directory-' + id + '.php', + basedir + '/business-directory-*.php', basedir + '/includes/class-' + id + '.php', basedir + '/includes/class-wpbdp.php', basedir + '/classes/class-wpbdp-premium-module.php', @@ -243,6 +243,11 @@ module.exports = function( grunt ) { grunt.registerTask('default', []); grunt.registerTask('i18n', '', function(t) { + // Modules without a translations folder have no makepot target. + if ( 'undefined' === typeof grunt.config.get( 'makepot.' + t ) ) { + return; + } + grunt.task.run('makepot:' + t); grunt.task.run('potomo:' + t); }); @@ -277,6 +282,12 @@ module.exports = function( grunt ) { } }); + // Build the release ZIP only, without touching versions, assets or translations. + grunt.registerTask('zip', function(t, v) { + grunt.config.set('compress.version', v); + grunt.task.run('compress:' + t); + }); + grunt.registerTask('release', function(t, v) { // Release everything. if ( 'all' === t || 'undefined' === typeof t ) { @@ -327,7 +338,6 @@ module.exports = function( grunt ) { // Premium modules. grunt.wpbdp.registerModule({path: '../business-directory-2checkout', js: [], i18n: true}); - grunt.wpbdp.registerModule({path: '../business-directory-attachments', js: ['resources/*.js'], i18n: true}); grunt.wpbdp.registerModule({path: '../business-directory-categories', less: ['resources/*.css'], js: ['resources/*.js'], i18n: true}); grunt.wpbdp.registerModule({path: '../business-directory-claim-listings', less: ['resources/*.less'], js: ['resources/*.js'], i18n: true}); grunt.wpbdp.registerModule({path: '../business-directory-discount-codes', less: ['resources/*.less'], js: ['resources/*.js'], i18n: true}); @@ -336,6 +346,7 @@ module.exports = function( grunt ) { grunt.wpbdp.registerModule({path: '../business-directory-googlemaps', less: ['resources/*.css'], js: ['resources/*.js'], i18n: true}); grunt.wpbdp.registerModule({path: '../business-directory-payfast', i18n: true}); grunt.wpbdp.registerModule({path: '../business-directory-paypal', i18n: true}); + grunt.wpbdp.registerModule({path: '../business-directory-servicearea', js: ['*.js'], i18n: true}); grunt.wpbdp.registerModule({path: '../business-directory-ratings', less: ['resources/*.css'], js: ['resources/*.js'], i18n: true}); grunt.wpbdp.registerModule({path: '../business-directory-regions', less: ['resources/css/*.css'], js: ['resources/js/*.js'], i18n: true}); grunt.wpbdp.registerModule({path: '../business-directory-stripe', js: ['resources/*.js'], i18n: true});