Fix issue in Github connection with large diffs

When a PR consist of a large number of changes github3.py will raise an
exception, since Github will fail to generate the diff.

This case can be handled via the same mechanism that is used for getting
the list of files when Github doesn't return the full file list (e.g. if
there are more than 300 changed files in a PR)

Change-Id: Ic1dd6d381536268036dd845ecb5e2073383704a0
This commit is contained in:
Simon Westphahl 2018-10-24 10:33:12 +02:00
parent d4a142b97b
commit a7287ae461
2 changed files with 25 additions and 2 deletions

View File

@ -18,9 +18,10 @@ from testtools.matchers import MatchesRegex, StartsWith
import urllib
import socket
import time
from unittest import skip
from unittest import mock, skip
import git
import github3.exceptions
from tests.base import ZuulTestCase, simple_layout, random_sha1
from tests.base import ZuulWebFixture
@ -114,6 +115,19 @@ class TestGithubDriver(ZuulTestCase):
self.waitUntilSettled()
self.assertEqual(1, len(self.history))
@simple_layout('layouts/basic-github.yaml', driver='github')
def test_pull_github_files_error(self):
A = self.fake_github.openFakePullRequest(
'org/project', 'master', 'A')
with mock.patch("tests.fakegithub.FakePull.files") as files_mock:
files_mock.side_effect = github3.exceptions.ServerError(
mock.MagicMock())
self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
self.waitUntilSettled()
self.assertEqual(1, files_mock.call_count)
self.assertEqual(2, len(self.history))
@simple_layout('layouts/basic-github.yaml', driver='github')
def test_comment_event(self):
A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')

View File

@ -1044,7 +1044,16 @@ class GithubConnection(BaseConnection):
# Get the issue obj so we can get the labels (this is silly)
issueobj = probj.issue()
pr = probj.as_dict()
pr['files'] = [f.filename for f in probj.files()]
try:
pr['files'] = [f.filename for f in probj.files()]
except github3.exceptions.ServerError as exc:
# NOTE: For PRs with a lot of lines changed, Github will return
# an error (HTTP 500) because it can't generate the diff.
self.log.warning("Failed to get list of files from Github. "
"Using empty file list to trigger update "
"via the merger: %s", exc)
pr['files'] = []
pr['labels'] = [l.name for l in issueobj.labels()]
self.log.debug('Got PR %s#%s', project_name, number)
self.log_rate_limit(self.log, github)