Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7bc388c

Browse files
committed
feat!: default to gitlab.com if no URL given
1 parent3742405 commit7bc388c

File tree

5 files changed

+92
-31
lines changed

5 files changed

+92
-31
lines changed

‎docs/api-usage.rst

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,35 @@
22
Getting started with the API
33
############################
44

5-
python-gitlab only supports GitLabAPIs v4.
5+
python-gitlab only supports GitLabAPI v4.
66

77
``gitlab.Gitlab`` class
88
=======================
99

10-
To connect toaGitLabserver, create a ``gitlab.Gitlab`` object:
10+
To connect toGitLab.com or anotherGitLabinstance, create a ``gitlab.Gitlab`` object:
1111

1212
..code-block::python
1313
1414
import gitlab
1515
16-
# private token or personal token authentication
17-
# Note that a 'url' that results in 301/302 redirects will cause an error
18-
# (see below for more information).
16+
# anonymous read-only access for public resources (GitLab.com)
17+
gl= gitlab.Gitlab()
18+
19+
# anonymous read-only access for public resources (self-hosted GitLab instance)
20+
gl= gitlab.Gitlab('https://gitlab.example.com')
21+
22+
# private token or personal token authentication (GitLab.com)
23+
gl= gitlab.Gitlab(private_token='JVNSESs8EwWRx5yDxM5q')
24+
25+
# private token or personal token authentication (self-hosted GitLab instance)
1926
gl= gitlab.Gitlab(url='https://gitlab.example.com',private_token='JVNSESs8EwWRx5yDxM5q')
2027
2128
# oauth token authentication
22-
gl= gitlab.Gitlab('http://10.0.0.1',oauth_token='my_long_token_here')
29+
gl= gitlab.Gitlab('https://gitlab.example.com',oauth_token='my_long_token_here')
2330
2431
# job token authentication (to be used in CI)
2532
import os
26-
gl= gitlab.Gitlab('http://10.0.0.1',job_token=os.environ['CI_JOB_TOKEN'])
27-
28-
# anonymous gitlab instance, read-only for public resources
29-
gl= gitlab.Gitlab('http://10.0.0.1')
30-
31-
# Define your own custom user agent for requests
32-
gl= gitlab.Gitlab('http://10.0.0.1',user_agent='my-package/1.0.0')
33+
gl= gitlab.Gitlab('https://gitlab.example.com',job_token=os.environ['CI_JOB_TOKEN'])
3334
3435
# make an API request to create the gl.user object. This is mandatory if you
3536
# use the username/password authentication.
@@ -46,15 +47,17 @@ configuration files.
4647

4748
..warning::
4849

49-
If the GitLab server you are using redirects requests from http to https,
50-
make sure to use the ``https://`` protocol in the URL definition.
50+
Note that a url that results in 301/302 redirects will raise an error,
51+
so it is highly recommended to use the final destination in the ``url`` field.
52+
For example, if the GitLab server you are using redirects requests from http
53+
to https, make sure to use the ``https://`` protocol in the URL definition.
5154

52-
..note::
55+
A URL that redirects using 301/302 (rather than 307/308) will most likely
56+
`cause malformed POST and PUT requests<https://github.com/psf/requests/blob/c45a4dfe6bfc6017d4ea7e9f051d6cc30972b310/requests/sessions.py#L324-L332>`_.
5357

54-
It is highly recommended to use the final destination in the ``url`` field.
55-
What this means is that you should not use a URL which redirects as it will
56-
most likely cause errors. python-gitlab will raise a ``RedirectionError``
57-
when it encounters a redirect which it believes will cause an error.
58+
python-gitlab will therefore raise a ``RedirectionError`` when it encounters
59+
a redirect which it believes will cause such an error, to avoid confusion
60+
between successful GET and failing POST/PUT requests on the same instance.
5861

5962
Note on password authentication
6063
-------------------------------

‎docs/cli-usage.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,17 @@ You must define the ``url`` in each GitLab server section.
8686

8787
..warning::
8888

89-
If the GitLab server you are using redirects requests from http to https,
90-
make sure to use the ``https://`` protocol in the ``url`` definition.
89+
Note that a url that results in 301/302 redirects will raise an error,
90+
so it is highly recommended to use the final destination in the ``url`` field.
91+
For example, if the GitLab server you are using redirects requests from http
92+
to https, make sure to use the ``https://`` protocol in the URL definition.
9193

92-
..note::
94+
A URL that redirects using 301/302 (rather than 307/308) will most likely
95+
`cause malformed POST and PUT requests<https://github.com/psf/requests/blob/c45a4dfe6bfc6017d4ea7e9f051d6cc30972b310/requests/sessions.py#L324-L332>`_.
9396

94-
It is highly recommended to use the final destination in the ``url`` field.
95-
What this means is that you should not use a URL which redirects as it will
96-
most likely cause errors. python-gitlab will raise a ``RedirectionError``
97-
when it encounters a redirect which it believes will cause an error.
97+
python-gitlab will therefore raise a ``RedirectionError`` when it encounters
98+
a redirect which it believes will cause such an error, to avoid confusion
99+
between successful GET and failing POST/PUT requests on the same instance.
98100

99101
Only one of ``private_token``, ``oauth_token`` or ``job_token`` should be
100102
defined. If neither are defined an anonymous request will be sent to the Gitlab

‎gitlab/client.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Gitlab(object):
3939
"""Represents a GitLab server connection.
4040
4141
Args:
42-
url (str): The URL of the GitLab server.
42+
url (str): The URL of the GitLab server (defaults to https://gitlab.com).
4343
private_token (str): The user private token
4444
oauth_token (str): An oauth token
4545
job_token (str): A CI job token
@@ -59,7 +59,7 @@ class Gitlab(object):
5959

6060
def__init__(
6161
self,
62-
url:str,
62+
url:Optional[str]=None,
6363
private_token:Optional[str]=None,
6464
oauth_token:Optional[str]=None,
6565
job_token:Optional[str]=None,
@@ -79,7 +79,7 @@ def __init__(
7979
self._api_version=str(api_version)
8080
self._server_version:Optional[str]=None
8181
self._server_revision:Optional[str]=None
82-
self._base_url=url.rstrip("/")
82+
self._base_url=self._get_base_url(url)
8383
self._url="%s/api/v%s"% (self._base_url,api_version)
8484
#: Timeout to use for requests to gitlab server
8585
self.timeout=timeout
@@ -442,6 +442,17 @@ def _get_session_opts(self) -> Dict[str, Any]:
442442
"verify":self.ssl_verify,
443443
}
444444

445+
def_get_base_url(self,url:Optional[str]=None)->str:
446+
"""Return the base URL with the trailing slash stripped.
447+
If the URL is a Falsy value, return the default URL.
448+
Returns:
449+
str: The base URL
450+
"""
451+
ifnoturl:
452+
returngitlab.const.DEFAULT_URL
453+
454+
returnurl.rstrip("/")
455+
445456
def_build_url(self,path:str)->str:
446457
"""Returns the full url from path.
447458

‎gitlab/const.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
fromgitlab.__version__import__title__,__version__
1919

20+
DEFAULT_URL:str="https://gitlab.com"
21+
2022
NO_ACCESS:int=0
2123
MINIMAL_ACCESS:int=5
2224
GUEST_ACCESS:int=10

‎tests/unit/test_gitlab.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
importpytest
2222
fromhttmockimportHTTMock,response,urlmatch,with_httmock# noqa
2323

24-
fromgitlabimportGitlab,GitlabList,USER_AGENT
24+
fromgitlabimportDEFAULT_URL,Gitlab,GitlabList,USER_AGENT
2525
fromgitlab.v4.objectsimportCurrentUser
2626

27+
localhost="http://localhost"
2728
username="username"
2829
user_id=1
30+
token="abc123"
2931

3032

3133
@urlmatch(scheme="http",netloc="localhost",path="/api/v4/user",method="get")
@@ -127,6 +129,47 @@ def test_gitlab_token_auth(gl, callback=None):
127129
assertisinstance(gl.user,CurrentUser)
128130

129131

132+
deftest_gitlab_default_url():
133+
gl=Gitlab()
134+
assertgl.url==DEFAULT_URL
135+
136+
137+
@pytest.mark.parametrize(
138+
"args, kwargs, expected_url, expected_private_token, expected_oauth_token",
139+
[
140+
([], {},DEFAULT_URL,None,None),
141+
([None,token], {},DEFAULT_URL,token,None),
142+
([localhost], {},localhost,None,None),
143+
([localhost,token], {},localhost,token,None),
144+
([localhost,None,token], {},localhost,None,token),
145+
([], {"private_token":token},DEFAULT_URL,token,None),
146+
([], {"oauth_token":token},DEFAULT_URL,None,token),
147+
([], {"url":localhost},localhost,None,None),
148+
([], {"url":localhost,"private_token":token},localhost,token,None),
149+
([], {"url":localhost,"oauth_token":token},localhost,None,token),
150+
],
151+
ids=[
152+
"no_args",
153+
"args_private_token",
154+
"args_url",
155+
"args_url_private_token",
156+
"args_url_oauth_token",
157+
"kwargs_private_token",
158+
"kwargs_oauth_token",
159+
"kwargs_url",
160+
"kwargs_url_private_token",
161+
"kwargs_url_oauth_token",
162+
],
163+
)
164+
deftest_gitlab_args_kwargs(
165+
args,kwargs,expected_url,expected_private_token,expected_oauth_token
166+
):
167+
gl=Gitlab(*args,**kwargs)
168+
assertgl.url==expected_url
169+
assertgl.private_token==expected_private_token
170+
assertgl.oauth_token==expected_oauth_token
171+
172+
130173
deftest_gitlab_from_config(default_config):
131174
config_path=default_config
132175
Gitlab.from_config("one", [config_path])

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp