diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0bbdc16..3772e16 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -3,7 +3,7 @@ on: pull_request: jobs: - shopware-version: + shopware-version-fallback: runs-on: ubuntu-latest steps: - name: Checkout code @@ -20,7 +20,68 @@ jobs: echo 'shopware-version should not be empty' exit 1 fi - - - - + shopware-version: + runs-on: ubuntu-latest + strategy: + matrix: + test-case: + - ref: this-branch-does-not-exist + base-ref: "6.7.3.99" + expected-version: "6.7.3.x" + - ref: this-branch-does-not-exist + base-ref: "6.6.99.99" + expected-version: "6.6.x" + - ref: trunk + base-ref: "6.7.99.99" + expected-version: "trunk" + - ref: does-not-exist + head-ref: trunk + base-ref: "6.7.99.99" + expected-version: "trunk" + # for non pr events, base-ref can be empty + - ref: 6.7.2.99 + base-ref: " " + expected-version: "6.7.2.x" + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Test shopware-version + id: version + uses: ./shopware-version + with: + ref: ${{ matrix.test-case.ref }} + base-ref: ${{ matrix.test-case.base-ref }} + head-ref: ${{ matrix.test-case.head-ref || matrix.test-case.ref }} + repo: shopware/shopware + fallback: 'not-found' + - name: Check shopware-version matches expected version + run: | + SHOPWARE_VERSION="${{ steps.version.outputs.shopware-version }}" + EXPECTED_VERSION="${{ matrix.test-case.expected-version }}" + echo "shopware-version: '${SHOPWARE_VERSION}'" + echo "expected-version: '${EXPECTED_VERSION}'" + if [[ "${SHOPWARE_VERSION}" != "${EXPECTED_VERSION}" ]]; then + echo "shopware-version should be '${EXPECTED_VERSION}' but got '${SHOPWARE_VERSION}'" + exit 1 + fi + - name: Test shopware-version with refs/heads/ + id: version-heads + uses: ./shopware-version + if: ${{ matrix.test-case.base-ref != ' ' }} + with: + ref: refs/heads/${{ matrix.test-case.ref }} + base-ref: refs/heads/${{ matrix.test-case.base-ref }} + head-ref: refs/heads/${{ matrix.test-case.head-ref || matrix.test-case.ref }} + repo: shopware/shopware + fallback: 'not-found' + - name: Check shopware-version matches expected version + if: ${{ matrix.test-case.base-ref != ' ' }} + run: | + SHOPWARE_VERSION="${{ steps.version-heads.outputs.shopware-version }}" + EXPECTED_VERSION="${{ matrix.test-case.expected-version }}" + echo "shopware-version: '${SHOPWARE_VERSION}'" + echo "expected-version: '${EXPECTED_VERSION}'" + if [[ "${SHOPWARE_VERSION}" != "${EXPECTED_VERSION}" ]]; then + echo "shopware-version should be '${EXPECTED_VERSION}' but got '${SHOPWARE_VERSION}'" + exit 1 + fi \ No newline at end of file diff --git a/setup-extension/action.yml b/setup-extension/action.yml index fb63e6b..713ca47 100644 --- a/setup-extension/action.yml +++ b/setup-extension/action.yml @@ -190,7 +190,7 @@ runs: if: inputs.dependencies env: DEPS: ${{ inputs.dependencies }} - REF: ${{ inputs.shopwareVersion}} + REF: ${{ inputs.shopwareVersion }} FALLBACK: ${{ inputs.shopwareVersionFallback }} run: | for dep in $(echo "$DEPS" | envsubst | jq -c '.[]'); do diff --git a/shopware-version/action.yml b/shopware-version/action.yml index 01d0d4b..9c59db1 100644 --- a/shopware-version/action.yml +++ b/shopware-version/action.yml @@ -14,6 +14,18 @@ inputs: description: The repo where to look for matching refs default: shopware/shopware required: false + ref: + description: The git reference to match (defaults to github.ref) + required: false + default: "" + base-ref: + description: The base reference for pull requests (defaults to github.base_ref) + required: false + default: "" + head-ref: + description: The head reference for pull requests (defaults to github.head_ref) + required: false + default: "" shopware-github-token: description: Token used for checking out the shopware repository required: false @@ -36,9 +48,9 @@ runs: id: get-version shell: bash env: - REF: "${{ github.ref }}" - BASE_REF: "${{ github.base_ref }}" - HEAD_REF: "${{ github.head_ref }}" + REF: "${{ inputs.ref || github.ref }}" + HEAD_REF: "${{ inputs.head-ref || github.head_ref }}" + BASE_REF: "${{ inputs.base-ref || github.base_ref }}" REPO: "${{ inputs.repo || 'shopware/shopware' }}" CURRENT_REPO: "${{ github.repository }}" FALLBACK: "${{ inputs.fallback || 'trunk' }}" diff --git a/shopware-version/shopware-version.bash b/shopware-version/shopware-version.bash index 978bd04..f933035 100755 --- a/shopware-version/shopware-version.bash +++ b/shopware-version/shopware-version.bash @@ -1,47 +1,117 @@ #!/usr/bin/bash +# Global environment variables required: +# REF - The current git reference (e.g., refs/heads/6.4.1.0) +# BASE_REF - The base reference for pull requests (e.g., 6.4.1.0) +# HEAD_REF - The head reference for pull requests (optional) +# REPO - The target repository to search for matching branches (e.g., shopware/shopware or https://github.com/shopware/shopware) +# CURRENT_REPO - The current repository (e.g., shopware/platform) +# FALLBACK - The fallback branch to use if no matching branch is found (default: trunk) + set -e set pipefail get_ref() { local ref + PR_NR=$(echo "${REF}" | sed -E -n "s|^refs/heads/gh-readonly-queue/[^/]+/pr-([0-9]+)-.*$|\1|p") + if [[ -n "${PR_NR}" ]]; then + echo "refs/heads/$(gh pr view --repo "${CURRENT_REPO}" "${PR_NR}" --jq '.headRefName' --json headRefName)" + fi + + #remove whitespace from HEAD_REF with bash substitution + HEAD_REF=${HEAD_REF// /} if [[ -n "${HEAD_REF}" ]]; then - echo "refs/heads/${HEAD_REF}" + echo "refs/heads/${HEAD_REF#"refs/heads/"}" else - echo "${REF}" + #remove whitespace from REF with bash substitution + REF=${REF// /} + echo "refs/heads/${REF#"refs/heads/"}" fi - } -ref="$(get_ref)" -echo "Local ref: ${ref}" +get_base_ref() { + PR_NR=$(echo "${1}" | sed -E -n "s|^refs/heads/gh-readonly-queue/[^/]+/pr-([0-9]+)-.*$|\1|p") + if [[ -n "${PR_NR}" ]]; then + echo "$(gh pr view --repo "${CURRENT_REPO}" "${PR_NR}" --jq '.baseRefName' --json baseRefName)" + fi -PR_NR=$(echo "${ref}" | sed -E -n "s|^refs/heads/gh-readonly-queue/[^/]+/pr-([0-9]+)-.*$|\1|p") -if [[ -n "${PR_NR}" ]]; then - echo "Found merge queue ref with PR Nr. ${PR_NR}" 2>&1 + #remove whitespace from BASE_REF with bash substitution + BASE_REF=${BASE_REF// /} + if [[ -n "${BASE_REF}" ]]; then + echo "refs/heads/${BASE_REF#"refs/heads/"}" + fi +} - ref="refs/heads/$(gh pr view --repo "${CURRENT_REPO}" "${PR_NR}" --jq '.headRefName' --json headRefName)" - BASE_REF="$(gh pr view --repo "${CURRENT_REPO}" "${PR_NR}" --jq '.baseRefName' --json baseRefName)" -fi +REF="$(get_ref)" +echo "ref: ${REF}" +BASE_REF="$(get_base_ref "${REF}")" +echo "base ref: ${BASE_REF}" # if REPO does not start with https add https://github.com/ if [[ "${REPO}" != https://* ]]; then REPO="https://github.com/${REPO}" fi -remote_ref=$(git ls-remote --heads "${REPO}" "${ref}" | cut -f 2) +# Algo for finding the matching branch in another repo +# 1. if REF exists in target repo, use it +# 2. if BASE_REF exists in target repo, use it +# 3. if the next minor branch of BASE_REF exists in target repo, use it +# 4. if the next major branch of BASE_REF exists in target repo, use it +# 5. use fallback + +# to get the next minor from a patch branch replace last part with x +# to get the next major from a patch branch replace last two parts with x + + +echo "Step 1: Checking if REF '${REF}' exists in target repo '${REPO}'" +remote_ref=$(git ls-remote --heads "${REPO}" "${REF}" | cut -f 2) if [[ -n "${remote_ref}" ]]; then version="${remote_ref#"refs/heads/"}" + echo "✓ Found matching REF: ${version}" else - remote_ref=$(git ls-remote --heads "${REPO}" "refs/heads/${BASE_REF}" | cut -f 2) + BASE_REF=${BASE_REF// /} + if [[ -z "${BASE_REF}" ]]; then + echo "✗ BASE_REF not set, using REF '${REF}'" + BASE_REF="${REF}" + fi + + echo "✗ REF not found, checking BASE_REF '${BASE_REF}'" + # Check if BASE_REF exists in target repo + remote_ref=$(git ls-remote --heads "${REPO}" "${BASE_REF}" | cut -f 2) if [[ -n "${remote_ref}" ]]; then version="${remote_ref#"refs/heads/"}" + echo "✓ Found matching BASE_REF: ${version}" + else + echo "✗ BASE_REF not found, checking next minor branch" + # Check next minor branch (replace last digit with x) + next_minor=$(echo "${BASE_REF}" | sed -E 's/[0-9]+$/x/') + echo " Checking next minor: ${next_minor}" + remote_ref=$(git ls-remote --heads "${REPO}" "${next_minor}" | cut -f 2) + if [[ -n "${remote_ref}" ]]; then + version="${remote_ref#"refs/heads/"}" + echo "✓ Found matching next minor: ${version}" + else + echo "✗ Next minor not found, checking next major branch" + # Check next major branch (replace last two digits with x) + next_major=$(echo "${BASE_REF}" | sed -E 's/[0-9]+\.[0-9]+$/x/') + echo " Checking next major: ${next_major}" + remote_ref=$(git ls-remote --heads "${REPO}" "${next_major}" | cut -f 2) + + if [[ -n "${remote_ref}" ]]; then + version="${remote_ref#"refs/heads/"}" + echo "✓ Found matching next major: ${version}" + else + echo "✗ No matching branch found, using fallback: ${FALLBACK}" + # Use fallback + version="${FALLBACK}" + fi + fi fi fi if [[ -z "$version" ]]; then - echo "$ref not found in ${REPO}, using fallback " + echo "$REF not found in ${REPO}, using fallback " version="${FALLBACK}" fi