Rename "filter" parameters

The word "filter" doesn't help very much with clarity and it's
sometimes confusing whether you need to use "username" or
"username_filter".  Just drop the filter suffix where it's used.

Tests are intentionally not altered in this change to verify
backwords compatibility.

Change-Id: I2edddad2e1f6ce4c86265de5f7a8cda1ebf436df
This commit is contained in:
James E. Blair 2014-06-23 14:42:53 -07:00
parent 9c17dbf763
commit 1fbfceb04b
4 changed files with 78 additions and 48 deletions

View File

@ -401,28 +401,40 @@ explanation of each of the parameters::
``code-review: 2`` matches a ``+2`` vote on the code review category.
Multiple approvals may be listed.
*email_filter*
*email*
This is used for any event. It takes a regex applied on the performer
email, i.e. Gerrit account email address. If you want to specify
several email filters, you must use a YAML list. Make sure to use non
greedy matchers and to escapes dots!
Example: ``email_filter: ^.*?@example\.org$``.
Example: ``email: ^.*?@example\.org$``.
*username_filter*
*email_filter* (deprecated)
A deprecated alternate spelling of *email*. Only one of *email* or
*email_filter* should be used.
*username*
This is used for any event. It takes a regex applied on the performer
username, i.e. Gerrit account name. If you want to specify several
username filters, you must use a YAML list. Make sure to use non greedy
matchers and to escapes dots!
Example: ``username_filter: ^jenkins$``.
Example: ``username: ^jenkins$``.
*comment_filter*
*username_filter* (deprecated)
A deprecated alternate spelling of *username*. Only one of *username* or
*username_filter* should be used.
*comment*
This is only used for ``comment-added`` events. It accepts a list of
regexes that are searched for in the comment string. If any of these
regexes matches a portion of the comment string the trigger is
matched. ``comment_filter: retrigger`` will match when comments
matched. ``comment: retrigger`` will match when comments
containing 'retrigger' somewhere in the comment text are added to a
change.
*comment_filter* (deprecated)
A deprecated alternate spelling of *comment*. Only one of *comment* or
*comment_filter* should be used.
*require-approval*
This may be used for any event. It requires that a certain kind
of approval be present for the current patchset of the change (the
@ -457,10 +469,14 @@ explanation of each of the parameters::
*username*
If present, an approval from this username is required.
*email-filter*
*email*
If present, an approval with this email address is required. It
is treated as a regular expression as above.
*email-filter* (deprecated)
A deprecated alternate spelling of *email*. Only one of *email* or
*email_filter* should be used.
*older-than*
If present, the approval must be older than this amount of time
to match. Provide a time interval as a number with a suffix of

View File

@ -1,6 +1,7 @@
# Copyright 2013 OpenStack Foundation
# Copyright 2013 Antoine "hashar" Musso
# Copyright 2013 Wikimedia Foundation Inc.
# Copyright 2014 Hewlett-Packard Development Company, L.P.
#
# 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
@ -37,6 +38,7 @@ class LayoutSchema(object):
require_approval = v.Schema({'username': str,
'email-filter': str,
'email': str,
'older-than': str,
'newer-than': str,
}, extra=True)
@ -49,8 +51,11 @@ class LayoutSchema(object):
'comment-added',
'ref-updated')),
'comment_filter': toList(str),
'comment': toList(str),
'email_filter': toList(str),
'email': toList(str),
'username_filter': toList(str),
'username': toList(str),
'branch': toList(str),
'ref': toList(str),
'approval': toList(variable_dict),

View File

@ -987,8 +987,8 @@ class BaseFilter(object):
for k, v in a.items():
if k == 'username':
pass
elif k == 'email-filter':
a[k] = re.compile(v)
elif k in ['email', 'email-filter']:
a['email'] = re.compile(v)
elif k == 'newer-than':
a[k] = time_to_seconds(v)
elif k == 'older-than':
@ -996,6 +996,8 @@ class BaseFilter(object):
else:
if not isinstance(v, list):
a[k] = [v]
if 'email-filter' in a:
del a['email-filter']
def matchesRequiredApprovals(self, change):
now = time.time()
@ -1010,7 +1012,7 @@ class BaseFilter(object):
if k == 'username':
if (by.get('username', '') != v):
found_approval = False
elif k == 'email-filter':
elif k == 'email':
if (not v.search(by.get('email', ''))):
found_approval = False
elif k == 'newer-than':
@ -1035,22 +1037,22 @@ class BaseFilter(object):
class EventFilter(BaseFilter):
def __init__(self, types=[], branches=[], refs=[], event_approvals={},
comment_filters=[], email_filters=[], username_filters=[],
timespecs=[], required_approvals=[]):
comments=[], emails=[], usernames=[], timespecs=[],
required_approvals=[]):
super(EventFilter, self).__init__(
required_approvals=required_approvals)
self._types = types
self._branches = branches
self._refs = refs
self._comment_filters = comment_filters
self._email_filters = email_filters
self._username_filters = username_filters
self._comments = comments
self._emails = emails
self._usernames = usernames
self.types = [re.compile(x) for x in types]
self.branches = [re.compile(x) for x in branches]
self.refs = [re.compile(x) for x in refs]
self.comment_filters = [re.compile(x) for x in comment_filters]
self.email_filters = [re.compile(x) for x in email_filters]
self.username_filters = [re.compile(x) for x in username_filters]
self.comments = [re.compile(x) for x in comments]
self.emails = [re.compile(x) for x in emails]
self.usernames = [re.compile(x) for x in usernames]
self.event_approvals = event_approvals
self.timespecs = timespecs
@ -1069,12 +1071,12 @@ class EventFilter(BaseFilter):
if self.required_approvals:
ret += ' required_approvals: %s' % ', '.join(
['%s' % a for a in self.required_approvals])
if self._comment_filters:
ret += ' comment_filters: %s' % ', '.join(self._comment_filters)
if self._email_filters:
ret += ' email_filters: %s' % ', '.join(self._email_filters)
if self._username_filters:
ret += ' username_filters: %s' % ', '.join(self._username_filters)
if self._comments:
ret += ' comments: %s' % ', '.join(self._comments)
if self._emails:
ret += ' emails: %s' % ', '.join(self._emails)
if self._usernames:
ret += ' username_filters: %s' % ', '.join(self._usernames)
if self.timespecs:
ret += ' timespecs: %s' % ', '.join(self.timespecs)
ret += '>'
@ -1106,36 +1108,36 @@ class EventFilter(BaseFilter):
if self.refs and not matches_ref:
return False
# comment_filters are ORed
matches_comment_filter = False
for comment_filter in self.comment_filters:
# comments are ORed
matches_comment_re = False
for comment_re in self.comments:
if (event.comment is not None and
comment_filter.search(event.comment)):
matches_comment_filter = True
if self.comment_filters and not matches_comment_filter:
comment_re.search(event.comment)):
matches_comment_re = True
if self.comments and not matches_comment_re:
return False
# We better have an account provided by Gerrit to do
# email filtering.
if event.account is not None:
account_email = event.account.get('email')
# email_filters are ORed
matches_email_filter = False
for email_filter in self.email_filters:
# emails are ORed
matches_email_re = False
for email_re in self.emails:
if (account_email is not None and
email_filter.search(account_email)):
matches_email_filter = True
if self.email_filters and not matches_email_filter:
email_re.search(account_email)):
matches_email_re = True
if self.emails and not matches_email_re:
return False
# username_filters are ORed
# usernames are ORed
account_username = event.account.get('username')
matches_username_filter = False
for username_filter in self.username_filters:
matches_username_re = False
for username_re in self.usernames:
if (account_username is not None and
username_filter.search(account_username)):
matches_username_filter = True
if self.username_filters and not matches_username_filter:
username_re.search(account_username)):
matches_username_re = True
if self.usernames and not matches_username_re:
return False
# approvals are ANDed

View File

@ -292,16 +292,23 @@ class Scheduler(threading.Thread):
for approval_dict in toList(trigger.get('approval')):
for k, v in approval_dict.items():
approvals[k] = v
# Backwards compat for *_filter versions of these args
comments = toList(trigger.get('comment'))
if not comments:
comments = toList(trigger.get('comment_filter'))
emails = toList(trigger.get('email'))
if not emails:
emails = toList(trigger.get('email_filter'))
usernames = toList(trigger.get('username'))
if not usernames:
usernames = toList(trigger.get('username_filter'))
f = EventFilter(types=toList(trigger['event']),
branches=toList(trigger.get('branch')),
refs=toList(trigger.get('ref')),
event_approvals=approvals,
comment_filters=
toList(trigger.get('comment_filter')),
email_filters=
toList(trigger.get('email_filter')),
username_filters=
toList(trigger.get('username_filter')),
comments=comments,
emails=emails,
usernames=usernames,
required_approvals=
toList(trigger.get('require-approval')))
manager.event_filters.append(f)