|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to add replace directives to go.mod for OpenStack operator dependencies |
| 4 | +# Usage: add-replace-directive.sh <fork_name> <branch_name> |
| 5 | +# Example: add-replace-directive.sh dprince/keystone-operator drop_kube_rbac_proxy |
| 6 | + |
| 7 | +set -e |
| 8 | + |
| 9 | +if [ $# -ne 2 ]; then |
| 10 | + echo "Usage: $0 <fork_name> <branch_name>" |
| 11 | + echo "Example: $0 dprince/keystone-operator drop_kube_rbac_proxy" |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +FORK_NAME="$1" |
| 16 | +BRANCH_NAME="$2" |
| 17 | + |
| 18 | +# Convert fork name to official openstack-k8s-operators equivalent |
| 19 | +# e.g., dprince/keystone-operator -> keystone-operator |
| 20 | +REPO_NAME=$(echo "$FORK_NAME" | sed 's|.*/||') |
| 21 | +OFFICIAL_REPO="github.com/openstack-k8s-operators/$REPO_NAME" |
| 22 | + |
| 23 | +echo "Converting fork $FORK_NAME to official repo: $OFFICIAL_REPO" |
| 24 | + |
| 25 | +# Find all OpenStack dependencies that match the repo pattern |
| 26 | +echo "Finding matching OpenStack dependencies..." |
| 27 | +MATCHING_DEPS=$(go list -mod=readonly -m -json all | jq -r --arg repo "$REPO_NAME" '. | select(.Path | contains("openstack")) | .Replace // . | .Path | select(. | contains($repo))') |
| 28 | + |
| 29 | +if [ -z "$MATCHING_DEPS" ]; then |
| 30 | + echo "No matching dependencies found for $REPO_NAME" |
| 31 | + exit 1 |
| 32 | +fi |
| 33 | + |
| 34 | +echo "Found matching dependencies:" |
| 35 | +echo "$MATCHING_DEPS" |
| 36 | + |
| 37 | +# Get the latest commit hash for the branch |
| 38 | +echo "Fetching latest commit for branch $BRANCH_NAME from fork github.com/$FORK_NAME..." |
| 39 | +COMMIT_HASH=$(git ls-remote "https://github.com/$FORK_NAME.git" "refs/heads/$BRANCH_NAME" | cut -f1) |
| 40 | + |
| 41 | +if [ -z "$COMMIT_HASH" ]; then |
| 42 | + echo "Error: Could not find branch $BRANCH_NAME in fork github.com/$FORK_NAME" |
| 43 | + exit 1 |
| 44 | +fi |
| 45 | + |
| 46 | +echo "Latest commit hash: $COMMIT_HASH" |
| 47 | + |
| 48 | +# Get commit timestamp using shallow clone |
| 49 | +echo "Fetching commit timestamp..." |
| 50 | +TEMP_DIR=$(mktemp -d) |
| 51 | +pushd "$TEMP_DIR" |
| 52 | +git clone --depth=1 --branch "$BRANCH_NAME" "https://github.com/$FORK_NAME.git" repo >/dev/null 2>&1 |
| 53 | +if [ $? -ne 0 ]; then |
| 54 | + rm -rf "$TEMP_DIR" |
| 55 | + echo "Error: Could not clone branch $BRANCH_NAME from github.com/$FORK_NAME" |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +pushd repo |
| 60 | +COMMIT_TIMESTAMP=$(git log -1 --format="%ct") |
| 61 | +popd |
| 62 | +popd |
| 63 | +rm -rf "$TEMP_DIR" |
| 64 | + |
| 65 | +# Convert timestamp to the format needed for pseudoversion (YYYYMMDDHHMMSS) |
| 66 | +FORMATTED_TIMESTAMP=$(date -u -d "@$COMMIT_TIMESTAMP" +%Y%m%d%H%M%S) |
| 67 | + |
| 68 | +# Create pseudoversion in format: v0.0.0-YYYYMMDDHHMMSS-abcdefabcdef |
| 69 | +PSEUDOVERSION="v0.0.0-${FORMATTED_TIMESTAMP}-${COMMIT_HASH:0:12}" |
| 70 | + |
| 71 | +echo "Generated pseudoversion: $PSEUDOVERSION" |
| 72 | + |
| 73 | +# Add replace directives for each matching dependency |
| 74 | +echo "Adding replace directives to go.mod..." |
| 75 | +while IFS= read -r dep; do |
| 76 | + if [ -n "$dep" ]; then |
| 77 | + echo "Adding replace directive for: $dep" |
| 78 | + # Check if replace directive already exists |
| 79 | + if grep -q "^replace $dep =>" go.mod; then |
| 80 | + echo "Replace directive already exists for $dep, skipping..." |
| 81 | + else |
| 82 | + # Extract the suffix from the dependency (e.g., /api from github.com/openstack-k8s-operators/keystone-operator/api) |
| 83 | + SUFFIX=$(echo "$dep" | sed "s|github.com/openstack-k8s-operators/$REPO_NAME||") |
| 84 | + REPLACEMENT_TARGET="github.com/$FORK_NAME$SUFFIX" |
| 85 | + |
| 86 | + go mod edit -replace="$dep=$REPLACEMENT_TARGET@$PSEUDOVERSION" |
| 87 | + pushd apis |
| 88 | + go mod edit -replace="$dep=$REPLACEMENT_TARGET@$PSEUDOVERSION" |
| 89 | + popd |
| 90 | + echo "Added: replace $dep => $REPLACEMENT_TARGET@$PSEUDOVERSION" |
| 91 | + fi |
| 92 | + fi |
| 93 | +done <<< "$MATCHING_DEPS" |
| 94 | + |
| 95 | +echo "Successfully added replace directives to go.mod" |
| 96 | +echo "Run 'go mod tidy' to update go.sum" |
0 commit comments