Skip to content

Make gitlab.Gitlab.from_config a classmethod #738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions gitlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ def api_version(self):
"""The API version used (4 only)."""
return self._api_version

@staticmethod
def from_config(gitlab_id=None, config_files=None):
@classmethod
def from_config(cls, gitlab_id=None, config_files=None):
"""Create a Gitlab connection from configuration files.

Args:
Expand All @@ -181,13 +181,13 @@ def from_config(gitlab_id=None, config_files=None):
"""
config = gitlab.config.GitlabConfigParser(gitlab_id=gitlab_id,
config_files=config_files)
return Gitlab(config.url, private_token=config.private_token,
oauth_token=config.oauth_token,
ssl_verify=config.ssl_verify, timeout=config.timeout,
http_username=config.http_username,
http_password=config.http_password,
api_version=config.api_version,
per_page=config.per_page)
return cls(config.url, private_token=config.private_token,
oauth_token=config.oauth_token,
ssl_verify=config.ssl_verify, timeout=config.timeout,
http_username=config.http_username,
http_password=config.http_password,
api_version=config.api_version,
per_page=config.per_page)

def auth(self):
"""Performs an authentication.
Expand Down
32 changes: 32 additions & 0 deletions gitlab/tests/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

from __future__ import print_function

import os
import pickle
import tempfile
try:
import unittest
except ImportError:
Expand All @@ -34,6 +36,17 @@
from gitlab.v4.objects import * # noqa


valid_config = b"""[global]
default = one
ssl_verify = true
timeout = 2

[one]
url = http://one.url
private_token = ABCDEF
"""


class TestSanitize(unittest.TestCase):
def test_do_nothing(self):
self.assertEqual(1, gitlab._sanitize(1))
Expand Down Expand Up @@ -536,3 +549,22 @@ def resp_get_user(url, request):
self.assertEqual(type(user), User)
self.assertEqual(user.name, "name")
self.assertEqual(user.id, 1)

def _default_config(self):
fd, temp_path = tempfile.mkstemp()
os.write(fd, valid_config)
os.close(fd)
return temp_path

def test_from_config(self):
config_path = self._default_config()
gitlab.Gitlab.from_config('one', [config_path])
os.unlink(config_path)

def test_subclass_from_config(self):
class MyGitlab(gitlab.Gitlab):
pass
config_path = self._default_config()
gl = MyGitlab.from_config('one', [config_path])
self.assertEqual(type(gl).__name__, 'MyGitlab')
os.unlink(config_path)