Skip to content

Commit

Permalink
Limit number of MR per project. Separate project
Browse files Browse the repository at this point in the history
  • Loading branch information
ye11ow committed Jan 30, 2020
1 parent ca308b1 commit 60c237b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
52 changes: 32 additions & 20 deletions noti.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
# Shared configurations
'global': {
# Max number of MRs that will be shown on the list
'mr_limit': 20
'mr_limit': 5
}
}

Expand All @@ -58,6 +58,7 @@ def __init__(self, config):
raise NotiError('Missing dependencies', 'You need to install python-gitlab | href=https://python-gitlab.readthedocs.io/en/stable/install.html')

self._config = config.get('gitlab', {})
self._global_config = config.get('global', {})

host = self._config.get('host', '')
token = self._config.get('token', '')
Expand All @@ -67,13 +68,15 @@ def __init__(self, config):
self._gl = gitlab.Gitlab(host, private_token=token)

def get_mrs(self):
mrs = []
mrs = {}

for pid in self._config.get('project_id'):
project = self._gl.projects.get(pid)
for list_mr in project.mergerequests.list(state='opened'):
name = project.attributes.get('name')
mrs[name] = []
for list_mr in project.mergerequests.list(state='opened', per_page=self._global_config.get('mr_limit')):
mr = project.mergerequests.get(list_mr.get_id())
mrs.append(GitlabMR(project, mr))
mrs[name].append(GitlabMR(project, mr))

return mrs

Expand Down Expand Up @@ -182,20 +185,26 @@ def __init__(self, config):
raise NotiError('Missing dependencies', 'You need to install PyGithub | href=https://pygithub.readthedocs.io/en/latest/introduction.html#download-and-install')

self._config = config.get('github', {})
self._global_config = config.get('global', {})
token = self._config.get('token', '')
if len(token) == 0:
raise NotiError('Wrong Github configuration', 'Please make sure you have the right token')

self._gh = GH(token)

def get_mrs(self):
mrs = []
mrs = {}

for repo_name in self._config.get('repo'):
mrs[repo_name] = []
repo = self._gh.get_repo(repo_name)
pulls = repo.get_pulls(state='open', sort='created', base='master')
for pr in pulls:
mrs.append(GithubPR(repo, pr))
mrs[repo_name].append(GithubPR(repo, pr))

# Github SDK doesn't support per_page parameter
if len(mrs[repo_name]) >= self._global_config.get('mr_limit'):
break

return mrs

Expand Down Expand Up @@ -306,6 +315,12 @@ def print_error(self, title, extra):
bp.print_config()
exit(1)

def print_repo(self, repo, mrs):
if len(mrs) == 0:
return

print(repo)

def print_mr(self, mr):
pipeline_color_map = {
'success': 'green',
Expand Down Expand Up @@ -360,23 +375,21 @@ def print_title(self, mrs):
'approved': '👍'
}

for mr in mrs:
statistics['comments'] += len(mr.reviews)
for key, value in mrs.items():
for mr in value:
statistics['comments'] += len(mr.reviews)

if mr.approved:
statistics['approved'] += 1
if mr.approved:
statistics['approved'] += 1

if mr.ci_status in statistics:
statistics[mr.ci_status] += 1
if mr.ci_status in statistics:
statistics[mr.ci_status] += 1

title = ''
for key in statistics:
if statistics[key] > 0:
title += pipeline_icon_map[key] + str(statistics[key])

if len(title) == 0:
title = f"{len(mrs)} MRs"

print(title)
print('---\n')

Expand Down Expand Up @@ -422,11 +435,10 @@ def time_diff(self, before):
mrs = vcs.get_mrs()
except:
bp.print_error("failed to connect to the server", None)

if len(mrs) > config.get('global').get('mr_limit'):
mrs = mrs[:config.get('global').get('mr_limit')]

bp.print_title(mrs)
for mr in mrs:
bp.print_mr(mr)
for repo_name, repo_mrs in mrs.items():
bp.print_repo(repo_name, repo_mrs)
for mr in repo_mrs:
bp.print_mr(mr)
bp.print_config()
10 changes: 8 additions & 2 deletions tests/unit/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def config(self):
"token": "fake",
"project_id": [1],
"host": "https://example.com"
},
"global": {
"mr_limit": 5
}
}

Expand All @@ -37,9 +40,12 @@ def test_get_mrs(self, mock_gl, config):
project = MagicMock()
mock_gl.return_value.projects.get.return_value = project
project.mergerequests.list.return_value = [MagicMock(), MagicMock(), MagicMock()]
project.attributes.get.return_value = 'test_repo'

g = Gitlab(config)
mrs = g.get_mrs()

assert len(mrs) == 3
project.mergerequests.list.assert_called_with(state='opened')
assert 'test_repo' in mrs
project.attributes.get.assert_called_with('name')
assert len(mrs['test_repo']) == 3
project.mergerequests.list.assert_called_with(state='opened', per_page=5)

0 comments on commit 60c237b

Please sign in to comment.