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_exclude_unprotected.pop(project.name, None)
self._project_branch_cache_include_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 """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 This method must be called when a branch event is processed: if the
@ -258,9 +259,13 @@ class CachedBranchConnection(BaseConnection):
The project name. The project name.
:params event: :params event:
The event, inherit from `zuul.model.TriggerEvent` class. 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, if protected is None:
zuul_event_id=event) protected = self.isBranchProtected(project_name, event.branch,
zuul_event_id=event)
if protected is not None: if protected is not None:
# If the branch appears in the exclude_unprotected cache but # 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.zuul_event_id = self.delivery
event.timestamp = self.ts event.timestamp = self.ts
project = self.connection.source.getProject(event.project_name) project = self.connection.source.getProject(event.project_name)
change = None
if event.change_number: if event.change_number:
self.connection._getChange(project, change = self.connection._getChange(
event.change_number, project,
event.patch_number, event.change_number,
refresh=True, event.patch_number,
event=event) refresh=True,
event=event)
self.log.debug("Refreshed change %s,%s", self.log.debug("Refreshed change %s,%s",
event.change_number, event.patch_number) event.change_number, event.patch_number)
@ -448,7 +450,14 @@ class GithubEventProcessor(object):
# unprotected branches, we might need to check whether the # unprotected branches, we might need to check whether the
# branch is now protected. # branch is now protected.
if hasattr(event, "branch") and event.branch: 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 event.project_hostname = self.connection.canonical_hostname
self.event = event self.event = event
@ -1484,6 +1493,7 @@ class GithubConnection(CachedBranchConnection):
change.required_contexts = set( change.required_contexts = set(
canmerge_data['requiredStatusCheckContexts'] canmerge_data['requiredStatusCheckContexts']
) )
change.branch_protected = canmerge_data['protected']
def getGitUrl(self, project: Project): def getGitUrl(self, project: Project):
if self.git_ssh_key: if self.git_ssh_key:

View File

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

View File

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