James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1 | # Copyright 2012 Hewlett-Packard Development Company, L.P. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import re |
James E. Blair | ff986a1 | 2012-05-30 14:56:51 -0700 | [diff] [blame] | 16 | import time |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 17 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 18 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 19 | class Pipeline(object): |
| 20 | """A top-level pipeline such as check, gate, post, etc.""" |
| 21 | def __init__(self, name): |
| 22 | self.name = name |
| 23 | self.job_trees = {} # project -> JobTree |
| 24 | self.manager = None |
| 25 | |
James E. Blair | d09c17a | 2012-08-07 09:23:14 -0700 | [diff] [blame] | 26 | def __repr__(self): |
| 27 | return '<Pipeline %s>' % self.name |
| 28 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 29 | def setManager(self, manager): |
| 30 | self.manager = manager |
| 31 | |
| 32 | def addProject(self, project): |
| 33 | job_tree = JobTree(None) # Null job == job tree root |
| 34 | self.job_trees[project] = job_tree |
| 35 | return job_tree |
| 36 | |
| 37 | def getProjects(self): |
| 38 | return self.job_trees.keys() |
| 39 | |
| 40 | def getJobTree(self, project): |
| 41 | tree = self.job_trees.get(project) |
| 42 | return tree |
| 43 | |
| 44 | def getJobs(self, changeish): |
James E. Blair | d09c17a | 2012-08-07 09:23:14 -0700 | [diff] [blame] | 45 | tree = self.getJobTree(changeish.project) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 46 | if not tree: |
| 47 | return [] |
| 48 | return changeish.filterJobs(tree.getJobs()) |
| 49 | |
| 50 | def _findJobsToRun(self, job_trees, changeish): |
| 51 | torun = [] |
| 52 | if changeish.change_ahead: |
| 53 | # Only run jobs if any 'hold' jobs on the change ahead |
| 54 | # have completed successfully. |
| 55 | if self.isHoldingFollowingChanges(changeish.change_ahead): |
| 56 | return [] |
| 57 | for tree in job_trees: |
| 58 | job = tree.job |
| 59 | result = None |
| 60 | if job: |
| 61 | if not job.changeMatches(changeish): |
| 62 | continue |
| 63 | build = changeish.current_build_set.getBuild(job.name) |
| 64 | if build: |
| 65 | result = build.result |
| 66 | else: |
| 67 | # There is no build for the root of this job tree, |
| 68 | # so we should run it. |
| 69 | torun.append(job) |
| 70 | # If there is no job, this is a null job tree, and we should |
| 71 | # run all of its jobs. |
| 72 | if result == 'SUCCESS' or not job: |
| 73 | torun.extend(self._findJobsToRun(tree.job_trees, changeish)) |
| 74 | return torun |
| 75 | |
| 76 | def findJobsToRun(self, changeish): |
James E. Blair | d09c17a | 2012-08-07 09:23:14 -0700 | [diff] [blame] | 77 | tree = self.getJobTree(changeish.project) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 78 | if not tree: |
| 79 | return [] |
| 80 | return self._findJobsToRun(tree.job_trees, changeish) |
| 81 | |
| 82 | def areAllJobsComplete(self, changeish): |
| 83 | for job in self.getJobs(changeish): |
| 84 | build = changeish.current_build_set.getBuild(job.name) |
| 85 | if not build or not build.result: |
| 86 | return False |
| 87 | return True |
| 88 | |
| 89 | def didAllJobsSucceed(self, changeish): |
| 90 | for job in self.getJobs(changeish): |
| 91 | build = changeish.current_build_set.getBuild(job.name) |
| 92 | if not build: |
| 93 | return False |
| 94 | if build.result != 'SUCCESS': |
| 95 | return False |
| 96 | return True |
| 97 | |
| 98 | def didAnyJobFail(self, changeish): |
| 99 | for job in self.getJobs(changeish): |
| 100 | build = changeish.current_build_set.getBuild(job.name) |
| 101 | if build and build.result == 'FAILURE': |
| 102 | return True |
| 103 | return False |
| 104 | |
| 105 | def isHoldingFollowingChanges(self, changeish): |
| 106 | for job in self.getJobs(changeish): |
| 107 | if not job.hold_following_changes: |
| 108 | continue |
| 109 | build = changeish.current_build_set.getBuild(job.name) |
| 110 | if not build: |
| 111 | return True |
| 112 | if build.result != 'SUCCESS': |
| 113 | return True |
| 114 | if not changeish.change_ahead: |
| 115 | return False |
| 116 | return self.isHoldingFollowingChanges(changeish.change_ahead) |
| 117 | |
| 118 | def formatStatus(self, changeish, indent=0, html=False): |
| 119 | indent_str = ' ' * indent |
| 120 | ret = '' |
| 121 | if html and changeish.url is not None: |
| 122 | ret += '%sProject %s change <a href="%s">%s</a>\n' % ( |
| 123 | indent_str, |
| 124 | changeish.project.name, |
| 125 | changeish.url, |
| 126 | changeish._id()) |
| 127 | else: |
| 128 | ret += '%sProject %s change %s\n' % (indent_str, |
| 129 | changeish.project.name, |
| 130 | changeish._id()) |
| 131 | for job in self.getJobs(changeish): |
| 132 | build = changeish.current_build_set.getBuild(job.name) |
| 133 | if build: |
| 134 | result = build.result |
| 135 | else: |
| 136 | result = None |
| 137 | job_name = job.name |
| 138 | if html: |
| 139 | if build: |
| 140 | url = build.url |
| 141 | else: |
| 142 | url = None |
| 143 | if url is not None: |
| 144 | job_name = '<a href="%s">%s</a>' % (url, job_name) |
| 145 | ret += '%s %s: %s' % (indent_str, job_name, result) |
| 146 | ret += '\n' |
| 147 | if changeish.change_ahead: |
| 148 | ret += '%sWaiting on:\n' % (indent_str) |
| 149 | ret += self.formatStatus(changeish.change_ahead, |
| 150 | indent + 2, html) |
| 151 | return ret |
| 152 | |
| 153 | def formatReport(self, changeish): |
| 154 | ret = '' |
| 155 | if self.didAllJobsSucceed(changeish): |
| 156 | ret += 'Build successful\n\n' |
| 157 | else: |
| 158 | ret += 'Build failed\n\n' |
| 159 | |
| 160 | for job in self.getJobs(changeish): |
| 161 | build = changeish.current_build_set.getBuild(job.name) |
| 162 | result = build.result |
| 163 | if result == 'SUCCESS' and job.success_message: |
| 164 | result = job.success_message |
| 165 | elif result == 'FAILURE' and job.failure_message: |
| 166 | result = job.failure_message |
| 167 | url = build.url |
| 168 | if not url: |
| 169 | url = job.name |
| 170 | ret += '- %s : %s\n' % (url, result) |
| 171 | return ret |
| 172 | |
| 173 | def formatDescription(self, build): |
| 174 | concurrent_changes = '' |
| 175 | concurrent_builds = '' |
| 176 | other_builds = '' |
| 177 | |
| 178 | for change in build.build_set.other_changes: |
| 179 | concurrent_changes += '<li><a href="{change.url}">\ |
| 180 | {change.number},{change.patchset}</a></li>'.format( |
| 181 | change=change) |
| 182 | |
| 183 | change = build.build_set.change |
| 184 | |
| 185 | for build in build.build_set.getBuilds(): |
| 186 | if build.base_url: |
| 187 | concurrent_builds += """\ |
| 188 | <li> |
| 189 | <a href="{build.base_url}"> |
| 190 | {build.job.name} #{build.number}</a>: {build.result} |
| 191 | </li> |
| 192 | """.format(build=build) |
| 193 | else: |
| 194 | concurrent_builds += """\ |
| 195 | <li> |
| 196 | {build.job.name}: {build.result} |
| 197 | </li>""".format(build=build) |
| 198 | |
| 199 | if build.build_set.previous_build_set: |
| 200 | other_build = build.build_set.previous_build_set.getBuild( |
| 201 | build.job.name) |
| 202 | if other_build: |
| 203 | other_builds += """\ |
| 204 | <li> |
| 205 | Preceded by: <a href="{build.base_url}"> |
| 206 | {build.job.name} #{build.number}</a> |
| 207 | </li> |
| 208 | """.format(build=other_build) |
| 209 | |
| 210 | if build.build_set.next_build_set: |
| 211 | other_build = build.build_set.next_build_set.getBuild( |
| 212 | build.job.name) |
| 213 | if other_build: |
| 214 | other_builds += """\ |
| 215 | <li> |
| 216 | Succeeded by: <a href="{build.base_url}"> |
| 217 | {build.job.name} #{build.number}</a> |
| 218 | </li> |
| 219 | """.format(build=other_build) |
| 220 | |
| 221 | result = build.build_set.result |
| 222 | |
| 223 | if change.number: |
| 224 | ret = """\ |
| 225 | <p> |
| 226 | Triggered by change: |
| 227 | <a href="{change.url}">{change.number},{change.patchset}</a><br/> |
| 228 | Branch: <b>{change.branch}</b><br/> |
| 229 | Pipeline: <b>{self.name}</b> |
| 230 | </p>""" |
| 231 | else: |
| 232 | ret = """\ |
| 233 | <p> |
| 234 | Triggered by reference: |
| 235 | {change.ref}</a><br/> |
| 236 | Old revision: <b>{change.oldrev}</b><br/> |
| 237 | New revision: <b>{change.newrev}</b><br/> |
| 238 | Pipeline: <b>{self.name}</b> |
| 239 | </p>""" |
| 240 | |
| 241 | if concurrent_changes: |
| 242 | ret += """\ |
| 243 | <p> |
| 244 | Other changes tested concurrently with this change: |
| 245 | <ul>{concurrent_changes}</ul> |
| 246 | </p> |
| 247 | """ |
| 248 | if concurrent_builds: |
| 249 | ret += """\ |
| 250 | <p> |
| 251 | All builds for this change set: |
| 252 | <ul>{concurrent_builds}</ul> |
| 253 | </p> |
| 254 | """ |
| 255 | |
| 256 | if other_builds: |
| 257 | ret += """\ |
| 258 | <p> |
| 259 | Other build sets for this change: |
| 260 | <ul>{other_builds}</ul> |
| 261 | </p> |
| 262 | """ |
| 263 | if result: |
| 264 | ret += """\ |
| 265 | <p> |
| 266 | Reported result: <b>{result}</b> |
| 267 | </p> |
| 268 | """ |
| 269 | |
| 270 | ret = ret.format(**locals()) |
| 271 | return ret |
| 272 | |
| 273 | def setResult(self, changeish, build): |
| 274 | if build.result != 'SUCCESS': |
| 275 | # Get a JobTree from a Job so we can find only its dependent jobs |
| 276 | root = self.getJobTree(changeish.project) |
| 277 | tree = root.getJobTreeForJob(build.job) |
| 278 | for job in tree.getJobs(): |
| 279 | fakebuild = Build(job, None) |
| 280 | fakebuild.result = 'SKIPPED' |
| 281 | changeish.addBuild(fakebuild) |
| 282 | |
| 283 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 284 | class ChangeQueue(object): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 285 | """DependentPipelines have multiple parallel queues shared by |
| 286 | different projects; this is one of them. For instance, there may |
| 287 | a queue shared by interrelated projects foo and bar, and a second |
| 288 | queue for independent project baz. Pipelines have one or more |
| 289 | PipelineQueues.""" |
| 290 | def __init__(self, pipeline): |
| 291 | self.pipeline = pipeline |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 292 | self.name = '' |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 293 | self.projects = [] |
| 294 | self._jobs = set() |
| 295 | self.queue = [] |
| 296 | |
James E. Blair | 9f9667e | 2012-06-12 17:51:08 -0700 | [diff] [blame] | 297 | def __repr__(self): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 298 | return '<ChangeQueue %s: %s>' % (self.pipeline.name, self.name) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 299 | |
| 300 | def getJobs(self): |
| 301 | return self._jobs |
| 302 | |
| 303 | def addProject(self, project): |
| 304 | if project not in self.projects: |
| 305 | self.projects.append(project) |
| 306 | names = [x.name for x in self.projects] |
| 307 | names.sort() |
| 308 | self.name = ', '.join(names) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 309 | self._jobs |= set(self.pipeline.getJobTree(project).getJobs()) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 310 | |
| 311 | def enqueueChange(self, change): |
| 312 | if self.queue: |
| 313 | self.queue[-1].change_behind = change |
| 314 | change.change_ahead = self.queue[-1] |
| 315 | self.queue.append(change) |
| 316 | change.queue = self |
| 317 | |
| 318 | def dequeueChange(self, change): |
| 319 | if change in self.queue: |
| 320 | self.queue.remove(change) |
| 321 | |
| 322 | def mergeChangeQueue(self, other): |
| 323 | for project in other.projects: |
| 324 | self.addProject(project) |
| 325 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 326 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 327 | class Project(object): |
| 328 | def __init__(self, name): |
| 329 | self.name = name |
| 330 | |
| 331 | def __str__(self): |
| 332 | return self.name |
| 333 | |
| 334 | def __repr__(self): |
| 335 | return '<Project %s>' % (self.name) |
| 336 | |
| 337 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 338 | class Job(object): |
| 339 | def __init__(self, name): |
James E. Blair | 222d498 | 2012-07-16 09:31:19 -0700 | [diff] [blame] | 340 | # If you add attributes here, be sure to add them to the copy method. |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 341 | self.name = name |
| 342 | self.failure_message = None |
| 343 | self.success_message = None |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 344 | self.parameter_function = None |
James E. Blair | 222d498 | 2012-07-16 09:31:19 -0700 | [diff] [blame] | 345 | self.hold_following_changes = False |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 346 | self.branches = [] |
| 347 | self._branches = [] |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 348 | |
| 349 | def __str__(self): |
| 350 | return self.name |
| 351 | |
| 352 | def __repr__(self): |
| 353 | return '<Job %s>' % (self.name) |
| 354 | |
James E. Blair | b095465 | 2012-06-01 11:32:01 -0700 | [diff] [blame] | 355 | def copy(self, other): |
| 356 | self.failure_message = other.failure_message |
James E. Blair | 1543741 | 2012-07-18 08:45:55 -0700 | [diff] [blame] | 357 | self.success_message = other.success_message |
James E. Blair | 222d498 | 2012-07-16 09:31:19 -0700 | [diff] [blame] | 358 | self.parameter_function = other.parameter_function |
| 359 | self.hold_following_changes = other.hold_following_changes |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 360 | self.branches = other.branches[:] |
| 361 | self._branches = other._branches[:] |
James E. Blair | b095465 | 2012-06-01 11:32:01 -0700 | [diff] [blame] | 362 | |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 363 | def changeMatches(self, change): |
| 364 | if not self.branches: |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 365 | return True |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 366 | for branch in self.branches: |
| 367 | if branch.match(change.branch): |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 368 | return True |
| 369 | return False |
| 370 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 371 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 372 | class JobTree(object): |
| 373 | """ A JobTree represents an instance of one Job, and holds JobTrees |
| 374 | whose jobs should be run if that Job succeeds. A root node of a |
| 375 | JobTree will have no associated Job. """ |
| 376 | |
| 377 | def __init__(self, job): |
| 378 | self.job = job |
| 379 | self.job_trees = [] |
| 380 | |
| 381 | def addJob(self, job): |
| 382 | if job not in [x.job for x in self.job_trees]: |
| 383 | t = JobTree(job) |
| 384 | self.job_trees.append(t) |
| 385 | return t |
| 386 | |
| 387 | def getJobs(self): |
| 388 | jobs = [] |
| 389 | for x in self.job_trees: |
| 390 | jobs.append(x.job) |
| 391 | jobs.extend(x.getJobs()) |
| 392 | return jobs |
| 393 | |
| 394 | def getJobTreeForJob(self, job): |
| 395 | if self.job == job: |
| 396 | return self |
| 397 | for tree in self.job_trees: |
| 398 | ret = tree.getJobTreeForJob(job) |
| 399 | if ret: |
| 400 | return ret |
| 401 | return None |
| 402 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 403 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 404 | class Build(object): |
| 405 | def __init__(self, job, uuid): |
| 406 | self.job = job |
| 407 | self.uuid = uuid |
| 408 | self.base_url = None |
| 409 | self.url = None |
| 410 | self.number = None |
| 411 | self.result = None |
| 412 | self.build_set = None |
| 413 | self.launch_time = time.time() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 414 | |
| 415 | def __repr__(self): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 416 | return '<Build %s of %s>' % (self.uuid, self.job.name) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 417 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 418 | |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 419 | class BuildSet(object): |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 420 | def __init__(self, change): |
| 421 | self.change = change |
| 422 | self.other_changes = [] |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 423 | self.builds = {} |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 424 | self.result = None |
| 425 | self.next_build_set = None |
| 426 | self.previous_build_set = None |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 427 | |
| 428 | def addBuild(self, build): |
| 429 | self.builds[build.job.name] = build |
| 430 | build.build_set = self |
| 431 | |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 432 | # The change isn't enqueued until after it's created |
| 433 | # so we don't know what the other changes ahead will be |
| 434 | # until jobs start. |
| 435 | if not self.other_changes: |
| 436 | next_change = self.change.change_ahead |
| 437 | while next_change: |
| 438 | self.other_changes.append(next_change) |
| 439 | next_change = next_change.change_ahead |
| 440 | |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 441 | def getBuild(self, job_name): |
| 442 | return self.builds.get(job_name) |
| 443 | |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 444 | def getBuilds(self): |
| 445 | keys = self.builds.keys() |
| 446 | keys.sort() |
| 447 | return [self.builds.get(x) for x in keys] |
| 448 | |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 449 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 450 | class Changeish(object): |
| 451 | """Something like a change; either a change or a ref""" |
| 452 | is_reportable = False |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 453 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 454 | def __init__(self, project): |
| 455 | self.project = project |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 456 | self.build_sets = [] |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 457 | self.change_ahead = None |
| 458 | self.change_behind = None |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 459 | self.current_build_set = BuildSet(self) |
| 460 | self.build_sets.append(self.current_build_set) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 461 | |
James E. Blair | 0dc8ba9 | 2012-07-16 14:23:52 -0700 | [diff] [blame] | 462 | def equals(self, other): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 463 | raise NotImplementedError() |
James E. Blair | 0dc8ba9 | 2012-07-16 14:23:52 -0700 | [diff] [blame] | 464 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 465 | def filterJobs(self, jobs): |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 466 | return filter(lambda job: job.changeMatches(self), jobs) |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 467 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 468 | def resetAllBuilds(self): |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 469 | old = self.current_build_set |
| 470 | self.current_build_set.result = 'CANCELED' |
| 471 | self.current_build_set = BuildSet(self) |
| 472 | old.next_build_set = self.current_build_set |
| 473 | self.current_build_set.previous_build_set = old |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 474 | self.build_sets.append(self.current_build_set) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 475 | |
| 476 | def addBuild(self, build): |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 477 | self.current_build_set.addBuild(build) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 478 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 479 | def delete(self): |
| 480 | if self.change_behind: |
| 481 | self.change_behind.change_ahead = None |
Clark Boylan | ba2efee | 2012-07-17 10:46:13 -0700 | [diff] [blame] | 482 | self.change_behind = None |
| 483 | self.queue.dequeueChange(self) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 484 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 485 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 486 | class Change(Changeish): |
| 487 | is_reportable = True |
| 488 | |
| 489 | def __init__(self, project): |
| 490 | super(Change, self).__init__(project) |
| 491 | self.branch = None |
| 492 | self.number = None |
| 493 | self.url = None |
| 494 | self.patchset = None |
| 495 | self.refspec = None |
| 496 | |
| 497 | self.reported = False |
| 498 | self.needs_change = None |
| 499 | self.needed_by_changes = [] |
| 500 | self.is_current_patchset = True |
| 501 | self.can_merge = False |
| 502 | self.is_merged = False |
| 503 | |
| 504 | def _id(self): |
| 505 | if self.number: |
| 506 | return '%s,%s' % (self.number, self.patchset) |
| 507 | return self.newrev |
| 508 | |
| 509 | def __repr__(self): |
| 510 | return '<Change 0x%x %s>' % (id(self), self._id()) |
| 511 | |
| 512 | def equals(self, other): |
| 513 | if (self.number == other.number and |
| 514 | self.patchset == other.patchset): |
| 515 | return True |
| 516 | return False |
| 517 | |
| 518 | def setReportedResult(self, result): |
| 519 | self.current_build_set.result = result |
| 520 | |
| 521 | |
| 522 | class Ref(Changeish): |
| 523 | is_reportable = False |
| 524 | |
| 525 | def __init__(self, project): |
| 526 | super(Change, self).__init__(project) |
| 527 | self.ref = None |
| 528 | self.oldrev = None |
| 529 | self.newrev = None |
| 530 | |
| 531 | def equals(self, other): |
| 532 | if (self.ref == other.ref and |
| 533 | self.newrev == other.newrev): |
| 534 | return True |
| 535 | return False |
| 536 | |
| 537 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 538 | class TriggerEvent(object): |
| 539 | def __init__(self): |
| 540 | self.data = None |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 541 | # common |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 542 | self.type = None |
| 543 | self.project_name = None |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 544 | # patchset-created, comment-added, etc. |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 545 | self.change_number = None |
Clark Boylan | fc56df3 | 2012-06-28 15:25:57 -0700 | [diff] [blame] | 546 | self.change_url = None |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 547 | self.patch_number = None |
James E. Blair | a03262c | 2012-05-30 09:41:16 -0700 | [diff] [blame] | 548 | self.refspec = None |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 549 | self.approvals = [] |
| 550 | self.branch = None |
Clark Boylan | b9bcb40 | 2012-06-29 17:44:05 -0700 | [diff] [blame] | 551 | self.comment = None |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 552 | # ref-updated |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 553 | self.ref = None |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 554 | self.oldrev = None |
James E. Blair | 89cae0f | 2012-07-18 11:18:32 -0700 | [diff] [blame] | 555 | self.newrev = None |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 556 | |
James E. Blair | 9f9667e | 2012-06-12 17:51:08 -0700 | [diff] [blame] | 557 | def __repr__(self): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 558 | ret = '<TriggerEvent %s %s' % (self.type, self.project_name) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 559 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 560 | if self.branch: |
| 561 | ret += " %s" % self.branch |
| 562 | if self.change_number: |
| 563 | ret += " %s,%s" % (self.change_number, self.patch_number) |
| 564 | if self.approvals: |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 565 | ret += ' ' + ', '.join( |
| 566 | ['%s:%s' % (a['type'], a['value']) for a in self.approvals]) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 567 | ret += '>' |
| 568 | |
| 569 | return ret |
| 570 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 571 | def getChange(self, project, trigger): |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 572 | # TODO: make the scheduler deal with events (which may have |
| 573 | # changes) rather than changes so that we don't have to create |
| 574 | # "fake" changes for events that aren't associated with changes. |
| 575 | |
| 576 | if self.change_number: |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 577 | change = trigger.getChange(self.change_number, self.patch_number) |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 578 | if self.ref: |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 579 | change = Ref(project) |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 580 | change.ref = self.ref |
| 581 | change.oldrev = self.oldrev |
| 582 | change.newrev = self.newrev |
| 583 | |
| 584 | return change |
| 585 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 586 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 587 | class EventFilter(object): |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 588 | def __init__(self, types=[], branches=[], refs=[], approvals={}, |
Clark Boylan | b9bcb40 | 2012-06-29 17:44:05 -0700 | [diff] [blame] | 589 | comment_filters=[]): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 590 | self._types = types |
| 591 | self._branches = branches |
| 592 | self._refs = refs |
| 593 | self.types = [re.compile(x) for x in types] |
| 594 | self.branches = [re.compile(x) for x in branches] |
| 595 | self.refs = [re.compile(x) for x in refs] |
Clark Boylan | b9bcb40 | 2012-06-29 17:44:05 -0700 | [diff] [blame] | 596 | self.comment_filters = [re.compile(x) for x in comment_filters] |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 597 | self.approvals = approvals |
| 598 | |
James E. Blair | 9f9667e | 2012-06-12 17:51:08 -0700 | [diff] [blame] | 599 | def __repr__(self): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 600 | ret = '<EventFilter' |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 601 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 602 | if self._types: |
| 603 | ret += ' types: %s' % ', '.join(self._types) |
| 604 | if self._branches: |
| 605 | ret += ' branches: %s' % ', '.join(self._branches) |
| 606 | if self._refs: |
| 607 | ret += ' refs: %s' % ', '.join(self._refs) |
| 608 | if self.approvals: |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 609 | ret += ' approvals: %s' % ', '.join( |
| 610 | ['%s:%s' % a for a in self.approvals.items()]) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 611 | ret += '>' |
| 612 | |
| 613 | return ret |
| 614 | |
| 615 | def matches(self, event): |
| 616 | def normalizeCategory(name): |
| 617 | name = name.lower() |
| 618 | return re.sub(' ', '-', name) |
| 619 | |
| 620 | # event types are ORed |
| 621 | matches_type = False |
| 622 | for etype in self.types: |
| 623 | if etype.match(event.type): |
| 624 | matches_type = True |
| 625 | if self.types and not matches_type: |
| 626 | return False |
| 627 | |
| 628 | # branches are ORed |
| 629 | matches_branch = False |
| 630 | for branch in self.branches: |
| 631 | if branch.match(event.branch): |
| 632 | matches_branch = True |
| 633 | if self.branches and not matches_branch: |
| 634 | return False |
| 635 | |
| 636 | # refs are ORed |
| 637 | matches_ref = False |
| 638 | for ref in self.refs: |
| 639 | if ref.match(event.ref): |
| 640 | matches_ref = True |
| 641 | if self.refs and not matches_ref: |
| 642 | return False |
| 643 | |
Clark Boylan | b9bcb40 | 2012-06-29 17:44:05 -0700 | [diff] [blame] | 644 | # comment_filters are ORed |
| 645 | matches_comment_filter = False |
| 646 | for comment_filter in self.comment_filters: |
| 647 | if (event.comment is not None and |
| 648 | comment_filter.search(event.comment)): |
| 649 | matches_comment_filter = True |
| 650 | if self.comment_filters and not matches_comment_filter: |
| 651 | return False |
| 652 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 653 | # approvals are ANDed |
| 654 | for category, value in self.approvals.items(): |
| 655 | matches_approval = False |
| 656 | for eapproval in event.approvals: |
| 657 | if (normalizeCategory(eapproval['description']) == category and |
| 658 | int(eapproval['value']) == int(value)): |
| 659 | matches_approval = True |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 660 | if not matches_approval: |
| 661 | return False |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 662 | return True |