Skip to content

Commit d22fbe6

Browse files
committed
refactor with helper scripts
1 parent 2f010d4 commit d22fbe6

File tree

5 files changed

+187
-70
lines changed

5 files changed

+187
-70
lines changed

.github/workflows/rust-release.yml

Lines changed: 10 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
working-directory: ./rust/main
4848
run: |
4949
# Get latest agents-v* tag
50-
LATEST_TAG=$(git tag -l "agents-v*" --sort=-version:refname | grep -E "^agents-v[0-9]+\.[0-9]+\.[0-9]+$" | head -1)
50+
LATEST_TAG=$(../scripts/get-latest-agents-tag.sh)
5151
5252
if [ -z "$LATEST_TAG" ]; then
5353
echo "No previous release found"
@@ -70,12 +70,12 @@ jobs:
7070
working-directory: ./rust/main
7171
run: |
7272
# Get current version from Cargo.toml workspace.package.version
73-
CURRENT_VERSION=$(grep -A 10 '^\[workspace\.package\]' Cargo.toml | grep '^version = ' | head -1 | sed 's/version = "\(.*\)"/\1/')
73+
CURRENT_VERSION=$(../scripts/get-workspace-version.sh)
7474
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
7575
echo "Current workspace version: $CURRENT_VERSION"
7676
7777
# Get latest agents-v* tag
78-
LATEST_TAG=$(git tag -l "agents-v*" --sort=-version:refname | grep -E "^agents-v[0-9]+\.[0-9]+\.[0-9]+$" | head -1)
78+
LATEST_TAG=$(../scripts/get-latest-agents-tag.sh)
7979
8080
if [ -z "$LATEST_TAG" ]; then
8181
echo "latest_version=" >> $GITHUB_OUTPUT
@@ -109,63 +109,14 @@ jobs:
109109
- uses: dtolnay/rust-toolchain@stable
110110
- name: Determine next version from conventional commits
111111
id: next_version
112-
working-directory: ./rust/main
113112
env:
114113
CURRENT_VERSION: ${{ needs.check-release-status.outputs.current_version }}
115114
run: |
116-
# Get commits since last release
117-
LATEST_TAG=$(git tag -l "agents-v*" --sort=-version:refname | grep -E "^agents-v[0-9]+\.[0-9]+\.[0-9]+$" | head -1)
118-
119-
if [ -z "$LATEST_TAG" ]; then
120-
COMMITS=$(git log --oneline --no-merges -- .)
121-
else
122-
COMMITS=$(git log "${LATEST_TAG}..HEAD" --oneline --no-merges -- .)
123-
fi
124-
125-
# Parse current version
126-
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
127-
128-
# Analyze commits for conventional commit types
129-
HAS_BREAKING=false
130-
HAS_MINOR=false
131-
HAS_PATCH=false
132-
133-
while IFS= read -r commit; do
134-
if echo "$commit" | grep -qE "^[a-f0-9]+ [a-z]+(\(.+\))?!:"; then
135-
HAS_BREAKING=true
136-
elif echo "$commit" | grep -qE "^[a-f0-9]+ (feat|refactor|perf|chore)(\(.+\))?:"; then
137-
HAS_MINOR=true
138-
elif echo "$commit" | grep -qE "^[a-f0-9]+ (fix|docs|test|ci|style|build)(\(.+\))?:"; then
139-
HAS_PATCH=true
140-
fi
141-
142-
# Check commit body for BREAKING CHANGE
143-
COMMIT_HASH=$(echo "$commit" | cut -d' ' -f1)
144-
if git show -s --format=%B "$COMMIT_HASH" | grep -q "BREAKING CHANGE:"; then
145-
HAS_BREAKING=true
146-
fi
147-
done <<< "$COMMITS"
148-
149-
# Determine version bump
150-
if [ "$HAS_BREAKING" = true ]; then
151-
MAJOR=$((MAJOR + 1))
152-
MINOR=0
153-
PATCH=0
154-
BUMP_TYPE="major"
155-
elif [ "$HAS_MINOR" = true ]; then
156-
MINOR=$((MINOR + 1))
157-
PATCH=0
158-
BUMP_TYPE="minor"
159-
elif [ "$HAS_PATCH" = true ]; then
160-
PATCH=$((PATCH + 1))
161-
BUMP_TYPE="patch"
162-
else
163-
# Default to patch for any other changes
164-
PATCH=$((PATCH + 1))
165-
BUMP_TYPE="patch"
166-
fi
115+
# Use helper script to determine next version
116+
OUTPUT=$(./rust/scripts/determine-next-version.sh "$CURRENT_VERSION")
117+
NEW_VERSION=$(echo "$OUTPUT" | sed -n '1p')
118+
BUMP_TYPE=$(echo "$OUTPUT" | sed -n '2p')
167119
168-
NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}"
169120
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
170121
echo "bump_type=$BUMP_TYPE" >> $GITHUB_OUTPUT
171122
echo "Next version: $NEW_VERSION ($BUMP_TYPE bump from $CURRENT_VERSION)"
@@ -175,7 +126,7 @@ jobs:
175126
NEW_VERSION: ${{ steps.next_version.outputs.new_version }}
176127
run: |
177128
# Get commit range for changelog generation
178-
LATEST_TAG=$(git tag -l "agents-v*" --sort=-version:refname | grep -E "^agents-v[0-9]+\.[0-9]+\.[0-9]+$" | head -1)
129+
LATEST_TAG=$(./rust/scripts/get-latest-agents-tag.sh)
179130
180131
if [ -z "$LATEST_TAG" ]; then
181132
COMMIT_RANGE=""
@@ -203,25 +154,14 @@ jobs:
203154
# Generate per-workspace CHANGELOG.md files
204155
./rust/scripts/generate-workspace-changelog.sh "$COMMIT_RANGE" "" --write-to-workspace "$NEW_VERSION"
205156
- name: Update version files
206-
working-directory: ./rust/main
207157
env:
208158
NEW_VERSION: ${{ steps.next_version.outputs.new_version }}
209159
run: |
210160
# Update workspace version in Cargo.toml
211-
# Use awk to find [workspace.package] section and update version within it
212-
awk -v new_version="$NEW_VERSION" '
213-
/^\[workspace\.package\]/ { in_workspace=1 }
214-
/^\[/ && !/^\[workspace\.package\]/ { in_workspace=0 }
215-
in_workspace && /^version = / {
216-
print "version = \"" new_version "\""
217-
next
218-
}
219-
{ print }
220-
' Cargo.toml > Cargo.toml.new
221-
mv Cargo.toml.new Cargo.toml
222-
echo "Updated Cargo.toml workspace version to $NEW_VERSION"
161+
./rust/scripts/update-workspace-version.sh "$NEW_VERSION"
223162
224163
# Update Cargo.lock in rust/main
164+
cd rust/main
225165
cargo update --workspace --offline 2>/dev/null || cargo update --workspace
226166
echo "Updated rust/main/Cargo.lock"
227167
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env bash
2+
# Determine next version based on conventional commits
3+
#
4+
# Usage:
5+
# determine-next-version.sh CURRENT_VERSION [COMMIT_RANGE]
6+
#
7+
# Arguments:
8+
# CURRENT_VERSION - The current version (e.g., "1.4.0")
9+
# COMMIT_RANGE - Optional git commit range (e.g., "v1.4.0..HEAD"). If omitted, uses latest tag..HEAD
10+
#
11+
# Returns:
12+
# Outputs two lines:
13+
# 1. The next version (e.g., "1.5.0")
14+
# 2. The bump type ("major", "minor", or "patch")
15+
#
16+
# Examples:
17+
# ./determine-next-version.sh "1.4.0"
18+
# ./determine-next-version.sh "1.4.0" "v1.4.0..HEAD"
19+
#
20+
set -euo pipefail
21+
22+
if [ $# -lt 1 ]; then
23+
echo "Error: CURRENT_VERSION is required" >&2
24+
echo "Usage: $0 CURRENT_VERSION [COMMIT_RANGE]" >&2
25+
exit 1
26+
fi
27+
28+
CURRENT_VERSION="$1"
29+
COMMIT_RANGE="${2:-}"
30+
31+
# Determine script directory
32+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
33+
RUST_MAIN_DIR="$SCRIPT_DIR/../main"
34+
35+
# If no commit range specified, use commits since latest tag
36+
if [ -z "$COMMIT_RANGE" ]; then
37+
LATEST_TAG=$("$SCRIPT_DIR/get-latest-agents-tag.sh")
38+
39+
if [ -z "$LATEST_TAG" ]; then
40+
# Get all commits in rust/main
41+
cd "$RUST_MAIN_DIR"
42+
COMMITS=$(git log --oneline --no-merges -- .)
43+
else
44+
cd "$RUST_MAIN_DIR"
45+
COMMITS=$(git log "${LATEST_TAG}..HEAD" --oneline --no-merges -- .)
46+
fi
47+
else
48+
cd "$RUST_MAIN_DIR"
49+
COMMITS=$(git log "$COMMIT_RANGE" --oneline --no-merges -- .)
50+
fi
51+
52+
# Parse current version
53+
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
54+
55+
# Analyze commits for conventional commit types
56+
HAS_BREAKING=false
57+
HAS_MINOR=false
58+
HAS_PATCH=false
59+
60+
while IFS= read -r commit; do
61+
# Skip empty lines
62+
[ -z "$commit" ] && continue
63+
64+
# Check for breaking changes via ! suffix
65+
if echo "$commit" | grep -qE "^[a-f0-9]+ [a-z]+(\(.+\))?!:"; then
66+
HAS_BREAKING=true
67+
# Check for minor changes (new features, refactors, perf improvements, chores)
68+
elif echo "$commit" | grep -qE "^[a-f0-9]+ (feat|refactor|perf|chore)(\(.+\))?:"; then
69+
HAS_MINOR=true
70+
# Check for patch changes (fixes, docs, tests, ci, style, build)
71+
elif echo "$commit" | grep -qE "^[a-f0-9]+ (fix|docs|test|ci|style|build)(\(.+\))?:"; then
72+
HAS_PATCH=true
73+
fi
74+
75+
# Check commit body for BREAKING CHANGE
76+
COMMIT_HASH=$(echo "$commit" | cut -d' ' -f1)
77+
if git show -s --format=%B "$COMMIT_HASH" | grep -q "BREAKING CHANGE:"; then
78+
HAS_BREAKING=true
79+
fi
80+
done <<< "$COMMITS"
81+
82+
# Determine version bump
83+
if [ "$HAS_BREAKING" = true ]; then
84+
MAJOR=$((MAJOR + 1))
85+
MINOR=0
86+
PATCH=0
87+
BUMP_TYPE="major"
88+
elif [ "$HAS_MINOR" = true ]; then
89+
MINOR=$((MINOR + 1))
90+
PATCH=0
91+
BUMP_TYPE="minor"
92+
elif [ "$HAS_PATCH" = true ]; then
93+
PATCH=$((PATCH + 1))
94+
BUMP_TYPE="patch"
95+
else
96+
# Default to patch for any other changes
97+
PATCH=$((PATCH + 1))
98+
BUMP_TYPE="patch"
99+
fi
100+
101+
NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}"
102+
103+
# Output results (two lines)
104+
echo "$NEW_VERSION"
105+
echo "$BUMP_TYPE"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
# Get the latest stable agents-v* tag (excluding prereleases)
3+
#
4+
# Usage:
5+
# get-latest-agents-tag.sh
6+
#
7+
# Returns:
8+
# The latest agents-v* tag (e.g., "agents-v1.4.0"), or empty string if none found
9+
#
10+
set -euo pipefail
11+
12+
git tag -l "agents-v*" --sort=-version:refname | grep -E "^agents-v[0-9]+\.[0-9]+\.[0-9]+$" | head -1 || true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
# Extract the version from rust/main/Cargo.toml workspace.package.version
3+
#
4+
# Usage:
5+
# get-workspace-version.sh
6+
#
7+
# Returns:
8+
# The current workspace version (e.g., "1.4.0")
9+
#
10+
set -euo pipefail
11+
12+
# Determine script directory and repo structure
13+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
14+
RUST_MAIN_DIR="$SCRIPT_DIR/../main"
15+
16+
grep -A 10 '^\[workspace\.package\]' "$RUST_MAIN_DIR/Cargo.toml" | \
17+
grep '^version = ' | \
18+
head -1 | \
19+
sed 's/version = "\(.*\)"/\1/'
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
# Update the workspace version in rust/main/Cargo.toml
3+
#
4+
# Usage:
5+
# update-workspace-version.sh NEW_VERSION
6+
#
7+
# Arguments:
8+
# NEW_VERSION - The new version to set (e.g., "1.5.0")
9+
#
10+
# Returns:
11+
# Updates rust/main/Cargo.toml in place
12+
#
13+
set -euo pipefail
14+
15+
if [ $# -ne 1 ]; then
16+
echo "Error: NEW_VERSION is required" >&2
17+
echo "Usage: $0 NEW_VERSION" >&2
18+
exit 1
19+
fi
20+
21+
NEW_VERSION="$1"
22+
23+
# Determine script directory and repo structure
24+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
25+
RUST_MAIN_DIR="$SCRIPT_DIR/../main"
26+
CARGO_TOML="$RUST_MAIN_DIR/Cargo.toml"
27+
28+
# Use awk to find [workspace.package] section and update version within it
29+
awk -v new_version="$NEW_VERSION" '
30+
/^\[workspace\.package\]/ { in_workspace=1 }
31+
/^\[/ && !/^\[workspace\.package\]/ { in_workspace=0 }
32+
in_workspace && /^version = / {
33+
print "version = \"" new_version "\""
34+
next
35+
}
36+
{ print }
37+
' "$CARGO_TOML" > "$CARGO_TOML.new"
38+
39+
mv "$CARGO_TOML.new" "$CARGO_TOML"
40+
41+
echo "Updated $CARGO_TOML to version $NEW_VERSION"

0 commit comments

Comments
 (0)