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 | |
James E. Blair | 1b26531 | 2014-06-24 09:35:21 -0700 | [diff] [blame] | 15 | import copy |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 16 | import re |
James E. Blair | ff986a1 | 2012-05-30 14:56:51 -0700 | [diff] [blame] | 17 | import time |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 18 | from uuid import uuid4 |
James E. Blair | 5a9918a | 2013-08-27 10:06:27 -0700 | [diff] [blame] | 19 | import extras |
| 20 | |
| 21 | OrderedDict = extras.try_imports(['collections.OrderedDict', |
| 22 | 'ordereddict.OrderedDict']) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 23 | |
| 24 | |
James E. Blair | 19deff2 | 2013-08-25 13:17:35 -0700 | [diff] [blame] | 25 | MERGER_MERGE = 1 # "git merge" |
| 26 | MERGER_MERGE_RESOLVE = 2 # "git merge -s resolve" |
| 27 | MERGER_CHERRY_PICK = 3 # "git cherry-pick" |
| 28 | |
| 29 | MERGER_MAP = { |
| 30 | 'merge': MERGER_MERGE, |
| 31 | 'merge-resolve': MERGER_MERGE_RESOLVE, |
| 32 | 'cherry-pick': MERGER_CHERRY_PICK, |
| 33 | } |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 34 | |
James E. Blair | 64ed6f2 | 2013-07-10 14:07:23 -0700 | [diff] [blame] | 35 | PRECEDENCE_NORMAL = 0 |
| 36 | PRECEDENCE_LOW = 1 |
| 37 | PRECEDENCE_HIGH = 2 |
| 38 | |
| 39 | PRECEDENCE_MAP = { |
| 40 | None: PRECEDENCE_NORMAL, |
| 41 | 'low': PRECEDENCE_LOW, |
| 42 | 'normal': PRECEDENCE_NORMAL, |
| 43 | 'high': PRECEDENCE_HIGH, |
| 44 | } |
| 45 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 46 | |
James E. Blair | c053d02 | 2014-01-22 14:57:33 -0800 | [diff] [blame] | 47 | def time_to_seconds(s): |
| 48 | if s.endswith('s'): |
| 49 | return int(s[:-1]) |
| 50 | if s.endswith('m'): |
| 51 | return int(s[:-1]) * 60 |
| 52 | if s.endswith('h'): |
| 53 | return int(s[:-1]) * 60 * 60 |
| 54 | if s.endswith('d'): |
| 55 | return int(s[:-1]) * 24 * 60 * 60 |
| 56 | if s.endswith('w'): |
| 57 | return int(s[:-1]) * 7 * 24 * 60 * 60 |
| 58 | raise Exception("Unable to parse time value: %s" % s) |
| 59 | |
| 60 | |
James E. Blair | 11041d2 | 2014-05-02 14:49:53 -0700 | [diff] [blame] | 61 | def normalizeCategory(name): |
| 62 | name = name.lower() |
| 63 | return re.sub(' ', '-', name) |
| 64 | |
| 65 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 66 | class Pipeline(object): |
| 67 | """A top-level pipeline such as check, gate, post, etc.""" |
| 68 | def __init__(self, name): |
| 69 | self.name = name |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 70 | self.description = None |
James E. Blair | 5637019 | 2013-01-14 15:47:28 -0800 | [diff] [blame] | 71 | self.failure_message = None |
Joshua Hesketh | b717977 | 2014-01-30 23:30:46 +1100 | [diff] [blame] | 72 | self.merge_failure_message = None |
James E. Blair | 5637019 | 2013-01-14 15:47:28 -0800 | [diff] [blame] | 73 | self.success_message = None |
Joshua Hesketh | 3979e3e | 2014-03-04 11:21:10 +1100 | [diff] [blame] | 74 | self.footer_message = None |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 75 | self.dequeue_on_new_patchset = True |
James E. Blair | 17dd677 | 2015-02-09 14:45:18 -0800 | [diff] [blame] | 76 | self.ignore_dependencies = False |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 77 | self.job_trees = {} # project -> JobTree |
| 78 | self.manager = None |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 79 | self.queues = [] |
James E. Blair | 64ed6f2 | 2013-07-10 14:07:23 -0700 | [diff] [blame] | 80 | self.precedence = PRECEDENCE_NORMAL |
James E. Blair | c0dedf8 | 2014-08-06 09:37:52 -0700 | [diff] [blame] | 81 | self.source = None |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 82 | self.start_actions = None |
| 83 | self.success_actions = None |
| 84 | self.failure_actions = None |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 85 | self.window = None |
| 86 | self.window_floor = None |
| 87 | self.window_increase_type = None |
| 88 | self.window_increase_factor = None |
| 89 | self.window_decrease_type = None |
| 90 | self.window_decrease_factor = None |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 91 | |
James E. Blair | d09c17a | 2012-08-07 09:23:14 -0700 | [diff] [blame] | 92 | def __repr__(self): |
| 93 | return '<Pipeline %s>' % self.name |
| 94 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 95 | def setManager(self, manager): |
| 96 | self.manager = manager |
| 97 | |
| 98 | def addProject(self, project): |
| 99 | job_tree = JobTree(None) # Null job == job tree root |
| 100 | self.job_trees[project] = job_tree |
| 101 | return job_tree |
| 102 | |
| 103 | def getProjects(self): |
James E. Blair | c3d428e | 2013-12-03 15:06:48 -0800 | [diff] [blame] | 104 | return sorted(self.job_trees.keys(), lambda a, b: cmp(a.name, b.name)) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 105 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 106 | def addQueue(self, queue): |
| 107 | self.queues.append(queue) |
| 108 | |
| 109 | def getQueue(self, project): |
| 110 | for queue in self.queues: |
| 111 | if project in queue.projects: |
| 112 | return queue |
| 113 | return None |
| 114 | |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 115 | def removeQueue(self, queue): |
| 116 | self.queues.remove(queue) |
| 117 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 118 | def getJobTree(self, project): |
| 119 | tree = self.job_trees.get(project) |
| 120 | return tree |
| 121 | |
James E. Blair | 107c385 | 2015-02-07 08:23:10 -0800 | [diff] [blame] | 122 | def getJobs(self, item): |
| 123 | if not item.live: |
| 124 | return [] |
| 125 | tree = self.getJobTree(item.change.project) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 126 | if not tree: |
| 127 | return [] |
James E. Blair | 107c385 | 2015-02-07 08:23:10 -0800 | [diff] [blame] | 128 | return item.change.filterJobs(tree.getJobs()) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 129 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 130 | def _findJobsToRun(self, job_trees, item): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 131 | torun = [] |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 132 | if item.item_ahead: |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 133 | # Only run jobs if any 'hold' jobs on the change ahead |
| 134 | # have completed successfully. |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 135 | if self.isHoldingFollowingChanges(item.item_ahead): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 136 | return [] |
| 137 | for tree in job_trees: |
| 138 | job = tree.job |
| 139 | result = None |
| 140 | if job: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 141 | if not job.changeMatches(item.change): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 142 | continue |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 143 | build = item.current_build_set.getBuild(job.name) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 144 | if build: |
| 145 | result = build.result |
| 146 | else: |
| 147 | # There is no build for the root of this job tree, |
| 148 | # so we should run it. |
| 149 | torun.append(job) |
| 150 | # If there is no job, this is a null job tree, and we should |
| 151 | # run all of its jobs. |
| 152 | if result == 'SUCCESS' or not job: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 153 | torun.extend(self._findJobsToRun(tree.job_trees, item)) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 154 | return torun |
| 155 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 156 | def findJobsToRun(self, item): |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 157 | if not item.live: |
| 158 | return [] |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 159 | tree = self.getJobTree(item.change.project) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 160 | if not tree: |
| 161 | return [] |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 162 | return self._findJobsToRun(tree.job_trees, item) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 163 | |
James E. Blair | bea9ef1 | 2013-07-15 11:52:23 -0700 | [diff] [blame] | 164 | def haveAllJobsStarted(self, item): |
James E. Blair | 107c385 | 2015-02-07 08:23:10 -0800 | [diff] [blame] | 165 | for job in self.getJobs(item): |
James E. Blair | bea9ef1 | 2013-07-15 11:52:23 -0700 | [diff] [blame] | 166 | build = item.current_build_set.getBuild(job.name) |
| 167 | if not build or not build.start_time: |
| 168 | return False |
| 169 | return True |
| 170 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 171 | def areAllJobsComplete(self, item): |
James E. Blair | 107c385 | 2015-02-07 08:23:10 -0800 | [diff] [blame] | 172 | for job in self.getJobs(item): |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 173 | build = item.current_build_set.getBuild(job.name) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 174 | if not build or not build.result: |
| 175 | return False |
| 176 | return True |
| 177 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 178 | def didAllJobsSucceed(self, item): |
James E. Blair | 107c385 | 2015-02-07 08:23:10 -0800 | [diff] [blame] | 179 | for job in self.getJobs(item): |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 180 | if not job.voting: |
| 181 | continue |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 182 | build = item.current_build_set.getBuild(job.name) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 183 | if not build: |
| 184 | return False |
| 185 | if build.result != 'SUCCESS': |
| 186 | return False |
| 187 | return True |
| 188 | |
Joshua Hesketh | b717977 | 2014-01-30 23:30:46 +1100 | [diff] [blame] | 189 | def didMergerSucceed(self, item): |
| 190 | if item.current_build_set.unable_to_merge: |
| 191 | return False |
| 192 | return True |
| 193 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 194 | def didAnyJobFail(self, item): |
James E. Blair | 107c385 | 2015-02-07 08:23:10 -0800 | [diff] [blame] | 195 | for job in self.getJobs(item): |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 196 | if not job.voting: |
| 197 | continue |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 198 | build = item.current_build_set.getBuild(job.name) |
James E. Blair | 0018a6c | 2013-02-27 14:11:45 -0800 | [diff] [blame] | 199 | if build and build.result and (build.result != 'SUCCESS'): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 200 | return True |
| 201 | return False |
| 202 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 203 | def isHoldingFollowingChanges(self, item): |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 204 | if not item.live: |
| 205 | return False |
James E. Blair | 107c385 | 2015-02-07 08:23:10 -0800 | [diff] [blame] | 206 | for job in self.getJobs(item): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 207 | if not job.hold_following_changes: |
| 208 | continue |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 209 | build = item.current_build_set.getBuild(job.name) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 210 | if not build: |
| 211 | return True |
| 212 | if build.result != 'SUCCESS': |
| 213 | return True |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 214 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 215 | if not item.item_ahead: |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 216 | return False |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 217 | return self.isHoldingFollowingChanges(item.item_ahead) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 218 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 219 | def setResult(self, item, build): |
James E. Blair | 4a28a88 | 2013-08-23 15:17:33 -0700 | [diff] [blame] | 220 | if build.retry: |
| 221 | item.removeBuild(build) |
| 222 | elif build.result != 'SUCCESS': |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 223 | # Get a JobTree from a Job so we can find only its dependent jobs |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 224 | root = self.getJobTree(item.change.project) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 225 | tree = root.getJobTreeForJob(build.job) |
| 226 | for job in tree.getJobs(): |
| 227 | fakebuild = Build(job, None) |
| 228 | fakebuild.result = 'SKIPPED' |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 229 | item.addBuild(fakebuild) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 230 | |
Joshua Hesketh | b717977 | 2014-01-30 23:30:46 +1100 | [diff] [blame] | 231 | def setUnableToMerge(self, item): |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 232 | item.current_build_set.unable_to_merge = True |
| 233 | root = self.getJobTree(item.change.project) |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 234 | for job in root.getJobs(): |
| 235 | fakebuild = Build(job, None) |
| 236 | fakebuild.result = 'SKIPPED' |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 237 | item.addBuild(fakebuild) |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 238 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 239 | def setDequeuedNeedingChange(self, item): |
| 240 | item.dequeued_needing_change = True |
| 241 | root = self.getJobTree(item.change.project) |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 242 | for job in root.getJobs(): |
| 243 | fakebuild = Build(job, None) |
| 244 | fakebuild.result = 'SKIPPED' |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 245 | item.addBuild(fakebuild) |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 246 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 247 | def getChangesInQueue(self): |
| 248 | changes = [] |
| 249 | for shared_queue in self.queues: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 250 | changes.extend([x.change for x in shared_queue.queue]) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 251 | return changes |
| 252 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 253 | def getAllItems(self): |
| 254 | items = [] |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 255 | for shared_queue in self.queues: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 256 | items.extend(shared_queue.queue) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 257 | return items |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 258 | |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 259 | def formatStatusJSON(self): |
| 260 | j_pipeline = dict(name=self.name, |
| 261 | description=self.description) |
| 262 | j_queues = [] |
| 263 | j_pipeline['change_queues'] = j_queues |
| 264 | for queue in self.queues: |
| 265 | j_queue = dict(name=queue.name) |
| 266 | j_queues.append(j_queue) |
| 267 | j_queue['heads'] = [] |
Clark Boylan | af2476f | 2014-01-23 14:47:36 -0800 | [diff] [blame] | 268 | j_queue['window'] = queue.window |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 269 | |
| 270 | j_changes = [] |
| 271 | for e in queue.queue: |
| 272 | if not e.item_ahead: |
| 273 | if j_changes: |
| 274 | j_queue['heads'].append(j_changes) |
| 275 | j_changes = [] |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 276 | j_changes.append(e.formatJSON()) |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 277 | if (len(j_changes) > 1 and |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 278 | (j_changes[-2]['remaining_time'] is not None) and |
| 279 | (j_changes[-1]['remaining_time'] is not None)): |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 280 | j_changes[-1]['remaining_time'] = max( |
| 281 | j_changes[-2]['remaining_time'], |
| 282 | j_changes[-1]['remaining_time']) |
| 283 | if j_changes: |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 284 | j_queue['heads'].append(j_changes) |
| 285 | return j_pipeline |
| 286 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 287 | |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 288 | class ActionReporter(object): |
Alex Gaynor | 813d39b | 2014-05-17 16:17:16 -0700 | [diff] [blame] | 289 | """An ActionReporter has a reporter and its configured parameters""" |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 290 | |
| 291 | def __repr__(self): |
| 292 | return '<ActionReporter %s, %s>' % (self.reporter, self.params) |
| 293 | |
| 294 | def __init__(self, reporter, params): |
| 295 | self.reporter = reporter |
| 296 | self.params = params |
| 297 | |
| 298 | def report(self, change, message): |
| 299 | """Sends the built message off to the configured reporter. |
| 300 | Takes the change and message and adds the configured parameters. |
| 301 | """ |
| 302 | return self.reporter.report(change, message, self.params) |
| 303 | |
| 304 | def getSubmitAllowNeeds(self): |
| 305 | """Gets the submit allow needs from the reporter based off the |
| 306 | parameters.""" |
| 307 | return self.reporter.getSubmitAllowNeeds(self.params) |
| 308 | |
| 309 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 310 | class ChangeQueue(object): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 311 | """DependentPipelines have multiple parallel queues shared by |
| 312 | different projects; this is one of them. For instance, there may |
| 313 | a queue shared by interrelated projects foo and bar, and a second |
| 314 | queue for independent project baz. Pipelines have one or more |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 315 | ChangeQueues.""" |
| 316 | def __init__(self, pipeline, window=0, window_floor=1, |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 317 | window_increase_type='linear', window_increase_factor=1, |
| 318 | window_decrease_type='exponential', window_decrease_factor=2): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 319 | self.pipeline = pipeline |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 320 | self.name = '' |
James E. Blair | c8a1e05 | 2014-02-25 09:29:26 -0800 | [diff] [blame] | 321 | self.assigned_name = None |
| 322 | self.generated_name = None |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 323 | self.projects = [] |
| 324 | self._jobs = set() |
| 325 | self.queue = [] |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 326 | self.window = window |
| 327 | self.window_floor = window_floor |
| 328 | self.window_increase_type = window_increase_type |
| 329 | self.window_increase_factor = window_increase_factor |
| 330 | self.window_decrease_type = window_decrease_type |
| 331 | self.window_decrease_factor = window_decrease_factor |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 332 | |
James E. Blair | 9f9667e | 2012-06-12 17:51:08 -0700 | [diff] [blame] | 333 | def __repr__(self): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 334 | return '<ChangeQueue %s: %s>' % (self.pipeline.name, self.name) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 335 | |
| 336 | def getJobs(self): |
| 337 | return self._jobs |
| 338 | |
| 339 | def addProject(self, project): |
| 340 | if project not in self.projects: |
| 341 | self.projects.append(project) |
James E. Blair | c8a1e05 | 2014-02-25 09:29:26 -0800 | [diff] [blame] | 342 | self._jobs |= set(self.pipeline.getJobTree(project).getJobs()) |
| 343 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 344 | names = [x.name for x in self.projects] |
| 345 | names.sort() |
James E. Blair | c8a1e05 | 2014-02-25 09:29:26 -0800 | [diff] [blame] | 346 | self.generated_name = ', '.join(names) |
| 347 | |
| 348 | for job in self._jobs: |
| 349 | if job.queue_name: |
| 350 | if (self.assigned_name and |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 351 | job.queue_name != self.assigned_name): |
James E. Blair | c8a1e05 | 2014-02-25 09:29:26 -0800 | [diff] [blame] | 352 | raise Exception("More than one name assigned to " |
| 353 | "change queue: %s != %s" % |
| 354 | (self.assigned_name, job.queue_name)) |
| 355 | self.assigned_name = job.queue_name |
| 356 | self.name = self.assigned_name or self.generated_name |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 357 | |
| 358 | def enqueueChange(self, change): |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 359 | item = QueueItem(self, change) |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 360 | self.enqueueItem(item) |
| 361 | item.enqueue_time = time.time() |
| 362 | return item |
| 363 | |
| 364 | def enqueueItem(self, item): |
James E. Blair | 4a035d9 | 2014-01-23 13:10:48 -0800 | [diff] [blame] | 365 | item.pipeline = self.pipeline |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 366 | item.queue = self |
| 367 | if self.queue: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 368 | item.item_ahead = self.queue[-1] |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 369 | item.item_ahead.items_behind.append(item) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 370 | self.queue.append(item) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 371 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 372 | def dequeueItem(self, item): |
| 373 | if item in self.queue: |
| 374 | self.queue.remove(item) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 375 | if item.item_ahead: |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 376 | item.item_ahead.items_behind.remove(item) |
| 377 | for item_behind in item.items_behind: |
| 378 | if item.item_ahead: |
| 379 | item.item_ahead.items_behind.append(item_behind) |
| 380 | item_behind.item_ahead = item.item_ahead |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 381 | item.item_ahead = None |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 382 | item.items_behind = [] |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 383 | item.dequeue_time = time.time() |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 384 | |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 385 | def moveItem(self, item, item_ahead): |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 386 | if item.item_ahead == item_ahead: |
| 387 | return False |
| 388 | # Remove from current location |
| 389 | if item.item_ahead: |
| 390 | item.item_ahead.items_behind.remove(item) |
| 391 | for item_behind in item.items_behind: |
| 392 | if item.item_ahead: |
| 393 | item.item_ahead.items_behind.append(item_behind) |
| 394 | item_behind.item_ahead = item.item_ahead |
| 395 | # Add to new location |
| 396 | item.item_ahead = item_ahead |
James E. Blair | 0045126 | 2013-09-20 11:40:17 -0700 | [diff] [blame] | 397 | item.items_behind = [] |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 398 | if item.item_ahead: |
| 399 | item.item_ahead.items_behind.append(item) |
| 400 | return True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 401 | |
| 402 | def mergeChangeQueue(self, other): |
| 403 | for project in other.projects: |
| 404 | self.addProject(project) |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 405 | self.window = min(self.window, other.window) |
| 406 | # TODO merge semantics |
| 407 | |
Clark Boylan | 3d2f7a7 | 2014-01-23 11:07:42 -0800 | [diff] [blame] | 408 | def isActionable(self, item): |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 409 | if self.window: |
Clark Boylan | 3d2f7a7 | 2014-01-23 11:07:42 -0800 | [diff] [blame] | 410 | return item in self.queue[:self.window] |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 411 | else: |
Clark Boylan | 3d2f7a7 | 2014-01-23 11:07:42 -0800 | [diff] [blame] | 412 | return True |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 413 | |
| 414 | def increaseWindowSize(self): |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 415 | if self.window: |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 416 | if self.window_increase_type == 'linear': |
| 417 | self.window += self.window_increase_factor |
| 418 | elif self.window_increase_type == 'exponential': |
| 419 | self.window *= self.window_increase_factor |
| 420 | |
| 421 | def decreaseWindowSize(self): |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 422 | if self.window: |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 423 | if self.window_decrease_type == 'linear': |
| 424 | self.window = max( |
| 425 | self.window_floor, |
| 426 | self.window - self.window_decrease_factor) |
| 427 | elif self.window_decrease_type == 'exponential': |
| 428 | self.window = max( |
| 429 | self.window_floor, |
| 430 | self.window / self.window_decrease_factor) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 431 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 432 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 433 | class Project(object): |
| 434 | def __init__(self, name): |
| 435 | self.name = name |
James E. Blair | 19deff2 | 2013-08-25 13:17:35 -0700 | [diff] [blame] | 436 | self.merge_mode = MERGER_MERGE_RESOLVE |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 437 | |
| 438 | def __str__(self): |
| 439 | return self.name |
| 440 | |
| 441 | def __repr__(self): |
| 442 | return '<Project %s>' % (self.name) |
| 443 | |
| 444 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 445 | class Job(object): |
| 446 | def __init__(self, name): |
James E. Blair | 222d498 | 2012-07-16 09:31:19 -0700 | [diff] [blame] | 447 | # 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] | 448 | self.name = name |
James E. Blair | c8a1e05 | 2014-02-25 09:29:26 -0800 | [diff] [blame] | 449 | self.queue_name = None |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 450 | self.failure_message = None |
| 451 | self.success_message = None |
James E. Blair | 6aea36d | 2012-12-17 13:03:24 -0800 | [diff] [blame] | 452 | self.failure_pattern = None |
| 453 | self.success_pattern = None |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 454 | self.parameter_function = None |
Maru Newby | 79427a4 | 2015-02-17 17:54:45 +0000 | [diff] [blame] | 455 | # A metajob should only supply values for attributes that have |
| 456 | # been explicitly provided, so avoid setting boolean defaults. |
| 457 | if self.is_metajob: |
| 458 | self.hold_following_changes = None |
| 459 | self.voting = None |
| 460 | else: |
| 461 | self.hold_following_changes = False |
| 462 | self.voting = True |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 463 | self.branches = [] |
| 464 | self._branches = [] |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 465 | self.files = [] |
| 466 | self._files = [] |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 467 | self.skip_if_matcher = None |
Joshua Hesketh | 36c3fa5 | 2014-01-22 11:40:52 +1100 | [diff] [blame] | 468 | self.swift = {} |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 469 | |
| 470 | def __str__(self): |
| 471 | return self.name |
| 472 | |
| 473 | def __repr__(self): |
| 474 | return '<Job %s>' % (self.name) |
| 475 | |
Maru Newby | 79427a4 | 2015-02-17 17:54:45 +0000 | [diff] [blame] | 476 | @property |
| 477 | def is_metajob(self): |
| 478 | return self.name.startswith('^') |
| 479 | |
James E. Blair | b095465 | 2012-06-01 11:32:01 -0700 | [diff] [blame] | 480 | def copy(self, other): |
James E. Blair | c28d1b0 | 2013-07-19 11:37:06 -0700 | [diff] [blame] | 481 | if other.failure_message: |
| 482 | self.failure_message = other.failure_message |
| 483 | if other.success_message: |
| 484 | self.success_message = other.success_message |
| 485 | if other.failure_pattern: |
| 486 | self.failure_pattern = other.failure_pattern |
| 487 | if other.success_pattern: |
| 488 | self.success_pattern = other.success_pattern |
| 489 | if other.parameter_function: |
| 490 | self.parameter_function = other.parameter_function |
| 491 | if other.branches: |
| 492 | self.branches = other.branches[:] |
| 493 | self._branches = other._branches[:] |
| 494 | if other.files: |
| 495 | self.files = other.files[:] |
| 496 | self._files = other._files[:] |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 497 | if other.skip_if_matcher: |
| 498 | self.skip_if_matcher = other.skip_if_matcher.copy() |
Joshua Hesketh | 36c3fa5 | 2014-01-22 11:40:52 +1100 | [diff] [blame] | 499 | if other.swift: |
| 500 | self.swift.update(other.swift) |
Maru Newby | 79427a4 | 2015-02-17 17:54:45 +0000 | [diff] [blame] | 501 | # Only non-None values should be copied for boolean attributes. |
| 502 | if other.hold_following_changes is not None: |
| 503 | self.hold_following_changes = other.hold_following_changes |
| 504 | if other.voting is not None: |
| 505 | self.voting = other.voting |
James E. Blair | b095465 | 2012-06-01 11:32:01 -0700 | [diff] [blame] | 506 | |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 507 | def changeMatches(self, change): |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 508 | matches_branch = False |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 509 | for branch in self.branches: |
James E. Blair | 45865f3 | 2012-10-05 09:39:46 -0700 | [diff] [blame] | 510 | if hasattr(change, 'branch') and branch.match(change.branch): |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 511 | matches_branch = True |
James E. Blair | 45865f3 | 2012-10-05 09:39:46 -0700 | [diff] [blame] | 512 | if hasattr(change, 'ref') and branch.match(change.ref): |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 513 | matches_branch = True |
| 514 | if self.branches and not matches_branch: |
| 515 | return False |
| 516 | |
| 517 | matches_file = False |
| 518 | for f in self.files: |
| 519 | if hasattr(change, 'files'): |
| 520 | for cf in change.files: |
| 521 | if f.match(cf): |
| 522 | matches_file = True |
| 523 | if self.files and not matches_file: |
| 524 | return False |
| 525 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 526 | if self.skip_if_matcher and self.skip_if_matcher.matches(change): |
| 527 | return False |
| 528 | |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 529 | return True |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 530 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 531 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 532 | class JobTree(object): |
| 533 | """ A JobTree represents an instance of one Job, and holds JobTrees |
| 534 | whose jobs should be run if that Job succeeds. A root node of a |
| 535 | JobTree will have no associated Job. """ |
| 536 | |
| 537 | def __init__(self, job): |
| 538 | self.job = job |
| 539 | self.job_trees = [] |
| 540 | |
| 541 | def addJob(self, job): |
James E. Blair | 12a92b1 | 2014-03-26 11:54:53 -0700 | [diff] [blame] | 542 | if job not in [x.job for x in self.job_trees]: |
| 543 | t = JobTree(job) |
| 544 | self.job_trees.append(t) |
| 545 | return t |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 546 | |
| 547 | def getJobs(self): |
| 548 | jobs = [] |
| 549 | for x in self.job_trees: |
| 550 | jobs.append(x.job) |
| 551 | jobs.extend(x.getJobs()) |
| 552 | return jobs |
| 553 | |
| 554 | def getJobTreeForJob(self, job): |
| 555 | if self.job == job: |
| 556 | return self |
| 557 | for tree in self.job_trees: |
| 558 | ret = tree.getJobTreeForJob(job) |
| 559 | if ret: |
| 560 | return ret |
| 561 | return None |
| 562 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 563 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 564 | class Build(object): |
| 565 | def __init__(self, job, uuid): |
| 566 | self.job = job |
| 567 | self.uuid = uuid |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 568 | self.url = None |
| 569 | self.number = None |
| 570 | self.result = None |
| 571 | self.build_set = None |
| 572 | self.launch_time = time.time() |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 573 | self.start_time = None |
| 574 | self.end_time = None |
James E. Blair | bea9ef1 | 2013-07-15 11:52:23 -0700 | [diff] [blame] | 575 | self.estimated_time = None |
James E. Blair | 66eeebf | 2013-07-27 17:44:32 -0700 | [diff] [blame] | 576 | self.pipeline = None |
James E. Blair | 0aac487 | 2013-08-23 14:02:38 -0700 | [diff] [blame] | 577 | self.canceled = False |
James E. Blair | 4a28a88 | 2013-08-23 15:17:33 -0700 | [diff] [blame] | 578 | self.retry = False |
James E. Blair | d78576a | 2013-07-09 10:39:17 -0700 | [diff] [blame] | 579 | self.parameters = {} |
Joshua Hesketh | ba8776a | 2014-01-12 14:35:40 +0800 | [diff] [blame] | 580 | self.worker = Worker() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 581 | |
| 582 | def __repr__(self): |
Joshua Hesketh | ba8776a | 2014-01-12 14:35:40 +0800 | [diff] [blame] | 583 | return ('<Build %s of %s on %s>' % |
| 584 | (self.uuid, self.job.name, self.worker)) |
| 585 | |
| 586 | |
| 587 | class Worker(object): |
| 588 | """A model of the worker running a job""" |
| 589 | def __init__(self): |
| 590 | self.name = "Unknown" |
| 591 | self.hostname = None |
| 592 | self.ips = [] |
| 593 | self.fqdn = None |
| 594 | self.program = None |
| 595 | self.version = None |
| 596 | self.extra = {} |
| 597 | |
| 598 | def updateFromData(self, data): |
| 599 | """Update worker information if contained in the WORK_DATA response.""" |
| 600 | self.name = data.get('worker_name', self.name) |
| 601 | self.hostname = data.get('worker_hostname', self.hostname) |
| 602 | self.ips = data.get('worker_ips', self.ips) |
| 603 | self.fqdn = data.get('worker_fqdn', self.fqdn) |
| 604 | self.program = data.get('worker_program', self.program) |
| 605 | self.version = data.get('worker_version', self.version) |
| 606 | self.extra = data.get('worker_extra', self.extra) |
| 607 | |
| 608 | def __repr__(self): |
| 609 | return '<Worker %s>' % self.name |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 610 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 611 | |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 612 | class BuildSet(object): |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 613 | # Merge states: |
| 614 | NEW = 1 |
| 615 | PENDING = 2 |
| 616 | COMPLETE = 3 |
| 617 | |
Antoine Musso | 9b22928 | 2014-08-18 23:45:43 +0200 | [diff] [blame] | 618 | states_map = { |
| 619 | 1: 'NEW', |
| 620 | 2: 'PENDING', |
| 621 | 3: 'COMPLETE', |
| 622 | } |
| 623 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 624 | def __init__(self, item): |
| 625 | self.item = item |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 626 | self.other_changes = [] |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 627 | self.builds = {} |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 628 | self.result = None |
| 629 | self.next_build_set = None |
| 630 | self.previous_build_set = None |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 631 | self.ref = None |
James E. Blair | 81515ad | 2012-10-01 18:29:08 -0700 | [diff] [blame] | 632 | self.commit = None |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 633 | self.zuul_url = None |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 634 | self.unable_to_merge = False |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 635 | self.failing_reasons = [] |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 636 | self.merge_state = self.NEW |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 637 | |
Antoine Musso | 9b22928 | 2014-08-18 23:45:43 +0200 | [diff] [blame] | 638 | def __repr__(self): |
| 639 | return '<BuildSet item: %s #builds: %s merge state: %s>' % ( |
| 640 | self.item, |
| 641 | len(self.builds), |
| 642 | self.getStateName(self.merge_state)) |
| 643 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 644 | def setConfiguration(self): |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 645 | # The change isn't enqueued until after it's created |
| 646 | # so we don't know what the other changes ahead will be |
| 647 | # until jobs start. |
| 648 | if not self.other_changes: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 649 | next_item = self.item.item_ahead |
| 650 | while next_item: |
| 651 | self.other_changes.append(next_item.change) |
| 652 | next_item = next_item.item_ahead |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 653 | if not self.ref: |
| 654 | self.ref = 'Z' + uuid4().hex |
| 655 | |
Antoine Musso | 9b22928 | 2014-08-18 23:45:43 +0200 | [diff] [blame] | 656 | def getStateName(self, state_num): |
| 657 | return self.states_map.get( |
| 658 | state_num, 'UNKNOWN (%s)' % state_num) |
| 659 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 660 | def addBuild(self, build): |
| 661 | self.builds[build.job.name] = build |
| 662 | build.build_set = self |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 663 | |
James E. Blair | 4a28a88 | 2013-08-23 15:17:33 -0700 | [diff] [blame] | 664 | def removeBuild(self, build): |
| 665 | del self.builds[build.job.name] |
| 666 | |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 667 | def getBuild(self, job_name): |
| 668 | return self.builds.get(job_name) |
| 669 | |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 670 | def getBuilds(self): |
| 671 | keys = self.builds.keys() |
| 672 | keys.sort() |
| 673 | return [self.builds.get(x) for x in keys] |
| 674 | |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 675 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 676 | class QueueItem(object): |
| 677 | """A changish inside of a Pipeline queue""" |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 678 | |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 679 | def __init__(self, queue, change): |
| 680 | self.pipeline = queue.pipeline |
| 681 | self.queue = queue |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 682 | self.change = change # a changeish |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 683 | self.build_sets = [] |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 684 | self.dequeued_needing_change = False |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 685 | self.current_build_set = BuildSet(self) |
| 686 | self.build_sets.append(self.current_build_set) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 687 | self.item_ahead = None |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 688 | self.items_behind = [] |
James E. Blair | 8fa1697 | 2013-01-15 16:57:20 -0800 | [diff] [blame] | 689 | self.enqueue_time = None |
| 690 | self.dequeue_time = None |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 691 | self.reported = False |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 692 | self.active = False # Whether an item is within an active window |
| 693 | self.live = True # Whether an item is intended to be processed at all |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 694 | |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 695 | def __repr__(self): |
| 696 | if self.pipeline: |
| 697 | pipeline = self.pipeline.name |
| 698 | else: |
| 699 | pipeline = None |
| 700 | return '<QueueItem 0x%x for %s in %s>' % ( |
| 701 | id(self), self.change, pipeline) |
| 702 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 703 | def resetAllBuilds(self): |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 704 | old = self.current_build_set |
| 705 | self.current_build_set.result = 'CANCELED' |
| 706 | self.current_build_set = BuildSet(self) |
| 707 | old.next_build_set = self.current_build_set |
| 708 | self.current_build_set.previous_build_set = old |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 709 | self.build_sets.append(self.current_build_set) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 710 | |
| 711 | def addBuild(self, build): |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 712 | self.current_build_set.addBuild(build) |
James E. Blair | 66eeebf | 2013-07-27 17:44:32 -0700 | [diff] [blame] | 713 | build.pipeline = self.pipeline |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 714 | |
James E. Blair | 4a28a88 | 2013-08-23 15:17:33 -0700 | [diff] [blame] | 715 | def removeBuild(self, build): |
| 716 | self.current_build_set.removeBuild(build) |
| 717 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 718 | def setReportedResult(self, result): |
| 719 | self.current_build_set.result = result |
| 720 | |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 721 | def formatJSON(self): |
| 722 | changeish = self.change |
| 723 | ret = {} |
| 724 | ret['active'] = self.active |
James E. Blair | 107c385 | 2015-02-07 08:23:10 -0800 | [diff] [blame] | 725 | ret['live'] = self.live |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 726 | if hasattr(changeish, 'url') and changeish.url is not None: |
| 727 | ret['url'] = changeish.url |
| 728 | else: |
| 729 | ret['url'] = None |
| 730 | ret['id'] = changeish._id() |
| 731 | if self.item_ahead: |
| 732 | ret['item_ahead'] = self.item_ahead.change._id() |
| 733 | else: |
| 734 | ret['item_ahead'] = None |
| 735 | ret['items_behind'] = [i.change._id() for i in self.items_behind] |
| 736 | ret['failing_reasons'] = self.current_build_set.failing_reasons |
| 737 | ret['zuul_ref'] = self.current_build_set.ref |
| 738 | ret['project'] = changeish.project.name |
| 739 | ret['enqueue_time'] = int(self.enqueue_time * 1000) |
| 740 | ret['jobs'] = [] |
Davanum Srinivas | b6bfbcc | 2014-11-18 13:26:52 -0500 | [diff] [blame] | 741 | if hasattr(changeish, 'owner'): |
| 742 | ret['owner'] = changeish.owner |
| 743 | else: |
| 744 | ret['owner'] = None |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 745 | max_remaining = 0 |
James E. Blair | 107c385 | 2015-02-07 08:23:10 -0800 | [diff] [blame] | 746 | for job in self.pipeline.getJobs(self): |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 747 | now = time.time() |
| 748 | build = self.current_build_set.getBuild(job.name) |
| 749 | elapsed = None |
| 750 | remaining = None |
| 751 | result = None |
| 752 | url = None |
| 753 | worker = None |
| 754 | if build: |
| 755 | result = build.result |
| 756 | url = build.url |
| 757 | if build.start_time: |
| 758 | if build.end_time: |
| 759 | elapsed = int((build.end_time - |
| 760 | build.start_time) * 1000) |
| 761 | remaining = 0 |
| 762 | else: |
| 763 | elapsed = int((now - build.start_time) * 1000) |
| 764 | if build.estimated_time: |
| 765 | remaining = max( |
| 766 | int(build.estimated_time * 1000) - elapsed, |
| 767 | 0) |
| 768 | worker = { |
| 769 | 'name': build.worker.name, |
| 770 | 'hostname': build.worker.hostname, |
| 771 | 'ips': build.worker.ips, |
| 772 | 'fqdn': build.worker.fqdn, |
| 773 | 'program': build.worker.program, |
| 774 | 'version': build.worker.version, |
| 775 | 'extra': build.worker.extra |
| 776 | } |
| 777 | if remaining and remaining > max_remaining: |
| 778 | max_remaining = remaining |
| 779 | |
| 780 | ret['jobs'].append({ |
| 781 | 'name': job.name, |
| 782 | 'elapsed_time': elapsed, |
| 783 | 'remaining_time': remaining, |
| 784 | 'url': url, |
| 785 | 'result': result, |
| 786 | 'voting': job.voting, |
| 787 | 'uuid': build.uuid if build else None, |
| 788 | 'launch_time': build.launch_time if build else None, |
| 789 | 'start_time': build.start_time if build else None, |
| 790 | 'end_time': build.end_time if build else None, |
| 791 | 'estimated_time': build.estimated_time if build else None, |
| 792 | 'pipeline': build.pipeline.name if build else None, |
| 793 | 'canceled': build.canceled if build else None, |
| 794 | 'retry': build.retry if build else None, |
| 795 | 'number': build.number if build else None, |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 796 | 'worker': worker |
| 797 | }) |
| 798 | |
| 799 | if self.pipeline.haveAllJobsStarted(self): |
| 800 | ret['remaining_time'] = max_remaining |
| 801 | else: |
| 802 | ret['remaining_time'] = None |
| 803 | return ret |
| 804 | |
| 805 | def formatStatus(self, indent=0, html=False): |
| 806 | changeish = self.change |
| 807 | indent_str = ' ' * indent |
| 808 | ret = '' |
| 809 | if html and hasattr(changeish, 'url') and changeish.url is not None: |
| 810 | ret += '%sProject %s change <a href="%s">%s</a>\n' % ( |
| 811 | indent_str, |
| 812 | changeish.project.name, |
| 813 | changeish.url, |
| 814 | changeish._id()) |
| 815 | else: |
| 816 | ret += '%sProject %s change %s based on %s\n' % ( |
| 817 | indent_str, |
| 818 | changeish.project.name, |
| 819 | changeish._id(), |
| 820 | self.item_ahead) |
James E. Blair | 107c385 | 2015-02-07 08:23:10 -0800 | [diff] [blame] | 821 | for job in self.pipeline.getJobs(self): |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 822 | build = self.current_build_set.getBuild(job.name) |
| 823 | if build: |
| 824 | result = build.result |
| 825 | else: |
| 826 | result = None |
| 827 | job_name = job.name |
| 828 | if not job.voting: |
| 829 | voting = ' (non-voting)' |
| 830 | else: |
| 831 | voting = '' |
| 832 | if html: |
| 833 | if build: |
| 834 | url = build.url |
| 835 | else: |
| 836 | url = None |
| 837 | if url is not None: |
| 838 | job_name = '<a href="%s">%s</a>' % (url, job_name) |
| 839 | ret += '%s %s: %s%s' % (indent_str, job_name, result, voting) |
| 840 | ret += '\n' |
| 841 | return ret |
| 842 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 843 | |
| 844 | class Changeish(object): |
| 845 | """Something like a change; either a change or a ref""" |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 846 | |
| 847 | def __init__(self, project): |
| 848 | self.project = project |
| 849 | |
Joshua Hesketh | 36c3fa5 | 2014-01-22 11:40:52 +1100 | [diff] [blame] | 850 | def getBasePath(self): |
| 851 | base_path = '' |
| 852 | if hasattr(self, 'refspec'): |
| 853 | base_path = "%s/%s/%s" % ( |
| 854 | self.number[-2:], self.number, self.patchset) |
| 855 | elif hasattr(self, 'ref'): |
| 856 | base_path = "%s/%s" % (self.newrev[:2], self.newrev) |
| 857 | |
| 858 | return base_path |
| 859 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 860 | def equals(self, other): |
| 861 | raise NotImplementedError() |
| 862 | |
| 863 | def isUpdateOf(self, other): |
| 864 | raise NotImplementedError() |
| 865 | |
| 866 | def filterJobs(self, jobs): |
| 867 | return filter(lambda job: job.changeMatches(self), jobs) |
| 868 | |
| 869 | def getRelatedChanges(self): |
| 870 | return set() |
| 871 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 872 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 873 | class Change(Changeish): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 874 | def __init__(self, project): |
| 875 | super(Change, self).__init__(project) |
| 876 | self.branch = None |
| 877 | self.number = None |
| 878 | self.url = None |
| 879 | self.patchset = None |
| 880 | self.refspec = None |
| 881 | |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 882 | self.files = [] |
James E. Blair | 6965a4b | 2014-12-16 17:19:04 -0800 | [diff] [blame] | 883 | self.needs_changes = [] |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 884 | self.needed_by_changes = [] |
| 885 | self.is_current_patchset = True |
| 886 | self.can_merge = False |
| 887 | self.is_merged = False |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 888 | self.failed_to_merge = False |
James E. Blair | c053d02 | 2014-01-22 14:57:33 -0800 | [diff] [blame] | 889 | self.approvals = [] |
James E. Blair | 11041d2 | 2014-05-02 14:49:53 -0700 | [diff] [blame] | 890 | self.open = None |
| 891 | self.status = None |
Davanum Srinivas | b6bfbcc | 2014-11-18 13:26:52 -0500 | [diff] [blame] | 892 | self.owner = None |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 893 | |
| 894 | def _id(self): |
James E. Blair | be765db | 2012-08-07 08:36:20 -0700 | [diff] [blame] | 895 | return '%s,%s' % (self.number, self.patchset) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 896 | |
| 897 | def __repr__(self): |
| 898 | return '<Change 0x%x %s>' % (id(self), self._id()) |
| 899 | |
| 900 | def equals(self, other): |
Zhongyue Luo | aa85ebf | 2012-09-21 16:38:33 +0800 | [diff] [blame] | 901 | if self.number == other.number and self.patchset == other.patchset: |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 902 | return True |
| 903 | return False |
| 904 | |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 905 | def isUpdateOf(self, other): |
Clark Boylan | 0197624 | 2013-02-17 18:41:48 -0800 | [diff] [blame] | 906 | if ((hasattr(other, 'number') and self.number == other.number) and |
James E. Blair | 7a192e4 | 2013-07-11 14:10:36 -0700 | [diff] [blame] | 907 | (hasattr(other, 'patchset') and |
| 908 | self.patchset is not None and |
| 909 | other.patchset is not None and |
| 910 | int(self.patchset) > int(other.patchset))): |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 911 | return True |
| 912 | return False |
| 913 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 914 | def getRelatedChanges(self): |
| 915 | related = set() |
James E. Blair | 6965a4b | 2014-12-16 17:19:04 -0800 | [diff] [blame] | 916 | for c in self.needs_changes: |
| 917 | related.add(c) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 918 | for c in self.needed_by_changes: |
| 919 | related.add(c) |
| 920 | related.update(c.getRelatedChanges()) |
| 921 | return related |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 922 | |
| 923 | |
| 924 | class Ref(Changeish): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 925 | def __init__(self, project): |
James E. Blair | be765db | 2012-08-07 08:36:20 -0700 | [diff] [blame] | 926 | super(Ref, self).__init__(project) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 927 | self.ref = None |
| 928 | self.oldrev = None |
| 929 | self.newrev = None |
| 930 | |
James E. Blair | be765db | 2012-08-07 08:36:20 -0700 | [diff] [blame] | 931 | def _id(self): |
| 932 | return self.newrev |
| 933 | |
Antoine Musso | 68bdcd7 | 2013-01-17 12:31:28 +0100 | [diff] [blame] | 934 | def __repr__(self): |
| 935 | rep = None |
| 936 | if self.newrev == '0000000000000000000000000000000000000000': |
| 937 | rep = '<Ref 0x%x deletes %s from %s' % ( |
| 938 | id(self), self.ref, self.oldrev) |
| 939 | elif self.oldrev == '0000000000000000000000000000000000000000': |
| 940 | rep = '<Ref 0x%x creates %s on %s>' % ( |
| 941 | id(self), self.ref, self.newrev) |
| 942 | else: |
| 943 | # Catch all |
| 944 | rep = '<Ref 0x%x %s updated %s..%s>' % ( |
| 945 | id(self), self.ref, self.oldrev, self.newrev) |
| 946 | |
| 947 | return rep |
| 948 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 949 | def equals(self, other): |
James E. Blair | 9358c61 | 2012-09-28 08:29:39 -0700 | [diff] [blame] | 950 | if (self.project == other.project |
| 951 | and self.ref == other.ref |
| 952 | and self.newrev == other.newrev): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 953 | return True |
| 954 | return False |
| 955 | |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 956 | def isUpdateOf(self, other): |
| 957 | return False |
| 958 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 959 | |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 960 | class NullChange(Changeish): |
James E. Blair | e591020 | 2013-12-27 09:50:31 -0800 | [diff] [blame] | 961 | def __repr__(self): |
| 962 | return '<NullChange for %s>' % (self.project) |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 963 | |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 964 | def _id(self): |
Alex Gaynor | ddb9ef3 | 2013-09-16 21:04:58 -0700 | [diff] [blame] | 965 | return None |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 966 | |
| 967 | def equals(self, other): |
James E. Blair | 4f6033c | 2014-03-27 15:49:09 -0700 | [diff] [blame] | 968 | if (self.project == other.project): |
| 969 | return True |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 970 | return False |
| 971 | |
| 972 | def isUpdateOf(self, other): |
| 973 | return False |
| 974 | |
| 975 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 976 | class TriggerEvent(object): |
| 977 | def __init__(self): |
| 978 | self.data = None |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 979 | # common |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 980 | self.type = None |
| 981 | self.project_name = None |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 982 | self.trigger_name = None |
Antoine Musso | b4e809e | 2012-12-06 16:58:06 +0100 | [diff] [blame] | 983 | # Representation of the user account that performed the event. |
| 984 | self.account = None |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 985 | # patchset-created, comment-added, etc. |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 986 | self.change_number = None |
Clark Boylan | fc56df3 | 2012-06-28 15:25:57 -0700 | [diff] [blame] | 987 | self.change_url = None |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 988 | self.patch_number = None |
James E. Blair | a03262c | 2012-05-30 09:41:16 -0700 | [diff] [blame] | 989 | self.refspec = None |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 990 | self.approvals = [] |
| 991 | self.branch = None |
Clark Boylan | b9bcb40 | 2012-06-29 17:44:05 -0700 | [diff] [blame] | 992 | self.comment = None |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 993 | # ref-updated |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 994 | self.ref = None |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 995 | self.oldrev = None |
James E. Blair | 89cae0f | 2012-07-18 11:18:32 -0700 | [diff] [blame] | 996 | self.newrev = None |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 997 | # timer |
| 998 | self.timespec = None |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 999 | # zuultrigger |
| 1000 | self.pipeline_name = None |
James E. Blair | ad28e91 | 2013-11-27 10:43:22 -0800 | [diff] [blame] | 1001 | # For events that arrive with a destination pipeline (eg, from |
| 1002 | # an admin command, etc): |
| 1003 | self.forced_pipeline = None |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1004 | |
James E. Blair | 9f9667e | 2012-06-12 17:51:08 -0700 | [diff] [blame] | 1005 | def __repr__(self): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1006 | ret = '<TriggerEvent %s %s' % (self.type, self.project_name) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 1007 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1008 | if self.branch: |
| 1009 | ret += " %s" % self.branch |
| 1010 | if self.change_number: |
| 1011 | ret += " %s,%s" % (self.change_number, self.patch_number) |
| 1012 | if self.approvals: |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 1013 | ret += ' ' + ', '.join( |
| 1014 | ['%s:%s' % (a['type'], a['value']) for a in self.approvals]) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1015 | ret += '>' |
| 1016 | |
| 1017 | return ret |
| 1018 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 1019 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1020 | class BaseFilter(object): |
| 1021 | def __init__(self, required_approvals=[]): |
James E. Blair | 1b26531 | 2014-06-24 09:35:21 -0700 | [diff] [blame] | 1022 | self._required_approvals = copy.deepcopy(required_approvals) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1023 | self.required_approvals = required_approvals |
| 1024 | |
| 1025 | for a in self.required_approvals: |
| 1026 | for k, v in a.items(): |
| 1027 | if k == 'username': |
| 1028 | pass |
James E. Blair | 1fbfceb | 2014-06-23 14:42:53 -0700 | [diff] [blame] | 1029 | elif k in ['email', 'email-filter']: |
| 1030 | a['email'] = re.compile(v) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1031 | elif k == 'newer-than': |
| 1032 | a[k] = time_to_seconds(v) |
| 1033 | elif k == 'older-than': |
| 1034 | a[k] = time_to_seconds(v) |
| 1035 | else: |
| 1036 | if not isinstance(v, list): |
| 1037 | a[k] = [v] |
James E. Blair | 1fbfceb | 2014-06-23 14:42:53 -0700 | [diff] [blame] | 1038 | if 'email-filter' in a: |
| 1039 | del a['email-filter'] |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1040 | |
| 1041 | def matchesRequiredApprovals(self, change): |
| 1042 | now = time.time() |
| 1043 | for rapproval in self.required_approvals: |
| 1044 | matches_approval = False |
| 1045 | for approval in change.approvals: |
| 1046 | if 'description' not in approval: |
| 1047 | continue |
| 1048 | found_approval = True |
| 1049 | by = approval.get('by', {}) |
| 1050 | for k, v in rapproval.items(): |
| 1051 | if k == 'username': |
| 1052 | if (by.get('username', '') != v): |
| 1053 | found_approval = False |
James E. Blair | 1fbfceb | 2014-06-23 14:42:53 -0700 | [diff] [blame] | 1054 | elif k == 'email': |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1055 | if (not v.search(by.get('email', ''))): |
| 1056 | found_approval = False |
| 1057 | elif k == 'newer-than': |
| 1058 | t = now - v |
| 1059 | if (approval['grantedOn'] < t): |
| 1060 | found_approval = False |
| 1061 | elif k == 'older-than': |
| 1062 | t = now - v |
| 1063 | if (approval['grantedOn'] >= t): |
| 1064 | found_approval = False |
| 1065 | else: |
| 1066 | if (normalizeCategory(approval['description']) != k or |
| 1067 | int(approval['value']) not in v): |
| 1068 | found_approval = False |
| 1069 | if found_approval: |
| 1070 | matches_approval = True |
| 1071 | break |
| 1072 | if not matches_approval: |
| 1073 | return False |
| 1074 | return True |
| 1075 | |
| 1076 | |
| 1077 | class EventFilter(BaseFilter): |
James E. Blair | c0dedf8 | 2014-08-06 09:37:52 -0700 | [diff] [blame] | 1078 | def __init__(self, trigger, types=[], branches=[], refs=[], |
| 1079 | event_approvals={}, comments=[], emails=[], usernames=[], |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 1080 | timespecs=[], required_approvals=[], pipelines=[]): |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1081 | super(EventFilter, self).__init__( |
| 1082 | required_approvals=required_approvals) |
James E. Blair | c0dedf8 | 2014-08-06 09:37:52 -0700 | [diff] [blame] | 1083 | self.trigger = trigger |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1084 | self._types = types |
| 1085 | self._branches = branches |
| 1086 | self._refs = refs |
James E. Blair | 1fbfceb | 2014-06-23 14:42:53 -0700 | [diff] [blame] | 1087 | self._comments = comments |
| 1088 | self._emails = emails |
| 1089 | self._usernames = usernames |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 1090 | self._pipelines = pipelines |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1091 | self.types = [re.compile(x) for x in types] |
| 1092 | self.branches = [re.compile(x) for x in branches] |
| 1093 | self.refs = [re.compile(x) for x in refs] |
James E. Blair | 1fbfceb | 2014-06-23 14:42:53 -0700 | [diff] [blame] | 1094 | self.comments = [re.compile(x) for x in comments] |
| 1095 | self.emails = [re.compile(x) for x in emails] |
| 1096 | self.usernames = [re.compile(x) for x in usernames] |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 1097 | self.pipelines = [re.compile(x) for x in pipelines] |
James E. Blair | c053d02 | 2014-01-22 14:57:33 -0800 | [diff] [blame] | 1098 | self.event_approvals = event_approvals |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 1099 | self.timespecs = timespecs |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1100 | |
James E. Blair | 9f9667e | 2012-06-12 17:51:08 -0700 | [diff] [blame] | 1101 | def __repr__(self): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1102 | ret = '<EventFilter' |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 1103 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1104 | if self._types: |
| 1105 | ret += ' types: %s' % ', '.join(self._types) |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 1106 | if self._pipelines: |
| 1107 | ret += ' pipelines: %s' % ', '.join(self._pipelines) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1108 | if self._branches: |
| 1109 | ret += ' branches: %s' % ', '.join(self._branches) |
| 1110 | if self._refs: |
| 1111 | ret += ' refs: %s' % ', '.join(self._refs) |
James E. Blair | c053d02 | 2014-01-22 14:57:33 -0800 | [diff] [blame] | 1112 | if self.event_approvals: |
| 1113 | ret += ' event_approvals: %s' % ', '.join( |
| 1114 | ['%s:%s' % a for a in self.event_approvals.items()]) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1115 | if self.required_approvals: |
| 1116 | ret += ' required_approvals: %s' % ', '.join( |
James E. Blair | 1b26531 | 2014-06-24 09:35:21 -0700 | [diff] [blame] | 1117 | ['%s' % a for a in self._required_approvals]) |
James E. Blair | 1fbfceb | 2014-06-23 14:42:53 -0700 | [diff] [blame] | 1118 | if self._comments: |
| 1119 | ret += ' comments: %s' % ', '.join(self._comments) |
| 1120 | if self._emails: |
| 1121 | ret += ' emails: %s' % ', '.join(self._emails) |
| 1122 | if self._usernames: |
| 1123 | ret += ' username_filters: %s' % ', '.join(self._usernames) |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 1124 | if self.timespecs: |
| 1125 | ret += ' timespecs: %s' % ', '.join(self.timespecs) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1126 | ret += '>' |
| 1127 | |
| 1128 | return ret |
| 1129 | |
James E. Blair | c053d02 | 2014-01-22 14:57:33 -0800 | [diff] [blame] | 1130 | def matches(self, event, change): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1131 | # event types are ORed |
| 1132 | matches_type = False |
| 1133 | for etype in self.types: |
| 1134 | if etype.match(event.type): |
| 1135 | matches_type = True |
| 1136 | if self.types and not matches_type: |
| 1137 | return False |
| 1138 | |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 1139 | # pipelines are ORed |
| 1140 | matches_pipeline = False |
| 1141 | for epipe in self.pipelines: |
| 1142 | if epipe.match(event.pipeline_name): |
| 1143 | matches_pipeline = True |
| 1144 | if self.pipelines and not matches_pipeline: |
| 1145 | return False |
| 1146 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1147 | # branches are ORed |
| 1148 | matches_branch = False |
| 1149 | for branch in self.branches: |
| 1150 | if branch.match(event.branch): |
| 1151 | matches_branch = True |
| 1152 | if self.branches and not matches_branch: |
| 1153 | return False |
| 1154 | |
| 1155 | # refs are ORed |
| 1156 | matches_ref = False |
Yolanda Robla | 1669887 | 2014-08-25 11:59:27 +0200 | [diff] [blame] | 1157 | if event.ref is not None: |
| 1158 | for ref in self.refs: |
| 1159 | if ref.match(event.ref): |
| 1160 | matches_ref = True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1161 | if self.refs and not matches_ref: |
| 1162 | return False |
| 1163 | |
James E. Blair | 1fbfceb | 2014-06-23 14:42:53 -0700 | [diff] [blame] | 1164 | # comments are ORed |
| 1165 | matches_comment_re = False |
| 1166 | for comment_re in self.comments: |
Clark Boylan | b9bcb40 | 2012-06-29 17:44:05 -0700 | [diff] [blame] | 1167 | if (event.comment is not None and |
James E. Blair | 1fbfceb | 2014-06-23 14:42:53 -0700 | [diff] [blame] | 1168 | comment_re.search(event.comment)): |
| 1169 | matches_comment_re = True |
| 1170 | if self.comments and not matches_comment_re: |
Clark Boylan | b9bcb40 | 2012-06-29 17:44:05 -0700 | [diff] [blame] | 1171 | return False |
| 1172 | |
Antoine Musso | b4e809e | 2012-12-06 16:58:06 +0100 | [diff] [blame] | 1173 | # We better have an account provided by Gerrit to do |
| 1174 | # email filtering. |
| 1175 | if event.account is not None: |
James E. Blair | cf429f3 | 2012-12-20 14:28:24 -0800 | [diff] [blame] | 1176 | account_email = event.account.get('email') |
James E. Blair | 1fbfceb | 2014-06-23 14:42:53 -0700 | [diff] [blame] | 1177 | # emails are ORed |
| 1178 | matches_email_re = False |
| 1179 | for email_re in self.emails: |
Antoine Musso | b4e809e | 2012-12-06 16:58:06 +0100 | [diff] [blame] | 1180 | if (account_email is not None and |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 1181 | email_re.search(account_email)): |
James E. Blair | 1fbfceb | 2014-06-23 14:42:53 -0700 | [diff] [blame] | 1182 | matches_email_re = True |
| 1183 | if self.emails and not matches_email_re: |
Antoine Musso | b4e809e | 2012-12-06 16:58:06 +0100 | [diff] [blame] | 1184 | return False |
| 1185 | |
James E. Blair | 1fbfceb | 2014-06-23 14:42:53 -0700 | [diff] [blame] | 1186 | # usernames are ORed |
Joshua Hesketh | b8a817e | 2013-12-27 11:21:38 +1100 | [diff] [blame] | 1187 | account_username = event.account.get('username') |
James E. Blair | 1fbfceb | 2014-06-23 14:42:53 -0700 | [diff] [blame] | 1188 | matches_username_re = False |
| 1189 | for username_re in self.usernames: |
Joshua Hesketh | b8a817e | 2013-12-27 11:21:38 +1100 | [diff] [blame] | 1190 | if (account_username is not None and |
James E. Blair | 1fbfceb | 2014-06-23 14:42:53 -0700 | [diff] [blame] | 1191 | username_re.search(account_username)): |
| 1192 | matches_username_re = True |
| 1193 | if self.usernames and not matches_username_re: |
Joshua Hesketh | b8a817e | 2013-12-27 11:21:38 +1100 | [diff] [blame] | 1194 | return False |
| 1195 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1196 | # approvals are ANDed |
James E. Blair | c053d02 | 2014-01-22 14:57:33 -0800 | [diff] [blame] | 1197 | for category, value in self.event_approvals.items(): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1198 | matches_approval = False |
| 1199 | for eapproval in event.approvals: |
| 1200 | if (normalizeCategory(eapproval['description']) == category and |
| 1201 | int(eapproval['value']) == int(value)): |
| 1202 | matches_approval = True |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 1203 | if not matches_approval: |
| 1204 | return False |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 1205 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1206 | if self.required_approvals and not change.approvals: |
James E. Blair | c053d02 | 2014-01-22 14:57:33 -0800 | [diff] [blame] | 1207 | # A change with no approvals can not match |
| 1208 | return False |
| 1209 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1210 | # required approvals are ANDed |
| 1211 | if not self.matchesRequiredApprovals(change): |
| 1212 | return False |
James E. Blair | c053d02 | 2014-01-22 14:57:33 -0800 | [diff] [blame] | 1213 | |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 1214 | # timespecs are ORed |
| 1215 | matches_timespec = False |
| 1216 | for timespec in self.timespecs: |
| 1217 | if (event.timespec == timespec): |
| 1218 | matches_timespec = True |
| 1219 | if self.timespecs and not matches_timespec: |
| 1220 | return False |
| 1221 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1222 | return True |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 1223 | |
| 1224 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1225 | class ChangeishFilter(BaseFilter): |
Clark Boylan | a9702ad | 2014-05-08 17:17:24 -0700 | [diff] [blame] | 1226 | def __init__(self, open=None, current_patchset=None, |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1227 | statuses=[], required_approvals=[]): |
| 1228 | super(ChangeishFilter, self).__init__( |
| 1229 | required_approvals=required_approvals) |
James E. Blair | 11041d2 | 2014-05-02 14:49:53 -0700 | [diff] [blame] | 1230 | self.open = open |
Clark Boylan | a9702ad | 2014-05-08 17:17:24 -0700 | [diff] [blame] | 1231 | self.current_patchset = current_patchset |
James E. Blair | 11041d2 | 2014-05-02 14:49:53 -0700 | [diff] [blame] | 1232 | self.statuses = statuses |
James E. Blair | 11041d2 | 2014-05-02 14:49:53 -0700 | [diff] [blame] | 1233 | |
| 1234 | def __repr__(self): |
| 1235 | ret = '<ChangeishFilter' |
| 1236 | |
| 1237 | if self.open is not None: |
| 1238 | ret += ' open: %s' % self.open |
Clark Boylan | a9702ad | 2014-05-08 17:17:24 -0700 | [diff] [blame] | 1239 | if self.current_patchset is not None: |
| 1240 | ret += ' current-patchset: %s' % self.current_patchset |
James E. Blair | 11041d2 | 2014-05-02 14:49:53 -0700 | [diff] [blame] | 1241 | if self.statuses: |
| 1242 | ret += ' statuses: %s' % ', '.join(self.statuses) |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1243 | if self.required_approvals: |
| 1244 | ret += ' required_approvals: %s' % str(self.required_approvals) |
James E. Blair | 11041d2 | 2014-05-02 14:49:53 -0700 | [diff] [blame] | 1245 | ret += '>' |
| 1246 | |
| 1247 | return ret |
| 1248 | |
| 1249 | def matches(self, change): |
| 1250 | if self.open is not None: |
| 1251 | if self.open != change.open: |
| 1252 | return False |
| 1253 | |
Clark Boylan | a9702ad | 2014-05-08 17:17:24 -0700 | [diff] [blame] | 1254 | if self.current_patchset is not None: |
| 1255 | if self.current_patchset != change.is_current_patchset: |
| 1256 | return False |
| 1257 | |
James E. Blair | 11041d2 | 2014-05-02 14:49:53 -0700 | [diff] [blame] | 1258 | if self.statuses: |
| 1259 | if change.status not in self.statuses: |
| 1260 | return False |
| 1261 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1262 | if self.required_approvals and not change.approvals: |
James E. Blair | 11041d2 | 2014-05-02 14:49:53 -0700 | [diff] [blame] | 1263 | # A change with no approvals can not match |
| 1264 | return False |
| 1265 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 1266 | # required approvals are ANDed |
| 1267 | if not self.matchesRequiredApprovals(change): |
| 1268 | return False |
James E. Blair | 11041d2 | 2014-05-02 14:49:53 -0700 | [diff] [blame] | 1269 | |
| 1270 | return True |
| 1271 | |
| 1272 | |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 1273 | class Layout(object): |
| 1274 | def __init__(self): |
| 1275 | self.projects = {} |
James E. Blair | 5a9918a | 2013-08-27 10:06:27 -0700 | [diff] [blame] | 1276 | self.pipelines = OrderedDict() |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 1277 | self.jobs = {} |
James E. Blair | c28d1b0 | 2013-07-19 11:37:06 -0700 | [diff] [blame] | 1278 | self.metajobs = [] |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 1279 | |
| 1280 | def getJob(self, name): |
| 1281 | if name in self.jobs: |
| 1282 | return self.jobs[name] |
| 1283 | job = Job(name) |
Maru Newby | 79427a4 | 2015-02-17 17:54:45 +0000 | [diff] [blame] | 1284 | if job.is_metajob: |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 1285 | regex = re.compile(name) |
James E. Blair | c28d1b0 | 2013-07-19 11:37:06 -0700 | [diff] [blame] | 1286 | self.metajobs.append((regex, job)) |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 1287 | else: |
| 1288 | # Apply attributes from matching meta-jobs |
James E. Blair | c28d1b0 | 2013-07-19 11:37:06 -0700 | [diff] [blame] | 1289 | for regex, metajob in self.metajobs: |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 1290 | if regex.match(name): |
| 1291 | job.copy(metajob) |
| 1292 | self.jobs[name] = job |
| 1293 | return job |