Skip to content

Commit bf2d944

Browse files
committed
feat(api,cli): make user agent configurable
1 parent 48cb89b commit bf2d944

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

gitlab/__init__.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,20 @@
2424
import requests.utils
2525

2626
import gitlab.config
27+
from gitlab.__version__ import (
28+
__author__,
29+
__copyright__,
30+
__email__,
31+
__license__,
32+
__title__,
33+
__version__,
34+
)
2735
from gitlab.const import * # noqa
2836
from gitlab.exceptions import * # noqa
2937
from gitlab import utils # noqa
3038
from requests_toolbelt.multipart.encoder import MultipartEncoder
3139

3240

33-
__title__ = "python-gitlab"
34-
__version__ = "2.6.0"
35-
__author__ = "Gauvain Pocentek"
36-
__email__ = "gauvainpocentek@gmail.com"
37-
__license__ = "LGPL3"
38-
__copyright__ = "Copyright 2013-2019 Gauvain Pocentek"
39-
4041
warnings.filterwarnings("default", category=DeprecationWarning, module="^gitlab")
4142

4243
REDIRECT_MSG = (
@@ -81,6 +82,7 @@ def __init__(
8182
per_page=None,
8283
pagination=None,
8384
order_by=None,
85+
user_agent=USER_AGENT,
8486
):
8587

8688
self._api_version = str(api_version)
@@ -90,7 +92,7 @@ def __init__(
9092
#: Timeout to use for requests to gitlab server
9193
self.timeout = timeout
9294
#: Headers that will be used in request to GitLab
93-
self.headers = {"User-Agent": "%s/%s" % (__title__, __version__)}
95+
self.headers = {"User-Agent": user_agent}
9496

9597
#: Whether SSL certificates should be validated
9698
self.ssl_verify = ssl_verify
@@ -204,6 +206,7 @@ def from_config(cls, gitlab_id=None, config_files=None):
204206
per_page=config.per_page,
205207
pagination=config.pagination,
206208
order_by=config.order_by,
209+
user_agent=config.user_agent,
207210
)
208211

209212
def auth(self):

gitlab/__version__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
__author__ = "Gauvain Pocentek, python-gitlab team"
2+
__copyright__ = "Copyright 2013-2019 Gauvain Pocentek, 2019-2021 python-gitlab team"
3+
__email__ = "gauvainpocentek@gmail.com"
4+
__license__ = "LGPL3"
5+
__title__ = "python-gitlab"
6+
__version__ = "2.6.0"

gitlab/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import os
1919
import configparser
2020

21+
from gitlab.const import USER_AGENT
22+
2123

2224
def _env_config():
2325
if "PYTHON_GITLAB_CFG" in os.environ:
@@ -177,3 +179,9 @@ def __init__(self, gitlab_id=None, config_files=None):
177179
self.order_by = self._config.get(self.gitlab_id, "order_by")
178180
except Exception:
179181
pass
182+
183+
self.user_agent = USER_AGENT
184+
try:
185+
self.user_agent = self._config.get("global", "user_agent")
186+
except Exception:
187+
pass

gitlab/const.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
# You should have received a copy of the GNU Lesser General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18+
from gitlab.__version__ import __title__, __version__
19+
20+
1821
NO_ACCESS = 0
1922
MINIMAL_ACCESS = 5
2023
GUEST_ACCESS = 10
@@ -51,3 +54,5 @@
5154

5255
# specific project scope
5356
SEARCH_SCOPE_PROJECT_NOTES = "notes"
57+
58+
USER_AGENT = "{}/{}".format(__title__, __version__)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
def get_version():
9-
with open("gitlab/__init__.py") as f:
9+
with open("gitlab/__version__.py") as f:
1010
for line in f:
1111
if line.startswith("__version__"):
1212
return eval(line.split("=")[-1])

0 commit comments

Comments
 (0)