Skip to content

Commit

Permalink
[#patch] code smell
Browse files Browse the repository at this point in the history
  • Loading branch information
notdodo committed Dec 14, 2024
1 parent e778425 commit 6ccf54b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 73 deletions.
1 change: 0 additions & 1 deletion auto-tagger/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 7 additions & 13 deletions auto-tagger/github_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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()
94 changes: 51 additions & 43 deletions auto-tagger/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
16 changes: 1 addition & 15 deletions auto-tagger/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion auto-tagger/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 6ccf54b

Please sign in to comment.