[extensions-web/wip/gnome-40] extensions: allow GNOME 40+ versions without minor part




commit b9558d063c5cff8b42538b613597eede73262d3e
Author: Yuri Konotopov <ykonotopov gnome org>
Date:   Fri Mar 26 23:59:00 2021 +0400

    extensions: allow GNOME 40+ versions without minor part

 .../migrations/0009_shell40_minor_version.py       | 30 ++++++++++++
 sweettooth/extensions/models.py                    | 56 +++++++++++++---------
 sweettooth/extensions/tests.py                     | 22 ++++++++-
 sweettooth/static/js/extensionUtils.js             |  6 +--
 4 files changed, 87 insertions(+), 27 deletions(-)
---
diff --git a/sweettooth/extensions/migrations/0009_shell40_minor_version.py 
b/sweettooth/extensions/migrations/0009_shell40_minor_version.py
new file mode 100644
index 00000000..f6fe6e22
--- /dev/null
+++ b/sweettooth/extensions/migrations/0009_shell40_minor_version.py
@@ -0,0 +1,30 @@
+from django.db import migrations, models
+
+
+def apply(apps, schema_editor):
+    ShellVersion = apps.get_model("extensions", "ShellVersion")
+
+    ShellVersion.objects.all() \
+        .filter(major__gte=40) \
+        .filter(minor__lt=0) \
+        .update(minor=models.F('minor') - 1)
+
+
+def revert(apps, schema_editor):
+    ShellVersion = apps.get_model("extensions", "ShellVersion")
+
+    ShellVersion.objects.all() \
+        .filter(major__gte=40) \
+        .filter(minor__lt=-1) \
+        .update(minor=models.F('minor') + 1)
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('extensions', '0008_auto_20210225_1248'),
+    ]
+
+    operations = [
+        migrations.RunPython(apply, revert),
+    ]
diff --git a/sweettooth/extensions/models.py b/sweettooth/extensions/models.py
index 81653cfe..948de435 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -39,6 +39,12 @@ STATUSES = {
     STATUS_WAITING: u"Waiting for author",
 }
 
+PRERELEASE_VERSIONS = {
+    -4: 'alpha',
+    -3: 'beta',
+    -2: 'rc'
+}
+
 def validate_uuid(uuid):
     if re.match(r'[-a-zA-Z0-9@._]+$', uuid) is None:
         return False
@@ -199,19 +205,20 @@ class InvalidShellVersion(Exception):
 
 def parse_version_string(version_string):
     prerelease_versions = {
-        'alpha': -3,
-        'beta': -2,
-        'rc': -1,
+        'alpha': -4,
+        'beta': -3,
+        'rc': -2,
     }
     version = version_string.split('.')
     version_parts = len(version)
 
-    if version_parts < 2 or version_parts > 4:
+    if version_parts < 1 or version_parts > 4:
         raise InvalidShellVersion()
 
     try:
-        major, minor = version[:2]
-        major = int(major)
+        major = int(version[0])
+        minor = version[1] if version_parts > 1 else -1
+
         # GNOME 40+
         # https://discourse.gnome.org/t/new-gnome-versioning-scheme/4235
         if major >= 40 and minor in prerelease_versions.keys():
@@ -221,16 +228,18 @@ def parse_version_string(version_string):
     except ValueError:
         raise InvalidShellVersion()
 
-    if version_parts in (3, 4):
-        # 3.0.1, 3.1.4
-        try:
-            point = int(version[2])
-        except ValueError:
+    point = -1
+    if version_parts > 2:
+        if major >= 40:
             raise InvalidShellVersion()
-
+        else:
+            # 3.0.1, 3.1.4
+            try:
+                point = int(version[2])
+            except ValueError:
+                raise InvalidShellVersion()
     else:
-        point = -1
-        if major < 40 and minor % 2 != 0:
+        if major < 40 and (version_parts < 2 or minor % 2 != 0):
             # Two-digit pre-40 odd versions are illegal: 3.1, 3.3
             raise InvalidShellVersion()
 
@@ -269,18 +278,21 @@ class ShellVersion(models.Model):
     @property
     def version_string(self):
         prerelease_versions = {
-            -3: 'alpha',
-            -2: 'beta',
-            -1: 'rc'
+            -4: 'alpha',
+            -3: 'beta',
+            -2: 'rc'
         }
 
         # GNOME 40+: unstable versions
         # https://discourse.gnome.org/t/new-gnome-versioning-scheme/4235
-        if self.major >= 40 and self.minor < 0:
-            return "%d.%s" % (
-                self.major,
-                prerelease_versions.get(self.minor, 'unknown')
-            )
+        if self.major >= 40:
+            if self.minor < -1:
+                return "%d.%s" % (
+                    self.major,
+                    prerelease_versions.get(self.minor, 'unknown')
+                )
+            elif self.minor == -1:
+                return "%d" % (self.major)
 
         if self.point == -1:
             return "%d.%d" % (self.major, self.minor)
diff --git a/sweettooth/extensions/tests.py b/sweettooth/extensions/tests.py
index 180978f2..0b807272 100644
--- a/sweettooth/extensions/tests.py
+++ b/sweettooth/extensions/tests.py
@@ -354,11 +354,18 @@ class ShellVersionTest(TestCase):
         self.assertEqual(version.minor, 2)
         self.assertEqual(version.point, -1)
 
+        self.assertEqual(lookup_version("40"), None)
+        version = get_version("40")
+        self.assertEqual(lookup_version("40"), version)
+        self.assertEqual(version.major, 40)
+        self.assertEqual(version.minor, -1)
+        self.assertEqual(version.point, -1)
+
         self.assertEqual(lookup_version("40.alpha"), None)
         version = get_version("40.alpha")
         self.assertEqual(lookup_version("40.alpha"), version)
         self.assertEqual(version.major, 40)
-        self.assertEqual(version.minor, -3)
+        self.assertEqual(version.minor, -4)
         self.assertEqual(version.point, -1)
 
         self.assertEqual(lookup_version("51.6"), None)
@@ -372,7 +379,7 @@ class ShellVersionTest(TestCase):
         version = get_version("123.rc")
         self.assertEqual(lookup_version("123.rc"), version)
         self.assertEqual(version.major, 123)
-        self.assertEqual(version.minor, -1)
+        self.assertEqual(version.minor, -2)
         self.assertEqual(version.point, -1)
 
         self.assertEqual(lookup_version("41.3"), None)
@@ -410,6 +417,12 @@ class ShellVersionTest(TestCase):
         with self.assertRaises(models.InvalidShellVersion):
             models.parse_version_string("40.teta")
 
+        with self.assertRaises(models.InvalidShellVersion):
+            models.parse_version_string("40.0.1")
+
+        with self.assertRaises(models.InvalidShellVersion):
+            models.parse_version_string("51.0.1.2")
+
 class DownloadExtensionTest(BasicUserTestCase, TestCase):
     def download(self, uuid, shell_version):
         url = reverse('extensions-shell-download', kwargs=dict(uuid=uuid))
@@ -738,6 +751,9 @@ class QueryExtensionsTest(BasicUserTestCase, TestCase):
         v = models.ExtensionVersion.objects.create(extension=extension, status=models.STATUS_ACTIVE)
         v.parse_metadata_json({"shell-version": ["3.38.0", "40.alpha", "42.3"]})
 
+        v = models.ExtensionVersion.objects.create(extension=extension, status=models.STATUS_ACTIVE)
+        v.parse_metadata_json({"shell-version": ["40"]})
+
         self.assertEqual(views.grab_proper_extension_version(extension, "3.17.1").version, 3)
         self.assertEqual(views.grab_proper_extension_version(extension, "3.20.0").version, 4)
         self.assertEqual(views.grab_proper_extension_version(extension, "3.2.0"), None)
@@ -758,6 +774,8 @@ class QueryExtensionsTest(BasicUserTestCase, TestCase):
         self.assertEqual(views.grab_proper_extension_version(extension, "3.18.3", True).version, 3)
         self.assertEqual(views.grab_proper_extension_version(extension, "3.20.0", True).version, 4)
         self.assertEqual(views.grab_proper_extension_version(extension, "3.24.0", True).version, 4)
+        self.assertEqual(views.grab_proper_extension_version(extension, "40.alpha", True).version, 5)
         self.assertEqual(views.grab_proper_extension_version(extension, "40.beta", True).version, 5)
+        self.assertEqual(views.grab_proper_extension_version(extension, "40.2", True).version, 6)
         self.assertEqual(views.grab_proper_extension_version(extension, "44.2", True).version, 5)
         self.assertEqual(views.grab_proper_extension_version(extension, "129.rc", True).version, 5)
diff --git a/sweettooth/static/js/extensionUtils.js b/sweettooth/static/js/extensionUtils.js
index 99a7757d..261e6060 100644
--- a/sweettooth/static/js/extensionUtils.js
+++ b/sweettooth/static/js/extensionUtils.js
@@ -38,9 +38,9 @@ define([], function () {
        };
 
        let prerelease_versions = {
-               'alpha': -3,
-               'beta': -2,
-               'rc': -1,
+               'alpha': -4,
+               'beta': -3,
+               'rc': -2,
        };
 
        function versionCompare(a, b) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]