Skip to content

Commit

Permalink
Merge pull request #6 from awhipp/feature/adding_version_bumping
Browse files Browse the repository at this point in the history
feat: 💚 Adding version bumping to main branch
  • Loading branch information
awhipp authored Mar 13, 2024
2 parents 4a450e9 + 463872b commit 63e5d26
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
57 changes: 57 additions & 0 deletions .github/analyze_git_history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Python script to analyze git history and determine version bump."""

import subprocess


def get_commit_messages():
"""Get commit messages using git log."""
# Get commit messages using git log
commit_log = (
subprocess.check_output(["git", "log", "--pretty=%s"]).decode().split("\n")
)
# Filter out empty lines
commit_messages = [msg.strip() for msg in commit_log if msg.strip()]

# Only get the commit messages between the last two merge requests
merge_request_count = 0
valid_commit_messages = []
for _, message in enumerate(commit_messages):
if merge_request_count == 2:
break
if "Merge pull request" in message:
merge_request_count += 1
else:
valid_commit_messages.append(message)

return valid_commit_messages


def determine_version_bump(valid_messages):
"""Determine version bump based on commit messages."""
version_bump = None

# Check commit messages for specific keywords to determine version bump
for message in valid_messages:
if message.startswith("fix") and version_bump in [None, "patch"]:
version_bump = "patch"
elif message.startswith("feat") and version_bump in [None, "patch", "minor"]:
version_bump = "minor"
elif message.startswith("BREAKING CHANGE") and version_bump in [
None,
"patch",
"minor",
"major",
]:
version_bump = "major"
# Add more conditions based on your project's conventions

# Default to 'patch' if no specific keyword found
return version_bump


if __name__ == "__main__":
# Determine version bump
version_bump = determine_version_bump(get_commit_messages())

# Output version bump
print(version_bump)
38 changes: 38 additions & 0 deletions .github/workflows/bump_version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Versioning Workflow

on:
push:
branches:
- main

jobs:
determine-version:
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Analyze Commit Messages and Bump Version
run: |
# Custom script to analyze commit messages and determine version bump
## Stored in .github/analyze_git_history.py
## Assuming the script outputs the determined version bump as 'patch', 'minor', or 'major'
## Only None if there is no relevant commits.
version_bump=$(python .github/analyze_git_history.py)
if [ "$version_bump" == "patch" ]; then
poetry version patch
elif [ "$version_bump" == "minor" ]; then
poetry version minor
elif [ "$version_bump" == "major" ]; then
poetry version major
else
echo "Invalid version bump type ($version_bump). Skipping."
exit 0
fi
# Commit the version change
git add pyproject.toml
git commit -m "chore: Bump version ($version_bump)"
git push origin main

0 comments on commit 63e5d26

Please sign in to comment.