Skip to content

Commit 112526c

Browse files
committed
[gitlab] Add organization info
This code enhances the backend to fetch user data, which includes his organization.
1 parent dad170a commit 112526c

File tree

5 files changed

+278
-38
lines changed

5 files changed

+278
-38
lines changed

perceval/backends/core/gitlab.py

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class GitLab(Backend):
8181
:param sleep_time: time to sleep in case
8282
:param blacklist_ids: ids of items that must not be retrieved
8383
"""
84-
version = '0.6.2'
84+
version = '0.7.0'
8585

8686
CATEGORIES = [CATEGORY_ISSUE, CATEGORY_MERGE_REQUEST]
8787

@@ -221,13 +221,25 @@ def __fetch_issues(self, from_date):
221221

222222
self.__init_issue_extra_fields(issue)
223223

224+
issue['author_data'] = self.__get_user(issue['author'])
225+
issue['assignee_data'] = self.__get_user(issue['assignee'])
226+
issue['assignees_data'] = self.__get_issue_assignees(issue['assignees'])
224227
issue['notes_data'] = \
225228
self.__get_issue_notes(issue_id)
226229
issue['award_emoji_data'] = \
227230
self.__get_award_emoji(GitLabClient.ISSUES, issue_id)
228231

229232
yield issue
230233

234+
def __get_issue_assignees(self, raw_assignees):
235+
"""Get issue assignees"""
236+
237+
assignees = []
238+
for ra in raw_assignees:
239+
assignees.append(self.__get_user(ra))
240+
241+
return assignees
242+
231243
def __get_issue_notes(self, issue_id):
232244
"""Get issue notes"""
233245

@@ -239,6 +251,7 @@ def __get_issue_notes(self, issue_id):
239251

240252
for note in json.loads(raw_notes):
241253
note_id = note['id']
254+
note['author_data'] = self.__get_user(note['author'])
242255
note['award_emoji_data'] = \
243256
self.__get_note_award_emoji(GitLabClient.ISSUES, issue_id, note_id)
244257
notes.append(note)
@@ -267,6 +280,9 @@ def __fetch_merge_requests(self, from_date):
267280

268281
self.__init_merge_extra_fields(merge_full)
269282

283+
merge_full['author_data'] = self.__get_user(merge_full['author'])
284+
merge_full['assignee_data'] = self.__get_user(merge_full['assignee'])
285+
270286
merge_full['notes_data'] = self.__get_merge_notes(merge_id)
271287
merge_full['award_emoji_data'] = self.__get_award_emoji(GitLabClient.MERGES, merge_id)
272288
merge_full['versions_data'] = self.__get_merge_versions(merge_id)
@@ -283,6 +299,7 @@ def __get_merge_notes(self, merge_id):
283299
for raw_notes in group_notes:
284300
for note in json.loads(raw_notes):
285301
note_id = note['id']
302+
note['author_data'] = self.__get_user(note['author'])
286303
note['award_emoji_data'] = \
287304
self.__get_note_award_emoji(GitLabClient.MERGES, merge_id, note_id)
288305
notes.append(note)
@@ -333,22 +350,45 @@ def __get_note_award_emoji(self, item_type, item_id, note_id):
333350
emojis.append(emoji)
334351
except requests.exceptions.HTTPError as error:
335352
if error.response.status_code == 404:
336-
logger.warning("Emojis not available for %s ",
353+
logger.warning("Emojis not available for %s",
337354
urijoin(item_type, str(item_id), GitLabClient.NOTES,
338355
str(note_id), GitLabClient.EMOJI))
339356
return emojis
340357

341358
return emojis
342359

360+
def __get_user(self, user):
361+
"""Fetch user data"""
362+
363+
found = {}
364+
365+
if not user:
366+
return found
367+
368+
if 'id' not in user:
369+
logger.warning("User %s has no ID", user)
370+
return found
371+
372+
user_raw = self.client.user(user['id'])
373+
found = json.loads(user_raw)
374+
375+
return found
376+
343377
def __init_issue_extra_fields(self, issue):
344378
"""Add fields to an issue"""
345379

380+
issue['author_data'] = {}
381+
issue['assignee_data'] = {}
382+
issue['assignees_data'] = []
383+
issue['notes_data'] = []
346384
issue['notes_data'] = []
347385
issue['award_emoji_data'] = []
348386

349387
def __init_merge_extra_fields(self, merge):
350388
"""Add fields to a merge requests"""
351389

390+
merge['author_data'] = {}
391+
merge['assignee_data'] = {}
352392
merge['notes_data'] = []
353393
merge['award_emoji_data'] = []
354394
merge['versions_data'] = []
@@ -384,7 +424,7 @@ class GitLabClient(HttpClient, RateLimitHandler):
384424
PROJECTS = "projects"
385425
VERSIONS = "versions"
386426

387-
_users = {} # users cache
427+
_users = {} # users cache
388428

389429
def __init__(self, owner, repository, token, base_url=None,
390430
sleep_for_rate=False, min_rate_to_sleep=MIN_RATE_LIMIT,
@@ -517,6 +557,22 @@ def note_emojis(self, item_type, item_id, note_id):
517557

518558
return self.fetch_items(path, payload)
519559

560+
def user(self, user_id):
561+
"""Get the user information and update the user cache"""
562+
563+
if user_id in self._users:
564+
return self._users[user_id]
565+
566+
url_user = urijoin(self.base_url, 'users', user_id)
567+
568+
logging.info("Getting info for %s" % url_user)
569+
570+
r = self.fetch(url_user)
571+
user = r.text
572+
self._users[user_id] = user
573+
574+
return user
575+
520576
def calculate_time_to_reset(self):
521577
"""Calculate the seconds to reset the token requests, by obtaining the different
522578
between the current date and the next date when the token is fully regenerated.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[
2+
{
3+
"attachment": null,
4+
"author": {
5+
"name": "Ghost"
6+
},
7+
"body": "closed",
8+
"created_at": "2017-03-15T22:55:49.337Z",
9+
"id": 1,
10+
"noteable_id": 4693060,
11+
"noteable_iid": 635,
12+
"noteable_type": "Issue",
13+
"system": true,
14+
"updated_at": "2017-03-15T22:55:49.337Z"
15+
}
16+
]

tests/data/gitlab/user_1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"avatar_url": "https://gitlab.com/redfish64",
3+
"bio": "Staff Engineer",
4+
"created_at": "2015-04-09T13:17:31.003Z",
5+
"id": 1,
6+
"linkedin": "oh-oh",
7+
"location": "Cannes, France",
8+
"name": "Timothy Engler",
9+
"organization": "GitLab",
10+
"public_email": "",
11+
"skype": "oh-oh",
12+
"state": "active",
13+
"twitter": "oh-oh",
14+
"username": "redfish64",
15+
"web_url": "https://gitlab.com/redfish64",
16+
"website_url": "https://gitlab.com/redfish64"
17+
}

tests/data/gitlab/user_2

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"avatar_url": "https://gitlab.com/redfish64",
3+
"bio": "Staff Engineer",
4+
"created_at": "2015-04-09T13:17:31.003Z",
5+
"id": 1,
6+
"linkedin": "oh-oh",
7+
"location": "Cannes, France",
8+
"name": "Yoeri Nijs",
9+
"organization": "GitHub",
10+
"public_email": "",
11+
"skype": "oh-oh",
12+
"state": "active",
13+
"twitter": "oh-oh",
14+
"username": "YoeriNijs",
15+
"web_url": "https://gitlab.com/YoeriNijs",
16+
"website_url": "https://gitlab.com/YoeriNijs"
17+
}

0 commit comments

Comments
 (0)