Commit b3caeceb authored by Shrikrishna Singh's avatar Shrikrishna Singh

Repository: Add filter_merge_requests method

Closes #132
parent 847afeb5
Pipeline #23392888 passed with stage
in 38 seconds
......@@ -38,6 +38,10 @@ GH_ISSUE_STATE_TRANSLATION = {
'all': 'all'
}
GH_MR_STATE_TRANSLATION = {MergeRequestStates.MERGED: 'merged',
MergeRequestStates.OPEN: 'opened',
MergeRequestStates.CLOSED: 'closed'}
class GitHubRepository(GitHubMixin, Repository):
"""
......@@ -356,10 +360,36 @@ class GitHubRepository(GitHubMixin, Repository):
if hook['config'].get('url', None) == url:
delete(self._token, hook_url + '/' + str(hook['id']))
def filter_merge_requests(self, state: str='opened') -> set:
"""
Filters the merge requests from the repository based on the state
of the merge requests.
:param state: 'opened' or 'closed', 'merged', or 'all'.
"""
from IGitt.GitHub.GitHubMergeRequest import GitHubMergeRequest
if state == 'merged':
return {GitHubMergeRequest.from_data(mr, self._token,
self.full_name, mr['number'])
for mr in get(self._token, self.url + '/pulls',
{'state': 'closed'})
if mr['merged_at'] is not None}
elif state == 'closed':
return {GitHubMergeRequest.from_data(mr, self._token,
self.full_name, mr['number'])
for mr in get(self._token, self.url + '/pulls',
{'state': 'closed'})
if mr['merged_at'] is None}
else:
return {GitHubMergeRequest.from_data(mr, self._token,
self.full_name, mr['number'])
for mr in get(self._token, self.url + '/pulls',
{'state': state})}
@property
def merge_requests(self) -> set:
"""
Retrieves a set of merge request objects.
Retrieves a set of open merge request objects.
>>> from os import environ
>>> repo = GitHubRepository(environ['GITHUB_TEST_TOKEN'],
......@@ -367,9 +397,7 @@ class GitHubRepository(GitHubMixin, Repository):
>>> len(repo.merge_requests)
3
"""
from IGitt.GitHub.GitHubMergeRequest import GitHubMergeRequest
return {GitHubMergeRequest(self._token, self.full_name, res['number'])
for res in get(self._token, self.url + '/pulls')}
return self.filter_merge_requests(state='opened')
def filter_issues(self, state: str='opened') -> set:
"""
......
......@@ -389,10 +389,24 @@ class GitLabRepository(GitLabMixin, Repository):
"""
return GitLabIssue.create(self._token, self.full_name, title, body)
def filter_merge_requests(self, state: str='opened') -> set:
"""
Filters the merge requests from the repository based on the state
of the merge requests.
:param state: 'opened' or 'closed', or 'merged', or 'all'.
"""
from IGitt.GitLab.GitLabMergeRequest import GitLabMergeRequest
return {GitLabMergeRequest.from_data(mr, self._token,
self.full_name, mr['iid'])
for mr in get(self._token,
self.url + '/merge_requests',
{'state': state})}
@property
def merge_requests(self) -> set:
"""
Retrieves a set of merge request objects.
Retrieves a set of open merge request objects.
>>> from os import environ
>>> repo = GitLabRepository(
......@@ -402,10 +416,7 @@ class GitLabRepository(GitLabMixin, Repository):
>>> len(repo.merge_requests)
4
"""
from IGitt.GitLab.GitLabMergeRequest import GitLabMergeRequest
return {GitLabMergeRequest.from_data(res, self._token,
self.full_name, res['iid'])
for res in get(self._token, self.url + '/merge_requests')}
return self.filter_merge_requests(state='opened')
def filter_issues(self, state: str='opened') -> set:
"""
......
......@@ -234,10 +234,19 @@ class Repository(IGittObject):
"""
raise NotImplementedError
def filter_merge_requests(self, state: str='opened') -> set:
"""
Filters the merge requests from the repository based on the state
of the merge requests.
:param state: 'merged' or 'opened' or 'closed' or 'all'
"""
raise NotImplementedError
@property
def merge_requests(self) -> set:
"""
Retrieves a set of merge request objects.
Retrieves a set of open merge request objects.
"""
raise NotImplementedError
......
......@@ -82,8 +82,14 @@ class GitHubRepositoryTest(IGittTestCase):
def test_issues(self):
self.assertEqual(len(self.repo.issues), 89)
def test_filter_merge_requests(self):
self.assertEqual(len(self.repo.filter_merge_requests(state='all')), 34)
self.assertEqual(len(self.repo.filter_merge_requests(state='opened')), 23)
self.assertEqual(len(self.repo.filter_merge_requests(state='closed')), 7)
self.assertEqual(len(self.repo.filter_merge_requests(state='merged')), 4)
def test_merge_requests(self):
self.assertEqual(len(self.repo.merge_requests), 18)
self.assertEqual(len(self.repo.merge_requests), 23)
def test_create_issue(self):
self.assertEqual(self.repo.create_issue(
......
......@@ -88,8 +88,14 @@ class GitLabRepositoryTest(IGittTestCase):
self.assertIn('http://some.url/in/the/world', self.repo.hooks)
self.repo.delete_hook('http://some.url/in/the/world')
def test_filter_merge_request(self):
self.assertEqual(len(self.repo.filter_merge_requests(state='all')), 42)
self.assertEqual(len(self.repo.filter_merge_requests(state='opened')), 13)
self.assertEqual(len(self.repo.filter_merge_requests(state='closed')), 25)
self.assertEqual(len(self.repo.filter_merge_requests(state='merged')), 4)
def test_merge_requests(self):
self.assertEqual(len(self.repo.merge_requests), 32)
self.assertEqual(len(self.repo.merge_requests), 13)
def test_issues(self):
self.assertEqual(len(self.repo.issues), 14)
......
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