mirror of https://github.com/sipwise/repoapi.git
Change-Id: I83f5dc75315bfd9df22c78f3169c72484e7b2c19pull/9/head
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)
|
Loading…
Reference in new issue