From 1e25ee4fd817ce0d7ad4e09d11750ebd24badaa7 Mon Sep 17 00:00:00 2001 From: Ben van Werkhoven Date: Mon, 10 Oct 2022 11:14:01 +0200 Subject: [PATCH 1/5] add function to automate badge update --- howfairis/cli/cli.py | 11 ++++++--- howfairis/cli/print_call_to_action.py | 34 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/howfairis/cli/cli.py b/howfairis/cli/cli.py index 474fc35b..ba17f898 100644 --- a/howfairis/cli/cli.py +++ b/howfairis/cli/cli.py @@ -4,7 +4,7 @@ from howfairis.__version__ import __version__ from howfairis.checker import DEFAULT_CONFIG_FILENAME from howfairis.checker import Checker -from howfairis.cli.print_call_to_action import print_call_to_action +from howfairis.cli.print_call_to_action import print_call_to_action, automate_call_to_action from howfairis.cli.print_default_config import print_default_config from howfairis.cli.print_feedback_about_config_args import print_feedback_about_config_args from howfairis.cli.print_feedback_about_repo_args import print_feedback_about_repo_args @@ -14,6 +14,8 @@ # pylint: disable=too-many-arguments @click.command(context_settings=dict(help_option_names=["-h", "--help"])) +@click.option("-a", "--auto", default=False, is_flag=True, + help="Automatically update fair-software.eu badge in local README.") @click.option("-b", "--branch", default=None, type=click.STRING, help="Which git branch to use. Also accepts other git references like SHA or tag.") @click.option("-u", "--user-config-filename", default=None, type=click.Path(), @@ -37,7 +39,7 @@ @click.option("-v", "--version", default=False, is_flag=True, help="Show version and exit.") @click.argument("url", required=False) -def cli(url=None, branch=None, user_config_filename=None, repo_config_filename=None, path=None, +def cli(url=None, auto=False, branch=None, user_config_filename=None, repo_config_filename=None, path=None, show_trace=False, version=False, ignore_repo_config=False, show_default_config=False, quiet=False): """Determine compliance with recommendations from fair-software.eu for the repository at URL. The following @@ -70,7 +72,10 @@ def cli(url=None, branch=None, user_config_filename=None, repo_config_filename=N previous_compliance = checker.readme.get_compliance() current_compliance = checker.check_five_recommendations() - sys.exit(print_call_to_action(previous_compliance, current_compliance, checker, is_quiet=quiet)) + if not auto: + sys.exit(print_call_to_action(previous_compliance, current_compliance, checker, is_quiet=quiet)) + else: + sys.exit(automate_call_to_action(previous_compliance, current_compliance, checker, is_quiet=quiet)) if __name__ == "__main__": diff --git a/howfairis/cli/print_call_to_action.py b/howfairis/cli/print_call_to_action.py index 792ed0cc..57c81016 100644 --- a/howfairis/cli/print_call_to_action.py +++ b/howfairis/cli/print_call_to_action.py @@ -37,3 +37,37 @@ def print_call_to_action(previous_compliance, current_compliance, checker, is_qu github_caching_check(checker) return sys_exit_code + + +def automate_call_to_action(previous_compliance, current_compliance, checker, is_quiet=False): + """ Function to automate the process of updating the fair-software.eu badge """ + if checker.readme.text is None: + return 1 + + if previous_compliance is None: + badge = current_compliance.calc_badge(checker.readme.file_format) + message = "It seems you have not yet added the fair-software.eu badge to " + \ + f"your {checker.readme.filename}. You can do so by pasting the following snippet:\n\n{badge}" + sys_exit_code = 1 + + elif current_compliance == previous_compliance: + message = "Expected badge is equal to the actual badge. It's all good.\n" + sys_exit_code = 0 + + else: + message = "Repository has a fair-software.eu badge, but current compliance does not match the badge\n" + \ + f"Updating badge image URL in {checker.readme.filename}." + with open(checker.readme.filename, "r") as readme: + readme_contents = readme.read() + readme_contents = readme_contents.replace(previous_compliance.badge_image_url(), current_compliance.badge_image_url()) + with open(checker.readme.filename, "w") as readme: + readme.write(readme_contents) + sys_exit_code = 0 + + if not is_quiet: + print("\nCalculated compliance: " + " ".join(current_compliance.as_unicode()) + "\n") + print(message) + if checker.repo.platform == Platform.GITHUB: + github_caching_check(checker) + + return sys_exit_code From 18e26320e52492dde382a0da0aaf1d57dc0501c6 Mon Sep 17 00:00:00 2001 From: Ben van Werkhoven Date: Mon, 10 Oct 2022 12:57:20 +0200 Subject: [PATCH 2/5] add test for automated call to action --- tests/test_cli.py | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 16c188a1..e9a6e785 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,7 +1,9 @@ +import os from click.testing import CliRunner from requests_mock import Mocker +from howfairis import Compliance from howfairis.cli.cli import cli - +from howfairis.cli.print_call_to_action import automate_call_to_action def test_matching_badge(requests_mock: Mocker): owner = "fair-software" @@ -96,3 +98,43 @@ def test_missing_badge(requests_mock: Mocker): runner = CliRunner() response = runner.invoke(cli, [url]) assert response.exit_code == 1 + + +def test_automate_call_to_action(): + + test_filename = "test-automate-call-to-action--readme.md" + + # Mock the Checker + class Repo: + platform = "test" + class Readme: + filename = test_filename + text = 1 + class Checker: + readme = Readme + repo = Repo + + previous_compliance = Compliance(False, False, False, False, False) + current_compliance = Compliance(False, False, False, False, True) + + try: + test_README_string = previous_compliance.badge_image_url() + with open(test_filename, "w") as test_file: + test_file.write(test_README_string) + + automate_call_to_action(previous_compliance, current_compliance, Checker) + + with open(test_filename, "r") as test_file: + result = test_file.read() + assert result == current_compliance.badge_image_url() + + finally: + # cleanup the temporary file test_filename + try: + os.remove(test_filename) + except OSError as e: + # if the file does not exist, ignore the exception + if e.errno != errno.ENOENT: + raise e + + From 7c25622625ddd5a57274833ad6b2ed67494bb473 Mon Sep 17 00:00:00 2001 From: Ben van Werkhoven Date: Mon, 10 Oct 2022 14:47:37 +0200 Subject: [PATCH 3/5] fix issues reported by linters --- howfairis/cli/cli.py | 2 +- howfairis/cli/print_call_to_action.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/howfairis/cli/cli.py b/howfairis/cli/cli.py index ba17f898..2c8b1dd5 100644 --- a/howfairis/cli/cli.py +++ b/howfairis/cli/cli.py @@ -12,7 +12,7 @@ from howfairis.repo import Repo -# pylint: disable=too-many-arguments +# pylint: disable=too-many-arguments,too-many-locals @click.command(context_settings=dict(help_option_names=["-h", "--help"])) @click.option("-a", "--auto", default=False, is_flag=True, help="Automatically update fair-software.eu badge in local README.") diff --git a/howfairis/cli/print_call_to_action.py b/howfairis/cli/print_call_to_action.py index 57c81016..fa0a4cd1 100644 --- a/howfairis/cli/print_call_to_action.py +++ b/howfairis/cli/print_call_to_action.py @@ -57,10 +57,11 @@ def automate_call_to_action(previous_compliance, current_compliance, checker, is else: message = "Repository has a fair-software.eu badge, but current compliance does not match the badge\n" + \ f"Updating badge image URL in {checker.readme.filename}." - with open(checker.readme.filename, "r") as readme: + with open(checker.readme.filename, "r", encoding="utf8") as readme: readme_contents = readme.read() - readme_contents = readme_contents.replace(previous_compliance.badge_image_url(), current_compliance.badge_image_url()) - with open(checker.readme.filename, "w") as readme: + readme_contents = readme_contents.replace(previous_compliance.badge_image_url(), + current_compliance.badge_image_url()) + with open(checker.readme.filename, "w", encoding="utf8") as readme: readme.write(readme_contents) sys_exit_code = 0 From 5845c0ec5e576c21da289496724bcaa0d153e2e3 Mon Sep 17 00:00:00 2001 From: Ben van Werkhoven Date: Mon, 10 Oct 2022 14:55:22 +0200 Subject: [PATCH 4/5] fix import sort order --- howfairis/cli/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/howfairis/cli/cli.py b/howfairis/cli/cli.py index 2c8b1dd5..3419b657 100644 --- a/howfairis/cli/cli.py +++ b/howfairis/cli/cli.py @@ -4,7 +4,7 @@ from howfairis.__version__ import __version__ from howfairis.checker import DEFAULT_CONFIG_FILENAME from howfairis.checker import Checker -from howfairis.cli.print_call_to_action import print_call_to_action, automate_call_to_action +from howfairis.cli.print_call_to_action import automate_call_to_action, print_call_to_action from howfairis.cli.print_default_config import print_default_config from howfairis.cli.print_feedback_about_config_args import print_feedback_about_config_args from howfairis.cli.print_feedback_about_repo_args import print_feedback_about_repo_args From 18f8c0d2df777e18bc5459bcef1b167f99b04056 Mon Sep 17 00:00:00 2001 From: Ben van Werkhoven Date: Mon, 10 Oct 2022 14:59:30 +0200 Subject: [PATCH 5/5] fix import in the way isort wants it --- howfairis/cli/cli.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/howfairis/cli/cli.py b/howfairis/cli/cli.py index 3419b657..759f80fb 100644 --- a/howfairis/cli/cli.py +++ b/howfairis/cli/cli.py @@ -4,7 +4,8 @@ from howfairis.__version__ import __version__ from howfairis.checker import DEFAULT_CONFIG_FILENAME from howfairis.checker import Checker -from howfairis.cli.print_call_to_action import automate_call_to_action, print_call_to_action +from howfairis.cli.print_call_to_action import automate_call_to_action +from howfairis.cli.print_call_to_action import print_call_to_action from howfairis.cli.print_default_config import print_default_config from howfairis.cli.print_feedback_about_config_args import print_feedback_about_config_args from howfairis.cli.print_feedback_about_repo_args import print_feedback_about_repo_args