Skip to content

Commit

Permalink
Limit number of PR by using per_page.
Browse files Browse the repository at this point in the history
Bump mr_limit to 10
  • Loading branch information
ye11ow committed Mar 17, 2020
1 parent 2f832c5 commit d20e0c2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
13 changes: 6 additions & 7 deletions noti.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class NotiConfig:
# Shared configurations
'global': {
# Max number of MRs that will be shown on the list
'mr_limit': 5,
'mr_limit': 10,
},

'bitbar': {
Expand Down Expand Up @@ -287,21 +287,20 @@ def __init__(self, config):
except:
raise NotiError(self.name, 'Missing dependencies: You need to install PyGithub', 'https://pygithub.readthedocs.io/en/latest/introduction.html#download-and-install')

self._gh = github.Github(self.token, base_url=self.host)
self._gh = github.Github(self.token, base_url=self.host, per_page=self.get_config('mr_limit'))

def get_mrs(self):
mrs = {}

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

# Here we only get the first page.
# Github supports page size up to 100 and it is more than enough for us
pulls = repo.get_pulls(state='open', sort='created', base='master').get_page(0)
for pr in pulls:
mrs[repo_name].append(GithubPR(repo, pr))

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

return mrs

Expand Down
14 changes: 8 additions & 6 deletions tests/unit/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ class TestGithub:
@pytest.fixture
def config(self):
return {
'token': 'fake',
'token': '_token_',
'repo': ['_repo_'],
'host': 'https://example.com',
'mr_limit': 5
'host': '_host_',
'mr_limit': 10
}

def test_init(self, config):
@patch('github.Github')
def test_init(self, mock_gh, config):
g = Github(config)
mock_gh.assert_called_with(config.get('token'), base_url=config.get('host'), per_page=config.get('mr_limit'))
assert g != None

def test_init_with_wrong_config(self):
Expand All @@ -33,11 +35,11 @@ def test_init_with_wrong_config(self):
def test_get_mrs(self, mock_gh, config):
repo = MagicMock()
mock_gh.return_value.get_repo.return_value = repo
repo.get_pulls.return_value = [MagicMock(), MagicMock(), MagicMock(), MagicMock(), MagicMock(), MagicMock()]
repo.get_pulls.return_value.get_page.return_value = [MagicMock(), MagicMock(), MagicMock()]

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

assert len(mrs['_repo_']) == config.get('mr_limit')
assert len(mrs['_repo_']) == 3
mock_gh.return_value.get_repo.assert_called_with('_repo_')
repo.get_pulls.assert_called_with(state='open', sort='created', base='master')
2 changes: 1 addition & 1 deletion tests/unit/test_noti_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_get_config(self):

config = conf.get_config('github')

assert config.get('mr_limit') == 5
assert config.get('mr_limit') == 10
assert config.get('hello') == 'world'

def test_bitbar_config(self):
Expand Down

0 comments on commit d20e0c2

Please sign in to comment.