Skip to content

Commit c112429

Browse files
authored
Merge pull request openSUSE#1944 from dmach/git-obs-G-url
Change 'git-obs -G' to accept url to select a gitea login entry
2 parents 7c93ed4 + 2d6a7d4 commit c112429

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

osc/commandline_git.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ def init_arguments(self):
203203
"--gitea-login",
204204
help=(
205205
"Name of the login entry in the config file. Default: $GIT_OBS_LOGIN or the default entry from the config file.\n"
206-
"Alternatively, you can omit this argument and set GIT_OBS_GITEA_URL, GIT_OBS_GITEA_USER, and GIT_OBS_GITEA_TOKEN environmental variables instead.\n"
206+
"Alternatively, you can use gitea url as long as there's only a single matching entry in the config file.\n"
207+
"\n"
208+
"You can omit this argument and set GIT_OBS_GITEA_URL, GIT_OBS_GITEA_USER, and GIT_OBS_GITEA_TOKEN environmental variables instead.\n"
207209
"Optional variables: GIT_OBS_GITEA_SSH_KEY\n"
208210
"\n"
209211
"To override the existing values from the config file, you can specify the following environmental variables:\n"
@@ -262,8 +264,17 @@ def gitea_conf(self, value):
262264

263265
@property
264266
def gitea_login(self):
267+
from . import gitea_api
268+
265269
if self._gitea_login is None:
266-
self._gitea_login = self.gitea_conf.get_login(name=self._args.gitea_login)
270+
try:
271+
self._gitea_login = self.gitea_conf.get_login(name=self._args.gitea_login)
272+
except gitea_api.Login.DoesNotExist as e:
273+
try:
274+
self._gitea_login = self.gitea_conf.get_login_by_url_user(url=self._args.gitea_login)
275+
except gitea_api.Login.DoesNotExist:
276+
raise e
277+
267278
return self._gitea_login
268279

269280
@gitea_login.setter

osc/gitea_api/conf.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,21 @@ def __str__(self):
4444
" * either set the default entry by running `git-obs login update <login> --set-as-default`\n"
4545
" * or run the command with `-G/--gitea-login <login>` option"
4646
)
47-
kwargs_str = ", ".join([f"{key}={value}" for key, value in self.kwargs.items()])
47+
kwargs_str = ", ".join([f"{key}={value}" for key, value in self.kwargs.items() if value])
4848
return (
4949
f"Could not find a matching Gitea config entry: {kwargs_str}\n"
5050
" * see `git-obs login list` for valid entries"
5151
)
5252

53+
class MultipleEntriesReturned(oscerr.OscBaseError):
54+
def __init__(self, **kwargs):
55+
super().__init__()
56+
self.kwargs = kwargs
57+
58+
def __str__(self):
59+
kwargs_str = ", ".join([f"{key}={value}" for key, value in self.kwargs.items() if value])
60+
return f"Multiple login entries returned for the following args: {kwargs_str}"
61+
5362
def __init__(self, **kwargs):
5463
# ignore extra fields
5564
for key in list(kwargs):
@@ -150,16 +159,25 @@ def get_login(self, name: Optional[str] = None) -> Login:
150159
return login
151160
raise Login.DoesNotExist(name=name)
152161

153-
def get_login_by_url_user(self, url: str, user: str) -> Login:
162+
def get_login_by_url_user(self, url: str, user: Optional[str] = None) -> Login:
154163
"""
155164
Return ``Login`` object for the given ``url`` and ``user``.
156165
"""
157166
from .git import Git
158167

159168
# exact match
169+
matches = []
160170
for login in self.list_logins():
161-
if (login.url, login.user) == (url, user):
162-
return login
171+
if user is None:
172+
if login.url == url:
173+
matches.append(login)
174+
elif (login.url, login.user) == (url, user):
175+
matches.append(login)
176+
177+
if len(matches) == 1:
178+
return matches[0]
179+
if len(matches) >= 2:
180+
raise Login.MultipleEntriesReturned(url=url, user=user)
163181

164182
def url_to_hostname(value):
165183
netloc = Git.urlparse(value).netloc
@@ -175,9 +193,18 @@ def url_to_hostname(value):
175193
return netloc
176194

177195
# match only hostname (netloc without 'user@' and ':port') + user
196+
matches = []
178197
for login in self.list_logins():
179-
if (url_to_hostname(login.url), login.user) == (url_to_hostname(url), user):
180-
return login
198+
if user is None:
199+
if url_to_hostname(login.url) == url_to_hostname(url):
200+
matches.append(login)
201+
elif (url_to_hostname(login.url), login.user) == (url_to_hostname(url), user):
202+
matches.append(login)
203+
204+
if len(matches) == 1:
205+
return matches[0]
206+
if len(matches) >= 2:
207+
raise Login.MultipleEntriesReturned(url=url, user=user)
181208

182209
raise Login.DoesNotExist(url=url, user=user)
183210

0 commit comments

Comments
 (0)