Skip to content

Commit 8fdcfd3

Browse files
dprinceclaude
andcommitted
Add Makefile target and script for managing go.mod replace directives
usage: make add-replace-directive FORK=user/repo-name BRANCH=branch-name This adds functionality to dynamically add replace directives for OpenStack operator dependencies when working with forks. The script automatically detects matching dependencies, fetches commit information, and generates proper pseudoversions for replace directives. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 50fbf47 commit 8fdcfd3

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ tidy: ## Run go mod tidy on every mod file in the repo
186186
go mod tidy
187187
cd ./apis && go mod tidy
188188

189+
.PHONY: add-replace-directive
190+
add-replace-directive: ## Add replace directive for OpenStack operator dependency (usage: make add-replace-directive FORK=user/repo-name BRANCH=branch-name)
191+
@if [ -z "$(FORK)" ] || [ -z "$(BRANCH)" ]; then \
192+
echo "Usage: make add-replace-directive FORK=user/repo-name BRANCH=branch-name"; \
193+
echo "Example: make add-replace-directive FORK=dprince/keystone-operator BRANCH=drop_kube_rbac_proxy"; \
194+
exit 1; \
195+
fi
196+
./hack/add-replace-directive.sh $(FORK) $(BRANCH)
197+
189198
.PHONY: golangci-lint
190199
golangci-lint:
191200
test -s $(LOCALBIN)/golangci-lint || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.59.1

hack/add-replace-directive.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)