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 113 # review doesn't really work with multiple revisions, so let's 114 # just assume it's None there 115 if isinstance(revision, dict): 116 revision = None 117 118 if project is not None and revision is not None: 119 message, verified, reviewed = self.reviewCB(builderName, build, result, self.status, self.reviewArg) 120 if message is None: 121 return 122 self.sendCodeReview(project, revision, message, verified, reviewed) 123 return
124
125 - def sendCodeReview(self, project, revision, message=None, verified=0, reviewed=0):
126 command = ["ssh", self.gerrit_username + "@" + self.gerrit_server, "-p %d" % self.gerrit_port, 127 "gerrit", "review", "--project %s" % str(project)] 128 if message: 129 command.append("--message '%s'" % message.replace("'","\"")) 130 if verified: 131 command.extend(["--verified %d" % int(verified)]) 132 if reviewed: 133 command.extend(["--code-review %d" % int(reviewed)]) 134 command.append(str(revision)) 135 print command 136 reactor.spawnProcess(self.LocalPP(self), "ssh", command)
137 138 # vim: set ts=4 sts=4 sw=4 et: 139