Merge "Annotate canMerge check with event id"

This commit is contained in:
Zuul 2019-07-15 16:42:41 +00:00 committed by Gerrit Code Review
commit 4dc62c4dfa
10 changed files with 35 additions and 29 deletions

View File

@ -748,9 +748,10 @@ class GerritConnection(BaseConnection):
sha = refs.get(ref, '')
return sha
def canMerge(self, change, allow_needs):
def canMerge(self, change, allow_needs, event=None):
log = get_annotated_logger(self.log, event)
if not change.number:
self.log.debug("Change has no number; considering it merged")
log.debug("Change has no number; considering it merged")
# Good question. It's probably ref-updated, which, ah,
# means it's merged.
return True
@ -779,8 +780,8 @@ class GerritConnection(BaseConnection):
# CLOSED, RULE_ERROR
return False
except Exception:
self.log.exception("Exception determining whether change"
"%s can merge:" % change)
log.exception("Exception determining whether change"
"%s can merge:", change)
return False
return True

View File

@ -46,8 +46,8 @@ class GerritSource(BaseSource):
def isMerged(self, change, head=None):
return self.connection.isMerged(change, head)
def canMerge(self, change, allow_needs):
return self.connection.canMerge(change, allow_needs)
def canMerge(self, change, allow_needs, event=None):
return self.connection.canMerge(change, allow_needs, event=event)
def postConfig(self):
pass

View File

@ -32,7 +32,7 @@ class GitSource(BaseSource):
def isMerged(self, change, head=None):
raise NotImplementedError()
def canMerge(self, change, allow_needs):
def canMerge(self, change, allow_needs, event=None):
raise NotImplementedError()
def getChange(self, event, refresh=False):

View File

@ -1423,7 +1423,7 @@ class GithubConnection(BaseConnection):
log.debug('Got PR %s#%s', project_name, number)
return (pr, probj)
def canMerge(self, change, allow_needs):
def canMerge(self, change, allow_needs, event=None):
# NOTE: The mergeable call may get a false (null) while GitHub is
# calculating if it can merge. The github3.py library will just return
# that as false. This could lead to false negatives. So don't do this
@ -1431,12 +1431,12 @@ class GithubConnection(BaseConnection):
# conflicts which would block merging finally will be detected by
# the zuul-mergers anyway.
github = self.getGithubClient(change.project.name)
github = self.getGithubClient(change.project.name, zuul_event_id=event)
owner, proj = change.project.name.split('/')
pull = github.pull_request(owner, proj, change.number)
protection = self._getBranchProtection(
change.project.name, change.branch)
change.project.name, change.branch, zuul_event_id=event)
if not self._hasRequiredStatusChecks(allow_needs, protection, pull):
return False
@ -1542,8 +1542,10 @@ class GithubConnection(BaseConnection):
return reviews.values()
def _getBranchProtection(self, project_name: str, branch: str):
github = self.getGithubClient(project_name)
def _getBranchProtection(self, project_name: str, branch: str,
zuul_event_id=None):
github = self.getGithubClient(
project_name, zuul_event_id=zuul_event_id)
url = github.session.build_url('repos', project_name,
'branches', branch,
'protection')

View File

@ -50,13 +50,13 @@ class GithubSource(BaseSource):
# to perform the merge will ensure this is updated.
return change.is_merged
def canMerge(self, change, allow_needs):
def canMerge(self, change, allow_needs, event=None):
"""Determine if change can merge."""
if not change.number:
# Not a pull request, considering merged.
return True
return self.connection.canMerge(change, allow_needs)
return self.connection.canMerge(change, allow_needs, event=event)
def postConfig(self):
"""Called after configuration has been processed."""

View File

@ -27,6 +27,7 @@ import voluptuous as v
import gear
from zuul.connection import BaseConnection
from zuul.lib.logutil import get_annotated_logger
from zuul.web.handler import BaseWebController
from zuul.lib.config import get_default
from zuul.model import Ref, Branch, Tag
@ -700,7 +701,8 @@ class PagureConnection(BaseConnection):
flag = pagure.get_pr_flags(change.number, last=True)
return True if flag.get('status', '') == 'success' else False
def canMerge(self, change, allow_needs):
def canMerge(self, change, allow_needs, event=None):
log = get_annotated_logger(self.log, event)
pagure = self.get_project_api_client(change.project.name)
pr = pagure.get_pr(change.number)
@ -716,16 +718,15 @@ class PagureConnection(BaseConnection):
if threshold is None:
self.log.debug("No threshold_reached attribute found")
self.log.debug(
log.debug(
'PR %s#%s mergeability details mergeable: %s '
'flag: %s threshold: %s' % (
change.project.name, change.number, mergeable,
ci_flag, threshold))
'flag: %s threshold: %s', change.project.name, change.number,
mergeable, ci_flag, threshold)
can_merge = mergeable and ci_flag and threshold
self.log.info('Check PR %s#%s mergeability can_merge: %s' % (
change.project.name, change.number, can_merge))
log.info('Check PR %s#%s mergeability can_merge: %s',
change.project.name, change.number, can_merge)
return can_merge
def getPull(self, project_name, number):

View File

@ -47,12 +47,12 @@ class PagureSource(BaseSource):
return True
return change.is_merged
def canMerge(self, change, allow_needs):
def canMerge(self, change, allow_needs, event=None):
"""Determine if change can merge."""
if not change.number:
# Not a pull request, considering merged.
return True
return self.connection.canMerge(change, allow_needs)
return self.connection.canMerge(change, allow_needs, event=event)
def postConfig(self):
"""Called after configuration has been processed."""

View File

@ -179,7 +179,7 @@ class PipelineManager(object):
report_errors.append(str(e))
return report_errors
def isChangeReadyToBeEnqueued(self, change):
def isChangeReadyToBeEnqueued(self, change, event):
return True
def enqueueChangesAhead(self, change, event, quiet, ignore_requirements,
@ -298,7 +298,7 @@ class PipelineManager(object):
change, f, str(match_result)))
return False
if not self.isChangeReadyToBeEnqueued(change):
if not self.isChangeReadyToBeEnqueued(change, event):
log.debug("Change %s is not ready to be enqueued, ignoring" %
change)
return False

View File

@ -97,10 +97,12 @@ class DependentPipelineManager(PipelineManager):
items = change_queue.queue
return items.index(item)
def isChangeReadyToBeEnqueued(self, change):
def isChangeReadyToBeEnqueued(self, change, event):
log = get_annotated_logger(self.log, event)
source = change.project.source
if not source.canMerge(change, self.getSubmitAllowNeeds()):
self.log.debug("Change %s can not merge, ignoring" % change)
if not source.canMerge(change, self.getSubmitAllowNeeds(),
event=event):
log.debug("Change %s can not merge, ignoring", change)
return False
return True

View File

@ -41,7 +41,7 @@ class BaseSource(object, metaclass=abc.ABCMeta):
If head is provided the change is checked if it is at head."""
@abc.abstractmethod
def canMerge(self, change, allow_needs):
def canMerge(self, change, allow_needs, event=None):
"""Determine if change can merge."""
def postConfig(self):