Skip to content

Commit

Permalink
Update implementation of git-based tags
Browse files Browse the repository at this point in the history
  • Loading branch information
shreve committed Aug 6, 2024
1 parent 364e182 commit 353ddf3
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/mads/cli/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
github,
kube,
setup,
tag,
yq,
)

Expand All @@ -17,5 +18,6 @@
"github",
"kube",
"setup",
"tag",
"yq",
]
37 changes: 37 additions & 0 deletions src/mads/cli/commands/tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Generate an artifact tag based on the git environment"""

import argparse

from mads.environ.git import Git


def register_subcommand(parser: argparse.ArgumentParser):
"""Register the environ command"""

def run(args):
"""Run the query"""

git = Git()

print(
git.artifact_tag(
*args.prefix,
use_branch=args.use_branch,
default=args.default,
)
)

parser.set_defaults(func=run)
parser.add_argument("prefix", nargs="*", help="Prefix for the tag")
parser.add_argument(
"--use-branch",
action="store_true",
help="Use the branch name rather than a semantic equivalent",
default=False,
)

parser.add_argument(
"--default",
default="dev",
help="Default tag to use if no branch is found",
)
32 changes: 29 additions & 3 deletions src/mads/environ/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def git_config_source() -> dict[str, str]:

try:
# Given a commit hash, it should always be easy to get the contents
data = shell(f"git show -s --format='%s;;;%ae' {config['commit']}")
data = shell(f"git show -s --format='%s;;;%ae' {config['commit']}", silent=True)
config["message"], config["author"] = data.split(";;;")
except (CalledProcessError, KeyError, ValueError):
pass
Expand All @@ -62,5 +62,31 @@ class Git(BaseSettings):
branch: str | None = None

@classmethod
def settings_customise_sources(cls, *args, **kwargs) -> tuple:
return (git_config_source,)
def settings_customise_sources(cls, *args, init_settings, **kwargs) -> tuple:
return (init_settings, git_config_source)

def artifact_tag(
self,
*prefix: str,
use_branch: bool = False,
default: str = "dev",
) -> str:
"""
Use the git status to determine the tag we should be using
"""

bits = [*prefix] if prefix else []

if not self.branch:
bits.append(default)
elif self.branch in ["master", "main"]:
if not prefix:
bits.append(self.branch if use_branch else "latest")
elif self.branch in ["beta"]:
bits.append(self.branch if use_branch else "beta")
elif use_branch:
bits.append(self.branch)
else:
bits.append(default)

return "-".join(bits)
37 changes: 37 additions & 0 deletions test/mads/environ/test_git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from mads.environ.git import Git


def test_artifact_tag_main_branch():
g = Git(branch="main")

assert g.branch == "main"

# On the main branch, the default behavior is to return "latest"
assert g.artifact_tag() == "latest"

# If using a prefix, the main branch behavior includes no suffix
assert g.artifact_tag("feature") == "feature"

# If use_branch, prefer the branch name over "latest"
assert g.artifact_tag(use_branch=True) == "main"

# If use_branch and a prefix, include the branch name instead of omitting
assert g.artifact_tag("feature", use_branch=True) == "feature"


def test_artifact_tag_feature_branch():
g = Git(branch="feature/123")

assert g.branch == "feature/123"

# On a feature branch, the default behavior is to return "dev"
assert g.artifact_tag() == "dev"

# If using a prefix, the feature branch behavior includes the prefix
assert g.artifact_tag("feature") == "feature-dev"

# If use_branch, prefer the branch name over "dev"
assert g.artifact_tag(use_branch=True) == "feature/123"

# If use_branch and a prefix, include the branch name instead of omitting
assert g.artifact_tag("feature", use_branch=True) == "feature-feature/123"

0 comments on commit 353ddf3

Please sign in to comment.