From 37412ee1af5fbda9108f4096d0a08800058214d8 Mon Sep 17 00:00:00 2001 From: PhilNewm Date: Mon, 16 Sep 2024 16:44:31 +0200 Subject: [PATCH 1/5] Refactor cli calls --- action.yml | 9 +--- github_query.py | 112 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 96 insertions(+), 25 deletions(-) diff --git a/action.yml b/action.yml index c50d6d4..32a38dc 100644 --- a/action.yml +++ b/action.yml @@ -55,12 +55,7 @@ runs: shell: bash run: | cd $GITHUB_ACTION_PATH - label_list=$(python -c 'import github_query; print(github_query.get_labels(github_query.parse_args()))' "${{ inputs.repo }}" "${{ inputs.query_parameters }}" "${{ inputs.date }}") - if [[ "$label_list" == '[]' ]]; then - echo "label_list=''" >> $GITHUB_OUTPUT - exit 0 - fi - + label_list=$(python github_query.py pr-labels "${{ inputs.repo }}" "${{ inputs.query_parameters }}" "${{ inputs.date }}") echo "label_list=$label_list" >> $GITHUB_OUTPUT - name: Get version increment @@ -68,7 +63,7 @@ runs: shell: bash run: | cd $GITHUB_ACTION_PATH - increment=$(python -c 'import github_query; pr_labels = github_query.get_labels(github_query.parse_args()); patch_repo_var = github_query.get_repo_var(repo="${{ inputs.repo }}", var_name="PATCH_BUMP_LABEL"); minor_repo_var = github_query.get_repo_var(repo="${{ inputs.repo }}", var_name="MINOR_BUMP_LABEL"); print(github_query.get_version_increment(patch_bump_list=patch_repo_var, minor_bump_list=minor_repo_var, pr_label_list=pr_labels))' "${{ inputs.repo }}" "${{ inputs.query_parameters }}" "${{ inputs.date }}") + increment=$(python github_query.py version-increment "${{ inputs.repo }}" "${{ inputs.query_parameters }}" "${{ inputs.date }}") echo "increment=$increment" >> $GITHUB_OUTPUT # disabled until fixed diff --git a/github_query.py b/github_query.py index cec0841..a297869 100644 --- a/github_query.py +++ b/github_query.py @@ -4,7 +4,7 @@ Additonally it's test suite relies mainly on putest and therefore the functions need to be importable to the pytest script. """ -import argparse +import click import json import logging import re @@ -13,24 +13,34 @@ logger = logging.getLogger(__name__) -def parse_args() -> dict: - """Parse command-line arguments and store them in a global variable.""" +def query_merged_prs(latest_release_date, query_tags, repo_name): + """Run gh pull request query. - parser = argparse.ArgumentParser(description="A python script to convert GitHub PR information to a more simple format.") - parser.add_argument("repo", type=str, help="Repository name consisting of 'repo-owner/repo-name'") - parser.add_argument("query_parameters", type=str, help="Keys to query for.") - parser.add_argument("date", type=str, default="2024-07-08T09:48:33Z", help="Latest release date.") - parsed_args = parser.parse_args() + Args: + latest_release_date (str): datatime string + query_tags (str): csv string + repo_name (str): repo name as - repo_name = parsed_args.repo - query_tags = parsed_args.query_parameters.split(',') - latest_release_date = parsed_args.date + Returns: + dict: json-dictionary. + """ - command = f"gh pr list --state merged --search 'merged:>={latest_release_date}' --json {','.join(query_tags)} --repo {repo_name}" - pr_json = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + pr_list = subprocess.run( + [ + "gh", "pr", "list", + "--state", "merged", + "--search", f'merged:>={latest_release_date}', + "--json", ','.join(query_tags), + "--repo", repo_name + ], + capture_output=True, + text=True, + check=True + ) - return json.loads(pr_json.stdout) + return json.loads(pr_list.stdout) +# INFO not in use def get_changelog(pr_data, changelog_start="## Changelog", heading="##"): """Get list of changes from a PRs changelog. @@ -60,6 +70,7 @@ def get_changelog(pr_data, changelog_start="## Changelog", heading="##"): return changelog_lines +# INFO not in use def changelog_per_label(json_dict): # TODO replace with labels fetched from repo variables changelog_labels = ["bugfix", "enhancement", "feature"] @@ -69,6 +80,7 @@ def changelog_per_label(json_dict): if any(item in changelog_labels for item in labels): pass +# INFO not in use def prepare_changelog_markdown(pr_query, minor_bump_list, patch_bump_list): # ? should version bump labels also be filter for changelog ? label_list = minor_bump_list + patch_bump_list @@ -96,19 +108,22 @@ def get_labels(pr_data: dict) -> list: pr_data (dict): Github PR query result Returns: - [str]: Liste of unique labels strings found or `None`. + list: Liste of unique labels strings found or `None`. """ labels = set() for item in pr_data: if not item.get("labels"): + logger.warning("No PR label data found.") return [] for label in item["labels"]: if not label.get("name"): + logger.warning("No PR label names found.") return [] labels.add(label["name"]) + logger.debug("PR labels found.") return list(labels) @@ -120,7 +135,7 @@ def get_repo_var(repo: str, var_name: str) -> list: var_name (str): Repo variable name Returns: - str: Comma separated value string. + str: csv-string. """ labels = subprocess.run( ["gh", "variable", "get", var_name, "--repo", repo], @@ -129,9 +144,18 @@ def get_repo_var(repo: str, var_name: str) -> list: check=True ) - return csv_string_to_list(labels.stdout) + return labels.stdout def csv_string_to_list(input: str) -> list: + """Covnert string to list. + + Args: + input (str): Expected csv string. + + Returns: + list: List of strings. + """ + if input: return re.split(r',\s*', input.strip()) @@ -143,7 +167,7 @@ def get_version_increment(patch_bump_list: list, minor_bump_list: list, pr_label Args: patch_bump_list ([str]): Labels for bumping patch version minor_bump_list ([str]): Labels for bumping minor version - label_list([str]): Labels found in PRs + label_list ([str]): Labels found in PRs Returns: str: version increment @@ -159,4 +183,56 @@ def get_version_increment(patch_bump_list: list, minor_bump_list: list, pr_label if any(label in pr_label_list for label in patch_bump_list): return "patch" + logger.warning("No relevant labels found for version increment.") return "" + +# CLI using Click + +@click.group() +def cli(): + pass + +@cli.command() +@click.argument('repo_name', type=click.STRING) +@click.argument('query_tags', type=click.STRING) +@click.argument('latest_release_date', type=click.STRING) +def pr_labels(latest_release_date, query_tags, repo_name): + """Get a list of all version relevant PR labels. + + latest_release_date (str): datatime string\n + query_tags (str): csv string\n + repo_name (str): repo name as \n + """ + + pr_result = query_merged_prs(latest_release_date, query_tags, repo_name) + pr_labels = get_labels(pr_data=pr_result) + + if not pr_labels: + click.echo("") + + click.echo(pr_labels) + + +@cli.command() +@click.argument('repo_name', type=click.STRING) +@click.argument('query_tags', type=click.STRING) +@click.argument('latest_release_date', type=click.STRING) +def version_increment(latest_release_date, query_tags, repo_name): + """Output a calculated version increment suggestion. + + latest_release_date (str): datatime string\n + query_tags (str): csv string\n + repo_name (str): repo name as \n + """ + + pr_result = query_merged_prs(latest_release_date, query_tags, repo_name) + pr_labels = get_labels(pr_data=pr_result) + patch_repo_var_list = csv_string_to_list(get_repo_var(repo=repo_name, var_name="PATCH_BUMP_LABEL")) + minor_repo_var_list = csv_string_to_list(get_repo_var(repo=repo_name, var_name="MINOR_BUMP_LABEL")) + increment = get_version_increment(patch_bump_list=patch_repo_var_list, minor_bump_list=minor_repo_var_list, pr_label_list=pr_labels) + + click.echo(increment) + + +if __name__ == '__main__': + cli() \ No newline at end of file From 19044f60436b6a8857f4c6abfde9c0ae5c6efaa1 Mon Sep 17 00:00:00 2001 From: PhilNewm Date: Tue, 17 Sep 2024 13:00:07 +0200 Subject: [PATCH 2/5] Move logic into dedicated files --- github_query.py | 195 ++----------------------------------- src/__init__.py | 0 src/conversion_logic.py | 132 +++++++++++++++++++++++++ src/queries.py | 50 ++++++++++ tests/test_github_query.py | 27 +++-- 5 files changed, 203 insertions(+), 201 deletions(-) create mode 100644 src/__init__.py create mode 100644 src/conversion_logic.py create mode 100644 src/queries.py diff --git a/github_query.py b/github_query.py index a297869..f5c1316 100644 --- a/github_query.py +++ b/github_query.py @@ -5,186 +5,7 @@ """ import click -import json -import logging -import re -import subprocess - - -logger = logging.getLogger(__name__) - -def query_merged_prs(latest_release_date, query_tags, repo_name): - """Run gh pull request query. - - Args: - latest_release_date (str): datatime string - query_tags (str): csv string - repo_name (str): repo name as - - Returns: - dict: json-dictionary. - """ - - pr_list = subprocess.run( - [ - "gh", "pr", "list", - "--state", "merged", - "--search", f'merged:>={latest_release_date}', - "--json", ','.join(query_tags), - "--repo", repo_name - ], - capture_output=True, - text=True, - check=True - ) - - return json.loads(pr_list.stdout) - -# INFO not in use -def get_changelog(pr_data, changelog_start="## Changelog", heading="##"): - """Get list of changes from a PRs changelog. - - Args: - pr_body (list(str)): List of PR body contents. - changelog_start (str, optional): Indicates markdown changelog section. Defaults to "## Changes". - heading (str, optional): Markdown heading. Defaults to "##". - - Returns: - list(str): List of changes found. - """ - - lines = pr_data.splitlines() - changelog_section = None - changelog_lines = [] - - for line in lines: - if line.startswith(changelog_start): - changelog_section = True - continue - - if changelog_section and line.startswith(heading): - break - - if changelog_section and line.startswith("* "): - changelog_lines.append(line.strip("* ").strip()) - - return changelog_lines - -# INFO not in use -def changelog_per_label(json_dict): - # TODO replace with labels fetched from repo variables - changelog_labels = ["bugfix", "enhancement", "feature"] - labels = [] - for item in json_dict: - labels.append(item["labels"]) - if any(item in changelog_labels for item in labels): - pass - -# INFO not in use -def prepare_changelog_markdown(pr_query, minor_bump_list, patch_bump_list): - # ? should version bump labels also be filter for changelog ? - label_list = minor_bump_list + patch_bump_list - changelog = "" - - for pr in pr_query: - # get all label names in a list - pr_label_list = [label["name"] for label in pr["labels"]] - fitlered_label = list(set(label_list).intersection(pr_label_list))[0] - - if fitlered_label: - change_list = get_changelog(pr_data=pr["body"]) - - changelog += f"## {fitlered_label.capitalize()}\n" - changelog += "".join([f"* {change}\n" for change in change_list]) - changelog += "\n" - - return changelog - - -def get_labels(pr_data: dict) -> list: - """Filter all unique labels from dictionary. - - Args: - pr_data (dict): Github PR query result - - Returns: - list: Liste of unique labels strings found or `None`. - """ - - labels = set() - - for item in pr_data: - if not item.get("labels"): - logger.warning("No PR label data found.") - return [] - for label in item["labels"]: - if not label.get("name"): - logger.warning("No PR label names found.") - return [] - - labels.add(label["name"]) - logger.debug("PR labels found.") - - return list(labels) - -def get_repo_var(repo: str, var_name: str) -> list: - """Query labels from repository variables. - - Args: - repo (str): Repository name `owner/repo-name` - var_name (str): Repo variable name - - Returns: - str: csv-string. - """ - labels = subprocess.run( - ["gh", "variable", "get", var_name, "--repo", repo], - capture_output=True, - text=True, - check=True - ) - - return labels.stdout - -def csv_string_to_list(input: str) -> list: - """Covnert string to list. - - Args: - input (str): Expected csv string. - - Returns: - list: List of strings. - """ - - if input: - return re.split(r',\s*', input.strip()) - - return [] - -def get_version_increment(patch_bump_list: list, minor_bump_list: list, pr_label_list: list): - """Figure out version increment based on PR labels. - - Args: - patch_bump_list ([str]): Labels for bumping patch version - minor_bump_list ([str]): Labels for bumping minor version - label_list ([str]): Labels found in PRs - - Returns: - str: version increment - """ - - if not pr_label_list: - return "" - - # TODO add major bump option - if any(label in pr_label_list for label in minor_bump_list): - return "minor" - - if any(label in pr_label_list for label in patch_bump_list): - return "patch" - - logger.warning("No relevant labels found for version increment.") - return "" +from src import conversion_logic, queries # CLI using Click @@ -204,8 +25,8 @@ def pr_labels(latest_release_date, query_tags, repo_name): repo_name (str): repo name as \n """ - pr_result = query_merged_prs(latest_release_date, query_tags, repo_name) - pr_labels = get_labels(pr_data=pr_result) + pr_result = queries.query_merged_prs(latest_release_date, query_tags, repo_name) + pr_labels = conversion_logic.get_labels(pr_data=pr_result) if not pr_labels: click.echo("") @@ -225,11 +46,11 @@ def version_increment(latest_release_date, query_tags, repo_name): repo_name (str): repo name as \n """ - pr_result = query_merged_prs(latest_release_date, query_tags, repo_name) - pr_labels = get_labels(pr_data=pr_result) - patch_repo_var_list = csv_string_to_list(get_repo_var(repo=repo_name, var_name="PATCH_BUMP_LABEL")) - minor_repo_var_list = csv_string_to_list(get_repo_var(repo=repo_name, var_name="MINOR_BUMP_LABEL")) - increment = get_version_increment(patch_bump_list=patch_repo_var_list, minor_bump_list=minor_repo_var_list, pr_label_list=pr_labels) + pr_result = queries.query_merged_prs(latest_release_date, query_tags, repo_name) + pr_labels = conversion_logic.get_labels(pr_data=pr_result) + patch_repo_var_list = conversion_logic.csv_string_to_list(queries.get_repo_var(repo=repo_name, var_name="PATCH_BUMP_LABEL")) + minor_repo_var_list = conversion_logic.csv_string_to_list(queries.get_repo_var(repo=repo_name, var_name="MINOR_BUMP_LABEL")) + increment = conversion_logic.get_version_increment(patch_bump_list=patch_repo_var_list, minor_bump_list=minor_repo_var_list, pr_label_list=pr_labels) click.echo(increment) diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/conversion_logic.py b/src/conversion_logic.py new file mode 100644 index 0000000..d734bd2 --- /dev/null +++ b/src/conversion_logic.py @@ -0,0 +1,132 @@ +import logging +import re + + +logger = logging.getLogger(__name__) + +# INFO not in use +def get_changelog(pr_data, changelog_start="## Changelog", heading="##"): + """Get list of changes from a PRs changelog. + + Args: + pr_body (list(str)): List of PR body contents. + changelog_start (str, optional): Indicates markdown changelog section. Defaults to "## Changes". + heading (str, optional): Markdown heading. Defaults to "##". + + Returns: + list(str): List of changes found. + """ + + lines = pr_data.splitlines() + changelog_section = None + changelog_lines = [] + + for line in lines: + if line.startswith(changelog_start): + changelog_section = True + continue + + if changelog_section and line.startswith(heading): + break + + if changelog_section and line.startswith("* "): + changelog_lines.append(line.strip("* ").strip()) + + return changelog_lines + +# INFO not in use +def changelog_per_label(json_dict): + # TODO replace with labels fetched from repo variables + changelog_labels = ["bugfix", "enhancement", "feature"] + labels = [] + for item in json_dict: + labels.append(item["labels"]) + if any(item in changelog_labels for item in labels): + pass + +# INFO not in use +def prepare_changelog_markdown(pr_query, minor_bump_list, patch_bump_list): + # ? should version bump labels also be filter for changelog ? + label_list = minor_bump_list + patch_bump_list + changelog = "" + + for pr in pr_query: + # get all label names in a list + pr_label_list = [label["name"] for label in pr["labels"]] + fitlered_label = list(set(label_list).intersection(pr_label_list))[0] + + if fitlered_label: + change_list = get_changelog(pr_data=pr["body"]) + + changelog += f"## {fitlered_label.capitalize()}\n" + changelog += "".join([f"* {change}\n" for change in change_list]) + changelog += "\n" + + return changelog + + +def get_labels(pr_data: dict) -> list: + """Filter all unique labels from dictionary. + + Args: + pr_data (dict): Github PR query result + + Returns: + list: Liste of unique labels strings found or `None`. + """ + + labels = set() + + for item in pr_data: + if not item.get("labels"): + logger.warning("No PR label data found.") + return [] + for label in item["labels"]: + if not label.get("name"): + logger.warning("No PR label names found.") + return [] + + labels.add(label["name"]) + logger.debug("PR labels found.") + + return list(labels) + +def csv_string_to_list(input: str) -> list: + """Covnert string to list. + + Args: + input (str): Expected csv string. + + Returns: + list: List of strings. + """ + + if input: + return re.split(r',\s*', input.strip()) + + return [] + +def get_version_increment(patch_bump_list: list, minor_bump_list: list, pr_label_list: list): + """Figure out version increment based on PR labels. + + Args: + patch_bump_list ([str]): Labels for bumping patch version + minor_bump_list ([str]): Labels for bumping minor version + label_list ([str]): Labels found in PRs + + Returns: + str: version increment + """ + + if not pr_label_list: + return "" + + # TODO add major bump option + if any(label in pr_label_list for label in minor_bump_list): + return "minor" + + if any(label in pr_label_list for label in patch_bump_list): + return "patch" + + logger.warning("No relevant labels found for version increment.") + return "" \ No newline at end of file diff --git a/src/queries.py b/src/queries.py new file mode 100644 index 0000000..9bbcabb --- /dev/null +++ b/src/queries.py @@ -0,0 +1,50 @@ +import json +import subprocess + + +def query_merged_prs(latest_release_date, query_tags, repo_name): + """Run gh pull request query. + + Args: + latest_release_date (str): datatime string + query_tags (str): csv string + repo_name (str): repo name as + + Returns: + dict: json-dictionary. + """ + + pr_list = subprocess.run( + [ + "gh", "pr", "list", + "--state", "merged", + "--search", f'merged:>={latest_release_date}', + "--json", ','.join(query_tags), + "--repo", repo_name + ], + capture_output=True, + text=True, + check=True + ) + + return json.loads(pr_list.stdout) + + +def get_repo_var(repo: str, var_name: str) -> list: + """Query labels from repository variables. + + Args: + repo (str): Repository name `owner/repo-name` + var_name (str): Repo variable name + + Returns: + str: csv-string. + """ + labels = subprocess.run( + ["gh", "variable", "get", var_name, "--repo", repo], + capture_output=True, + text=True, + check=True + ) + + return labels.stdout diff --git a/tests/test_github_query.py b/tests/test_github_query.py index 2948f8d..d1fe6c6 100644 --- a/tests/test_github_query.py +++ b/tests/test_github_query.py @@ -1,7 +1,6 @@ import pytest -from unittest.mock import patch -import github_query +from src import conversion_logic @pytest.fixture def pr_api_output(): @@ -109,13 +108,13 @@ def csv_string_empty(): # Get PR Label test-cases def test_get_labels(pr_api_output): - labels = github_query.get_labels(pr_data=pr_api_output) + labels = conversion_logic.get_labels(pr_data=pr_api_output) assert isinstance(labels, list) assert set(labels) == {"bugfix", "enhancement"} def test_get_labels_missing_input(pr_api_output_missing_label): - labels = github_query.get_labels(pr_data=pr_api_output_missing_label) + labels = conversion_logic.get_labels(pr_data=pr_api_output_missing_label) assert labels == [] @@ -123,22 +122,22 @@ def test_get_labels_missing_input(pr_api_output_missing_label): # Convert repo label list def test_csv_string_to_list_spaces(csv_string_spaces): - string_list = github_query.csv_string_to_list(csv_string_spaces) + string_list = conversion_logic.csv_string_to_list(csv_string_spaces) assert string_list == ["bugfix", "enhancement", "feature"] def test_csv_string_to_list_no_spaces(csv_string_no_spaces): - string_list = github_query.csv_string_to_list(csv_string_no_spaces) + string_list = conversion_logic.csv_string_to_list(csv_string_no_spaces) assert string_list == ["bugfix", "enhancement", "feature"] def test_csv_string_to_list_no_comma(csv_string_no_comma): - string_list = github_query.csv_string_to_list(csv_string_no_comma) + string_list = conversion_logic.csv_string_to_list(csv_string_no_comma) assert string_list == ["bugfix"] def test_csv_string_to_list_empty(csv_string_empty): - string_list = github_query.csv_string_to_list(csv_string_empty) + string_list = conversion_logic.csv_string_to_list(csv_string_empty) assert string_list == [] @@ -146,26 +145,26 @@ def test_csv_string_to_list_empty(csv_string_empty): # Version Increment test-cases def test_get_version_increment_patch(minor_bump, patch_bump, pr_labels_bug): - increment = github_query.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_bug) + increment = conversion_logic.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_bug) assert increment == "patch" def test_get_version_increment_minor(minor_bump, patch_bump, pr_labels_enhancement): - increment = github_query.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_enhancement) + increment = conversion_logic.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_enhancement) assert increment == "minor" def test_get_version_increment_wrong_labels(minor_bump, patch_bump, pr_labels_wrong_labels): - increment = github_query.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_wrong_labels) + increment = conversion_logic.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_wrong_labels) assert increment == "" def test_get_version_increment_none(minor_bump, patch_bump, pr_labels_none): - increment = github_query.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_none) + increment = conversion_logic.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_none) assert increment == "" -def test_get_version_increment_ampty_list(minor_bump, patch_bump, pr_labels_empty_list): - increment = github_query.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_empty_list) +def test_get_version_increment_empty_list(minor_bump, patch_bump, pr_labels_empty_list): + increment = conversion_logic.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_empty_list) assert increment == "" From 172207bd492f2462f0d4675b626377f3f78b7b02 Mon Sep 17 00:00:00 2001 From: PhilNewm Date: Tue, 17 Sep 2024 13:00:54 +0200 Subject: [PATCH 3/5] Fix typo --- github_query.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/github_query.py b/github_query.py index f5c1316..3bfb2bc 100644 --- a/github_query.py +++ b/github_query.py @@ -5,9 +5,7 @@ """ import click -from src import conversion_logic, queries - -# CLI using Click +from src import conversion_logic, queries @click.group() def cli(): From c06979aa88bdcfc8c0fb54af783fa8e80bda1a5c Mon Sep 17 00:00:00 2001 From: PhilNewm Date: Mon, 30 Sep 2024 16:42:48 +0200 Subject: [PATCH 4/5] Add major bump Upgrade tests --- src/conversion_logic.py | 17 ++++++++------ tests/test_github_query.py | 48 ++++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/conversion_logic.py b/src/conversion_logic.py index d734bd2..81af801 100644 --- a/src/conversion_logic.py +++ b/src/conversion_logic.py @@ -53,12 +53,12 @@ def prepare_changelog_markdown(pr_query, minor_bump_list, patch_bump_list): for pr in pr_query: # get all label names in a list pr_label_list = [label["name"] for label in pr["labels"]] - fitlered_label = list(set(label_list).intersection(pr_label_list))[0] + filtered_label = list(set(label_list).intersection(pr_label_list))[0] - if fitlered_label: + if filtered_label: change_list = get_changelog(pr_data=pr["body"]) - changelog += f"## {fitlered_label.capitalize()}\n" + changelog += f"## {filtered_label.capitalize()}\n" changelog += "".join([f"* {change}\n" for change in change_list]) changelog += "\n" @@ -72,7 +72,7 @@ def get_labels(pr_data: dict) -> list: pr_data (dict): Github PR query result Returns: - list: Liste of unique labels strings found or `None`. + list: List of unique labels strings found or `None`. """ labels = set() @@ -92,7 +92,7 @@ def get_labels(pr_data: dict) -> list: return list(labels) def csv_string_to_list(input: str) -> list: - """Covnert string to list. + """Convert string to list. Args: input (str): Expected csv string. @@ -106,7 +106,7 @@ def csv_string_to_list(input: str) -> list: return [] -def get_version_increment(patch_bump_list: list, minor_bump_list: list, pr_label_list: list): +def get_version_increment(pr_label_list: list, patch_bump_list: list=[], minor_bump_list: list=[], major_bump_list: list=[]): """Figure out version increment based on PR labels. Args: @@ -119,9 +119,12 @@ def get_version_increment(patch_bump_list: list, minor_bump_list: list, pr_label """ if not pr_label_list: + logger.warning("PR label list was empty") return "" - # TODO add major bump option + if any(label in pr_label_list for label in major_bump_list): + return "major" + if any(label in pr_label_list for label in minor_bump_list): return "minor" diff --git a/tests/test_github_query.py b/tests/test_github_query.py index d1fe6c6..e5cdefb 100644 --- a/tests/test_github_query.py +++ b/tests/test_github_query.py @@ -52,6 +52,10 @@ def pr_api_output_missing_label(): } ] +@pytest.fixture +def major_bump(): + return ["epic"] + @pytest.fixture def minor_bump(): return ["feature", "enhancement"] @@ -68,6 +72,10 @@ def pr_labels_bug(): def pr_labels_enhancement(): return ["bugfix", "documentation", "feature", "enhancement"] +@pytest.fixture +def pr_labels_epic(): + return ["bugfix", "documentation", "feature", "enhancement", "epic"] + @pytest.fixture def pr_labels_wrong_labels(): return ["documentation", "wontfix"] @@ -145,26 +153,56 @@ def test_csv_string_to_list_empty(csv_string_empty): # Version Increment test-cases def test_get_version_increment_patch(minor_bump, patch_bump, pr_labels_bug): - increment = conversion_logic.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_bug) + increment = conversion_logic.get_version_increment( + pr_label_list=pr_labels_bug, + patch_bump_list=patch_bump, + minor_bump_list=minor_bump, + ) assert increment == "patch" def test_get_version_increment_minor(minor_bump, patch_bump, pr_labels_enhancement): - increment = conversion_logic.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_enhancement) + increment = conversion_logic.get_version_increment( + pr_label_list=pr_labels_enhancement, + patch_bump_list=patch_bump, + minor_bump_list=minor_bump, + ) assert increment == "minor" +def test_get_version_increment_minor(minor_bump, patch_bump, major_bump, pr_labels_epic): + increment = conversion_logic.get_version_increment( + pr_label_list=pr_labels_epic, + patch_bump_list=patch_bump, + minor_bump_list=minor_bump, + major_bump_list=major_bump, + ) + + assert increment == "major" + def test_get_version_increment_wrong_labels(minor_bump, patch_bump, pr_labels_wrong_labels): - increment = conversion_logic.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_wrong_labels) + increment = conversion_logic.get_version_increment( + pr_label_list=pr_labels_wrong_labels, + patch_bump_list=patch_bump, + minor_bump_list=minor_bump, + ) assert increment == "" def test_get_version_increment_none(minor_bump, patch_bump, pr_labels_none): - increment = conversion_logic.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_none) + increment = conversion_logic.get_version_increment( + pr_label_list=pr_labels_none, + patch_bump_list=patch_bump, + minor_bump_list=minor_bump, + ) assert increment == "" def test_get_version_increment_empty_list(minor_bump, patch_bump, pr_labels_empty_list): - increment = conversion_logic.get_version_increment(patch_bump_list=patch_bump, minor_bump_list=minor_bump, pr_label_list=pr_labels_empty_list) + increment = conversion_logic.get_version_increment( + pr_label_list=pr_labels_empty_list, + patch_bump_list=patch_bump, + minor_bump_list=minor_bump, + ) assert increment == "" From 5e45bb50182f29d257df1a73a3bf076a7b62bca8 Mon Sep 17 00:00:00 2001 From: PhilNewm Date: Mon, 30 Sep 2024 16:53:19 +0200 Subject: [PATCH 5/5] Add raise in terms of wrong datatype --- github_query.py | 2 +- src/conversion_logic.py | 4 ++++ tests/test_github_query.py | 13 +++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/github_query.py b/github_query.py index 3bfb2bc..4761ec3 100644 --- a/github_query.py +++ b/github_query.py @@ -39,7 +39,7 @@ def pr_labels(latest_release_date, query_tags, repo_name): def version_increment(latest_release_date, query_tags, repo_name): """Output a calculated version increment suggestion. - latest_release_date (str): datatime string\n + latest_release_date (str): datetime string\n query_tags (str): csv string\n repo_name (str): repo name as \n """ diff --git a/src/conversion_logic.py b/src/conversion_logic.py index 81af801..f8ba18f 100644 --- a/src/conversion_logic.py +++ b/src/conversion_logic.py @@ -122,6 +122,10 @@ def get_version_increment(pr_label_list: list, patch_bump_list: list=[], minor_b logger.warning("PR label list was empty") return "" + for name, param in locals().items(): + if not isinstance(param, list): + raise ValueError(f"{name} must be a list.") + if any(label in pr_label_list for label in major_bump_list): return "major" diff --git a/tests/test_github_query.py b/tests/test_github_query.py index e5cdefb..57d9766 100644 --- a/tests/test_github_query.py +++ b/tests/test_github_query.py @@ -56,6 +56,10 @@ def pr_api_output_missing_label(): def major_bump(): return ["epic"] +@pytest.fixture +def major_bump_no_list(): + return "epic" + @pytest.fixture def minor_bump(): return ["feature", "enhancement"] @@ -206,3 +210,12 @@ def test_get_version_increment_empty_list(minor_bump, patch_bump, pr_labels_empt ) assert increment == "" + +def test_get_version_increment_no_list(minor_bump, patch_bump, major_bump_no_list, pr_labels_epic): + with pytest.raises(ValueError, match="must be a list"): + conversion_logic.get_version_increment( + pr_label_list=pr_labels_epic, + patch_bump_list=patch_bump, + minor_bump_list=minor_bump, + major_bump_list=major_bump_no_list, + )