Skip to content

Commit

Permalink
use plugin configuration (#398)
Browse files Browse the repository at this point in the history
* use plugin configuration

* precommit

* banditignore subprocess

* precommit

* nosec multiline and imports

* run publish on changes to version.toml

---------

Co-authored-by: Benjamin Morris <[email protected]>
Co-authored-by: Benjamin Morris <[email protected]>
  • Loading branch information
3 people authored Jul 2, 2024
1 parent 3fecc1b commit 3cbdd91
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 5 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/create_publish_pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# takes the most recent commit on main, bumps version based on
# semver_component input, and PRs change back to main
name: Bump version and PR

on:
workflow_dispatch:
inputs:
semver_component:
description: "Semantic versioning component to bump"
required: true
type: choice
default: "patch"
options:
- major
- minor
- patch
- dev

jobs:
publish:
name: Bump and PR
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
pull-requests: write

steps:
- uses: actions/checkout@v4
with:
ref: main

- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: "3.10"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install bumpver
- name: Bump version
run: |
git config --global user.name 'aicsgithub'
git config --global user.email '[email protected]'
python scripts/publish_bumpver_handler.py ${{ inputs.semver_component }}
# takes the commit from the last step, pushes to new branch, release, and creates PR
- name: Create pull request
uses: peter-evans/create-pull-request@v6
with:
branch: workflow-release
base: main
title: Bump version and publish
body: See commit message or diff for new version number

- name: Tag version
run: |
git checkout workflow-release
python scripts/tag_with_current_version.py
git push origin --tags
10 changes: 5 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
name: publish
on:
pull_request:
types: [closed]
branches: [main]
push:
branches:
- main
paths:
- version.toml
jobs:
publish:
# only publish when the PR is a version bump PR and the pr is merged to main
if: github.event.pull_request.title == 'admin/version-bump' && github.event.pull_request.merged == true
runs-on: ubuntu-latest
environment: release
permissions:
Expand Down
14 changes: 14 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,17 @@ exclude_lines = [
"raise NotImplementedError()",
"if __name__ == .__main__.:",
]

# https://pypi.org/project/bumpver
[tool.bumpver]
current_version = "0.2.1"
version_pattern = "MAJOR.MINOR.PATCH[.PYTAGNUM]"
commit_message = "Bump version {old_version} -> {new_version}"
commit = true
tag = false # no longer useful to tag here, must happen in create_publish_pr.yaml
push = false

[tool.bumpver.file_patterns]
"pyproject.toml" = ['current_version = "{version}"', 'version = "{version}"']
"version.toml" = ['version = "{version}"']
"cyto_dl/__init__.py" = ['__version__ = "{version}"']
95 changes: 95 additions & 0 deletions scripts/publish_bumpver_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# this file is intended to be called by a github workflow (.github/workflows/publish_to_pypi.yaml)
# it makes decisions based on the current version and the component specified for bumping,
# which the workflow cannot do

"""
TESTING:
- add and commit any changes (keep track of this commit hash)
- bumpver update --set-version 1.0.0
- python publish_bumpver_handler.py
- expect: ValueError
- python publish_bumpver_handler.py fake
- expect: ValueError
- python publish_bumpver_handler.py major
- expect: version updated to 2.0.0
- python publish_bumpver_handler.py minor
- expect: version updated to 2.1.0
- python publish_bumpver_handler.py patch
- expect: version updated to 2.1.1
- python publish_bumpver_handler.py dev
- expect: version updated to 2.1.2.dev0
- python publish_bumpver_handler.py dev
- expect: version updated to 2.1.2.dev1
- python publish_bumpver_handler.py major
- expect: ValueError
- python publish_bumpver_handler.py minor
- expect: ValueError
- python publish_bumpver_handler.py patch
- expect: version updated to 2.1.2
- git reset --hard {hash of the commit made at the beginning}
- git tag --delete 1.0.0 2.0.0 2.1.0 2.1.1 2.1.2 2.1.2.dev0 2.1.2.dev1
"""

import subprocess # nosec
import sys
from typing import List, Set

import toml


def main():
if len(sys.argv) < 2:
raise ValueError("No component specified for bumping version")

component: str = sys.argv[1].lower()
valid_options: Set[str] = {"major", "minor", "patch", "dev"}

if component not in valid_options:
raise ValueError(f"Component must be one of {valid_options}")

version: str = toml.load("pyproject.toml")["project"]["version"]
version_components: List[str] = version.split(".")

update_output: subprocess.CompletedProcess = None
# 4 components means we currently have a dev version
if len(version_components) == 4:
if component == "dev":
# increment the dev tag (e.g. 1.0.0.dev0 -> 1.0.0.dev1)
update_output = subprocess.run(["bumpver", "update", "--tag-num", "-n"]) # nosec
elif component == "patch":
# finalize the patch by removing dev tag (e.g. 1.0.0.dev1 -> 1.0.0)
update_output = subprocess.run(["bumpver", "update", "--tag=final", "-n"]) # nosec
else:
raise ValueError("Cannot update major or minor version while dev version is current")

elif len(version_components) == 3:
if component == "dev":
# increment patch and begin at dev0 (e.g. 1.0.0 -> 1.0.1.dev0)
update_output = subprocess.run( # nosec
["bumpver", "update", "--patch", "--tag=dev", "-n"]
)
else:
update_output = subprocess.run(["bumpver", "update", f"--{component}", "-n"]) # nosec

else:
raise ValueError(
f"Unknown version format: {version}. Expected MAJOR.MINOR.PATCH[.PYTAGNUM]"
)

if update_output.returncode != 0:
raise RuntimeError(f"bumpver exited with code {update_output.returncode}")


if __name__ == "__main__":
main()
17 changes: 17 additions & 0 deletions scripts/tag_with_current_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This file is intended to be called by a github workflow
import subprocess # nosec

import toml


def main():
version: str = toml.load("pyproject.toml")["project"]["version"]
tag_output: subprocess.CompletedProcess = subprocess.run( # nosec
["git", "tag", f"v{version}"]
)
if tag_output.returncode != 0:
raise RuntimeError("failed to tag")


if __name__ == "__main__":
main()
4 changes: 4 additions & 0 deletions version.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -----DO NOT MODIFY THIS FILE-----
# This file should only be modified by bumpver, which should in turn only be ran
# via a GH Action
version = "0.1.7"

0 comments on commit 3cbdd91

Please sign in to comment.