Skip to content

Commit

Permalink
🚧 improved support of custom URLs in RepositoryService
Browse files Browse the repository at this point in the history
- removed url handling code from GogsService
- added handling of new URL details parameters in RepositoryService

Fixes: #18
Signed-off-by: Guyzmo <[email protected]>
  • Loading branch information
guyzmo committed Feb 2, 2017
1 parent f7856dc commit 1b0237f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
35 changes: 6 additions & 29 deletions git_repo/services/ext/gogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
from git.exc import GitCommandError

class GogsClient(GogsApi):
def __init__(self, *args, **kwarg):
def __init__(self):
self.session = Session()

def setup(self, *args, **kwarg):
super().__init__(*args, session=self.session, **kwarg)

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

def __init__(self, *args, **kwargs):
self.url_base, self.fqdn = self._url_parse(self.fqdn)
self.gg = GogsClient(self.url_base)
self.gg = GogsClient()

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

self.gg.setup(self.url_ro)
self.gg.set_token(self._privatekey)
self.gg.set_default_private(self.default_create_private)
self.gg.setup_session(
Expand All @@ -95,36 +97,11 @@ def connect(self):
else:
raise err

@classmethod
def _url_parse(cls, url):
if '://' not in url:
url = 'https://'+url
parse = urlparse(url)
url_base = urlunparse((parse.scheme, parse.netloc)+('',)*4)
fqdn = parse.hostname
return url_base, fqdn

@property
def url_ro(self):
return self.url_base

@property
def url_rw(self):
url = self.ssh_url
if '@' in url:
return url
return '@'.join([self.git_user, url])

@classmethod
def get_auth_token(cls, login, password, prompt=None):
import platform
name = 'git-repo token used on {}'.format(platform.node())
if '/' in login:
url, login = login.rsplit('/', 1)
else:
url = input('URL [{}]> '.format(cls.fqdn)) or cls.fqdn
url_base, fqdn = cls._url_parse(url)
gg = GogsApi(url_base)
gg = GogsApi(cls.build_url())
auth = UsernamePassword(login, password)
tokens = gg.get_tokens(auth, login)
tokens = dict((token.name, token.token) for token in tokens)
Expand Down
13 changes: 11 additions & 2 deletions git_repo/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from git import RemoteProgress, config as git_config
from progress.bar import IncrementalBar as Bar

from urllib.parse import ParseResult
from subprocess import call

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

@classmethod
def build_url(cls):
netloc = cls.fqdn if not getattr(cls, 'port', None) else ':'.join([cls.fqdn, cls.port])
if not getattr(cls, 'scheme', None):
cls.scheme = 'https'
return ParseResult(cls.scheme, netloc, *['']*4).geturl()

@property
def url_ro(self):
'''Property that returns the HTTP URL of the service'''
return 'https://{}'.format(self.fqdn)
return self.build_url()

@property
def url_rw(self):
return '{}@{}'.format(self.git_user, self.fqdn)
url = self.ssh_url
return url if '@' in url else '@'.join([self.git_user, url])

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

0 comments on commit 1b0237f

Please sign in to comment.