Merge "Don't query branch protection on pull request events"

This commit is contained in:
Zuul 2021-03-01 23:10:50 +00:00 committed by Gerrit Code Review
commit 1bafeb4b73
4 changed files with 27 additions and 9 deletions

View File

@ -247,7 +247,8 @@ class CachedBranchConnection(BaseConnection):
self._project_branch_cache_exclude_unprotected.pop(project.name, None)
self._project_branch_cache_include_unprotected.pop(project.name, None)
def checkBranchCache(self, project_name: str, event) -> None:
def checkBranchCache(self, project_name: str, event,
protected: bool = None) -> None:
"""Clear the cache for a project when a branch event is processed
This method must be called when a branch event is processed: if the
@ -258,9 +259,13 @@ class CachedBranchConnection(BaseConnection):
The project name.
:params event:
The event, inherit from `zuul.model.TriggerEvent` class.
:params protected:
If defined the caller already knows if the branch is protected
so the query can be skipped.
"""
protected = self.isBranchProtected(project_name, event.branch,
zuul_event_id=event)
if protected is None:
protected = self.isBranchProtected(project_name, event.branch,
zuul_event_id=event)
if protected is not None:
# If the branch appears in the exclude_unprotected cache but

View File

@ -435,12 +435,14 @@ class GithubEventProcessor(object):
event.zuul_event_id = self.delivery
event.timestamp = self.ts
project = self.connection.source.getProject(event.project_name)
change = None
if event.change_number:
self.connection._getChange(project,
event.change_number,
event.patch_number,
refresh=True,
event=event)
change = self.connection._getChange(
project,
event.change_number,
event.patch_number,
refresh=True,
event=event)
self.log.debug("Refreshed change %s,%s",
event.change_number, event.patch_number)
@ -448,7 +450,14 @@ class GithubEventProcessor(object):
# unprotected branches, we might need to check whether the
# branch is now protected.
if hasattr(event, "branch") and event.branch:
self.connection.checkBranchCache(project.name, event)
protected = None
if change:
# PR based events already have the information if the
# target branch is protected so take the information
# from there.
protected = change.branch_protected
self.connection.checkBranchCache(project.name, event,
protected=protected)
event.project_hostname = self.connection.canonical_hostname
self.event = event
@ -1484,6 +1493,7 @@ class GithubConnection(CachedBranchConnection):
change.required_contexts = set(
canmerge_data['requiredStatusCheckContexts']
)
change.branch_protected = canmerge_data['protected']
def getGitUrl(self, project: Project):
if self.git_ssh_key:

View File

@ -42,6 +42,7 @@ class PullRequest(Change):
self.review_decision = None
self.required_contexts = set()
self.contexts = set()
self.branch_protected = False
@property
def status(self):

View File

@ -107,8 +107,10 @@ class GraphQLClient:
'requiresApprovingReviews')
result['requiresCodeOwnerReviews'] = matching_rule.get(
'requiresCodeOwnerReviews')
result['protected'] = True
else:
result['requiredStatusCheckContexts'] = []
result['protected'] = False
# Check for draft
pull_request = nested_get(repository, 'pullRequest')