Skip to content

Commit b43c79a

Browse files
committed
Add git-constraint-helper
1 parent 43ad1b9 commit b43c79a

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

.github/scripts/create-multidev.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#!/bin/bash
22
set -euo pipefail
33

4+
# Source the constraint helper to auto-detect the right Composer version.
5+
. "$(dirname "${BASH_SOURCE[0]}")/git-constraint-helper"
6+
47
MULTIDEV_NAME="$1"
58
TERMINUS_SITE="$2"
69
GITHUB_ENV_FILE="${3:-${GITHUB_ENV:-}}"
7-
GIT_REF="${4:-dev-1.0.x}"
10+
CONSTRAINT=$(get_current_constraint)
811

912
# Limit multidev name to 11 characters
1013
MULTIDEV="${MULTIDEV_NAME:0:11}"
@@ -32,9 +35,12 @@ cd pantheon-site
3235
echo "Checking out branch $MULTIDEV..."
3336
git checkout "$MULTIDEV"
3437

35-
# Add pantheon_content_publisher module via composer
38+
# Add pantheon_content_publisher module via composer.
39+
# The VCS repo is needed so Composer can resolve dev branch constraints
40+
# (packages.drupal.org only serves tagged releases).
3641
composer config repositories.pantheon_content_publisher '{"type": "vcs", "url": "git@github.com:pantheon-systems/pantheon-content-publisher-drupal.git", "canonical": false}'
37-
composer require drupal/pantheon_content_publisher:"${GIT_REF}"
42+
echo "Installing drupal/pantheon_content_publisher:${CONSTRAINT}"
43+
composer require drupal/pantheon_content_publisher:"${CONSTRAINT}"
3844

3945
# Show where the module was installed for diagnostics.
4046
echo "Module installed at:"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
#
3+
# @file Git Constraint Helper
4+
# Determines the correct Composer version constraint based on the current
5+
# Git state (branch, tag, or PR context).
6+
#
7+
# Usage:
8+
# . "$(dirname "${BASH_SOURCE[0]}")/git-constraint-helper"
9+
# CONSTRAINT=$(get_current_constraint)
10+
# composer require drupal/pantheon_content_publisher:"$CONSTRAINT"
11+
12+
# Format a branch name as a Composer dev constraint.
13+
# Composer uses two formats:
14+
# - Version-like branches (1.0.x, 2.x) → "1.0.x-dev" (suffix)
15+
# - Non-version branches (main, feature/foo) → "dev-main" (prefix)
16+
_branch_to_constraint() {
17+
local branch="$1"
18+
if [[ "$branch" =~ ^v?[0-9]+(\.[0-9x]+)+ ]]; then
19+
echo "${branch}-dev"
20+
else
21+
echo "dev-${branch}"
22+
fi
23+
}
24+
25+
get_current_constraint() {
26+
# 1. On a named branch?
27+
local branch
28+
branch=$(git rev-parse --abbrev-ref HEAD | tr -d '[:space:]')
29+
if [ "$branch" != "HEAD" ]; then
30+
_branch_to_constraint "$branch"
31+
return
32+
fi
33+
34+
# 2. On an exact tag (detached HEAD at a release)?
35+
local tag
36+
tag=$(git describe --exact-match --tags "$(git log -n1 --pretty='%h')" 2>/dev/null | tr -d '[:space:]')
37+
if [ -n "$tag" ]; then
38+
echo "$tag"
39+
return
40+
fi
41+
42+
# 3. In a GitHub Actions PR (detached HEAD)? Use GITHUB_HEAD_REF.
43+
if [ -n "$GITHUB_HEAD_REF" ]; then
44+
local pr_branch
45+
IFS='/' read -ra parts <<< "$GITHUB_HEAD_REF"
46+
pr_branch="${parts[-1]}"
47+
if [ -n "$pr_branch" ]; then
48+
_branch_to_constraint "$pr_branch"
49+
return
50+
fi
51+
fi
52+
53+
# 4. Fallback to latest stable.
54+
echo "^1"
55+
}

.github/workflows/pull_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ jobs:
107107
ssh-private-key: ${{ secrets.PANTHEON_CI_SSH_KEY }}
108108

109109
- name: Create multidev environment
110-
run: .github/scripts/create-multidev.sh "$MULTIDEV_NAME" "$TERMINUS_SITE" "$GITHUB_ENV" "dev-${{ github.head_ref || github.ref_name }}"
110+
run: .github/scripts/create-multidev.sh "$MULTIDEV_NAME" "$TERMINUS_SITE" "$GITHUB_ENV"
111111

112112
- name: Clear cache
113113
run: |

0 commit comments

Comments
 (0)