From 79d3d9626454811956098e8b491a0add0fde905f Mon Sep 17 00:00:00 2001 From: taleksovska Date: Fri, 5 Apr 2024 10:48:02 +0200 Subject: [PATCH] Modify version() and release() functions --- enabler/commands/cmd_platform.py | 134 ++++++++++++++----------------- enabler/helpers/git.py | 23 +++--- 2 files changed, 71 insertions(+), 86 deletions(-) diff --git a/enabler/commands/cmd_platform.py b/enabler/commands/cmd_platform.py index b573b68..f8303e4 100644 --- a/enabler/commands/cmd_platform.py +++ b/enabler/commands/cmd_platform.py @@ -147,27 +147,59 @@ def keys(ctx, kube_context_cli, bits): @click.argument('version', type=semver.BasedVersionParamType(), required=True) -@click.argument('submodules', - required=True, - default='all') +@click.argument('submodule', + required=True) @click.argument('repopath', required=True, type=click.Path(exists=True), default=os.getcwd()) @click.pass_context @pass_environment -def release(ctx, kube_context_cli, version, submodules, repopath): +def release(ctx, kube_context_cli, version, submodule, repopath): """Release platform by tagging platform repo and tagging all individual components (git submodules) using their respective SHA that the submodules point at""" # Get the repo from arguments defaults to cwd repo = get_repo(repopath) - submodules = get_submodules(repo, submodules) - # TODO: Tag platform and all submodules at their respective SHAs - pass - # TODO: beautify output, check if remotes are ahead, warn anti-patern + # Check if submodule exists in the repository + if submodule not in repo.submodules: + click.echo(f"Submodule '{submodule}' not found in the repository.") + return + + # Tag platform at provided version + platform_tag_name = f"v{version}" + tag_result = tag_repo(repo, platform_tag_name) + + if tag_result: + click.echo(f"Platform tagged with version {platform_tag_name}") + else: + click.echo("Failed to tag platform") + + submodule_path = os.path.join(repo.working_dir, submodule) + submodule_repo = git.Repo(submodule_path) + submodule_sha = submodule_repo.head.commit.hexsha + submodule_tag_name = f"{submodule}-{platform_tag_name}" + tag_result = tag_repo(submodule_repo, submodule_tag_name, submodule_sha) + if tag_result: + click.echo(f"{submodule} tagged with version {platform_tag_name}") + else: + click.echo(f"Failed to tag {submodule} at {submodule_sha}") + + +def tag_repo(repo, tag_name, commit_sha=None): + try: + if commit_sha: + repo.create_tag(tag_name, ref=commit_sha) + else: + repo.create_tag(tag_name) + return True + except git.GitCommandError as e: + if "already exists" in str(e): + return True # Tag already exists + logger.error(f"Error tagging repository: {e}") + return False @cli.command('version', short_help='Get all versions of components') @@ -182,74 +214,30 @@ def release(ctx, kube_context_cli, version, submodules, repopath): @pass_environment def version(ctx, kube_context_cli, submodules, repopath): """Check versions of microservices in git submodules - You can provide a comma separated list of submodules + You can provide a comma-separated list of submodules or you can use 'all' for all submodules""" # Get the repo from arguments defaults to cwd try: repo = get_repo(repopath) + logger.info("REPO") + logger.info(repo) submodules = get_submodules(repo, submodules) - except Exception as e: - logger.error(f'An error occurred while getting {submodule}: {e}'.format(submodule, e)) # noqa - - # Do something with the submodules - all_sm_details = [] - with click_spinner.spinner(): - for submodule in submodules: - logger.debug('Switched to submodule: ' + submodule) - sm_details = {} - sm_details['repo'] = submodule - # Are we on an active branch? on a tag? if not then get sha? - try: - smrepo = git.Repo(submodule) - sm_details['present'] = True - except git.InvalidGitRepositoryError as error: # noqa - logger.warning(submodule + ': not present') - sm_details['present'] = False - all_sm_details.append(sm_details) - continue - - # Get branch - try: - branch = smrepo.active_branch.name - sm_details['branch'] = branch - - # Check if remotes are ahead or behind - origin = smrepo.remotes.origin - origin.fetch() - commits_behind = smrepo.iter_commits(branch + - '..origin/' + branch) - commits_ahead = smrepo.iter_commits('origin/' + branch + - '..' + branch) - sm_details['commits_ahead'] = sum(1 for c in commits_ahead) - sm_details['commits_behind'] = sum(1 for c in commits_behind) - except TypeError as error: - if smrepo.head.is_detached: - commit = smrepo.head.commit.hexsha - sm_details['branch'] = 'HEAD detached at ' + str(commit) - else: - logger.error(error) - - # Check if we point to any tags - points_at_tag = smrepo.git.tag('--points-at', 'HEAD') - sm_details['tag'] = points_at_tag - - # Get sha of HEAD - sha = smrepo.head.commit.hexsha - sm_details['sha'] = sha - - # Add submodule details to the list - all_sm_details.append(sm_details) - - for sm_details in all_sm_details: - logger.info(sm_details['repo'] + ':') - if 'branch' in sm_details: - logger.info(u'\u2023' + ' Branch: ' + sm_details['branch']) - logger.info(u'\u2023' + ' SHA: ' + sm_details['sha']) - if 'tag' in sm_details: - logger.info(u'\u2023' + ' Tag: ' + str(sm_details['tag'])) - if 'commits_ahead' in sm_details and sm_details['commits_ahead'] > 0: - logger.info(u'\u2023' + ' Ahead by: ' + str(sm_details['commits_ahead']) + ' commits') # noqa - - if 'commits_behind' in sm_details and sm_details['commits_behind'] > 0: - logger.info(u'\u2023' + ' Behind by: ' + str(sm_details['commits_behind']) + ' commits') # noqa \ No newline at end of file + except Exception as e: # noqa + logger.info("An error occurred while getting submodule") + + version_info = [] + # Retrieve version information for each submodule + for submodule in submodules: + submodule_path = os.path.join(repo.working_dir, submodule) + try: + smrepo = git.Repo(submodule_path) + tags = smrepo.tags + # Choose the latest tag as version + latest_tag = max(tags, key=lambda t: t.commit.committed_datetime) + version_info.append((submodule, latest_tag.name)) + except Exception as e: # noqa + version_info.append((submodule, "Error retrieving version")) + + for submodule, version in version_info: + logger.info(f"{submodule}: {version}") diff --git a/enabler/helpers/git.py b/enabler/helpers/git.py index 421625c..660feee 100644 --- a/enabler/helpers/git.py +++ b/enabler/helpers/git.py @@ -1,31 +1,28 @@ from enabler.cli import logger import click import git +import os def get_repo(repopath): + """Function to get the repository.""" + if not os.path.exists(repopath): + logger.critical('The repo path ' + repopath + ' does not exist') + raise click.Abort() + try: - return git.Repo(repopath, odbt=git.GitDB) + return git.Repo(repopath, odbt=git.GitDB, search_parent_directories=True) # noqa except git.InvalidGitRepositoryError: logger.critical('The repo path ' + repopath + ' is not a git repo') raise click.Abort() def get_submodules(repo, submodules): - # Based on provided submodules through arguments set the repo objects - # that we want to work with + """Function to get submodules.""" if submodules == 'all': - submodules = repo.submodules - submodule_list = [] - for submodule in submodules: - submodule_list.append(submodule.name) - submodules = submodule_list + submodules = [submodule.name for submodule in repo.submodules] else: submodules = submodules.split(',') - submodule_list = [] - for submodule in submodules: - submodule_list.append('platform/' + submodule) - submodules = submodule_list logger.debug('The provided submodules are:') logger.debug(submodules) - return (submodules) + return submodules