Skip to content

Commit 30a865b

Browse files
Refactor increment_version.py to modify_changelog.py with subcommands and tests
- Renamed increment_version.py to modify_changelog.py - Consolidated backward compatibility logic into main() - Added subcommands: bump_version and update_changelog - Updated GitHub workflows to use new command format - Added comprehensive tests for both functionalities - Improved argument parsing and error handling Co-authored-by: saulshanabrook <[email protected]>
1 parent 7d0f459 commit 30a865b

File tree

4 files changed

+381
-85
lines changed

4 files changed

+381
-85
lines changed

.github/workflows/update-changelog.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ jobs:
2929

3030
- name: Update changelog
3131
run: |
32-
python increment_version.py --add-pr \
33-
--pr-number="${{ github.event.pull_request.number }}" \
34-
--pr-title="${{ github.event.pull_request.title }}" \
35-
--pr-url="${{ github.event.pull_request.html_url }}"
32+
python modify_changelog.py update_changelog \
33+
"${{ github.event.pull_request.number }}" \
34+
"${{ github.event.pull_request.title }}"
3635
3736
- name: Check for changes
3837
id: changes

.github/workflows/version.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
- run: |
4040
git config user.name github-actions[bot]
4141
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
42-
VERSION=$(python increment_version.py $TYPE)
42+
VERSION=$(python modify_changelog.py bump_version $TYPE)
4343
git checkout -b "version-$VERSION"
4444
git commit -am "Version $VERSION"
4545
git push -u origin HEAD

increment_version.py renamed to modify_changelog.py

Lines changed: 77 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
"""
2-
Version Bumper for Cargo.toml and Changelog.md
2+
Changelog Modifier and Version Bumper for Cargo.toml and Changelog.md
33
4-
This script automates the process of version bumping for Rust projects managed with Cargo. It reads the version
5-
from the cargo.toml file, increments it based on the specified component (major, minor, or patch), and updates
6-
both the cargo.toml and changelog.md files accordingly.
4+
This script automates the process of version bumping and changelog updates for Rust projects managed with Cargo.
5+
It reads the version from the cargo.toml file, increments it based on the specified component (major, minor, or patch),
6+
and updates both the cargo.toml and changelog.md files accordingly.
77
8-
It will also print out the new version number.
9-
10-
Additionally, this script can add PR entries to the UNRELEASED section of the changelog.
8+
It can also add PR entries to the UNRELEASED section of the changelog.
119
1210
Usage:
1311
Version bumping:
14-
$ python increment_version.py [major|minor|patch]
12+
$ python modify_changelog.py bump_version [major|minor|patch]
1513
1614
Adding PR entry:
17-
$ python increment_version.py --add-pr --pr-number=123 --pr-title="Fix bug" --pr-url="https://github.com/..."
18-
19-
Arguments:
20-
---------
21-
major - Increments the major component of the version, sets minor and patch to 0
22-
minor - Increments the minor component of the version, sets patch to 0
23-
patch - Increments the patch component of the version
24-
--add-pr - Add a PR entry to the UNRELEASED section
25-
--pr-number - PR number (required with --add-pr)
26-
--pr-title - PR title (required with --add-pr)
27-
--pr-url - PR URL (required with --add-pr)
15+
$ python modify_changelog.py update_changelog <number> <title>
2816
29-
From https://chat.openai.com/share/6b08906d-23a3-4193-9f4e-87076ce56ddb
17+
Subcommands:
18+
-----------
19+
bump_version - Increments version and updates changelog
20+
major - Increments the major component of the version, sets minor and patch to 0
21+
minor - Increments the minor component of the version, sets patch to 0
22+
patch - Increments the patch component of the version
23+
24+
update_changelog - Add a PR entry to the UNRELEASED section
25+
number - PR number
26+
title - PR title
3027
28+
From https://chat.openai.com/share/6b08906d-23a3-4193-9f4e-87076ce56ddb
3129
3230
"""
3331

@@ -128,48 +126,20 @@ def update_changelog_pr(file_path: Path, pr_number: str, pr_title: str, pr_url:
128126
return True
129127

130128

131-
def main():
132-
parser = argparse.ArgumentParser(description='Version bumper and changelog updater')
133-
134-
# Create mutually exclusive group for version bump vs PR add
135-
group = parser.add_mutually_exclusive_group(required=True)
136-
group.add_argument('bump_type', nargs='?', choices=['major', 'minor', 'patch'],
137-
help='Type of version bump')
138-
group.add_argument('--add-pr', action='store_true', help='Add PR entry to changelog')
139-
140-
# PR-specific arguments
141-
parser.add_argument('--pr-number', help='Pull request number (required with --add-pr)')
142-
parser.add_argument('--pr-title', help='Pull request title (required with --add-pr)')
143-
parser.add_argument('--pr-url', help='Pull request URL (required with --add-pr)')
144-
parser.add_argument('--changelog-path', default='docs/changelog.md', help='Path to changelog file')
145-
146-
args = parser.parse_args()
147-
148-
# Handle PR addition
149-
if args.add_pr:
150-
if not all([args.pr_number, args.pr_title, args.pr_url]):
151-
print("ERROR: --pr-number, --pr-title, and --pr-url are required with --add-pr")
152-
sys.exit(1)
153-
154-
changelog_path = Path(args.changelog_path)
155-
if not changelog_path.exists():
156-
print(f"ERROR: Changelog file not found: {changelog_path}")
157-
sys.exit(1)
158-
159-
success = update_changelog_pr(changelog_path, args.pr_number, args.pr_title, args.pr_url)
160-
if not success:
161-
sys.exit(1)
162-
return
163-
164-
# Handle version bump (existing functionality)
165-
if not args.bump_type:
166-
print("ERROR: Either specify bump type (major|minor|patch) or use --add-pr")
167-
sys.exit(1)
168-
129+
def handle_bump_version(args):
130+
"""Handle version bump subcommand."""
169131
part = args.bump_type
170132
cargo_path = Path("Cargo.toml")
171133
changelog_path = Path("docs/changelog.md")
172134

135+
if not cargo_path.exists():
136+
print("ERROR: Cargo.toml not found.")
137+
sys.exit(1)
138+
139+
if not changelog_path.exists():
140+
print("ERROR: Changelog file not found.")
141+
sys.exit(1)
142+
173143
cargo_content = cargo_path.read_text()
174144
version_match = re.search(r'version = "(\d+)\.(\d+)\.(\d+)"', cargo_content)
175145
if version_match:
@@ -179,31 +149,58 @@ def main():
179149
sys.exit(1)
180150

181151
new_version = bump_version(major, minor, patch, part)
182-
old_version = f"{major}.{minor}.{patch}"
183152
update_cargo_toml(cargo_path, new_version)
184153
update_changelog_version(changelog_path, new_version)
185154
print(new_version)
186155

187156

157+
def handle_update_changelog(args):
158+
"""Handle update changelog subcommand."""
159+
pr_number = args.number
160+
pr_title = args.title
161+
162+
# Construct PR URL from repository info and PR number
163+
# Default to the egglog-python repository
164+
pr_url = f"https://github.com/egraphs-good/egglog-python/pull/{pr_number}"
165+
166+
changelog_path = Path(getattr(args, 'changelog_path', 'docs/changelog.md'))
167+
168+
if not changelog_path.exists():
169+
print(f"ERROR: Changelog file not found: {changelog_path}")
170+
sys.exit(1)
171+
172+
success = update_changelog_pr(changelog_path, pr_number, pr_title, pr_url)
173+
if not success:
174+
sys.exit(1)
175+
176+
177+
def main():
178+
parser = argparse.ArgumentParser(description='Changelog modifier and version bumper')
179+
subparsers = parser.add_subparsers(dest='command', help='Available commands')
180+
181+
# Bump version subcommand
182+
bump_parser = subparsers.add_parser('bump_version', help='Bump version and update changelog')
183+
bump_parser.add_argument('bump_type', choices=['major', 'minor', 'patch'],
184+
help='Type of version bump')
185+
186+
# Update changelog subcommand
187+
changelog_parser = subparsers.add_parser('update_changelog', help='Add PR entry to changelog')
188+
changelog_parser.add_argument('number', help='Pull request number')
189+
changelog_parser.add_argument('title', help='Pull request title')
190+
changelog_parser.add_argument('--changelog-path', default='docs/changelog.md',
191+
help='Path to changelog file')
192+
193+
args = parser.parse_args()
194+
195+
if not args.command:
196+
parser.print_help()
197+
sys.exit(1)
198+
199+
if args.command == 'bump_version':
200+
handle_bump_version(args)
201+
elif args.command == 'update_changelog':
202+
handle_update_changelog(args)
203+
204+
188205
if __name__ == "__main__":
189-
# For backward compatibility, support old command line format
190-
if len(sys.argv) == 2 and sys.argv[1] in ("major", "minor", "patch"):
191-
part = sys.argv[1]
192-
cargo_path = Path("Cargo.toml")
193-
changelog_path = Path("docs/changelog.md")
194-
195-
cargo_content = cargo_path.read_text()
196-
version_match = re.search(r'version = "(\d+)\.(\d+)\.(\d+)"', cargo_content)
197-
if version_match:
198-
major, minor, patch = map(int, version_match.groups())
199-
else:
200-
print("Current version not found in cargo.toml.")
201-
sys.exit(1)
202-
203-
new_version = bump_version(major, minor, patch, part)
204-
old_version = f"{major}.{minor}.{patch}"
205-
update_cargo_toml(cargo_path, new_version)
206-
update_changelog_version(changelog_path, new_version)
207-
print(new_version)
208-
else:
209-
main()
206+
main()

0 commit comments

Comments
 (0)