Skip to content

Commit dfc11c3

Browse files
committed
🚧 improved support of custom URLs in RepositoryService
- removed url handling code from GogsService - added handling of new URL details parameters in RepositoryService Fixes: #18 Signed-off-by: Guyzmo <guyzmo+github+pub@m0g.net>
1 parent 647e556 commit dfc11c3

File tree

2 files changed

+17
-31
lines changed

2 files changed

+17
-31
lines changed

git_repo/services/ext/gogs.py

+6-29
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
from git.exc import GitCommandError
1616

1717
class GogsClient(GogsApi):
18-
def __init__(self, *args, **kwarg):
18+
def __init__(self):
1919
self.session = Session()
20+
21+
def setup(self, *args, **kwarg):
2022
super().__init__(*args, session=self.session, **kwarg)
2123

2224
def set_token(self, token):
@@ -69,11 +71,11 @@ class GogsService(RepositoryService):
6971
fqdn = 'try.gogs.io'
7072

7173
def __init__(self, *args, **kwargs):
72-
self.url_base, self.fqdn = self._url_parse(self.fqdn)
73-
self.gg = GogsClient(self.url_base)
74+
self.gg = GogsClient()
7475

7576
super().__init__(*args, **kwargs)
7677

78+
self.gg.setup(self.url_ro)
7779
self.gg.set_token(self._privatekey)
7880
self.gg.set_default_private(self.default_create_private)
7981
self.gg.setup_session(
@@ -95,36 +97,11 @@ def connect(self):
9597
else:
9698
raise err
9799

98-
@classmethod
99-
def _url_parse(cls, url):
100-
if '://' not in url:
101-
url = 'https://'+url
102-
parse = urlparse(url)
103-
url_base = urlunparse((parse.scheme, parse.netloc)+('',)*4)
104-
fqdn = parse.hostname
105-
return url_base, fqdn
106-
107-
@property
108-
def url_ro(self):
109-
return self.url_base
110-
111-
@property
112-
def url_rw(self):
113-
url = self.ssh_url
114-
if '@' in url:
115-
return url
116-
return '@'.join([self.git_user, url])
117-
118100
@classmethod
119101
def get_auth_token(cls, login, password, prompt=None):
120102
import platform
121103
name = 'git-repo token used on {}'.format(platform.node())
122-
if '/' in login:
123-
url, login = login.rsplit('/', 1)
124-
else:
125-
url = input('URL [{}]> '.format(cls.fqdn)) or cls.fqdn
126-
url_base, fqdn = cls._url_parse(url)
127-
gg = GogsApi(url_base)
104+
gg = GogsApi(cls.build_url())
128105
auth = UsernamePassword(login, password)
129106
tokens = gg.get_tokens(auth, login)
130107
tokens = dict((token.name, token.token) for token in tokens)

git_repo/services/service.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from git import RemoteProgress, config as git_config
1010
from progress.bar import IncrementalBar as Bar
1111

12+
from urllib.parse import ParseResult
1213
from subprocess import call
1314

1415
from ..exceptions import (
@@ -202,14 +203,22 @@ def __init__(self, r=None, c=None, hc=[]):
202203
'''name of the git user to use for SSH remotes'''
203204
git_user = 'git'
204205

206+
@classmethod
207+
def build_url(cls):
208+
netloc = cls.fqdn if not getattr(cls, 'port', None) else ':'.join([cls.fqdn, cls.port])
209+
if not getattr(cls, 'scheme', None):
210+
cls.scheme = 'https'
211+
return ParseResult(cls.scheme, netloc, *['']*4).geturl()
212+
205213
@property
206214
def url_ro(self):
207215
'''Property that returns the HTTP URL of the service'''
208-
return 'https://{}'.format(self.fqdn)
216+
return self.build_url()
209217

210218
@property
211219
def url_rw(self):
212-
return '{}@{}'.format(self.git_user, self.fqdn)
220+
url = self.ssh_url
221+
return url if '@' in url else '@'.join([self.git_user, url])
213222

214223
def format_path(self, repository, namespace=None, rw=False):
215224
'''format the repository's URL

0 commit comments

Comments
 (0)