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 
 23  from twisted.internet import reactor 
 24  from twisted.internet.protocol import ProcessProtocol 
 25   
26 -def defaultReviewCB(builderName, build, result, arg):
27 message = "Buildbot finished compiling your patchset\n" 28 message += "on configuration: %s\n" % builderName 29 message += "The result is: %s\n" % Results[result].upper() 30 31 # message, verified, reviewed 32 return message, (result == 0 or -1), 0
33
34 -class GerritStatusPush(StatusReceiverMultiService):
35 """Event streamer to a gerrit ssh server.""" 36
37 - def __init__(self, server, username, reviewCB=defaultReviewCB, port=29418, reviewArg=None, 38 **kwargs):
39 """ 40 @param server: Gerrit SSH server's address to use for push event notifications. 41 @param username: Gerrit SSH server's username. 42 @param reviewCB: Callback that is called each time a build is finished, and that is used 43 to define the message and review approvals depending on the build result. 44 @param port: Gerrit SSH server's port. 45 @param reviewArg: Optional argument that is passed to the callback. 46 """ 47 StatusReceiverMultiService.__init__(self) 48 # Parameters. 49 self.gerrit_server = server 50 self.gerrit_username = username 51 self.gerrit_port = port 52 self.reviewCB = reviewCB 53 self.reviewArg = reviewArg
54
55 - class LocalPP(ProcessProtocol):
56 - def __init__(self, status):
57 self.status = status
58
59 - def outReceived(self, data):
60 print "gerritout:", data
61
62 - def errReceived(self, data):
63 print "gerriterr:", data
64
65 - def processEnded(self, status_object):
66 if status_object.value.exitCode: 67 print "gerrit status: ERROR:", status_object 68 else: 69 print "gerrit status: OK"
70
71 - def setServiceParent(self, parent):
72 print """Starting up.""" 73 StatusReceiverMultiService.setServiceParent(self, parent) 74 self.status = self.parent.getStatus() 75 self.status.subscribe(self)
76
77 - def builderAdded(self, name, builder):
78 return self # subscribe to this builder
79
80 - def buildFinished(self, builderName, build, result):
81 """Do the SSH gerrit verify command to the server.""" 82 repo, git = False, False 83 84 # Gerrit + Repo 85 try: 86 downloads = build.getProperty("repo_downloads") 87 downloaded = build.getProperty("repo_downloaded").split(" ") 88 repo = True 89 except KeyError: 90 pass 91 92 if repo: 93 if downloads and 2 * len(downloads) == len(downloaded): 94 message, verified, reviewed = self.reviewCB(builderName, build, result, self.reviewArg) 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 try: 110 build.getProperty("gerrit_branch") # used only to verify Gerrit source 111 project = build.getProperty("project") 112 revision = build.getProperty("got_revision") 113 git = True 114 except KeyError: 115 pass 116 117 if git: 118 message, verified, reviewed = self.reviewCB(builderName, build, result, self.reviewArg) 119 self.sendCodeReview(project, revision, message, verified, reviewed) 120 return
121
122 - def sendCodeReview(self, project, revision, message=None, verified=0, reviewed=0):
123 command = ["ssh", self.gerrit_username + "@" + self.gerrit_server, "-p %d" % self.gerrit_port, 124 "gerrit", "review", "--project %s" % str(project)] 125 if message: 126 command.append("--message '%s'" % message) 127 if verified: 128 command.extend(["--verified %d" % int(verified)]) 129 if reviewed: 130 command.extend(["--code-review %d" % int(reviewed)]) 131 command.append(str(revision)) 132 print command 133 reactor.spawnProcess(self.LocalPP(self), "ssh", command)
134 135 # vim: set ts=4 sts=4 sw=4 et: 136