diff --git a/gerrit/conf.py b/gerrit/conf.py
index f3d52b2..d97890f 100644
--- a/gerrit/conf.py
+++ b/gerrit/conf.py
@@ -21,6 +21,7 @@ class GerritConf(AppConf):
URL = "https://gerrit.local/{}"
REST_HTTP_USER = "jenkins"
REST_HTTP_PASSWD = "verysecrethttppasswd"
+ DATETIME_FMT = "%Y-%m-%d %H:%M:%S.%f000"
class Meta:
prefix = "gerrit"
diff --git a/gerrit/fixtures/test_gerrit_commands.yaml b/gerrit/fixtures/test_gerrit_commands.yaml
new file mode 100644
index 0000000..755ba5b
--- /dev/null
+++ b/gerrit/fixtures/test_gerrit_commands.yaml
@@ -0,0 +1,24 @@
+- model: repoapi.gerritrepoinfo
+ pk: 47979
+ fields:
+ created: '2023-03-03 08:46:17'
+ gerrit_change: '67631'
+ modified: '2023-03-03 09:09:25'
+ param_ppa: gerrit_alessio_56718_bis_10_5_1,
+ projectname: templates
+- model: repoapi.gerritrepoinfo
+ pk: 47877
+ fields:
+ created: '1977-01-01 00:00:00'
+ gerrit_change: '67510'
+ modified: '2023-03-02 16:24:25'
+ param_ppa: gerrit_alessio_56718_bis_11_1
+ projectname: sems-pbx
+- model: repoapi.gerritrepoinfo
+ pk: 12
+ fields:
+ created: '1977-01-01 00:00:00'
+ gerrit_change: '13200'
+ modified: '2023-03-02 07:57:21'
+ param_ppa: gerrit_pu_collectd-abolition
+ projectname: unknown
diff --git a/gerrit/management/__init__.py b/gerrit/management/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/gerrit/management/commands/__init__.py b/gerrit/management/commands/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/gerrit/management/commands/gerrit.py b/gerrit/management/commands/gerrit.py
new file mode 100644
index 0000000..d05bc1d
--- /dev/null
+++ b/gerrit/management/commands/gerrit.py
@@ -0,0 +1,42 @@
+# Copyright (C) 2023 The Sipwise Team - http://sipwise.com
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program. If not, see .
+from datetime import date
+
+from django.core.management.base import BaseCommand
+
+from gerrit.utils import get_change_info
+from gerrit.utils import get_datetime
+from repoapi.models.gri import GerritRepoInfo
+
+
+class Command(BaseCommand):
+ help = "gerrit actions"
+
+ def add_arguments(self, parser):
+ parser.add_argument("action", choices=["refresh"])
+
+ def refresh(self, *args, **options):
+ qs = GerritRepoInfo.objects.filter(created__date=date(1977, 1, 1))
+ for gri in qs.iterator():
+ info = get_change_info(gri.gerrit_change)
+ gri.created = get_datetime(info["created"])
+ gri.modified = get_datetime(info["updated"])
+ # don't update modified field on save
+ gri.update_modified = False
+ gri.save()
+
+ def handle(self, *args, **options):
+ action = getattr(self, options["action"])
+ action(*args, **options)
diff --git a/gerrit/test_commands.py b/gerrit/test_commands.py
new file mode 100644
index 0000000..3db88f6
--- /dev/null
+++ b/gerrit/test_commands.py
@@ -0,0 +1,68 @@
+# Copyright (C) 2023 The Sipwise Team - http://sipwise.com
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program. If not, see .
+import datetime
+import json
+from unittest.mock import patch
+
+from django.core.management import call_command
+from django.test import TestCase
+
+from repoapi.models.gri import GerritRepoInfo
+
+change_info = """
+{
+ "id": "templates~mr10.5.1~I3a066ae039b06beb892a1f9e7b8dadf7aa476742",
+ "project": "templates",
+ "branch": "mr10.5.1",
+ "topic": "alessio_56718_bis_10_5_1",
+ "hashtags": [],
+ "change_id": "I3a066ae039b06beb892a1f9e7b8dadf7aa476742",
+ "subject": "MT#56718 Decrement counter of B in blind transfer",
+ "status": "NEW",
+ "created": "2023-03-03 08:45:06.000000000",
+ "updated": "2023-03-03 09:09:25.000000000",
+ "submit_type": "FAST_FORWARD_ONLY",
+ "mergeable": true,
+ "insertions": 104,
+ "deletions": 0,
+ "total_comment_count": 0,
+ "unresolved_comment_count": 0,
+ "has_review_started": true,
+ "_number": 67631,
+ "owner": {
+ "_account_id": 1000069
+ },
+ "requirements": []
+}
+"""
+value = json.loads(change_info)
+
+
+class refreshTest(TestCase):
+ fixtures = ["test_gerrit_commands"]
+
+ @patch("gerrit.management.commands.gerrit.get_change_info")
+ def test_refresh(self, gci):
+ gci.return_value = value
+ qs = GerritRepoInfo.objects
+ self.assertEqual(qs.count(), 3)
+ qs_filter = qs.filter(created__date=datetime.date(1977, 1, 1))
+ self.assertEqual(qs_filter.count(), 2)
+ call_command("gerrit", "refresh")
+ self.assertEqual(qs.count(), 3)
+ self.assertEqual(qs_filter.count(), 0)
+
+ qs_filter = qs.filter(modified__time=datetime.time(9, 9, 25))
+ self.assertEqual(qs_filter.count(), 3)
diff --git a/gerrit/test_utils.py b/gerrit/test_utils.py
index fd035c2..d9b418b 100644
--- a/gerrit/test_utils.py
+++ b/gerrit/test_utils.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2022 The Sipwise Team - http://sipwise.com
+# Copyright (C) 2023 The Sipwise Team - http://sipwise.com
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
@@ -12,6 +12,8 @@
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see .
+from datetime import datetime
+
from django.test import SimpleTestCase
from gerrit import utils
@@ -55,3 +57,14 @@ class GerritUtils(SimpleTestCase):
res = utils.get_filtered_json(GERRIT_REST_BRANCHES)
self.assertEqual(res, FILTERED_BRANCHES)
+
+ def test_get_datetime(self):
+ val = "2023-03-03 08:45:06.000000000"
+ expected = datetime(2023, 3, 3, 8, 45, 6)
+ res = utils.get_datetime(val)
+ self.assertEqual(res, expected)
+
+ val = "2023-03-03 09:09:25.000000000"
+ res = utils.get_datetime(val)
+ expected = datetime(2023, 3, 3, 9, 9, 25)
+ self.assertEqual(res, expected)
diff --git a/gerrit/utils.py b/gerrit/utils.py
index 1791a04..7a056e3 100644
--- a/gerrit/utils.py
+++ b/gerrit/utils.py
@@ -12,6 +12,7 @@
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see .
+import datetime
import json
import requests
@@ -60,3 +61,16 @@ def get_gerrit_tags(project: str, regex=None):
def get_gerrit_branches(project: str, regex=None):
url = gerrit_settings.URL.format(f"a/projects/{project}/branches/")
return get_gerrit_info(url)
+
+
+def get_gerrit_change(id: str) -> str:
+ url = gerrit_settings.URL.format(f"changes/{id}/")
+ return get_gerrit_info(url)
+
+
+def get_change_info(id: str):
+ return get_filtered_json(get_gerrit_info(id))
+
+
+def get_datetime(val: str) -> datetime.datetime:
+ return datetime.datetime.strptime(val, gerrit_settings.DATETIME_FMT)