1 # Copyright 2015 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 """A Model for holding a mapping of bug labels to sets of tests. 6 7 This is used to decide which bug labels should be applied by default 8 to bugs filed for alerts on particular tests. 9 """ 10 11 from google.appengine.ext import ndb 12 13 from dashboard import utils 14 15 # String ID for the single BugLabelPatterns entity. 16 _ID = 'patterns' 17 18 19 class BugLabelPatterns(ndb.Model): 20 """A model for storing the mapping of bug labels to test path patterns. 21 22 There should only ever be one BugLabelPatterns entity, and it has a single 23 property, which is a dict mapping bug label strings to lists of test path 24 pattern strings. 25 """ 26 labels_to_patterns = ndb.JsonProperty(indexed=False) 27 28 29 def _Get(): 30 """Fetches the single BugLabelPatterns entity.""" 31 entity = ndb.Key(BugLabelPatterns, _ID).get() 32 if entity: 33 return entity 34 entity = BugLabelPatterns(id=_ID) 35 entity.labels_to_patterns = {} 36 entity.put() 37 return entity 38 39 40 def GetBugLabelPatterns(): 41 """Returns the dict of bug labels to test path patterns.""" 42 return _Get().labels_to_patterns 43 44 45 def GetBugLabelsForTest(test): 46 """Returns a list of bug labels to be applied to the test.""" 47 matching = [] 48 for label, patterns in GetBugLabelPatterns().iteritems(): 49 for pattern in patterns: 50 if utils.TestMatchesPattern(test, pattern): 51 matching.append(label) 52 return sorted(matching) 53 54 55 def AddBugLabelPattern(label, pattern): 56 """Adds the given test path pattern for the given bug label.""" 57 entity = _Get() 58 if label not in entity.labels_to_patterns: 59 entity.labels_to_patterns[label] = [] 60 entity.labels_to_patterns[label].append(pattern) 61 entity.put() 62 63 64 def RemoveBugLabel(label): 65 """Adds the given test path pattern for the given bug label.""" 66 entity = _Get() 67 if label in entity.labels_to_patterns: 68 del entity.labels_to_patterns[label] 69 entity.put() 70