MT#54973 gerrit: move everything to its own app

Change-Id: I83f5dc75315bfd9df22c78f3169c72484e7b2c19
pull/9/head
Victor Seva 3 years ago
parent f0c9a16f90
commit f153d0109d

@ -0,0 +1,24 @@
# Copyright (C) 2022 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 django.apps import AppConfig
class GerritConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "gerrit"
def ready(self):
from .conf import settings # noqa

@ -0,0 +1,26 @@
# Copyright (C) 2022 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 django.conf import settings # noqa
from appconf import AppConf
class GerritConf(AppConf):
URL = "https://gerrit.local/{}"
REST_HTTP_USER = "jenkins"
REST_HTTP_PASSWD = "verysecrethttppasswd"
class Meta:
prefix = "gerrit"

@ -0,0 +1,57 @@
# Copyright (C) 2022 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 django.test import SimpleTestCase
from gerrit import utils
GERRIT_REST_TAGS = """
)]}'
[
{
"ref": "refs/tags/mr2.0.0"
},
{
"ref": "refs/tags/mr1.0.0"
}
]
"""
FILTERED_TAGS = [
{"ref": "refs/tags/mr2.0.0"},
{"ref": "refs/tags/mr1.0.0"},
]
GERRIT_REST_BRANCHES = """
)]}'
[
{
"ref": "refs/heads/master"
},
{
"ref": "refs/heads/vseva/1789"
}
]
"""
FILTERED_BRANCHES = [
{"ref": "refs/heads/master"},
{"ref": "refs/heads/vseva/1789"},
]
class GerritUtils(SimpleTestCase):
def test_filtered_json(self):
res = utils.get_filtered_json(GERRIT_REST_TAGS)
self.assertEqual(res, FILTERED_TAGS)
res = utils.get_filtered_json(GERRIT_REST_BRANCHES)
self.assertEqual(res, FILTERED_BRANCHES)

@ -0,0 +1,62 @@
# Copyright (C) 2022 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 json
import requests
import structlog
from requests.auth import HTTPBasicAuth
from .conf import GerritConf
gerrit_settings = GerritConf()
logger = structlog.get_logger(__name__)
def get_gerrit_response(url: str) -> requests.Response:
auth = HTTPBasicAuth(
gerrit_settings.REST_HTTP_USER,
gerrit_settings.REST_HTTP_PASSWD,
)
response = requests.get(url, auth=auth)
return response
def get_filtered_json(text: str):
"""gerrit responds with malformed json
https://gerrit-review.googlesource.com/Documentation/rest-api.html#output
"""
return json.loads(text[5:])
def get_gerrit_info(url: str) -> str:
from django.conf import settings
if settings.DEBUG:
logger.debug(f"Debug mode, would trigger: {url}")
return r")]}'\n[]"
else:
response = get_gerrit_response(url)
response.raise_for_status()
return response.text
def get_gerrit_tags(project: str, regex=None):
url = gerrit_settings.URL.format(f"a/projects/{project}/tags/")
return get_gerrit_info(url)
def get_gerrit_branches(project: str, regex=None):
url = gerrit_settings.URL.format(f"a/projects/{project}/branches/")
return get_gerrit_info(url)

@ -20,6 +20,7 @@ from django.db import models
from django_extensions.db.fields import ModificationDateTimeField
from .conf import settings
from gerrit.utils import get_filtered_json
class Project(models.Model):
@ -45,13 +46,6 @@ class Project(models.Model):
res.add(val_ok)
return sorted(res, reverse=True)
@classmethod
def _get_filtered_json(cls, text):
"""gerrit responds with malformed json
https://gerrit-review.googlesource.com/Documentation/rest-api.html#output
"""
return json.loads(text[5:])
def __str__(self):
return self.name
@ -63,7 +57,7 @@ class Project(models.Model):
@tags.setter
def tags(self, value):
self.json_tags = Project._get_filtered_json(value)
self.json_tags = get_filtered_json(value)
@property
def branches(self):
@ -73,7 +67,7 @@ class Project(models.Model):
@branches.setter
def branches(self, value):
self.json_branches = Project._get_filtered_json(value)
self.json_branches = get_filtered_json(value)
def filter_tags(self, regex):
return Project._filter_values(

@ -78,10 +78,6 @@ class ProjectTestCase(TestCase):
self.assertIsInstance(proj.branches, list)
self.assertCountEqual(proj.branches, [])
def test_filtered_json(self):
res = Project._get_filtered_json(GERRIT_REST_TAGS)
self.assertEqual(res, FILTERED_TAGS)
def test_filter_values_null(self):
res = Project._filter_values(None, "^refs/tags/(.+)$")
self.assertIsInstance(res, list)

@ -15,12 +15,12 @@
import urllib
import uuid
import requests
import structlog
from requests.auth import HTTPBasicAuth
from ..conf import settings
from ..models import Project
from gerrit.utils import get_gerrit_branches
from gerrit.utils import get_gerrit_tags
from repoapi.utils import open_jenkins_url
logger = structlog.get_logger(__name__)
@ -41,14 +41,6 @@ hotfix_url = (
)
def get_response(url):
auth = HTTPBasicAuth(
settings.GERRIT_REST_HTTP_USER, settings.GERRIT_REST_HTTP_PASSWD
)
response = requests.get(url, auth=auth)
return response
def trigger_hotfix(project, branch, user, push="yes", empty=False):
flow_uuid = uuid.uuid4()
if empty:
@ -86,26 +78,6 @@ def fetch_gerrit_info(projectname):
project.save()
def get_gerrit_info(url):
if settings.DEBUG:
logger.debug("Debug mode, would trigger: %s", url)
return r")]}'\n[]"
else:
response = get_response(url)
response.raise_for_status()
return response.text
def get_gerrit_tags(project, regex=None):
url = settings.GERRIT_URL.format("a/projects/%s/tags/" % project)
return get_gerrit_info(url)
def get_gerrit_branches(project, regex=None):
url = settings.GERRIT_URL.format("a/projects/%s/branches/" % project)
return get_gerrit_info(url)
def is_ngcp_project(projectname):
if projectname in settings.RELEASE_DASHBOARD_PROJECTS:
return True

@ -38,6 +38,7 @@ INSTALLED_APPS = [
"crispy_forms",
"jsonify",
"import_export",
"gerrit",
"tracker",
"hotfix",
"panel",

@ -52,9 +52,6 @@ TEMPLATES[0]["OPTIONS"]["debug"] = True # noqa
DJANGO_LOG_LEVEL = "DEBUG"
JENKINS_URL = "http://localhost"
GERRIT_URL = "https://gerrit.local/{}"
GERRIT_REST_HTTP_USER = "jenkins"
GERRIT_REST_HTTP_PASSWD = "verysecrethttppasswd"
GITWEB_URL = "https://git.local/gitweb/?p={}.git;a=commit;h={}"
TRACKER_WORKFRONT_CREDENTIALS = BASE_DIR / ".workfront.ini"
DOCKER_REGISTRY_URL = "https://localhost:5000/v2/{}"

Loading…
Cancel
Save