|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to dynamically generate WooCommerce version matrix for L-1 policy |
| 4 | +# This script fetches the latest WC version and calculates the L-1 version |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# Function to get the latest WooCommerce version from WordPress.org API |
| 9 | +get_latest_wc_version() { |
| 10 | + curl -s https://api.wordpress.org/plugins/info/1.0/woocommerce.json | jq -r '.version' |
| 11 | +} |
| 12 | + |
| 13 | +# Function to get the latest stable version for a specific major version |
| 14 | +get_latest_stable_for_major() { |
| 15 | + local major_version=$1 |
| 16 | + curl -s https://api.wordpress.org/plugins/info/1.0/woocommerce.json | \ |
| 17 | + jq -r --arg major "$major_version" '.versions | with_entries(select(.key | startswith($major + ".") and (contains("-") | not))) | keys | sort_by( . | split(".") | map(tonumber) ) | last' |
| 18 | +} |
| 19 | + |
| 20 | +# Function to get the L-1 version (previous major version's latest stable) |
| 21 | +get_l1_version() { |
| 22 | + local latest_version=$1 |
| 23 | + local major_version=$(echo "$latest_version" | cut -d. -f1) |
| 24 | + local l1_major=$((major_version - 1)) |
| 25 | + get_latest_stable_for_major "$l1_major" |
| 26 | +} |
| 27 | + |
| 28 | +# Function to get specific major versions' latest stable |
| 29 | +get_major_versions_latest() { |
| 30 | + local latest_version=$1 |
| 31 | + local major_version=$(echo "$latest_version" | cut -d. -f1) |
| 32 | + local versions=() |
| 33 | + |
| 34 | + # Dynamically calculate L-1 major version |
| 35 | + local l1_major=$((major_version - 1)) |
| 36 | + |
| 37 | + # Only get L-1 version (previous major) and current major |
| 38 | + # Skip intermediate major versions as they don't align with L-1 policy |
| 39 | + for ((i=l1_major; i<=major_version; i++)); do |
| 40 | + latest_stable=$(get_latest_stable_for_major "$i") |
| 41 | + if [[ -n "$latest_stable" && "$latest_stable" != "null" ]]; then |
| 42 | + versions+=("$latest_stable") |
| 43 | + fi |
| 44 | + done |
| 45 | + |
| 46 | + echo "${versions[@]}" |
| 47 | +} |
| 48 | + |
| 49 | +# Function to get the latest RC version from WordPress.org API |
| 50 | +get_latest_rc_version() { |
| 51 | + curl -s https://api.wordpress.org/plugins/info/1.0/woocommerce.json | \ |
| 52 | + jq -r '.versions | with_entries(select(.key|match("rc";"i"))) | keys | sort_by( . | split("-")[0] | split(".") | map(tonumber) ) | last' |
| 53 | +} |
| 54 | + |
| 55 | +# Function to get the latest beta version from WordPress.org API |
| 56 | +get_latest_beta_version() { |
| 57 | + local latest_version=$1 |
| 58 | + local major_version=$(echo "$latest_version" | cut -d. -f1) |
| 59 | + curl -s https://api.wordpress.org/plugins/info/1.0/woocommerce.json | \ |
| 60 | + jq -r --arg major "$major_version" '.versions | with_entries(select(.key | startswith($major + ".") and contains("beta"))) | keys | sort_by( . | split("-")[0] | split(".") | map(tonumber) ) | last' |
| 61 | +} |
| 62 | + |
| 63 | +# Get the latest WooCommerce version |
| 64 | +echo "Fetching latest WooCommerce version..." >&2 |
| 65 | +LATEST_WC_VERSION=$(get_latest_wc_version) |
| 66 | +echo "Latest WC version: $LATEST_WC_VERSION" >&2 |
| 67 | + |
| 68 | +# Get the L-1 version |
| 69 | +L1_VERSION=$(get_l1_version "$LATEST_WC_VERSION") |
| 70 | +echo "L-1 version: $L1_VERSION" >&2 |
| 71 | + |
| 72 | +# Get major versions latest stable |
| 73 | +MAJOR_VERSIONS=($(get_major_versions_latest "$LATEST_WC_VERSION")) |
| 74 | +echo "Major versions latest stable: ${MAJOR_VERSIONS[*]}" >&2 |
| 75 | + |
| 76 | +# Get latest RC and beta versions |
| 77 | +echo "Fetching latest RC and beta versions..." >&2 |
| 78 | +LATEST_RC_VERSION=$(get_latest_rc_version) |
| 79 | +LATEST_BETA_VERSION=$(get_latest_beta_version "$LATEST_WC_VERSION") |
| 80 | +echo "Latest RC version: $LATEST_RC_VERSION" >&2 |
| 81 | +echo "Latest beta version: $LATEST_BETA_VERSION" >&2 |
| 82 | + |
| 83 | +# Build the version array |
| 84 | +VERSIONS=("7.7.0") # Keep for business reasons (significant TPV) |
| 85 | + |
| 86 | +# Add major versions latest stable (excluding current major since we'll use 'latest') |
| 87 | +for version in "${MAJOR_VERSIONS[@]}"; do |
| 88 | + # Skip the current major version since we'll use 'latest' instead |
| 89 | + if [[ "$version" != "$LATEST_WC_VERSION" ]]; then |
| 90 | + VERSIONS+=("$version") |
| 91 | + fi |
| 92 | +done |
| 93 | + |
| 94 | +# Add latest, beta, rc (with actual versions) |
| 95 | +VERSIONS+=("latest") |
| 96 | +if [[ -n "$LATEST_BETA_VERSION" && "$LATEST_BETA_VERSION" != "null" ]]; then |
| 97 | + VERSIONS+=("$LATEST_BETA_VERSION") |
| 98 | + echo "Including beta version: $LATEST_BETA_VERSION" >&2 |
| 99 | +else |
| 100 | + echo "No beta version available, skipping beta tests" >&2 |
| 101 | +fi |
| 102 | + |
| 103 | +# Decide whether to include RC: only include if RC base version (without suffix) is strictly greater than the latest stable. |
| 104 | +INCLUDED_RC_VERSION="" |
| 105 | +if [[ -n "$LATEST_RC_VERSION" && "$LATEST_RC_VERSION" != "null" ]]; then |
| 106 | + RC_BASE="${LATEST_RC_VERSION%%-*}" |
| 107 | + # Compare RC_BASE vs LATEST_WC_VERSION using sort -V |
| 108 | + HIGHEST=$(printf '%s\n%s\n' "$RC_BASE" "$LATEST_WC_VERSION" | sort -V | tail -n1) |
| 109 | + if [[ "$HIGHEST" == "$RC_BASE" && "$RC_BASE" != "$LATEST_WC_VERSION" ]]; then |
| 110 | + INCLUDED_RC_VERSION="$LATEST_RC_VERSION" |
| 111 | + VERSIONS+=("$LATEST_RC_VERSION") |
| 112 | + echo "Including RC version: $LATEST_RC_VERSION (base $RC_BASE > latest $LATEST_WC_VERSION)" >&2 |
| 113 | + else |
| 114 | + echo "Skipping RC version $LATEST_RC_VERSION because stable $LATEST_WC_VERSION is already released for this line." >&2 |
| 115 | + fi |
| 116 | +else |
| 117 | + echo "No RC version available, skipping rc tests" >&2 |
| 118 | +fi |
| 119 | + |
| 120 | +# Validate versions before output |
| 121 | +if [[ -z "$L1_VERSION" || "$L1_VERSION" == "null" ]]; then |
| 122 | + echo "Error: Could not extract L-1 version" >&2 |
| 123 | + exit 1 |
| 124 | +fi |
| 125 | + |
| 126 | +if [[ ! "$L1_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 127 | + echo "Error: Invalid L-1 version: $L1_VERSION" >&2 |
| 128 | + exit 1 |
| 129 | +fi |
| 130 | + |
| 131 | +# RC is optional; do not fail if not present or skipped |
| 132 | + |
| 133 | +# Only validate beta if it's available |
| 134 | +if [[ -n "$LATEST_BETA_VERSION" && "$LATEST_BETA_VERSION" != "null" ]]; then |
| 135 | + if [[ ! "$LATEST_BETA_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+ ]]; then |
| 136 | + echo "Error: Invalid beta version: $LATEST_BETA_VERSION" >&2 |
| 137 | + exit 1 |
| 138 | + fi |
| 139 | +fi |
| 140 | + |
| 141 | +# Convert to JSON array and output only the JSON (no extra whitespace or newlines) |
| 142 | +# Output a single JSON object with both versions and metadata |
| 143 | +RESULT=$(jq -n \ |
| 144 | + --argjson versions "$(printf '%s\n' "${VERSIONS[@]}" | jq -R . | jq -s .)" \ |
| 145 | + --arg l1_version "$L1_VERSION" \ |
| 146 | + --arg rc_version "${INCLUDED_RC_VERSION}" \ |
| 147 | + --arg beta_version "${LATEST_BETA_VERSION}" \ |
| 148 | + '{ |
| 149 | + versions: $versions, |
| 150 | + metadata: { |
| 151 | + l1_version: $l1_version, |
| 152 | + rc_version: (if ($rc_version // "") == "" or ($rc_version == "null") then null else $rc_version end), |
| 153 | + beta_version: (if ($beta_version // "") == "" or ($beta_version == "null") then null else $beta_version end) |
| 154 | + } |
| 155 | + }') |
| 156 | + |
| 157 | +echo "$RESULT" |
0 commit comments