|
| 1 | +"""This module provides dynamic versioning for the package, intended for use in GitHub Actions during a release event. |
| 2 | +
|
| 3 | +The version is extracted from the release tag in the GitHub event data. |
| 4 | +If this ins't a release, or isn't running in a GitHub runner, or something is missing, |
| 5 | +the script will fall back to version "0.0.1", to allow for testing. |
| 6 | +
|
| 7 | +Future Improvements: |
| 8 | +- I may either improve this to handle local building as well as merging into 'main' branch, |
| 9 | + or just go with setuptools_scm to replace all of this. |
| 10 | +""" |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import json |
| 14 | +import logging |
| 15 | +import os |
| 16 | + |
| 17 | + |
| 18 | +# NOTE: A single variable is set using this function below. |
| 19 | +def get_version(): |
| 20 | + """Retrieve the version from GitHub Actions environment variables. |
| 21 | +
|
| 22 | + Raises: |
| 23 | + RuntimeError: If not running in GitHub Actions or the event is not a release. |
| 24 | + FileNotFoundError: If the GitHub event path file is not found. |
| 25 | + json.JSONDecodeError: If the event data cannot be parsed as JSON. |
| 26 | + ValueError: If the release tag is not found in the event data. |
| 27 | +
|
| 28 | + Returns: |
| 29 | + str: The version extracted from the release tag. |
| 30 | + """ |
| 31 | + if os.getenv("GITHUB_ACTIONS") != "true": |
| 32 | + logging.error("Missing $GITHUB_ACTIONS environment variable. This script must (currently) be run in a GitHub Actions environment.") |
| 33 | + return "0.0.1" |
| 34 | + # May restrict the event type in the future. |
| 35 | + # if os.getenv("GITHUB_EVENT_NAME") != "release": |
| 36 | + # logging.error("Missing $GITHUB_EVENT_NAME environment variable or not a release event. This script must (currently) be triggered by a release event.") |
| 37 | + # return "0.0.1" |
| 38 | + |
| 39 | + event_path = os.getenv("GITHUB_EVENT_PATH") |
| 40 | + |
| 41 | + if not event_path: |
| 42 | + logging.error("Missing $GITHUB_EVENT_PATH environment variable. Unable to locate the GitHub event data file.") |
| 43 | + return "0.0.1" |
| 44 | + |
| 45 | + try: |
| 46 | + with open(event_path) as f: |
| 47 | + event_data = json.load(f) |
| 48 | + except FileNotFoundError as e: |
| 49 | + logging.exception("Error reading event data from file.") |
| 50 | + return "0.0.1" |
| 51 | + except json.JSONDecodeError as e: |
| 52 | + logging.exception("Error parsing event data as JSON.") |
| 53 | + return "0.0.1" |
| 54 | + |
| 55 | + # May restrict the action type in the future. |
| 56 | + # if event_data.get("action") != "published": |
| 57 | + # return "0.0.1" |
| 58 | + |
| 59 | + tag_name = event_data.get("release", {}).get("tag_name") |
| 60 | + |
| 61 | + if not tag_name: |
| 62 | + logging.error("No tag version found in event_data file.") |
| 63 | + return "0.0.1" |
| 64 | + |
| 65 | + return tag_name.lstrip("v") |
| 66 | + |
| 67 | + |
| 68 | +MODULE_VERSION = get_version() |
0 commit comments