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
113
114
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
139