| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from collections import defaultdict | 5 from collections import defaultdict |
| 6 import datetime | 6 import datetime |
| 7 import json | 7 import json |
| 8 import logging | 8 import logging |
| 9 | 9 |
| 10 from apiclient import discovery | 10 from apiclient import discovery |
| 11 from apiclient.errors import HttpError | 11 from apiclient.errors import HttpError |
| 12 import httplib2 | 12 import httplib2 |
| 13 from oauth2client import client | 13 from oauth2client import client |
| 14 | 14 |
| 15 from infra_libs import ts_mon | |
| 16 | |
| 17 | 15 |
| 18 # Dictionary mapping whitelisted lower-case labels to corresponding tree names. | 16 # Dictionary mapping whitelisted lower-case labels to corresponding tree names. |
| 19 WHITELISTED_LABELS = {'sheriff-chromium': 'chromium', | 17 WHITELISTED_LABELS = {'sheriff-chromium': 'chromium', |
| 20 'sheriff-blink': 'blink', | 18 'sheriff-blink': 'blink', |
| 21 'infra-troopers': 'trooper'} | 19 'infra-troopers': 'trooper'} |
| 22 BATCH_SIZE = 10 | 20 BATCH_SIZE = 10 |
| 23 | 21 |
| 24 | 22 |
| 25 issue_tracker_requests = ts_mon.CounterMetric( | |
| 26 'flakiness_pipeline/issue_tracker_requests', | |
| 27 description='Number of requests to the issue tracker') | |
| 28 | |
| 29 | |
| 30 def _build_crbug_service(crbug_service_account): # pragma: no cover | 23 def _build_crbug_service(crbug_service_account): # pragma: no cover |
| 31 with open(crbug_service_account) as crbug_sa_file: | 24 with open(crbug_service_account) as crbug_sa_file: |
| 32 service_account = json.load(crbug_sa_file) | 25 service_account = json.load(crbug_sa_file) |
| 33 | 26 |
| 34 creds = client.SignedJwtAssertionCredentials( | 27 creds = client.SignedJwtAssertionCredentials( |
| 35 service_account['client_email'], service_account['private_key'], | 28 service_account['client_email'], service_account['private_key'], |
| 36 'https://www.googleapis.com/auth/userinfo.email') | 29 'https://www.googleapis.com/auth/userinfo.email') |
| 37 http = creds.authorize(httplib2.Http()) | 30 http = creds.authorize(httplib2.Http()) |
| 38 return discovery.build( | 31 return discovery.build( |
| 39 'monorail', 'v1', discoveryServiceUrl='https://monorail-prod.appspot.com/' | 32 'monorail', 'v1', discoveryServiceUrl='https://monorail-prod.appspot.com/' |
| 40 '_ah/api/discovery/v1/apis/{api}/{apiVersion}/rest', http=http) | 33 '_ah/api/discovery/v1/apis/{api}/{apiVersion}/rest', http=http) |
| 41 | 34 |
| 42 | 35 |
| 43 def _list_issues(crbug_service_account): | 36 def _list_issues(crbug_service_account): |
| 44 service = _build_crbug_service(crbug_service_account) | 37 service = _build_crbug_service(crbug_service_account) |
| 45 issues = [] | 38 issues = [] |
| 46 seen_issue_ids = set() | 39 seen_issue_ids = set() |
| 47 for whitelisted_label in WHITELISTED_LABELS: | 40 for whitelisted_label in WHITELISTED_LABELS: |
| 48 start_index = 0 | 41 start_index = 0 |
| 49 while True: | 42 while True: |
| 50 request = service.issues().list( | 43 request = service.issues().list( |
| 51 projectId='chromium', label=whitelisted_label, | 44 projectId='chromium', label=whitelisted_label, |
| 52 startIndex=start_index, maxResults=BATCH_SIZE, can='open') | 45 startIndex=start_index, maxResults=BATCH_SIZE, can='open') |
| 53 response = request.execute(num_retries=5) | 46 response = request.execute(num_retries=5) |
| 54 issue_tracker_requests.increment( | |
| 55 {'source': 'builder_alerts', 'operation': 'issues_list'}) | |
| 56 logging.debug('Incremented issue_tracker_requests counter') | |
| 57 | 47 |
| 58 # Issue Tracker may omit certain issues occasionally, so counting whether | 48 # Issue Tracker may omit certain issues occasionally, so counting whether |
| 59 # they add up to 'totalResults' in response is not relaible. However, we | 49 # they add up to 'totalResults' in response is not relaible. However, we |
| 60 # can use the fact that 'items' is not present in response if we try to | 50 # can use the fact that 'items' is not present in response if we try to |
| 61 # list issues starting from an index beyond totalResults. | 51 # list issues starting from an index beyond totalResults. |
| 62 if not response.get('items'): | 52 if not response.get('items'): |
| 63 break | 53 break |
| 64 | 54 |
| 65 # Here we increment start_index by BATCH_SIZE rather then by the actual | 55 # Here we increment start_index by BATCH_SIZE rather then by the actual |
| 66 # number of returned issues in 'items' because as described above, issue | 56 # number of returned issues in 'items' because as described above, issue |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 | 163 |
| 174 sheriff_issue['tags'] = sorted(tags) # converts back to list | 164 sheriff_issue['tags'] = sorted(tags) # converts back to list |
| 175 if severity is not None: | 165 if severity is not None: |
| 176 sheriff_issue['severity'] = severity | 166 sheriff_issue['severity'] = severity |
| 177 | 167 |
| 178 # We assume that a tags have 1:1 mapping to trees here. | 168 # We assume that a tags have 1:1 mapping to trees here. |
| 179 for tree_name in sheriff_issue['tags']: | 169 for tree_name in sheriff_issue['tags']: |
| 180 sheriff_issues[tree_name].append(sheriff_issue) | 170 sheriff_issues[tree_name].append(sheriff_issue) |
| 181 | 171 |
| 182 return sheriff_issues | 172 return sheriff_issues |
| OLD | NEW |