From e4bca4d24dd6a482a0d3a4bf542c4671dd0a6238 Mon Sep 17 00:00:00 2001 From: Tristan Cacqueray Date: Fri, 22 Jun 2018 07:00:52 +0000 Subject: [PATCH] github: prevent AttributeError when missing branch protection It seems like the _getBranchProtection should return an empty dictionary to prevent: Traceback (most recent call last): File "zuul/scheduler.py", line 883, in run self.process_event_queue() File "zuul/scheduler.py", line 952, in process_event_queue pipeline.manager.addChange(change) File "zuul/manager/__init__.py", line 236, in addChange if not self.isChangeReadyToBeEnqueued(change): File "zuul/manager/dependent.py", line 99, in isChangeReadyToBeEnqueued if not source.canMerge(change, self.getSubmitAllowNeeds()): File "zuul/driver/github/githubsource.py", line 59, in canMerge return self.connection.canMerge(change, allow_needs) File "zuul/driver/github/githubconnection.py", line 961, in canMerge required_reviews = protection.get( AttributeError: 'NoneType' object has no attribute 'get' Change-Id: Ia033da9feac98b28dcf09381fe0f90de76619927 --- tests/fakegithub.py | 8 ++++++-- zuul/driver/github/githubconnection.py | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/fakegithub.py b/tests/fakegithub.py index 952b60b5b6..ac60176f54 100644 --- a/tests/fakegithub.py +++ b/tests/fakegithub.py @@ -152,6 +152,10 @@ class FakeRepository(object): def get_url_protection(self, branch): contexts = self.data.required_contexts.get((self.name, branch), []) + if not contexts: + # Note that GitHub returns 404 if branch protection is off so do + # the same here as well + return FakeResponse({}, 404) data = { 'required_status_checks': { 'contexts': contexts @@ -250,8 +254,8 @@ class FakeIssueSearchResult(object): class FakeResponse(object): - def __init__(self, data): - self.status_code = 200 + def __init__(self, data, status_code=200): + self.status_code = status_code self.data = data def json(self): diff --git a/zuul/driver/github/githubconnection.py b/zuul/driver/github/githubconnection.py index 7c8432795f..a5c78a9ae1 100644 --- a/zuul/driver/github/githubconnection.py +++ b/zuul/driver/github/githubconnection.py @@ -1061,7 +1061,7 @@ class GithubConnection(BaseConnection): resp = github.session.get(url, headers=headers) if resp.status_code == 404: - return None + return {} return resp.json()