Read about our upcoming Code of Conduct on this issue

Commit c6a9f145 authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

ProjectMirror: new helper class

Preferred to call them ProjectMirror rather than the context-lacking
RemoteMirror.

We should do something a like for the more feature-full Merge
Requests.

Amazingly, there was not direct GET for a given mirror, so we're
resorting to listing all of them.

We could later on add more interesting attributes, but this isn't
what we are doing for other objects (yet). Here's a poor man
advantage: by calling `api_get()` explicitely, there's no ambiguity
between local (maybe stale) state and the server state.
parent 7ef57c021091
Pipeline #13862 passed with stages
in 35 minutes and 58 seconds
# Copyright 2020 Georges Racinet <georges.racinet@octobus.net>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
#
# SPDX-License-Identifier: GPL-2.0-or-later
def test_create(test_project):
mirror = test_project.api_create_mirror(url='https://hg.test',
hg_mirror_type='hg-pull')
info = mirror.api_get()
assert info['hg_mirror_type'] == 'hg-pull'
assert info['enabled'] is False
......@@ -18,6 +18,7 @@ from . import wait_assert
from .constants import DATA_DIR
from .hg import LocalRepo
from .group import UserNameSpace
from .project_mirror import ProjectMirror
from .selenium import could_click_element
logger = logging.getLogger(__name__)
......@@ -1145,6 +1146,22 @@ class Project(object):
self._disk_path = None
return resp
def api_create_mirror(self, **params):
return ProjectMirror.api_create(self, **params)
def api_list_mirrors(self, check=True):
"""Return a list of dicts, not of ProjectMirror instances.
The reason is that ProjectMirror currently does not keep more
state than that's needed for identification.
"""
resp = self.owner_api_get(subpath='remote_mirrors')
if not check:
return resp
assert resp.status_code < 300
return resp.json()
def __enter__(self):
return self
......
# Copyright 2020 Georges Racinet <georges.racinet@octobus.net>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
#
# SPDX-License-Identifier: GPL-2.0-or-later
import attr
@attr.s
class ProjectMirror:
id = attr.ib()
project = attr.ib()
@classmethod
def api_create(cls, project, **data):
resp = project.owner_api_post(subpath='remote_mirrors',
data=data)
assert resp.status_code < 300
return cls(project=project, id=resp.json()['id'])
@classmethod
def api_list(cls, project, check=True):
"""Return a list of dicts, not of ProjectMirror instances.
The reason is that ProjectMirror currently does not keep more
state than that's needed for identification.
"""
resp = project.owner_api_get(subpath='remote_mirrors')
if not check:
return resp
assert resp.status_code < 300
return resp.json()
@property
def api_subpath(self):
return 'remote_mirrors/%d' % self.id
def api_update(self, check=True, **data):
resp = self.project.owner_api_put(subpath=self.api_subpath, **data)
if not check:
return resp
assert resp.status_code < 300
return resp.json()
def api_get(self):
# no direct method for that, have to resort to the list
for info in self.api_list(self.project):
if info['id'] == self.id:
return info
raise LookupError(self)
def api_trigger(self, check=True):
resp = self.project.owner_api_put(
subpath=self.api_subpath + '/trigger')
if not check:
return resp
assert resp.status_code < 300
return resp.json()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment