Commit 5a045e49 authored by Lasse Schuirmann's avatar Lasse Schuirmann

Hoster: Add get_user

Fixes #115
parent 3fe172fb
Pipeline #34268383 passed with stage
in 29 seconds
......@@ -89,6 +89,16 @@ class GitHub(GitHubMixin, Hoster):
"""
return GitHubRepository(self._token, repository)
def get_user(self, username: str=None) -> GitHubUser:
"""
Retrieves repository name from given webhook data.
:param username: Username to fetch, use None to fetch authenticated
user.
:return: The user.
"""
return GitHubUser(self._token, username=username)
@staticmethod
def get_repo_name(webhook: dict):
"""
......
......@@ -10,13 +10,14 @@ from IGitt.GitLab.GitLabComment import GitLabComment
from IGitt.GitLab.GitLabCommit import GitLabCommit
from IGitt.GitLab.GitLabIssue import GitLabIssue
from IGitt.GitLab.GitLabMergeRequest import GitLabMergeRequest
from IGitt.GitLab.GitLabRepository import GitLabRepository
from IGitt.GitLab.GitLabUser import GitLabUser
from IGitt.Interfaces import get
from IGitt.Interfaces import AccessLevel
from IGitt.Interfaces.Actions import IssueActions, MergeRequestActions, \
PipelineActions
from IGitt.Interfaces.Comment import CommentType
from IGitt.Interfaces.Hoster import Hoster
from IGitt.GitLab.GitLabRepository import GitLabRepository
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)
......@@ -136,6 +137,16 @@ class GitLab(GitLabMixin, Hoster):
"""
return GitLabRepository(self._token, repository)
def get_user(self, username: str=None) -> GitLabUser:
"""
Retrieves repository name from given webhook data.
:param username: Username to fetch, use None to fetch authenticated
user.
:return: The user.
"""
return GitLabUser(self._token, identifier=username)
@staticmethod
def get_repo_name(webhook: dict):
"""
......
......@@ -7,6 +7,7 @@ from IGitt.Interfaces import IGittObject, Token
from IGitt.Interfaces.Repository import Repository
from IGitt.Interfaces.Issue import Issue
from IGitt.Interfaces.MergeRequest import MergeRequest
from IGitt.Interfaces.User import User
class Hoster(IGittObject):
......@@ -52,6 +53,16 @@ class Hoster(IGittObject):
"""
raise NotImplementedError
def get_user(self, username: str=None) -> User:
"""
Retrieves repository name from given webhook data.
:param username: Username to fetch, use None to fetch authenticated
user.
:return: The user.
"""
raise NotImplementedError
def handle_webhook(self, event: str, data: dict):
"""
Handles a webhook for you.
......
interactions:
- request:
body: '{}'
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
Content-Length: ['2']
Content-Type: [application/json]
User-Agent: [IGitt]
method: GET
uri: https://api.github.com/user?per_page=100
response:
body:
string: !!binary |
H4sIAAAAAAAAA6WUwY6bMBCG38XnsECyIRukqpfdY1Zaqa2qXtBgHOKtsS17SJqivHsHAxttlEOV
3LA9880/vz10TJlaapazWmIDKCIUHqPWC8dmTFYsT7PsKU0WyYxpU4mi32Kb57fsx89Xxd9f5q/v
9Z/N8+ZA4bAHBFe0TlHMDtH6PI6HTb94oAK7tuzJ3GgUGh+4aeI2ngp83X95JEjtRkyoRBsXOCtH
0pBOOB9f077DRl1IGRSEvGsZW6OUORDvsoP/Khl/pJPm4Vvq+h4UpXexwZ0gS6nNU2+O9HijvJDa
UeMe6RJ7mKfLcqK6TeKYTAIPmrR1sRPWBGpbeu6kRWn0jVI/IQhpXA1a/oU7kITwROpF3igqpBJC
7Onp3sgYcrvYOrkHfuztcoILuadbuId7ASEsHq2g8fk+zLGXKAqomn7Qt6C8oGGGhgJ0q9SM0UBY
0MdpWdIvYRg9ZXgwfToRDUia7SFrJ52AUn1QSmmmI9uWSvJisCxfzNi4Ed4gy+lfch6W84reezjj
xEVyBJBUzJM0i5I0ShffklW+XObzxS/qr7XVp5inKE2i+TLEZHkaYoLL1Pi5KhoEVUz7ozyq2L/h
6sp+Jf1vmj2oqclV75NSUBoHaNzQBh5MsQVO6wJamlSNcnJs9NkqIPu60W+2dUL0Q2KBE3O9ypbZ
/HG9vsa+lHk6/QO6+UgMrQUAAA==
headers:
Access-Control-Allow-Origin: ['*']
Access-Control-Expose-Headers: ['ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit,
X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes,
X-Poll-Interval']
Cache-Control: ['private, max-age=60, s-maxage=60']
Content-Encoding: [gzip]
Content-Security-Policy: [default-src 'none']
Content-Type: [application/json; charset=utf-8]
Date: ['Thu, 25 Oct 2018 07:57:25 GMT']
ETag: [W/"817666ce956fe69797f001caaea4ff97"]
Last-Modified: ['Thu, 25 Oct 2018 07:56:13 GMT']
Referrer-Policy: ['origin-when-cross-origin, strict-origin-when-cross-origin']
Server: [GitHub.com]
Status: [200 OK]
Strict-Transport-Security: [max-age=31536000; includeSubdomains; preload]
Transfer-Encoding: [chunked]
Vary: ['Accept, Authorization, Cookie, X-GitHub-OTP']
X-Accepted-OAuth-Scopes: ['']
X-Content-Type-Options: [nosniff]
X-Frame-Options: [deny]
X-GitHub-Media-Type: [github.v3; format=json]
X-GitHub-Request-Id: ['B196:4F1F:57B1D77:B3B09BF:5BD17765']
X-OAuth-Scopes: ['repo, user']
X-RateLimit-Limit: ['5000']
X-RateLimit-Remaining: ['4999']
X-RateLimit-Reset: ['1540457845']
X-XSS-Protection: [1; mode=block]
status: {code: 200, message: OK}
version: 1
......@@ -44,6 +44,10 @@ class GitHubHosterTest(IGittTestCase):
self.assertEqual(self.gh.get_repo('gitmate-test-user/test').full_name,
'gitmate-test-user/test')
def test_get_user(self):
self.assertEqual(self.gh.get_user('sils').username, 'sils')
self.assertEqual(self.gh.get_user().username, 'gitmate-test-user')
class TestGitHubWebhook(IGittTestCase):
......
interactions:
- request:
body: '{}'
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
Content-Length: ['2']
Content-Type: [application/json]
User-Agent: [IGitt]
method: GET
uri: https://gitlab.com/api/v4/users?per_page=100&username=sils
response:
body: {string: '[{"id":104269,"name":"Lasse Schuirmann","username":"sils","state":"active","avatar_url":"https://secure.gravatar.com/avatar/ea9b9ed83df6acc43cc4ea26b0447ea7?s=80\u0026d=identicon","web_url":"https://gitlab.com/sils"}]'}
headers:
Cache-Control: ['max-age=0, private, must-revalidate']
Content-Length: ['217']
Content-Type: [application/json]
Date: ['Thu, 25 Oct 2018 07:53:43 GMT']
Etag: [W/"a37e5bb674e0601b24ce96829f3ce168"]
Link: ['<https://gitlab.com/api/v4/users?active=false&blocked=false&external=false&page=1&per_page=100&skip_ldap=false&username=sils&with_custom_attributes=false>;
rel="first", <https://gitlab.com/api/v4/users?active=false&blocked=false&external=false&page=1&per_page=100&skip_ldap=false&username=sils&with_custom_attributes=false>;
rel="last"']
RateLimit-Limit: ['600']
RateLimit-Observed: ['1']
RateLimit-Remaining: ['599']
RateLimit-Reset: ['1540454083']
RateLimit-ResetTime: ['Fri, 25 Oct 2018 07:54:43 GMT']
Server: [nginx]
Strict-Transport-Security: [max-age=31536000]
Vary: [Origin]
X-Content-Type-Options: [nosniff]
X-Frame-Options: [SAMEORIGIN]
X-Next-Page: ['']
X-Page: ['1']
X-Per-Page: ['100']
X-Prev-Page: ['']
X-Request-Id: [92599139-30da-4fd2-b900-0ea21840b286]
X-Runtime: ['0.025853']
X-Total: ['1']
X-Total-Pages: ['1']
status: {code: 200, message: OK}
- request:
body: '{}'
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
Content-Length: ['2']
Content-Type: [application/json]
User-Agent: [IGitt]
method: GET
uri: https://gitlab.com/api/v4/users/104269?per_page=100
response:
body: {string: '{"id":104269,"name":"Lasse Schuirmann","username":"sils","state":"active","avatar_url":"https://secure.gravatar.com/avatar/ea9b9ed83df6acc43cc4ea26b0447ea7?s=80\u0026d=identicon","web_url":"https://gitlab.com/sils","created_at":"2015-03-01T14:47:31.571Z","bio":"","location":"Hamburg","public_email":"[email protected]","skype":"","linkedin":"","twitter":"","website_url":"viperdev.io","organization":null}'}
headers:
Cache-Control: ['max-age=0, private, must-revalidate']
Content-Length: ['415']
Content-Type: [application/json]
Date: ['Thu, 25 Oct 2018 07:53:43 GMT']
Etag: [W/"64d163242b2d574c090d65f287ef86e1"]
RateLimit-Limit: ['600']
RateLimit-Observed: ['2']
RateLimit-Remaining: ['598']
RateLimit-Reset: ['1540454083']
RateLimit-ResetTime: ['Fri, 25 Oct 2018 07:54:43 GMT']
Server: [nginx]
Strict-Transport-Security: [max-age=31536000]
Vary: [Origin]
X-Content-Type-Options: [nosniff]
X-Frame-Options: [SAMEORIGIN]
X-Request-Id: [5be14315-e3f1-4ef4-a0cb-a76984f1426e]
X-Runtime: ['0.022270']
status: {code: 200, message: OK}
- request:
body: '{}'
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
Content-Length: ['2']
Content-Type: [application/json]
User-Agent: [IGitt]
method: GET
uri: https://gitlab.com/api/v4/user?per_page=100
response:
body: {string: "{\"id\":1369631,\"name\":\"GitMate Labs \U0001F47D\",\"username\":\"gitmate-test-user\",\"state\":\"active\",\"avatar_url\":\"https://assets.gitlab-static.net/uploads/-/system/user/avatar/1369631/avatar.png\",\"web_url\":\"https://gitlab.com/gitmate-test-user\",\"created_at\":\"2017-06-02T07:14:25.112Z\",\"bio\":\"\",\"location\":\"\",\"public_email\":\"\",\"skype\":\"\",\"linkedin\":\"\",\"twitter\":\"\",\"website_url\":\"\",\"organization\":\"\",\"last_sign_in_at\":\"2018-05-24T13:25:34.745Z\",\"confirmed_at\":\"2017-06-02T07:17:21.578Z\",\"last_activity_on\":\"2018-08-15\",\"email\":\"[email protected]\",\"theme_id\":null,\"color_scheme_id\":1,\"projects_limit\":100000,\"current_sign_in_at\":\"2018-08-15T11:02:55.378Z\",\"identities\":[],\"can_create_group\":true,\"can_create_project\":true,\"two_factor_enabled\":false,\"external\":false,\"private_profile\":null,\"shared_runners_minutes_limit\":2000}"}
headers:
Cache-Control: ['max-age=0, private, must-revalidate']
Content-Length: ['820']
Content-Type: [application/json]
Date: ['Thu, 25 Oct 2018 07:53:44 GMT']
Etag: [W/"3c0c3a2d516c38893e93aa1aaa48ab22"]
RateLimit-Limit: ['600']
RateLimit-Observed: ['1']
RateLimit-Remaining: ['599']
RateLimit-Reset: ['1540454084']
RateLimit-ResetTime: ['Fri, 25 Oct 2018 07:54:44 GMT']
Server: [nginx]
Strict-Transport-Security: [max-age=31536000]
Vary: [Origin]
X-Content-Type-Options: [nosniff]
X-Frame-Options: [SAMEORIGIN]
X-Request-Id: [11462a20-f463-4f45-a1d1-ce87e447bc0c]
X-Runtime: ['0.033563']
status: {code: 200, message: OK}
version: 1
......@@ -60,6 +60,10 @@ class GitLabHosterTest(IGittTestCase):
self.assertEqual(self.gl.get_repo('gitmate-test-user/test').full_name,
'gitmate-test-user/test')
def test_get_user(self):
self.assertEqual(self.gl.get_user('sils').username, 'sils')
self.assertEqual(self.gl.get_user().username, 'gitmate-test-user')
class GitLabWebhookTest(IGittTestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment