diff --git a/changelog.d/1907.added.md b/changelog.d/1907.added.md new file mode 100644 index 000000000..4bf7c5080 --- /dev/null +++ b/changelog.d/1907.added.md @@ -0,0 +1 @@ +Add management command for finding newest Argus release diff --git a/docs/development/management-commands.rst b/docs/development/management-commands.rst index d9dcc4039..76431216e 100644 --- a/docs/development/management-commands.rst +++ b/docs/development/management-commands.rst @@ -577,3 +577,20 @@ the command `rqworker`: .. code:: console $ python manage.py rqworker + +Look for new Argus release +========================== +To check if there is a new release of Argus available one can use the command `check_version`: + + .. code:: console + + $ python manage.py check_version + +To also save the latest version in the database, use the `--save` flag: + + .. code:: console + + $ python manage.py check_version --save + +This will currently not do anything, but in the future you will be able to get +a notification in the frontend if there is a new release registered in the database that you haven't seen yet. diff --git a/src/argus/dev/management/commands/check_version.py b/src/argus/dev/management/commands/check_version.py new file mode 100644 index 000000000..078211537 --- /dev/null +++ b/src/argus/dev/management/commands/check_version.py @@ -0,0 +1,21 @@ +from django.core.management.base import BaseCommand + +from argus.versioncheck.models import LastSeenVersion +from argus.versioncheck.tasks import get_latest_version + + +class Command(BaseCommand): + help = "Look for a new Argus version" + + def add_arguments(self, parser): + parser.add_argument("-s", "--save", action="store_true", help="Save the new version to the database") + + def handle(self, *args, **options): + latest_version = get_latest_version() + self.stdout.write(f"Latest version of Argus is: {latest_version}") + if options["save"]: + _, created = LastSeenVersion.objects.get_or_create(version=latest_version) + if created: + self.stdout.write("Saved to database.") + else: + self.stdout.write("This version is already in the database.") diff --git a/tests/dev/test_version_check.py b/tests/dev/test_version_check.py new file mode 100644 index 000000000..8fe1804eb --- /dev/null +++ b/tests/dev/test_version_check.py @@ -0,0 +1,39 @@ +from unittest.mock import patch + +from django.test import TestCase, tag + +from argus.versioncheck.models import LastSeenVersion +from django.core.management import call_command +from argus.util.testing import connect_signals, disconnect_signals + + +@tag("database") +class TestTaskRegisterLatestVersion(TestCase): + def setUp(self): + disconnect_signals() + + def tearDown(self): + connect_signals() + + @patch("argus.versioncheck.tasks.get_latest_version") + def test_when_new_version_is_not_already_registered_then_register_new_version(self, get_latest_version): + get_latest_version.return_value = "1.2.3" + assert not LastSeenVersion.objects.filter(version="1.2.3").exists() + call_command("check_version", "--save") + assert LastSeenVersion.objects.filter(version="1.2.3").exists() + + @patch("argus.versioncheck.tasks.get_latest_version") + def test_when_new_version_is_already_registered_then_do_not_register_it_again(self, get_latest_version): + get_latest_version.return_value = "1.2.3" + LastSeenVersion.objects.create(version="1.2.3") + assert LastSeenVersion.objects.filter(version="1.2.3").count() == 1 + call_command("check_version", "--save") + # Still only one instance of this version in the database + assert LastSeenVersion.objects.filter(version="1.2.3").count() == 1 + + @patch("argus.versioncheck.tasks.get_latest_version") + def test_when_save_flag_is_not_set_then_do_not_register_new_version(self, get_latest_version): + get_latest_version.return_value = "1.2.3" + assert not LastSeenVersion.objects.filter(version="1.2.3").exists() + call_command("check_version") + assert not LastSeenVersion.objects.filter(version="1.2.3").exists()