diff --git a/auto-tagger/configuration.py b/auto-tagger/configuration.py index ad7390f..54b0f5d 100644 --- a/auto-tagger/configuration.py +++ b/auto-tagger/configuration.py @@ -12,7 +12,6 @@ class BumpStrategy(StrEnum): """Enum containing the different version bump strategy for semver""" - # pylint: disable=invalid-name MAJOR: str = "major" MINOR: str = "minor" PATCH: str = "patch" diff --git a/auto-tagger/github_helpers.py b/auto-tagger/github_helpers.py index cc1b839..25b51ca 100644 --- a/auto-tagger/github_helpers.py +++ b/auto-tagger/github_helpers.py @@ -7,7 +7,6 @@ from datetime import datetime, timedelta from typing import List -import requests from github import Github, InputGitAuthor from semver import Version, VersionInfo @@ -18,9 +17,9 @@ class GitHubHelper: """PyGitHub support class""" - def __init__(self, token, config) -> None: - self.token: str = token - self.config: Configuration = config + def __init__(self, token: str, config: Configuration) -> None: + self.token = token + self.config = config self.repo = Github(token).get_repo(self.config.REPOSITORY) self.last_available_tag = self.get_latest_tag() self.last_available_major_tag = self.get_latest_major_tag() @@ -150,14 +149,9 @@ def create_git_tag(self, tag: Tag) -> None: date=str(datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")), ), ) - self.__create_git_ref(f"refs/tags/{tag.name}", tag.commit) - - def __create_git_ref(self, ref_name: str, sha: str) -> None: - """Internal function to create the reference on GitHub""" - self.repo.create_git_ref(ref_name, sha) + self.repo.create_git_ref(f"refs/tags/{tag.name}", tag.commit) def delete_git_tag(self, tag_name: str) -> None: - """Custom function to delete a tag on the repository""" - headers = {"Authorization": f"Bearer {self.token}"} - url = f"https://api.github.com/repos/{self.config.REPOSITORY}/git/refs/tags/{tag_name}" - requests.delete(url, headers=headers, timeout=10) + """Delete a tag on the repository""" + ref = self.repo.get_git_ref(f"tags/{tag_name}") + ref.delete() diff --git a/auto-tagger/main.py b/auto-tagger/main.py index b856a9b..4c87079 100644 --- a/auto-tagger/main.py +++ b/auto-tagger/main.py @@ -10,55 +10,63 @@ from configuration import BumpStrategy, Configuration from github_helpers import GitHubHelper -config = Configuration.from_env() -if config.DRY_RUN: - print("Running in dry-run mode!") +def main(): + config = Configuration.from_env() -if os.environ.get("GITHUB_REF_NAME") != config.DEFAULT_BRANCH: - print("Not running from the default branch") - sys.exit() + if config.DRY_RUN: + print("Running in dry-run mode!") -github_helper = GitHubHelper(os.environ.get("INPUT_GITHUB_TOKEN", ""), config) -last_tag = github_helper.last_available_tag + if os.environ.get("GITHUB_REF_NAME") != config.DEFAULT_BRANCH: + print("Not running from the default branch") + sys.exit() -print(f"Last available tag: {last_tag}") -bump_strategy = config.get_bump_strategy_from_commits( - github_helper.get_commits_since(last_tag.date) -) + github_helper = GitHubHelper(os.environ.get("INPUT_GITHUB_TOKEN", ""), config) + last_tag = github_helper.last_available_tag -if bump_strategy == BumpStrategy.SKIP: - print("No need to create a new tag, skipping") - sys.exit() + print(f"Last available tag: {last_tag}") + bump_strategy = config.get_bump_strategy_from_commits( + github_helper.get_commits_since(last_tag.date) + ) -new_tag = github_helper.bump_tag_version(bump_strategy, last_tag) -print(f"Creating new tag version: {new_tag}") -github_helper.create_git_tag(new_tag) + if bump_strategy == BumpStrategy.SKIP: + print("No need to create a new tag, skipping") + sys.exit() -if config.BIND_TO_MAJOR: - last_major_tag = github_helper.last_available_major_tag - last_major_tag.commit = os.environ.get( - "GITHUB_SHA", github_helper.get_last_commit().sha - ) - last_major_tag.message = os.environ.get( - "GITHUB_SHA", github_helper.get_last_commit().message - ) - if bump_strategy != BumpStrategy.MAJOR: - github_helper.delete_git_tag(last_major_tag.name) - print( - f"Binding major tag {last_major_tag} to latest commit: {last_major_tag.commit}" + new_tag = github_helper.bump_tag_version(bump_strategy, last_tag) + print(f"Creating new tag version: {new_tag}") + github_helper.create_git_tag(new_tag) + + if config.BIND_TO_MAJOR: + last_major_tag = github_helper.last_available_major_tag + last_major_tag.commit = os.environ.get( + "GITHUB_SHA", github_helper.get_last_commit().sha ) - github_helper.create_git_tag(last_major_tag) - else: - new_major_tag_name = ( - config.PREFIX - + str( - Version.parse( - new_tag.name.removeprefix(config.PREFIX).removesuffix(config.SUFFIX) - ).major - ) - + config.SUFFIX + last_major_tag.message = os.environ.get( + "GITHUB_SHA", github_helper.get_last_commit().message ) - last_major_tag.name = new_major_tag_name - print(f"Creating new major tag {last_major_tag}") - github_helper.create_git_tag(last_major_tag) + if bump_strategy != BumpStrategy.MAJOR: + github_helper.delete_git_tag(last_major_tag.name) + print( + f"Binding major tag {last_major_tag} to latest commit: {last_major_tag.commit}" + ) + github_helper.create_git_tag(last_major_tag) + else: + new_major_tag_name = ( + config.PREFIX + + str( + Version.parse( + new_tag.name.removeprefix(config.PREFIX).removesuffix( + config.SUFFIX + ) + ).major + ) + + config.SUFFIX + ) + last_major_tag.name = new_major_tag_name + print(f"Creating new major tag {last_major_tag}") + github_helper.create_git_tag(last_major_tag) + + +if __name__ == "__main__": + main() diff --git a/auto-tagger/poetry.lock b/auto-tagger/poetry.lock index 21cc73f..61f05a7 100644 --- a/auto-tagger/poetry.lock +++ b/auto-tagger/poetry.lock @@ -917,20 +917,6 @@ files = [ {file = "tomlkit-0.13.2.tar.gz", hash = "sha256:fff5fe59a87295b278abd31bec92c15d9bc4a06885ab12bcea52c71119392e79"}, ] -[[package]] -name = "types-requests" -version = "2.32.0.20241016" -description = "Typing stubs for requests" -optional = false -python-versions = ">=3.8" -files = [ - {file = "types-requests-2.32.0.20241016.tar.gz", hash = "sha256:0d9cad2f27515d0e3e3da7134a1b6f28fb97129d86b867f24d9c726452634d95"}, - {file = "types_requests-2.32.0.20241016-py3-none-any.whl", hash = "sha256:4195d62d6d3e043a4eaaf08ff8a62184584d2e8684e9d2aa178c7915a7da3747"}, -] - -[package.dependencies] -urllib3 = ">=2" - [[package]] name = "typing-extensions" version = "4.12.2" @@ -1036,4 +1022,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "888e5d49e41738fcf430e781841ff1d460ef4149e8db628be5076048c7b84e80" +content-hash = "bc250b5a38d0f05bbcd3370ee3a03b8350537f46189d4ef9ffb7d013e051cbd2" diff --git a/auto-tagger/pyproject.toml b/auto-tagger/pyproject.toml index 4641e06..b7d1102 100644 --- a/auto-tagger/pyproject.toml +++ b/auto-tagger/pyproject.toml @@ -18,7 +18,6 @@ mypy = { extras = ["faster-cache"], version = "^1.13.0" } isort = "^5.13.2" pydantic = "^2.10.3" pylint = "^3.3.2" -types-requests = "^2.32.0.20241016" [tool.isort] profile = "black"