MT#56231 gerrit: command to refresh creation/modification info

This will allow us to have real info of the reviews we keep in the
database

Change-Id: Ic89189a2b71d34c1ee9abf23e167270a78eb25be
master
Victor Seva 2 years ago
parent 11bef4494c
commit d2f948b1fe

@ -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"

@ -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

@ -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 <http://www.gnu.org/licenses/>.
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)

@ -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 <http://www.gnu.org/licenses/>.
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)

@ -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 <http://www.gnu.org/licenses/>.
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)

@ -12,6 +12,7 @@
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
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)

Loading…
Cancel
Save