From 857b9f63ed8fb3aae3876c06cbd5e6349f336ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Wed, 26 Jul 2023 16:48:56 +0200 Subject: [PATCH] Listing Merged PRs for Github and Gitlab --- did/plugins/github.py | 15 ++++++++++ did/plugins/gitlab.py | 65 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/did/plugins/github.py b/did/plugins/github.py index bd684e6e..41587375 100644 --- a/did/plugins/github.py +++ b/did/plugins/github.py @@ -218,6 +218,18 @@ def fetch(self): Issue(issue) for issue in self.parent.github.search(query)] +class PullRequestsMerged(Stats): + """ Pull requests merged """ + + def fetch(self): + log.info("Searching for merged pull requests authored by {0}".format( + self.user)) + query = "search/issues?q=author:{0}+merged:{1}..{2}".format( + self.user.login, self.options.since, self.options.until) + query += "+type:pr" + self.stats = [ + Issue(issue) for issue in self.parent.github.search(query)] + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Stats Group # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -263,4 +275,7 @@ def __init__(self, option, name=None, parent=None, user=None): PullRequestsReviewed( option=option + "-pull-requests-reviewed", parent=self, name="Pull requests reviewed on {0}".format(option)), + PullRequestsMerged( + option=option + "-pull-requests-merged", parent=self, + name="Merged pull requests on {0}".format(option)), ] diff --git a/did/plugins/gitlab.py b/did/plugins/gitlab.py index b9727d26..b9b0a521 100644 --- a/did/plugins/gitlab.py +++ b/did/plugins/gitlab.py @@ -60,12 +60,17 @@ def __init__(self, url, token, ssl_verify=GITLAB_SSL_VERIFY): self.project_mrs = {} self.project_issues = {} - def _get_gitlab_api_raw(self, url): - return requests.get(url, headers=self.headers, verify=self.ssl_verify) - - def _get_gitlab_api(self, endpoint): + def _get_gitlab_api_raw(self, url, params=None): + log.debug("Query: {0}, params: {1}".format(url, params)) + return requests.get(url, + headers=self.headers, + params=params, + verify=self.ssl_verify + ) + + def _get_gitlab_api(self, endpoint, params=None): url = '{0}/api/v{1}/{2}'.format(self.url, GITLAB_API, endpoint) - return self._get_gitlab_api_raw(url) + return self._get_gitlab_api_raw(url, params=params) def _get_gitlab_api_json(self, endpoint): log.debug("Query: {0}".format(endpoint)) @@ -74,11 +79,16 @@ def _get_gitlab_api_json(self, endpoint): return result def _get_gitlab_api_list( - self, endpoint, since=None, get_all_results=False): + self, endpoint, + params=None, + since=None, + get_all_results=False + ): results = [] - result = self._get_gitlab_api(endpoint) + result = self._get_gitlab_api(endpoint, params=params) result.raise_for_status() results.extend(result.json()) + log.data(pretty(results)) while ('next' in result.links and 'url' in result.links['next'] and get_all_results): result = self._get_gitlab_api_raw(result.links['next']['url']) @@ -111,6 +121,17 @@ def get_project_mr(self, project_id, mr_id): mrs = self.get_project_mrs(project_id) mr = next(filter(lambda x: x['id'] == mr_id, mrs), None) return mr + + def get_user_mr(self, username, state, since, until): + since = since.date.strftime('%Y-%m-%dT%H:%M:%S.000Z') + until = until.date.strftime('%Y-%m-%dT%H:%M:%S.000Z') + endpoint = 'merge_requests' + return self._get_gitlab_api_list(endpoint, params={ + 'author_username': username, + 'state': state, + 'updated_after': since, + 'updated_before': until + }) def get_project_mrs(self, project_id): if project_id not in self.project_mrs: @@ -168,7 +189,10 @@ def __init__(self, data, gitlabapi): self.gitlabapi = gitlabapi self.project = self.gitlabapi.get_project(data['project_id']) self.id = self.iid() - self.title = data['target_title'] + self.title = self._get_title() + + def _get_title(self): + return self.data['target_title'] def iid(self): return self.gitlabapi.get_project_issue( @@ -208,6 +232,13 @@ def iid(self): else: return "unknown" +class MergedRequest(MergeRequest): + + def iid(self): + return self.data['iid'] + + def _get_title(self): + return self.data['title'] # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Stats @@ -313,6 +344,21 @@ def fetch(self): for mr in results] +class MergeRequestsMerged(Stats): + """ Merge requests merged """ + + def fetch(self): + log.info("Searching for Merged requests authored by {0}".format( + self.user)) + results = self.parent.gitlab.get_user_mr( + self.user.login, 'merged', + self.options.since, self.options.until + ) + + self.stats = [ + MergedRequest(mr, self.parent.gitlab) + for mr in results] + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Stats Group # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -369,4 +415,7 @@ def __init__(self, option, name=None, parent=None, user=None): MergeRequestsClosed( option=option + "-merge-requests-closed", parent=self, name="Merge requests closed on {0}".format(option)), + MergeRequestsMerged( + option=option + "-merge-requests-merged", parent=self, + name="Merged requests on {0}".format(option)), ]