1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
35 return message, (result == SUCCESS or -1), 0
36
38 """Event streamer to a gerrit ssh server."""
39
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
52 self.gerrit_server = server
53 self.gerrit_username = username
54 self.gerrit_port = port
55 self.reviewCB = reviewCB
56 self.reviewArg = reviewArg
57
61
63 print "gerritout:", data
64
66 print "gerriterr:", data
67
69 if status_object.value.exitCode:
70 print "gerrit status: ERROR:", status_object
71 else:
72 print "gerrit status: OK"
73
79
82
84 """Do the SSH gerrit verify command to the server."""
85
86
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
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
106 return
107
108
109 if build.getProperty("gerrit_branch") is not None:
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
133