|
| 1 | +name: Check Branch Alias |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + release: |
| 6 | + types: [released] |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +# Cancels all previous workflow runs for pull requests that have not completed. |
| 10 | +concurrency: |
| 11 | + # The concurrency group contains the workflow name and the branch name for pull requests |
| 12 | + # or the commit hash for any other events. |
| 13 | + group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }} |
| 14 | + cancel-in-progress: true |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: read |
| 18 | + |
| 19 | +jobs: |
| 20 | + check-branch-alias: |
| 21 | + name: Check and update branch-alias |
| 22 | + runs-on: ubuntu-latest |
| 23 | + if: ${{ github.repository_owner == 'wp-cli' }} |
| 24 | + permissions: |
| 25 | + contents: write # Required for creating pull requests |
| 26 | + pull-requests: write # Required for creating pull requests |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: Check out source code |
| 30 | + uses: actions/checkout@v5 |
| 31 | + with: |
| 32 | + fetch-depth: 0 # Fetch all history for all tags |
| 33 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 34 | + |
| 35 | + - name: Set up PHP |
| 36 | + uses: shivammathur/setup-php@v2 |
| 37 | + with: |
| 38 | + php-version: 'latest' |
| 39 | + tools: composer |
| 40 | + |
| 41 | + - name: Check existence of composer.json file |
| 42 | + id: check_composer_file |
| 43 | + uses: andstor/file-existence-action@v3 |
| 44 | + with: |
| 45 | + files: "composer.json" |
| 46 | + |
| 47 | + - name: Check and update branch alias |
| 48 | + if: steps.check_composer_file.outputs.files_exists == 'true' |
| 49 | + id: check_alias |
| 50 | + run: | |
| 51 | + # Get the latest stable release tag (exclude pre-releases) |
| 52 | + LATEST_TAG=$(git tag --list 'v[0-9]*.[0-9]*.[0-9]*' --sort=-version:refname | head -n 1) |
| 53 | + |
| 54 | + if [ -z "$LATEST_TAG" ]; then |
| 55 | + echo "No stable release tags found, skipping branch-alias check" |
| 56 | + echo "needs_update=false" >> "$GITHUB_OUTPUT" |
| 57 | + exit 0 |
| 58 | + fi |
| 59 | + |
| 60 | + echo "Latest tag: $LATEST_TAG" |
| 61 | + |
| 62 | + # Extract version numbers (remove 'v' prefix and any pre-release/build metadata) |
| 63 | + VERSION="${LATEST_TAG#v}" |
| 64 | + # Remove any pre-release or build metadata (e.g., -alpha, +build) |
| 65 | + VERSION="${VERSION%%-*}" |
| 66 | + VERSION="${VERSION%%+*}" |
| 67 | + |
| 68 | + # Parse major, minor, and patch versions |
| 69 | + IFS='.' read -r MAJOR MINOR _ <<< "$VERSION" |
| 70 | + |
| 71 | + # Validate that MAJOR and MINOR are numeric |
| 72 | + if ! [[ "$MAJOR" =~ ^[0-9]+$ ]] || ! [[ "$MINOR" =~ ^[0-9]+$ ]]; then |
| 73 | + echo "Invalid version format: $VERSION" |
| 74 | + echo "needs_update=false" >> "$GITHUB_OUTPUT" |
| 75 | + exit 0 |
| 76 | + fi |
| 77 | + |
| 78 | + # Calculate next minor version |
| 79 | + NEXT_MINOR=$((MINOR + 1)) |
| 80 | + EXPECTED_ALIAS="${MAJOR}.${NEXT_MINOR}.x-dev" |
| 81 | + |
| 82 | + echo "Expected branch-alias: $EXPECTED_ALIAS" |
| 83 | + |
| 84 | + # Determine which branch key is being used (dev-main or dev-master) |
| 85 | + BRANCH_KEY="" |
| 86 | + CURRENT_ALIAS="" |
| 87 | + |
| 88 | + # Try dev-main first |
| 89 | + if composer config extra.branch-alias.dev-main > /dev/null 2>&1; then |
| 90 | + BRANCH_KEY="dev-main" |
| 91 | + CURRENT_ALIAS=$(composer config extra.branch-alias.dev-main) |
| 92 | + # Try dev-master if dev-main doesn't exist |
| 93 | + elif composer config extra.branch-alias.dev-master > /dev/null 2>&1; then |
| 94 | + BRANCH_KEY="dev-master" |
| 95 | + CURRENT_ALIAS=$(composer config extra.branch-alias.dev-master) |
| 96 | + else |
| 97 | + echo "No branch-alias found in composer.json, will use dev-main" |
| 98 | + BRANCH_KEY="dev-main" |
| 99 | + CURRENT_ALIAS="" |
| 100 | + fi |
| 101 | + |
| 102 | + echo "Current branch-alias ($BRANCH_KEY): $CURRENT_ALIAS" |
| 103 | + |
| 104 | + if [ "$CURRENT_ALIAS" != "$EXPECTED_ALIAS" ]; then |
| 105 | + echo "Branch alias needs update" |
| 106 | + { |
| 107 | + echo "needs_update=true" |
| 108 | + echo "expected_alias=$EXPECTED_ALIAS" |
| 109 | + echo "current_alias=$CURRENT_ALIAS" |
| 110 | + echo "branch_key=$BRANCH_KEY" |
| 111 | + echo "latest_tag=$LATEST_TAG" |
| 112 | + } >> "$GITHUB_OUTPUT" |
| 113 | + else |
| 114 | + echo "Branch alias is up to date" |
| 115 | + echo "needs_update=false" >> "$GITHUB_OUTPUT" |
| 116 | + fi |
| 117 | +
|
| 118 | + - name: Update composer.json |
| 119 | + if: steps.check_alias.outputs.needs_update == 'true' |
| 120 | + run: | |
| 121 | + BRANCH_KEY="${{ steps.check_alias.outputs.branch_key }}" |
| 122 | + EXPECTED_ALIAS="${{ steps.check_alias.outputs.expected_alias }}" |
| 123 | + |
| 124 | + # Update the branch-alias using composer command |
| 125 | + composer config "extra.branch-alias.$BRANCH_KEY" "$EXPECTED_ALIAS" |
| 126 | +
|
| 127 | + - name: Create Pull Request |
| 128 | + if: steps.check_alias.outputs.needs_update == 'true' |
| 129 | + uses: peter-evans/create-pull-request@v7 |
| 130 | + with: |
| 131 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 132 | + commit-message: "Update branch-alias to ${{ steps.check_alias.outputs.expected_alias }}" |
| 133 | + title: "Update branch-alias to ${{ steps.check_alias.outputs.expected_alias }}" |
| 134 | + body: | |
| 135 | + This PR updates the Composer `branch-alias` configuration to reflect the latest release. |
| 136 | + |
| 137 | + - Latest release: ${{ steps.check_alias.outputs.latest_tag }} |
| 138 | + - Previous branch-alias: `${{ steps.check_alias.outputs.current_alias }}` |
| 139 | + - Updated branch-alias: `${{ steps.check_alias.outputs.expected_alias }}` |
| 140 | + |
| 141 | + The branch-alias should point to the next minor development version after the latest release. |
| 142 | + branch: update-branch-alias |
| 143 | + delete-branch: true |
| 144 | + labels: | |
| 145 | + automated-pr |
0 commit comments