Package buildbot :: Package status :: Module status_gerrit
[frames] | no frames]

Source Code for Module buildbot.status.status_gerrit

  1  # This file is part of Buildbot.  Buildbot is free software: you can 
  2  # redistribute it and/or modify it under the terms of the GNU General Public 
  3  # License as published by the Free Software Foundation, version 2. 
  4  # 
  5  # This program is distributed in the hope that it will be useful, but WITHOUT 
  6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
  8  # details. 
  9  # 
 10  # You should have received a copy of the GNU General Public License along with 
 11  # this program; if not, write to the Free Software Foundation, Inc., 51 
 12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 13  # 
 14  # Copyright Buildbot Team Members 
 15   
 16   
 17  """Push events to gerrit 
 18   
 19  .""" 
 20   
 21  from buildbot.status.base import StatusReceiverMultiService 
 22  from buildbot.status.builder import Results, SUCCESS, RETRY 
 23  from twisted.internet import reactor 
 24  from twisted.internet.protocol import ProcessProtocol 
 25   
26 -def defaultReviewCB(builderName, build, result, status, arg):
27 if result == RETRY: 28 return None, 0, 0 29 30 message = "Buildbot finished compiling your patchset\n" 31 message += "on configuration: %s\n" % builderName 32 message += "The result is: %s\n" % Results[result].upper() 33 34 # message, verified, reviewed 35 return message, (result == SUCCESS or -1), 0
36
37 -class GerritStatusPush(StatusReceiverMultiService):
38 """Event streamer to a gerrit ssh server.""" 39
40 - def __init__(self, server, username, reviewCB=defaultReviewCB, port=29418, reviewArg=None, 41 **kwargs):
42 """ 43 @param server: Gerrit SSH server's address to use for push event notifications. 44 @param username: Gerrit SSH server's username. 45 @param reviewCB: Callback that is called each time a build is finished, and that is used 46 to define the message and review approvals depending on the build result. 47 @param port: Gerrit SSH server's port. 48 @param reviewArg: Optional argument that is passed to the callback. 49 """ 50 StatusReceiverMultiService.__init__(self) 51 # Parameters. 52 self.gerrit_server = server 53 self.gerrit_username = username 54 self.gerrit_port = port 55 self.reviewCB = reviewCB 56 self.reviewArg = reviewArg
57
58 - class LocalPP(ProcessProtocol):
59 - def __init__(self, status):
60 self.status = status
61
62 - def outReceived(self, data):
63 print "gerritout:", data
64
65 - def errReceived(self, data):
66 print "gerriterr:", data
67
68 - def processEnded(self, status_object):
69 if status_object.value.exitCode: 70 print "gerrit status: ERROR:", status_object 71 else: 72 print "gerrit status: OK"
73
74 - def startService(self):
75 print """Starting up.""" 76 StatusReceiverMultiService.startService(self) 77 self.status = self.parent.getStatus() 78 self.status.subscribe(self)
79
80 - def builderAdded(self, name, builder):
81 return self # subscribe to this builder
82
83 - def buildFinished(self, builderName, build, result):
84 """Do the SSH gerrit verify command to the server.""" 85 86 # Gerrit + Repo 87 downloads = build.getProperty("repo_downloads") 88 downloaded = build.getProperty("repo_downloaded") 89 if downloads is not None and downloaded is not None: 90 downloaded = downloaded.split(" ") 91 if downloads and 2 * len(downloads) == len(downloaded): 92 message, verified, reviewed = self.reviewCB(builderName, build, result, self.status, self.reviewArg) 93 if message is None: 94 return 95 for i in range(0, len(downloads)): 96 try: 97 project, change1 = downloads[i].split(" ") 98 except ValueError: 99 return # something is wrong, abort 100 change2 = downloaded[2 * i] 101 revision = downloaded[2 * i + 1] 102 if change1 == change2: 103 self.sendCodeReview(project, revision, message, verified, reviewed) 104 else: 105 return # something is wrong, abort 106 return 107 108 # Gerrit + Git 109 if build.getProperty("gerrit_branch") is not None: # used only to verify Gerrit source 110 project = build.getProperty("project") 111 revision = build.getProperty("got_revision") 112 if project is not None and revision is not None: 113 message, verified, reviewed = self.reviewCB(builderName, build, result, self.status, self.reviewArg) 114 if message is None: 115 return 116 self.sendCodeReview(project, revision, message, verified, reviewed) 117 return
118
119 - def sendCodeReview(self, project, revision, message=None, verified=0, reviewed=0):
120 command = ["ssh", self.gerrit_username + "@" + self.gerrit_server, "-p %d" % self.gerrit_port, 121 "gerrit", "review", "--project %s" % str(project)] 122 if message: 123 command.append("--message '%s'" % message.replace("'","\"")) 124 if verified: 125 command.extend(["--verified %d" % int(verified)]) 126 if reviewed: 127 command.extend(["--code-review %d" % int(reviewed)]) 128 command.append(str(revision)) 129 print command 130 reactor.spawnProcess(self.LocalPP(self), "ssh", command)
131 132 # vim: set ts=4 sts=4 sw=4 et: 133