def testGetBugLabelsForTest(self): bug_label_patterns.AddBugLabelPattern('foo', '*/*/foo') bug_label_patterns.AddBugLabelPattern('f-prefix', '*/*/f*') testing_common.AddTests(['M'], ['b'], {'foo': {}, 'bar': {}}) foo_test = utils.TestKey('M/b/foo').get() bar_test = utils.TestKey('M/b/bar').get() self.assertEqual(['f-prefix', 'foo'], bug_label_patterns.GetBugLabelsForTest(foo_test)) self.assertEqual([], bug_label_patterns.GetBugLabelsForTest(bar_test))
def _FetchLabelsAndComponents(alert_keys): """Fetches a list of bug labels and components for the given Alert keys.""" labels = set(_DEFAULT_LABELS) components = set() alerts = ndb.get_multi(alert_keys) sheriff_keys = set(alert.sheriff for alert in alerts) sheriff_labels = [sheriff.labels for sheriff in ndb.get_multi(sheriff_keys)] tags = [item for sublist in sheriff_labels for item in sublist] for tag in tags: if tag.startswith('Cr-'): components.add(_ComponentFromCrLabel(tag)) else: labels.add(tag) if any(a.internal_only for a in alerts): # This is a Chrome-specific behavior, and should ideally be made # more general (maybe there should be a list in datastore of bug # labels to add for internal bugs). labels.add('Restrict-View-Google') for test in {a.GetTestMetadataKey() for a in alerts}: labels_components = bug_label_patterns.GetBugLabelsForTest(test) for item in labels_components: if item.startswith('Cr-'): components.add(_ComponentFromCrLabel(item)) else: labels.add(item) return labels, components
def GetAlertInfo(alert, test): """Gets the alert info formatted for the given alert and test. Args: alert: An Anomaly entity. test: The TestMetadata entity for the given alert. Returns: A dictionary of string keys to values. Keys are 'email_subject', 'email_text', 'email_html', 'dashboard_link', 'alerts_link', 'bug_link'. """ percent_changed = alert.GetDisplayPercentChanged() change_type = 'improvement' if alert.is_improvement else 'regression' test_name = '/'.join(test.test_path.split('/')[2:]) sheriff_name = alert.sheriff.string_id() master = test.master_name bot = test.bot_name graph_url = GetGroupReportPageLink(alert) # Parameters to interpolate into strings below. interpolation_parameters = { 'percent_changed': percent_changed, 'change_type': change_type, 'master': master, 'bot': bot, 'test_name': test_name, 'sheriff_name': sheriff_name, 'start': alert.start_revision, 'end': alert.end_revision, 'graph_url': graph_url, } bug_comment = _BUG_REPORT_COMMENT % interpolation_parameters bug_summary = ( '%(percent_changed)s %(change_type)s in %(test_name)s ' 'on %(bot)s at %(start)d:%(end)d') % interpolation_parameters labels = (alert.sheriff.get().labels + bug_label_patterns.GetBugLabelsForTest(test)) bug_url = _BUG_REPORT_LINK_URL % ( urllib.quote(bug_summary), urllib.quote(bug_comment), ','.join(labels)) interpolation_parameters['bug_url'] = bug_url results = { 'email_subject': _SINGLE_EMAIL_SUBJECT % interpolation_parameters, 'email_text': _SUMMARY_EMAIL_TEXT_BODY % interpolation_parameters, 'email_html': _EMAIL_HTML_TABLE % interpolation_parameters, 'dashboard_link': graph_url, 'alerts_link': _ALL_ALERTS_LINK % urllib.quote(sheriff_name), 'bug_link': bug_url, } return results
def _FetchLabels(alert_keys): """Fetches a list of bug labels for the given list of Alert keys.""" labels = set(_DEFAULT_LABELS) alerts = ndb.get_multi(alert_keys) if any(a.internal_only for a in alerts): # This is a Chrome-specific behavior, and should ideally be made # more general (maybe there should be a list in datastore of bug # labels to add for internal bugs). labels.add('Restrict-View-Google') for test in {a.test for a in alerts}: labels.update(bug_label_patterns.GetBugLabelsForTest(test)) return labels
def _FetchLabelsAndComponents(alert_keys): """Fetches a list of bug labels and components for the given Alert keys.""" labels = set(_DEFAULT_LABELS) components = set() alerts = ndb.get_multi(alert_keys) if any(a.internal_only for a in alerts): # This is a Chrome-specific behavior, and should ideally be made # more general (maybe there should be a list in datastore of bug # labels to add for internal bugs). labels.add('Restrict-View-Google') for test in {a.test for a in alerts}: labels_components = bug_label_patterns.GetBugLabelsForTest(test) for item in labels_components: if item.startswith('Cr-'): components.add(item.replace('Cr-', '').replace('-', '>')) else: labels.add(item) return labels, components
def GetAnomalyDict(anomaly_entity, bisect_status=None, v2=False): """Returns a dictionary for an Anomaly which can be encoded as JSON. Args: anomaly_entity: An Anomaly entity. bisect_status: String status of bisect run. Returns: A dictionary which is safe to be encoded as JSON. """ test_key = anomaly_entity.GetTestMetadataKey() test_path = utils.TestPath(test_key) dashboard_link = email_template.GetReportPageLink( test_path, rev=anomaly_entity.end_revision, add_protocol_and_host=False) dct = { 'bug_id': anomaly_entity.bug_id, 'dashboard_link': dashboard_link, 'end_revision': anomaly_entity.end_revision, 'improvement': anomaly_entity.is_improvement, 'key': anomaly_entity.key.urlsafe(), 'median_after_anomaly': anomaly_entity.median_after_anomaly, 'median_before_anomaly': anomaly_entity.median_before_anomaly, 'recovered': anomaly_entity.recovered, 'start_revision': anomaly_entity.start_revision, 'units': anomaly_entity.units, } if v2: bug_labels = set() bug_components = set() if anomaly_entity.internal_only: bug_labels.add('Restrict-View-Google') tags = bug_label_patterns.GetBugLabelsForTest(test_key) if anomaly_entity.sheriff: try: tags += anomaly_entity.sheriff.get().labels except AssertionError: # The Sheriff is internal_only even though the alert isn't. pass for tag in tags: if tag.startswith('Cr-'): bug_components.add(tag.replace('Cr-', '').replace('-', '>')) else: bug_labels.add(tag) dct['bug_components'] = list(bug_components) dct['bug_labels'] = list(bug_labels) desc = descriptor.Descriptor.FromTestPathSync(test_path) dct['descriptor'] = { 'testSuite': desc.test_suite, 'measurement': desc.measurement, 'bot': desc.bot, 'testCase': desc.test_case, 'statistic': desc.statistic, } dct['pinpoint_bisects'] = anomaly_entity.pinpoint_bisects else: test_path_parts = test_path.split('/') dct['absolute_delta'] = '%s' % anomaly_entity.GetDisplayAbsoluteChanged( ) dct['bisect_status'] = bisect_status dct['bot'] = test_path_parts[1] dct['date'] = str(anomaly_entity.timestamp.date()) dct['display_end'] = anomaly_entity.display_end dct['display_start'] = anomaly_entity.display_start dct['master'] = test_path_parts[0] dct['percent_changed'] = '%s' % anomaly_entity.GetDisplayPercentChanged( ) dct['ref_test'] = anomaly_entity.GetRefTestPath() dct['test'] = '/'.join(test_path_parts[3:]) dct['testsuite'] = test_path_parts[2] dct['timestamp'] = anomaly_entity.timestamp.isoformat() dct['type'] = 'anomaly' return dct