zuul/zuul/change_matcher.py
James E. Blair a7f51ca625 Inherit playbooks and modify job variance
An earlier change dealt with inheritance for pre and post playbooks;
they are nested so that parent job pre and post playbooks run first
and last respectively.

As for the actual playbook, since it's implied by the job name, it's
not clear whether it should be overidden or not.  We could drop that
and say that if you specify a 'run' attribute, it means you want to
set the playbook for a job, but if you omit it, you want to use the
parent's playbook.

However, we could keep the implied playbook behavior by making the
'run' attribute a list and adding a playbook context to the list each
time a job is inherited.  Then the launcher can walk the list in order
and the first playbook it finds, it runs.

This is what is implemented here.

However, we need to restrict playbooks or other execution-related
job attributes from being overidden by out-of-repo variants (such
as the implicit variant which is created by every entry in a
project-pipeline).  To do this, we make more of a distinction
between inheritance and variance, implementing each with its own
method on Job.  This way we can better control when certain
attributes are allowed to be set.  The 'final' job attribute is
added to indicate that a job should not accept any further
modifications to execution-related attributes.

The attribute storage in Job is altered so that each Job object
explicitly stores whether an attribute was set on it.  This makes
it easier to start with a job and apply only the specified
attributes of each variant in turn.  Default values are still
handled.

Essentially, each "job" appearance in the configuration will
create a new Job entry with exactly those attributes (with the
exception that a job where "parent" is set will first copy
attributes which are explicitly set on its parent).

When a job is frozen after an item is enqueued, the first
matching job is copied, and each subsequent matching job is
applied as a varient.  When that is completed, if the job has
un-inheritable auth information, it is set as final, and then the
project-pipeline variant is applied.

New tests are added to exercise the new methods on Job.

Change-Id: Iaf6d661a7bd0085e55bc301f83fe158fd0a70166
2017-02-10 09:57:01 -08:00

139 lines
3.6 KiB
Python

# Copyright 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
This module defines classes used in matching changes based on job
configuration.
"""
import re
class AbstractChangeMatcher(object):
def __init__(self, regex):
self._regex = regex
self.regex = re.compile(regex)
def matches(self, change):
"""Return a boolean indication of whether change matches
implementation-specific criteria.
"""
raise NotImplementedError()
def copy(self):
return self.__class__(self._regex)
def __deepcopy__(self, memo):
return self.copy()
def __eq__(self, other):
return str(self) == str(other)
def __ne__(self, other):
return not self.__eq__(other)
def __str__(self):
return '{%s:%s}' % (self.__class__.__name__, self._regex)
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, self._regex)
class ProjectMatcher(AbstractChangeMatcher):
def matches(self, change):
return self.regex.match(str(change.project))
class BranchMatcher(AbstractChangeMatcher):
def matches(self, change):
return (
(hasattr(change, 'branch') and self.regex.match(change.branch)) or
(hasattr(change, 'ref') and self.regex.match(change.ref))
)
class FileMatcher(AbstractChangeMatcher):
def matches(self, change):
if not hasattr(change, 'files'):
return False
for file_ in change.files:
if self.regex.match(file_):
return True
return False
class AbstractMatcherCollection(AbstractChangeMatcher):
def __init__(self, matchers):
self.matchers = matchers
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return '{%s:%s}' % (self.__class__.__name__,
','.join([str(x) for x in self.matchers]))
def __repr__(self):
return '<%s>' % self.__class__.__name__
def copy(self):
return self.__class__(self.matchers[:])
class MatchAllFiles(AbstractMatcherCollection):
commit_regex = re.compile('^/COMMIT_MSG$')
@property
def regexes(self):
for matcher in self.matchers:
yield matcher.regex
yield self.commit_regex
def matches(self, change):
if not (hasattr(change, 'files') and len(change.files) > 1):
return False
for file_ in change.files:
matched_file = False
for regex in self.regexes:
if regex.match(file_):
matched_file = True
break
if not matched_file:
return False
return True
class MatchAll(AbstractMatcherCollection):
def matches(self, change):
for matcher in self.matchers:
if not matcher.matches(change):
return False
return True
class MatchAny(AbstractMatcherCollection):
def matches(self, change):
for matcher in self.matchers:
if matcher.matches(change):
return True
return False