| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 datetime import datetime | 5 from datetime import datetime |
| 6 import logging | 6 import logging |
| 7 import textwrap | 7 import textwrap |
| 8 | 8 |
| 9 from google.appengine.ext import ndb | 9 from google.appengine.ext import ndb |
| 10 | 10 |
| 11 from common.git_repository import GitRepository | 11 from common.git_repository import GitRepository |
| 12 from common.http_client_appengine import HttpClientAppengine as HttpClient | 12 from common.http_client_appengine import HttpClientAppengine as HttpClient |
| 13 from common.pipeline_wrapper import BasePipeline | 13 from common.pipeline_wrapper import BasePipeline |
| 14 from common.rietveld import Rietveld | 14 from common.rietveld import Rietveld |
| 15 from model import analysis_status as status | 15 from model import analysis_status as status |
| 16 from model.wf_culprit import WfCulprit | 16 from model.wf_culprit import WfCulprit |
| 17 from waterfall import waterfall_config |
| 17 | 18 |
| 18 | 19 |
| 19 @ndb.transactional | 20 @ndb.transactional |
| 20 def _ShouldSendNotification( | 21 def _ShouldSendNotification( |
| 21 master_name, builder_name, build_number, repo_name, revision): | 22 master_name, builder_name, build_number, repo_name, revision, |
| 23 build_num_threshold, time_limit_passed): |
| 22 """Returns True if a notification for the culprit should be sent.""" | 24 """Returns True if a notification for the culprit should be sent.""" |
| 23 culprit = (WfCulprit.Get(repo_name, revision) or | 25 culprit = (WfCulprit.Get(repo_name, revision) or |
| 24 WfCulprit.Create(repo_name, revision)) | 26 WfCulprit.Create(repo_name, revision)) |
| 25 if [master_name, builder_name, build_number] in culprit.builds: | 27 if [master_name, builder_name, build_number] in culprit.builds: |
| 26 return False | 28 return False |
| 27 | 29 |
| 28 culprit.builds.append([master_name, builder_name, build_number]) | 30 culprit.builds.append([master_name, builder_name, build_number]) |
| 29 # Send notification when the culprit is for 2+ failures in two different | 31 # Send notification only when: |
| 30 # builds to avoid false positive due to flakiness. | 32 # 1. It was not processed yet. |
| 31 # TODO(stgao): move to config. | 33 # 2. The culprit is for multiple failures in different builds to avoid false |
| 34 # positive due to flakiness. |
| 35 # 3. It is not too late after the culprit was committed. |
| 36 # * Try-job takes too long to complete and the failure got fixed. |
| 37 # * The whole analysis was rerun a long time after the failure occurred. |
| 32 should_send = not (culprit.cr_notification_processed or | 38 should_send = not (culprit.cr_notification_processed or |
| 33 len(culprit.builds) < 2) | 39 len(culprit.builds) < build_num_threshold or |
| 40 time_limit_passed) |
| 34 if should_send: | 41 if should_send: |
| 35 culprit.cr_notification_status = status.RUNNING | 42 culprit.cr_notification_status = status.RUNNING |
| 36 culprit.put() | 43 culprit.put() |
| 37 return should_send | 44 return should_send |
| 38 | 45 |
| 39 | 46 |
| 40 @ndb.transactional | 47 @ndb.transactional |
| 41 def _UpdateNotificationStatus(repo_name, revision, new_status): | 48 def _UpdateNotificationStatus(repo_name, revision, new_status): |
| 42 culprit = WfCulprit.Get(repo_name, revision) | 49 culprit = WfCulprit.Get(repo_name, revision) |
| 43 culprit.cr_notification_status = new_status | 50 culprit.cr_notification_status = new_status |
| 44 if culprit.cr_notified: | 51 if culprit.cr_notified: |
| 45 culprit.cr_notification_time = datetime.utcnow() | 52 culprit.cr_notification_time = datetime.utcnow() |
| 46 culprit.put() | 53 culprit.put() |
| 47 | 54 |
| 48 | 55 |
| 49 def _SendNotificationForCulprit(repo_name, revision): | 56 def _SendNotificationForCulprit(repo_name, revision, code_review_url): |
| 50 # TODO(stgao): get repo url at runtime based on the given repo name. | |
| 51 repo = GitRepository( | |
| 52 'https://chromium.googlesource.com/chromium/src.git', HttpClient()) | |
| 53 change_log = repo.GetChangeLog(revision) | |
| 54 sent = False | 57 sent = False |
| 55 if change_log.code_review_url: | 58 if code_review_url: |
| 56 # Occasionally, a commit was not uploaded for code-review. | 59 # Occasionally, a commit was not uploaded for code-review. |
| 57 culprit = WfCulprit.Get(repo_name, revision) | 60 culprit = WfCulprit.Get(repo_name, revision) |
| 58 rietveld = Rietveld() | 61 rietveld = Rietveld() |
| 59 message = textwrap.dedent(""" | 62 message = textwrap.dedent(""" |
| 60 Findit Try-job identified this CL (revision %s) as the culprit for failures | 63 Findit Try-job identified this CL (revision %s) as the culprit for failures |
| 61 in the build cycle(s) as shown on: | 64 in the build cycle(s) as shown on: |
| 62 https://findit-for-me.appspot.com/waterfall/culprit?key=%s | 65 https://findit-for-me.appspot.com/waterfall/culprit?key=%s |
| 63 """) % (revision, culprit.key.urlsafe()) | 66 """) % (revision, culprit.key.urlsafe()) |
| 64 sent = rietveld.PostMessage(change_log.code_review_url, message) | 67 sent = rietveld.PostMessage(code_review_url, message) |
| 65 else: | 68 else: |
| 66 logging.error('Can not get code-review url for %s/%s', repo_name, revision) | 69 logging.error('No code-review url for %s/%s', repo_name, revision) |
| 67 | 70 |
| 68 _UpdateNotificationStatus(repo_name, revision, | 71 _UpdateNotificationStatus(repo_name, revision, |
| 69 status.COMPLETED if sent else status.ERROR) | 72 status.COMPLETED if sent else status.ERROR) |
| 70 return sent | 73 return sent |
| 71 | 74 |
| 72 | 75 |
| 76 def _GetCulpritInfo(repo_name, revision): |
| 77 """Returns commit time and code-review url of the given revision.""" |
| 78 # TODO(stgao): get repo url at runtime based on the given repo name. |
| 79 # unused arg - pylint: disable=W0612,W0613 |
| 80 repo = GitRepository( |
| 81 'https://chromium.googlesource.com/chromium/src.git', HttpClient()) |
| 82 change_log = repo.GetChangeLog(revision) |
| 83 return change_log.committer_time, change_log.code_review_url |
| 84 |
| 85 |
| 86 def _NotificationTimeLimitPassed(commit_time, latency_limit_minutes): |
| 87 """Returns True if it is too late to send notification.""" |
| 88 latency_seconds = (datetime.utcnow() - commit_time).total_seconds() |
| 89 return latency_seconds > latency_limit_minutes * 60 |
| 90 |
| 91 |
| 73 class SendNotificationForCulpritPipeline(BasePipeline): | 92 class SendNotificationForCulpritPipeline(BasePipeline): |
| 74 | 93 |
| 75 # Arguments number differs from overridden method - pylint: disable=W0221 | 94 # Arguments number differs from overridden method - pylint: disable=W0221 |
| 76 def run(self, master_name, builder_name, build_number, repo_name, revision): | 95 def run(self, master_name, builder_name, build_number, repo_name, revision): |
| 96 action_settings = waterfall_config.GetActionSettings() |
| 97 # Set some impossible default values to prevent notification by default. |
| 98 build_threshold = action_settings.get( |
| 99 'cr_notification_build_threshold', 100000) |
| 100 latency_limit_minutes = action_settings.get( |
| 101 'cr_notification_latency_limit_minutes', 1) |
| 102 |
| 103 commit_time, code_review_url = _GetCulpritInfo(repo_name, revision) |
| 104 time_limit_passed = _NotificationTimeLimitPassed( |
| 105 commit_time, latency_limit_minutes) |
| 106 |
| 77 if not _ShouldSendNotification( | 107 if not _ShouldSendNotification( |
| 78 master_name, builder_name, build_number, repo_name, revision): | 108 master_name, builder_name, build_number, |
| 109 repo_name, revision, build_threshold, time_limit_passed): |
| 79 return False | 110 return False |
| 80 return _SendNotificationForCulprit(repo_name, revision) | 111 return _SendNotificationForCulprit(repo_name, revision, code_review_url) |
| OLD | NEW |