Last active
August 17, 2021 14:30
-
-
Save legendof-selda/83000a8b33c9d9562c05686045bb9b1a to your computer and use it in GitHub Desktop.
A quick version tagging script which uses git. Simply in cli run python _version.py {the method name}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
import sys | |
from typing import Union | |
BRANCH_TAG_MAP = { | |
"develop": "nightly", | |
"staging": "alpha", | |
"release": "alpha", | |
"master": None, | |
"main": None, | |
} | |
BRANCH_PREFIX = ["feature/", "hotfix/"] | |
def semver() -> Union[str, str, int]: | |
"""Creates the semver for current working branch | |
Returns: | |
Union[str, str, int]: Returns the version tag, suffix semver tag, commits ahead | |
""" | |
env = os.environ.copy() | |
output = subprocess.run( | |
"git describe --tags --long", | |
shell=True, | |
env=env, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
) | |
if output.returncode == 0: | |
output = output.stdout.decode("utf-8").strip() | |
else: | |
output = "v0.0.0-0-0" | |
output = output.split("-") | |
if len(output) <= 3: | |
post_tag = None | |
branch_name_output = subprocess.run( | |
"git branch --show-current", | |
shell=True, | |
env=env, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
) | |
if branch_name_output.returncode == 0: | |
post_tag = branch_name_output.stdout.decode("utf-8").strip() | |
prefix = list(filter(post_tag.startswith, BRANCH_PREFIX)) | |
if prefix: | |
post_tag = post_tag.replace(prefix[0], "") | |
if post_tag in BRANCH_TAG_MAP: | |
post_tag = BRANCH_TAG_MAP[post_tag] | |
commits = output[1] | |
else: | |
post_tag = output[1] | |
commits = output[2] | |
tag = output[0] | |
return tag, post_tag, int(commits) | |
def current_version() -> str: | |
"""The current version tag of the working branch | |
Returns: | |
str: Current version of working branch | |
""" | |
tag, post_tag, _ = semver() | |
return tag if post_tag is None else f"{tag}-{post_tag}" | |
def latest_version() -> str: | |
"""The latest version of the working branch which hasn't been tagged in git yet | |
Returns: | |
str: The latest version of the working branch | |
""" | |
tag, post_tag, patch = semver() | |
tag = tag.split(".") | |
patch = int(tag[-1]) + int(patch) | |
tag = ".".join(tag[:-1]) + "." + str(patch) | |
return tag if post_tag is None else f"{tag}-{post_tag}" | |
def update_version_tag() -> str: | |
"""Update the latest tag in git. Make sure to push the changed to origin. | |
Returns: | |
str: Current version of working branch after tagging | |
""" | |
tag = latest_version() | |
env = os.environ.copy() | |
subprocess.run(f'git tag -a {tag} -m "updated to new version {tag}"', shell=True, env=env) | |
return current_version() | |
def revert_version_tag() -> str: | |
"""Revert the current version tag to previous. Deleted the version tag in git. Make sure to push the changed to origin. | |
Returns: | |
str: Current version of working branch after removing tag | |
""" | |
tag = current_version() | |
env = os.environ.copy() | |
subprocess.run(f"git tag -d {tag}", shell=True, env=env) | |
return current_version() | |
def new_version(new_tag: str, update_patch: bool = True) -> str: | |
"""Creates new semver tag for new version. To be used in master branch | |
Args: | |
new_tag (str): The new release version | |
update_patch (bool, optional): Increments the latest patch number to the new version if True returns the new_tag as it is. Defaults to True. | |
Returns: | |
str: [description] | |
""" | |
tag = new_tag | |
if isinstance(update_patch, str): | |
update_patch = update_patch.lower() != "false" | |
if update_patch: | |
_, _, patch = semver() | |
tag = tag.split(".") | |
if len(tag) < 3 or tag[-1] == "": | |
tag = ".".join(tag[:-1]) + "." + str(patch) | |
else: | |
patch = int(tag[-1]) + int(patch) | |
tag = ".".join(tag[:-1]) + "." + str(patch) | |
return tag | |
def create_new_version_tag( | |
new_tag: str, | |
new_version_description: str = "Release Version", | |
update_patch: bool = True, | |
) -> str: | |
"""Tags new semver tag for new version in git. To be used in master branch | |
Args: | |
new_tag (str): The new release version | |
new_version_description (str, optional): A description for the new version tag. Defaults to "Release Version". | |
update_patch (bool, optional): Increments the latest patch number to the new version if True returns the new_tag as it is. Defaults to True. | |
Returns: | |
str: [description] | |
""" | |
tag = new_version(new_tag, update_patch) | |
env = os.environ.copy() | |
subprocess.run(f'git tag -a {tag} -m "{new_version_description} - {tag}"', shell=True, env=env) | |
return current_version() | |
def view_releases(): | |
env = os.environ.copy() | |
output = subprocess.run( | |
'git tag -l --sort=-version:refname "v*" -n3', | |
shell=True, | |
env=env, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
) | |
if output.returncode == 0: | |
return output.stdout.decode("utf-8") | |
else: | |
return None | |
def set_version_in_environemnt(env: str): | |
with open("./Dockerfile", "r+") as docker: | |
content = docker.read() | |
content.replace() | |
def get_remote_url(): | |
env = os.environ.copy() | |
output = subprocess.run( | |
"git config --get remote.origin.url", | |
shell=True, | |
env=env, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
) | |
if output.returncode == 0: | |
return output.stdout.decode("utf-8").replace(".git", "") | |
else: | |
return None | |
if __name__ == "__main__": | |
try: | |
function = sys.argv[1] | |
params = sys.argv[2:] | |
print(globals()[function](*params)) | |
except IndexError: | |
print(current_version()) | |
except KeyError: | |
raise Exception(f"Invalid argument {function}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment