summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2018-11-27 22:35:27 +0000
committerGerrit Code Review <review@openstack.org>2018-11-27 22:35:27 +0000
commit8a58a358d12b719a4f45410c185b3df2e25f666c (patch)
treec68915e7817d51e230e951b2be47e89392417eb0
parent2baecbeb4122db2525be0b5dde72e7ff7166a8b9 (diff)
parentf312bda11890d0bf61d08eaa9f4473bd783864f2 (diff)
Merge "Retry queries for commits"
-rw-r--r--tests/fakegithub.py23
-rw-r--r--tests/unit/test_github_driver.py31
-rw-r--r--zuul/driver/github/githubconnection.py15
3 files changed, 68 insertions, 1 deletions
diff --git a/tests/fakegithub.py b/tests/fakegithub.py
index 6b951ef..df34c69 100644
--- a/tests/fakegithub.py
+++ b/tests/fakegithub.py
@@ -14,6 +14,7 @@
14# License for the specific language governing permissions and limitations 14# License for the specific language governing permissions and limitations
15# under the License. 15# under the License.
16 16
17import github3.exceptions
17import re 18import re
18 19
19FAKE_BASE_URL = 'https://example.com/api/v3/' 20FAKE_BASE_URL = 'https://example.com/api/v3/'
@@ -82,6 +83,9 @@ class FakeRepository(object):
82 self.data = data 83 self.data = data
83 self.name = name 84 self.name = name
84 85
86 # fail the next commit requests with 404
87 self.fail_not_found = 0
88
85 def branches(self, protected=False): 89 def branches(self, protected=False):
86 if protected: 90 if protected:
87 # simulate there is no protected branch 91 # simulate there is no protected branch
@@ -121,6 +125,25 @@ class FakeRepository(object):
121 commit.set_status(state, url, description, context, user) 125 commit.set_status(state, url, description, context, user)
122 126
123 def commit(self, sha): 127 def commit(self, sha):
128
129 if self.fail_not_found > 0:
130 self.fail_not_found -= 1
131
132 class Response:
133 status_code = 0
134 message = ''
135
136 def json(self):
137 return {
138 'message': self.message
139 }
140
141 resp = Response()
142 resp.status_code = 404
143 resp.message = 'Not Found'
144
145 raise github3.exceptions.NotFoundError(resp)
146
124 commit = self._commits.get(sha, None) 147 commit = self._commits.get(sha, None)
125 if commit is None: 148 if commit is None:
126 commit = FakeCommit(sha) 149 commit = FakeCommit(sha)
diff --git a/tests/unit/test_github_driver.py b/tests/unit/test_github_driver.py
index 14b9979..2b2f9bd 100644
--- a/tests/unit/test_github_driver.py
+++ b/tests/unit/test_github_driver.py
@@ -1000,6 +1000,37 @@ class TestGithubDriver(ZuulTestCase):
1000 self.assertEquals('https://github.com/org/project/pull/1', 1000 self.assertEquals('https://github.com/org/project/pull/1',
1001 job2_params['zuul']['items'][0]['change_url']) 1001 job2_params['zuul']['items'][0]['change_url'])
1002 1002
1003 @simple_layout('layouts/basic-github.yaml', driver='github')
1004 def test_pull_commit_race(self):
1005 """Test graceful handling of delayed availability of commits"""
1006
1007 github = self.fake_github.getGithubClient('org/project')
1008 repo = github.repo_from_project('org/project')
1009 repo.fail_not_found = 1
1010
1011 A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
1012 self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
1013 self.waitUntilSettled()
1014
1015 self.assertEqual('SUCCESS',
1016 self.getJobFromHistory('project-test1').result)
1017 self.assertEqual('SUCCESS',
1018 self.getJobFromHistory('project-test2').result)
1019
1020 job = self.getJobFromHistory('project-test2')
1021 zuulvars = job.parameters['zuul']
1022 self.assertEqual(str(A.number), zuulvars['change'])
1023 self.assertEqual(str(A.head_sha), zuulvars['patchset'])
1024 self.assertEqual('master', zuulvars['branch'])
1025 self.assertEqual(1, len(A.comments))
1026 self.assertThat(
1027 A.comments[0],
1028 MatchesRegex(r'.*\[project-test1 \]\(.*\).*', re.DOTALL))
1029 self.assertThat(
1030 A.comments[0],
1031 MatchesRegex(r'.*\[project-test2 \]\(.*\).*', re.DOTALL))
1032 self.assertEqual(2, len(self.history))
1033
1003 1034
1004class TestGithubUnprotectedBranches(ZuulTestCase): 1035class TestGithubUnprotectedBranches(ZuulTestCase):
1005 config_file = 'zuul-github-driver.conf' 1036 config_file = 'zuul-github-driver.conf'
diff --git a/zuul/driver/github/githubconnection.py b/zuul/driver/github/githubconnection.py
index 84419d3..58c3818 100644
--- a/zuul/driver/github/githubconnection.py
+++ b/zuul/driver/github/githubconnection.py
@@ -1322,11 +1322,24 @@ class GithubConnection(BaseConnection):
1322 if not result: 1322 if not result:
1323 raise Exception('Pull request was not merged') 1323 raise Exception('Pull request was not merged')
1324 1324
1325 def _getCommit(self, repository, sha, retries=5):
1326 try:
1327 return repository.commit(sha)
1328 except github3.exceptions.NotFoundError:
1329 self.log.warning("Commit %s of project %s returned None",
1330 sha, repository.name)
1331 if retries <= 0:
1332 raise
1333 time.sleep(1)
1334 return self._getCommit(repository, sha, retries - 1)
1335
1325 def getCommitStatuses(self, project, sha): 1336 def getCommitStatuses(self, project, sha):
1326 github = self.getGithubClient(project) 1337 github = self.getGithubClient(project)
1327 owner, proj = project.split('/') 1338 owner, proj = project.split('/')
1328 repository = github.repository(owner, proj) 1339 repository = github.repository(owner, proj)
1329 commit = repository.commit(sha) 1340
1341 commit = self._getCommit(repository, sha, 5)
1342
1330 # make a list out of the statuses so that we complete our 1343 # make a list out of the statuses so that we complete our
1331 # API transaction 1344 # API transaction
1332 statuses = [status.as_dict() for status in commit.statuses()] 1345 statuses = [status.as_dict() for status in commit.statuses()]