Skip to content

Commit

Permalink
Merge pull request #1690 from yurboc/eight-contrib
Browse files Browse the repository at this point in the history
Add flag '--notify' to GerritStatusPush
  • Loading branch information
Mikhail Sobolev committed May 29, 2015
2 parents e000973 + d6158d1 commit a952066
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
13 changes: 12 additions & 1 deletion master/buildbot/status/status_gerrit.py
Expand Up @@ -21,6 +21,7 @@
import time
import warnings

from buildbot import config
from buildbot.status import buildset
from buildbot.status.base import StatusReceiverMultiService
from buildbot.status.builder import EXCEPTION
Expand Down Expand Up @@ -134,7 +135,7 @@ class GerritStatusPush(StatusReceiverMultiService, buildset.BuildSetSummaryNotif
def __init__(self, server, username, reviewCB=DEFAULT_REVIEW,
startCB=None, port=29418, reviewArg=None,
startArg=None, summaryCB=DEFAULT_SUMMARY, summaryArg=None,
identity_file=None, **kwargs):
identity_file=None, notify=None, **kwargs):
StatusReceiverMultiService.__init__(self)

# If neither reviewCB nor summaryCB were specified, default to sending
Expand All @@ -156,6 +157,13 @@ def __init__(self, server, username, reviewCB=DEFAULT_REVIEW,
self.gerrit_version = None
self.gerrit_version_time = 0
self.gerrit_identity_file = identity_file
notify_values = ['NONE', 'OWNER', 'OWNER_REVIEWERS', 'ALL']
if notify is None or notify in notify_values:
self.gerrit_notify = notify
else:
config.error(
"gerrit: wrong value of --notify %s "
"(possible values: %s)" % (notify, ','.join(notify_values)))
self.reviewCB = reviewCB
self.reviewArg = reviewArg
self.startCB = startCB
Expand Down Expand Up @@ -349,6 +357,9 @@ def sendCodeReview(self, project, revision, result):
if message:
command.append("--message '%s'" % message.replace("'", "\""))

if self.gerrit_notify:
command.extend(['--notify', self.gerrit_notify])

labels = result.get('labels', None)
if labels:
assert gerrit_version
Expand Down
43 changes: 40 additions & 3 deletions master/buildbot/test/unit/test_status_gerrit.py
Expand Up @@ -13,17 +13,21 @@
#
# Copyright Buildbot Team Members

from buildbot.config import ConfigErrors
from buildbot.status.results import FAILURE
from buildbot.status.results import SUCCESS
from buildbot.status.status_gerrit import GERRIT_LABEL_REVIEWED
from buildbot.status.status_gerrit import GERRIT_LABEL_VERIFIED
from buildbot.status.status_gerrit import GerritStatusPush
from buildbot.status.status_gerrit import makeReviewResult
from buildbot.test.fake.fakebuild import FakeBuildStatus
from buildbot.test.fake import fakedb
from buildbot.test.fake import fakemaster
from mock import call, Mock
from buildbot.test.fake.fakebuild import FakeBuildStatus
from mock import Mock
from mock import call
from mock import patch
from twisted.internet import defer
from twisted.internet import reactor
from twisted.trial import unittest


Expand Down Expand Up @@ -225,7 +229,7 @@ def test_gerrit_ssh_cmd(self):
self.assertEqual(expected1, without_identity._gerritCmd('foo'))

with_identity = GerritStatusPush(
identity_file='/path/to/id_rsa', **kwargs)
identity_file='/path/to/id_rsa', **kwargs)
expected2 = [
'ssh', '-i', '/path/to/id_rsa', 'buildbot@example.com', '-p', '29418',
'gerrit', 'foo',
Expand Down Expand Up @@ -333,3 +337,36 @@ def test_buildsetComplete_success_sends_review_legacy(self):

def test_buildsetComplete_failure_sends_review_legacy(self):
self.check_single_build_legacy(FAILURE, -1)

# check all possible values of flag 'notify'

@patch.object(reactor, 'spawnProcess')
@defer.inlineCallbacks
def check_notify_flag_in_ssh_command(self, spawn_process, notify, expected_value):
gsp = yield GerritStatusPush('host.example.com', 'username',
reviewCB=testReviewCB, notify=notify)

gsp.master = yield fakemaster.make_master()
gsp.master_status = gsp.master.status
gsp.getCachedVersion = yield Mock(return_value="2.9")

yield self.run_fake_single_build(gsp, SUCCESS)

ssh_args = spawn_process.call_args[0][2]
ssh_msg = ' '.join(ssh_args)
assert expected_value in ssh_msg, "Flag value wrong or not present"

@defer.inlineCallbacks
def test_valid_notify_flag(self):
# If any of these flag values fail, the test fails by
# GerritStatusPush raising a ConfigErrors exception.

yield _get_prepared_gsp(notify=None)

for flag in ['ALL', 'OWNER', 'OWNER_REVIEWERS', 'NONE']:
yield self.check_notify_flag_in_ssh_command(notify=flag,
expected_value='--notify %s' % flag)

@defer.inlineCallbacks
def test_invalid_notify_flag(self):
yield self.assertRaises(ConfigErrors, _get_prepared_gsp, notify='WRONG_VAL')
8 changes: 8 additions & 0 deletions master/docs/examples/git_gerrit.cfg
Expand Up @@ -184,7 +184,15 @@ def gerritSummaryCB(buildInfoList, results, status, arg):
})

c['buildbotURL'] = 'http://buildbot.example.com/'

# The 'GerritStatusPush' target is used to publish build results in Gerrit
# To create message (with Code-Review) used function 'gerritReviewCB'
# Notification about this message will be sent to to all (change owners,
# reviewers, watchers and any user who has starred the change)
# https://gerrit-documentation.storage.googleapis.com/Documentation/2.9/cmd-review.html#_options

c['status'].append(status.GerritStatusPush(gerrit_url, gerrit_user,
notify='ALL',
reviewCB=gerritReviewCB,
reviewArg=c['buildbotURL'],
startCB=gerritStartCB,
Expand Down
6 changes: 5 additions & 1 deletion master/docs/manual/cfg-statustargets.rst
Expand Up @@ -1478,10 +1478,14 @@ GerritStatusPush
:class:`GerritStatusPush` sends review of the :class:`Change` back to the Gerrit server, optionally also sending a message when a build is started.
GerritStatusPush can send a separate review for each build that completes, or a single review summarizing the results for all of the builds.

.. py:class:: GerritStatusPush(server, username, reviewCB, startCB, port, reviewArg, startArg, summaryCB, summaryArg, ...)
.. py:class:: GerritStatusPush(server, username, notify, reviewCB, startCB, port, reviewArg, startArg, summaryCB, summaryArg, ...)
:param string server: Gerrit SSH server's address to use for push event notifications.
:param string username: Gerrit SSH server's username.
:param string notify: Notify handling that defines to whom email notifications should be sent after the review is stored.
Allowed values are NONE, OWNER, OWNER_REVIEWERS and ALL. If not set, the default is ALL.
This parameter released in Gerrit 2.9:
https://gerrit-documentation.storage.googleapis.com/Documentation/2.9/cmd-review.html#_options
:param int port: (optional) Gerrit SSH server's port (default: 29418)
:param reviewCB: (optional) callback that is called each time a build is finished, and that is used to define the message and review approvals depending on the build result.
:param reviewArg: (optional) argument passed to the review callback.
Expand Down
2 changes: 2 additions & 0 deletions master/docs/relnotes/index.rst
Expand Up @@ -31,6 +31,8 @@ Slave
Features
~~~~~~~~

* :bb:status:`GerritStatusPush` now has parameter --notify which is used to control e-mail notifications from Gerrit.

Fixes
~~~~~

Expand Down

0 comments on commit a952066

Please sign in to comment.