diff --git a/fetch_github_data.py b/fetch_github_data.py index 21823f6..d26a643 100644 --- a/fetch_github_data.py +++ b/fetch_github_data.py @@ -1,25 +1,39 @@ +""" +This script is written in a certain - maybe even unconventional way - by intention. +It's supposed to be run from comamndline right away to be used in a github action workflow yaml file. +Additonally it's test suite relies mainly on putest and therefore the functions need to be importable to the pytest script. +""" + import argparse import json import subprocess -repo_name = None +parsed_args = None + +def parse_arguments(): + """Parse command-line arguments and store them in a global variable.""" + global parsed_args + if parsed_args is None: + 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() + def get_inputs(): - """Get terminal parameters. + """Get the parsed command-line arguments. Returns: tuple(str): Parameters as string tuple. """ - 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.') - args = parser.parse_args() - - repo_name = args.repo - query_tags = args.query_parameters.split(',') - latest_release_date = args.date + if parsed_args is None: + parse_arguments() + + repo_name = parsed_args.repo + query_tags = parsed_args.query_parameters.split(',') + latest_release_date = parsed_args.date return repo_name, query_tags, latest_release_date @@ -80,7 +94,7 @@ def prepare_changelog_markdown(): 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] # any(label in label_list for label in pr_label_list) + fitlered_label = list(set(label_list).intersection(pr_label_list))[0] if fitlered_label: change_list = get_changelog(pr_data=pr["body"], changelog_start="## Changes") @@ -125,3 +139,6 @@ def get_version_increment(): return "minor" if label.lower() in patch_bump_label_list: return "patch" + +if __name__ == "__main__": + parse_arguments() \ No newline at end of file diff --git a/tests/test_fetch_github_data.py b/tests/test_fetch_github_data.py new file mode 100644 index 0000000..f461963 --- /dev/null +++ b/tests/test_fetch_github_data.py @@ -0,0 +1,64 @@ + +import argparse +import json +from unittest import mock +import os +import sys + +from unittest.mock import patch + +# Add the parent directory of 'tests' to the system path +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) +import fetch_github_data + +# Mock data to return for your tests +mock_repo_name = 'ynput/ayon-addon-action-testing' +mock_query_parameters = 'title,body,labels' +mock_date = '2024-07-08T09:48:33Z' +mock_labels_output = ['bug', 'enhancement'] + +# Mocking the subprocess to prevent actual GitHub CLI calls +def mock_get_raw_output(repo_name, query_tags, latest_release_date): + return [ + { + "body": "# Bug Fix PR Template\r\n\r\n[ x] Feature/Enhancement
\r\n[ ] Bugfix
\r\n[ ] Documentation update
\r\n\r\n## Summary\r\n\r\n\r\n\r\n## Root Cause Analysis\r\n\r\n[Issue Link](https://github.com/ynput/ci-testing/blob/develop/.github/ISSUE_TEMPLATE/bug_report.yml)
\r\n\r\n\r\n## Changes\r\n\r\n\r\n* Add more test\r\n* Was very important\r\n* Needed to add this here\r\n\r\n## Testing Strategy\r\n\r\n\r\n\r\n## Checklist\r\n\r\n* [ x] The fix has been locally tested\r\n* [ x] New unit tests have been added to prevent future regressions\r\n* [ x] The documentation has been updated if necessary\r\n\r\n## Additional Notes\r\n\r\n", + "labels": [ + { + "id": "LA_kwDOMje8_88AAAABtOJ1Ig", + "name": "enhancement", + "description": "New feature or request", + "color": "b9f29d" + } + ], + "title": "Add more data" + }, + { + "body": "# Date file added\r\n\r\n## Summary\r\n\r\nSome awesome summary going on right here.\r\n\r\n## Root Cause Analysis\r\n\r\n[Issue Link](https://github.com/ynput/ci-testing/blob/develop/.github/ISSUE_TEMPLATE/bug_report.yml)
\r\nDate file so absolutely needed.\r\n\r\n## Changes\r\n\r\n\r\n* Run a command\r\n* Pipe its output to a text file\r\n* Commit dem stuff\r\n\r\n## Testing Strategy\r\n\r\nnahhhhh\r\n\r\n## Checklist\r\n\r\n* [x] The fix has been locally tested\r\n* [x] New unit tests have been added to prevent future regressions\r\n* [x] The documentation has been updated if necessary\r\n\r\n## Additional Notes\r\n\r\nNop", + "labels": [ + { + "id": "LA_kwDOMje8_88AAAABtOJ1Gw", + "name": "bug", + "description": "Something isn't working", + "color": "ff9195" + } + ], + "title": "Add date file" + } + ] + +@patch('argparse.ArgumentParser.parse_args') +@patch('fetch_github_data.get_raw_output', side_effect=mock_get_raw_output) +def test_get_labels(mock_parse_args, mock_get_raw_output): + # Configure the mock to return desired arguments + mock_parse_args.return_value = argparse.Namespace( + repo=mock_repo_name, + query_parameters=mock_query_parameters, + date=mock_date + ) + + fetch_github_data.parse_arguments() + + labels = fetch_github_data.get_labels() + + assert set(json.loads(labels)) == set(mock_labels_output) +