Skip to content

Commit

Permalink
ci(release): multiple tweaks and fixes (#38)
Browse files Browse the repository at this point in the history
* add version.txt and modify update_version.py to support it
* use packaging.version.Version in update_version.py
* simplify version.py format (drop intermediate vars)
* remove post-release reset PR step from release.yml
* fix changelog reference in release.yml
  • Loading branch information
wpbonelli authored Feb 8, 2024
1 parent 13739a7 commit 76031f3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 141 deletions.
70 changes: 1 addition & 69 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ jobs:
run: |
version=$(python scripts/update_version.py -g)
title="modflowapi $version"
notes=$(cat "changelog/CHANGELOG_$version.md" | grep -v "### Version $version")
notes=$(cat "changelog/CHANGELOG.md" | grep -v "### Version $version")
gh release create "$version" \
--target main \
--title "$title" \
Expand Down Expand Up @@ -207,71 +207,3 @@ jobs:

- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1

reset:
name: Draft reset PR
if: ${{ github.event_name == 'release' }}
runs-on: ubuntu-22.04
permissions:
contents: write
pull-requests: write
steps:

- name: Checkout main branch
uses: actions/checkout@v3
with:
ref: main

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.8
cache: 'pip'
cache-dependency-path: pyproject.toml

- name: Install Python dependencies
run: |
pip install --upgrade pip
pip install .
pip install ".[lint, test]"
- name: Get release tag
uses: oprypin/find-latest-tag@v1
id: latest_tag
with:
repository: ${{ github.repository }}
releases-only: true

- name: Draft pull request
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
# create reset branch from main
reset_branch="post-release-${{ steps.latest_tag.outputs.tag }}-reset"
git switch -c $reset_branch
# increment minor version
major_version=$(echo "${{ steps.latest_tag.outputs.tag }}" | cut -d. -f1)
minor_version=$(echo "${{ steps.latest_tag.outputs.tag }}" | cut -d. -f2)
patch_version=0
version="$major_version.$((minor_version + 1)).$patch_version"
python scripts/update_version.py -v "$version"
# format Python files
python scripts/pull_request_prepare.py
# commit and push reset branch
git config core.sharedRepository true
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "ci(release): update to development version $version"
git push -u origin $reset_branch
# create PR into develop
body='
# Reinitialize for development
Updates the `develop` branch from `main` following a successful release. Increments the minor version number.
'
gh pr create -B "develop" -H "$reset_branch" --title "Reinitialize develop branch" --draft --body "$body"
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ message: If you use this software, please cite both the article from preferred-c
and the software itself.
type: software
title: MODFLOW API
version: 0.2.0
version: 0.2.0.dev0
date-released: '2023-04-19'
abstract: An extension to xmipy for the MODFLOW API.
repository-artifact: https://pypi.org/project/modflowapi
Expand Down
9 changes: 3 additions & 6 deletions modflowapi/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# version file for modflowapi

major = 0
minor = 2
micro = 0
__version__ = f"{major}.{minor}.{micro}"
# modflowapi version file automatically created using...update_version.py
# created on...February 07, 2024 22:13:12
__version__ = "0.2.0.dev0"
89 changes: 24 additions & 65 deletions scripts/update_version.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,51 @@
import argparse
import textwrap
from datetime import datetime
from enum import Enum
from os import PathLike
from os.path import basename
from pathlib import Path
from typing import NamedTuple
from packaging.version import Version

from filelock import FileLock

_project_name = "modflowapi"
_project_root_path = Path(__file__).parent.parent
_version_txt_path = _project_root_path / "version.txt"
_version_py_path = _project_root_path / "modflowapi" / "version.py"
_citation_cff_path = _project_root_path / "CITATION.cff"

_initial_version = Version("0.0.1")
_current_version = Version(_version_txt_path.read_text().strip())

class Version(NamedTuple):
"""Semantic version number"""

major: int = 0
minor: int = 0
patch: int = 0
def log_update(path, version: Version):
print(f"Updated {path} with version {version}")

def __repr__(self):
return f"{self.major}.{self.minor}.{self.patch}"

@classmethod
def from_string(cls, version: str) -> "Version":
t = version.split(".")

vmajor = int(t[0])
vminor = int(t[1])
vpatch = int(t[2])

return cls(major=vmajor, minor=vminor, patch=vpatch)

@classmethod
def from_file(cls, path: PathLike) -> "Version":
lines = [
line.rstrip("\n")
for line in open(Path(path).expanduser().absolute(), "r")
]
vmajor = vminor = vpatch = None
for line in lines:
line = line.strip()
if not any(line):
continue

def get_ver(l):
return l.split("=")[1]

if "__version__" not in line:
if "major" in line:
vmajor = int(get_ver(line))
elif "minor" in line:
vminor = int(get_ver(line))
elif "patch" in line or "micro" in line:
vpatch = int(get_ver(line))

assert (
vmajor is not None and vminor is not None and vpatch is not None
), "version string must follow semantic version format: major.minor.patch"
return cls(major=vmajor, minor=vminor, patch=vpatch)


_initial_version = Version(0, 0, 1)
_current_version = Version.from_file(_version_py_path)
def update_version_txt(version: Version):
with open(_version_txt_path, "w") as f:
f.write(str(version))
log_update(_version_txt_path, version)


def update_version_py(timestamp: datetime, version: Version):
with open(_version_py_path, "w") as f:
f.write(
f"# {_project_name} version file automatically created using "
f"{Path(__file__).name} on {timestamp:%B %d, %Y %H:%M:%S}\n\n"
f"# {_project_name} version file automatically "
+ f"created using...{basename(__file__)}\n"
)
f.write(f"major = {version.major}\n")
f.write(f"minor = {version.minor}\n")
f.write(f"micro = {version.patch}\n")
f.write("__version__ = f'{major}.{minor}.{micro}'\n")
print(f"Updated {_version_py_path} to version {version}")
f.write("# created on..." + f"{timestamp.strftime('%B %d, %Y %H:%M:%S')}\n")
f.write(f'__version__ = "{version}"\n')
log_update(_version_py_path, version)


def update_citation_cff(timestamp: datetime, version: Version):
def update_citation_cff(version: Version):
lines = open(_citation_cff_path, "r").readlines()
with open(_citation_cff_path, "w") as f:
for line in lines:
if line.startswith("version:"):
line = f"version: {version}\n"
f.write(line)
print(f"Updated {_citation_cff_path} to version {version}")
log_update(_citation_cff_path, version)


def update_version(
Expand All @@ -97,16 +55,17 @@ def update_version(
lock_path = Path(_version_py_path.name + ".lock")
try:
lock = FileLock(lock_path)
previous = Version.from_file(_version_py_path)
previous = Version(_version_txt_path.read_text().strip())
version = (
version
if version
else Version(previous.major, previous.minor, previous.patch)
)

with lock:
update_version_txt(version)
update_version_py(timestamp, version)
update_citation_cff(timestamp, version)
update_citation_cff(version)
finally:
try:
lock_path.unlink()
Expand Down Expand Up @@ -148,7 +107,7 @@ def update_version(
else:
update_version(
timestamp=datetime.now(),
version=Version.from_string(args.version)
if args.version
else _current_version,
version=(
Version(args.version) if args.version else _current_version
),
)
1 change: 1 addition & 0 deletions version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2.0.dev0

0 comments on commit 76031f3

Please sign in to comment.