diff --git a/gerrit/__init__.py b/gerrit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gerrit/apps.py b/gerrit/apps.py new file mode 100644 index 0000000..b7274f5 --- /dev/null +++ b/gerrit/apps.py @@ -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 . +# +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 diff --git a/gerrit/conf.py b/gerrit/conf.py new file mode 100644 index 0000000..f3d52b2 --- /dev/null +++ b/gerrit/conf.py @@ -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 . +# +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" diff --git a/gerrit/migrations/__init__.py b/gerrit/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gerrit/test_utils.py b/gerrit/test_utils.py new file mode 100644 index 0000000..fd035c2 --- /dev/null +++ b/gerrit/test_utils.py @@ -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 . +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) diff --git a/gerrit/utils.py b/gerrit/utils.py new file mode 100644 index 0000000..1791a04 --- /dev/null +++ b/gerrit/utils.py @@ -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 . +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) diff --git a/release_dashboard/models.py b/release_dashboard/models.py index 7bd8df4..169950e 100644 --- a/release_dashboard/models.py +++ b/release_dashboard/models.py @@ -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( diff --git a/release_dashboard/test/test_models.py b/release_dashboard/test/test_models.py index e045e6a..1c1180d 100644 --- a/release_dashboard/test/test_models.py +++ b/release_dashboard/test/test_models.py @@ -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) diff --git a/release_dashboard/utils/build.py b/release_dashboard/utils/build.py index 560ff12..a6eef40 100644 --- a/release_dashboard/utils/build.py +++ b/release_dashboard/utils/build.py @@ -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 diff --git a/repoapi/settings/common.py b/repoapi/settings/common.py index 35b0f96..26a40f0 100644 --- a/repoapi/settings/common.py +++ b/repoapi/settings/common.py @@ -38,6 +38,7 @@ INSTALLED_APPS = [ "crispy_forms", "jsonify", "import_export", + "gerrit", "tracker", "hotfix", "panel", diff --git a/repoapi/settings/test.py b/repoapi/settings/test.py index ecd6e1e..7ebcb85 100644 --- a/repoapi/settings/test.py +++ b/repoapi/settings/test.py @@ -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/{}"