From 86d01331877212baf2081dbddc37f3de20983b56 Mon Sep 17 00:00:00 2001 From: Marco Donadoni Date: Thu, 8 Feb 2024 16:40:21 +0100 Subject: [PATCH] fix(reana-dev): create commits that conform to conventional style (#777) --- reana/config.py | 6 ++++++ reana/reana_dev/git.py | 17 ++++++++++++----- reana/reana_dev/utils.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/reana/config.py b/reana/config.py index 9da9bf6b..4f26000b 100644 --- a/reana/config.py +++ b/reana/config.py @@ -8,6 +8,9 @@ """``reana-dev`` CLI configuration.""" +import re + + REPO_LIST_DEMO_RUNNABLE = [ "reana-demo-alice-lego-train-test-run", "reana-demo-alice-pt-analysis", @@ -298,3 +301,6 @@ PYTHON_DOCKER_IMAGE = "docker.io/library/python:3.8" """Python docker image with the same version as cluster components.""" + +RELEASE_COMMIT_REGEX = re.compile("^(release:|chore.*: release)") +"""Regex to find out if commit message refers to a new release.""" diff --git a/reana/reana_dev/git.py b/reana/reana_dev/git.py index e0d2b67f..9b2e1a7c 100644 --- a/reana/reana_dev/git.py +++ b/reana/reana_dev/git.py @@ -24,6 +24,7 @@ OPENAPI_VERSION_FILE, PYTHON_REQUIREMENTS_FILE, PYTHON_VERSION_FILE, + RELEASE_COMMIT_REGEX, REPO_LIST_ALL, REPO_LIST_PYTHON_REQUIREMENTS, REPO_LIST_SHARED, @@ -46,6 +47,7 @@ update_module_in_cluster_components, upgrade_requirements, validate_directory, + get_commit_pr_suffix, ) @@ -163,7 +165,7 @@ def git_create_release_commit( next_version: Optional[str] = None, ) -> bool: """Create a release commit for the given component.""" - if "release:" in get_current_commit(get_srcdir(component)): + if is_last_commit_release_commit(component): display_message("Nothing to do, last commit is a release commit.", component) return False @@ -199,8 +201,11 @@ def git_create_release_commit( if modified_files: run_command(f"git add {' '.join(modified_files)}", component) + commit_msg = ( + f"chore({base}): release {next_version}{get_commit_pr_suffix(component)}" + ) run_command( - f"git commit -m 'release: {next_version}' {'--allow-empty' if not modified_files else ''}", + f"git commit -m '{commit_msg}' {'--allow-empty' if not modified_files else ''}", component, ) return True @@ -289,7 +294,8 @@ def print_branch_difference_report( def is_last_commit_release_commit(package): """Check whether the last commit is a release commit.""" current_commit = get_current_commit(get_srcdir(package)) - return current_commit.split()[1] == "release:" + commit_msg = current_commit.split(maxsplit=1)[1] + return RELEASE_COMMIT_REGEX.match(commit_msg) def git_push_to_origin(components): @@ -1280,7 +1286,7 @@ def git_upgrade_shared_modules( def _create_commit_or_amend(components): for c in components: - commit_cmd = 'git commit -m "installation: bump shared modules"' + commit_cmd = f'git commit -m "build(python): bump shared modules{get_commit_pr_suffix(component)}"' if amend: commit_cmd = "git commit --amend --no-edit" @@ -1365,7 +1371,8 @@ def git_upgrade_requirements(ctx, component, exclude_components): # noqa: D301 if upgrade_requirements(component): run_command(f"git add {PYTHON_REQUIREMENTS_FILE}", component) run_command( - 'git commit -m "installation: bump all dependencies"', component + f'git commit -m "build(python): bump all dependencies{get_commit_pr_suffix(component)}"', + component, ) diff --git a/reana/reana_dev/utils.py b/reana/reana_dev/utils.py index 364e977e..a05cc1b1 100644 --- a/reana/reana_dev/utils.py +++ b/reana/reana_dev/utils.py @@ -968,3 +968,32 @@ def validate_directory(ctx, param, target_directory): click.echo(click.style(message, fg="red"), err=True) ctx.exit(1) return target_directory + + +def get_next_available_issue_pr_number(component): + """Get the next available number for issues/PRs.""" + last_used = 0 + for type_ in ("pr", "issue"): + res = json.loads( + run_command( + f"gh {type_} list --state all --limit 1 --json number", + component=component, + display=False, + return_output=True, + ) + ) + if res: + last_used = max(last_used, res[0]["number"]) + + return last_used + 1 + + +def get_commit_pr_suffix(component): + """Get the commit message suffix containing the expected PR number.""" + pr_number_suffix = "" + try: + pr_number = get_next_available_issue_pr_number(component) + pr_number_suffix = f" (#{pr_number})" + except Exception as e: + display_message(f"Could not find next available PR number: {e}", component) + return pr_number_suffix