Skip to content
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

Support merged pull requests for github and gitlab #311

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
15 changes: 15 additions & 0 deletions did/plugins/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -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)),
]
65 changes: 57 additions & 8 deletions did/plugins/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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'])
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -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)),
]