Update Bug Filing anomalies to include Skia link.

When filing a bug by manually triaging it, it didn't include link to Skia. With this update, we use the skia_host parameter to generate a URL.

Bug: b/430819819

Change-Id: I81bd35500f6ffe7060271bfd6656facd0390793c
Reviewed-on: https://chromium-review.googlesource.com/c/catapult/+/6779946
Commit-Queue: Eduardo Yap <[email protected]>
Reviewed-by: Maggie Dong <[email protected]>
Reviewed-by: Jeff Yoon <[email protected]>
diff --git a/dashboard/dashboard/common/file_bug.py b/dashboard/dashboard/common/file_bug.py
index cdb1f0e..1f115db 100644
--- a/dashboard/dashboard/common/file_bug.py
+++ b/dashboard/dashboard/common/file_bug.py
@@ -48,10 +48,11 @@
   return docs[0]
 
 
-def _AdditionalDetails(bug_id, project_id, alerts):
+def _AdditionalDetails(bug_id, project_id, alerts, skia_host=None):
   """Returns a message with additional information to add to a bug."""
   base_url = '%s/group_report' % _GetServerURL()
   bug_page_url = '%s?bug_id=%s&project_id=%s' % (base_url, bug_id, project_id)
+
   alert_keys = utils.ConvertBytesBeforeJsonDumps(_UrlsafeKeys(alerts))
   sid = short_uri.GetOrCreatePageState(json.dumps(alert_keys))
   alerts_url = '%s?sid=%s' % (base_url, sid)
@@ -59,6 +60,22 @@
   comment += (
       '(For debugging:) Original alerts at time of bug-filing:\n  %s\n' %
       alerts_url)
+
+  if skia_host:
+    # For Skia, alerts are in integer ID form
+    integer_ids = [a.key.id() for a in alerts]
+
+    skia_id_string = ','.join(map(str, integer_ids))
+    skia_sid = short_uri.GetOrCreatePageState(skia_id_string)
+
+    skia_base_url = '%s/u/' % skia_host
+    skia_bug_page_url = '%s?bugID=%s' % (skia_base_url, bug_id)
+    skia_alerts_url = '%s?sid=%s' % (skia_base_url, skia_sid)
+
+    comment += ('\n<b>Graphs on Skia host:</b>\n  %s\n\n' % skia_bug_page_url)
+    comment += ('(For debugging:) Original alerts on Skia host:\n  %s\n' %
+                skia_alerts_url)
+
   bot_names = {a.bot_name for a in alerts}
   if bot_names:
     comment += '\n\nBot(s) for this bug\'s original alert(s):\n\n'
@@ -305,7 +322,8 @@
             components,
             keys,
             needs_bisect=True,
-            keys_are_urlsafe=True):
+            keys_are_urlsafe=True,
+            skia_host=None):
   logging.info('file a bug from legacy chromeperf UI')
   if keys_are_urlsafe:
     alert_keys = [ndb.Key(urlsafe=k) for k in keys]
@@ -343,7 +361,8 @@
 
   ndb.put_multi(alerts)
   logging.info('bug mapped to alert')
-  comment_body = _AdditionalDetails(bug_id, project_id, alerts)
+  comment_body = _AdditionalDetails(
+      bug_id, project_id, alerts, skia_host=skia_host)
 
   # Add the bug comment with the service account, so that there are no
   # permissions issues.
diff --git a/dashboard/dashboard/file_bug.py b/dashboard/dashboard/file_bug.py
index b567194..8264b64 100644
--- a/dashboard/dashboard/file_bug.py
+++ b/dashboard/dashboard/file_bug.py
@@ -137,6 +137,8 @@
   ccs = data.get('ccs', [])
   # list of labels in string
   labels = data.get('labels', [])
+  # Skia host link
+  skia_host = data.get('host', '')
 
   create_bug_result = _CreateBug(
       owner=assignee,
@@ -147,7 +149,8 @@
       labels=labels,
       components=[component],  # FileBug expects a list of components.
       urlsafe_keys='',
-      integer_keys=keys)
+      integer_keys=keys,
+      skia_host=skia_host)
 
   if 'error' in create_bug_result:
     return make_response(
@@ -184,8 +187,16 @@
       })
 
 
-def _CreateBug(owner, cc, summary, description, project_id, labels, components,
-               urlsafe_keys, integer_keys):
+def _CreateBug(owner,
+               cc,
+               summary,
+               description,
+               project_id,
+               labels,
+               components,
+               urlsafe_keys,
+               integer_keys,
+               skia_host=None):
   """Creates a bug, associates it with the alerts, sends a HTML response.
 
   Args:
@@ -211,9 +222,16 @@
             project_domain
     }
   if urlsafe_keys:
-    template_params = file_bug.FileBug(owner, cc, summary, description,
-                                       project_id, labels, components,
-                                       urlsafe_keys.split(','))
+    template_params = file_bug.FileBug(
+        owner,
+        cc,
+        summary,
+        description,
+        project_id,
+        labels,
+        components,
+        urlsafe_keys.split(','),
+        skia_host=skia_host)
   else:
     template_params = file_bug.FileBug(
         owner,
@@ -224,5 +242,6 @@
         labels,
         components,
         integer_keys,
-        keys_are_urlsafe=False)
+        keys_are_urlsafe=False,
+        skia_host=skia_host)
   return template_params
diff --git a/dashboard/dashboard/file_bug_test.py b/dashboard/dashboard/file_bug_test.py
index dd5d931..f1d4f79 100644
--- a/dashboard/dashboard/file_bug_test.py
+++ b/dashboard/dashboard/file_bug_test.py
@@ -25,6 +25,8 @@
 from dashboard.models import bug_label_patterns
 from dashboard.models import histogram
 from dashboard.models.subscription import Subscription
+from dashboard import short_uri
+
 
 from tracing.value.diagnostics import generic_set
 from tracing.value.diagnostics import reserved_infos
@@ -1178,3 +1180,40 @@
     self.assertIn(
         b'<input type="checkbox" checked name="component" value="Abc&gt;Def">',
         response.body)
+
+  @mock.patch('google.appengine.api.app_identity.get_default_version_hostname',
+              mock.MagicMock(return_value='chromeperf.appspot.com'))
+  @mock.patch.object(file_bug.file_bug, '_GetAllCurrentVersionsFromOmahaProxy',
+                     mock.MagicMock(return_value=[]))
+  @mock.patch('dashboard.common.file_bug.auto_bisect.StartNewBisectForBug',
+              mock.MagicMock(return_value={'issue_id': 123}))
+  def testSkiaFileBug_WithHost_IncludesSkiaLinksInComment(self):
+    """Tests that providing a host adds Skia-specific links to the bug comment."""
+    anomaly_keys = self._AddSampleAlerts()
+    alerts_to_file = [anomaly_keys[0], anomaly_keys[1]]
+    integer_ids = [k.id() for k in alerts_to_file]
+    skia_host_name = 'https://perf.skia.org'
+
+    self.testapp.post_json(
+        '/file_bug_skia', {
+            'keys': integer_ids,
+            'title': 'Test Skia Links',
+            'description': 'A test bug.',
+            'component': 'Test>Component',
+            'host': skia_host_name,
+        })
+
+    comment = self._issue_tracker_service.add_comment_kwargs['comment']
+
+    self.assertIn('<b>All graphs for this bug:</b>', comment)
+    self.assertIn('https://chromeperf.appspot.com/group_report?bug_id=',
+                  comment)
+
+    self.assertIn('<b>Graphs on Skia host:</b>', comment)
+    self.assertIn('https://perf.skia.org/u/?bugID=', comment)
+
+    expected_id_string = ','.join(map(str, integer_ids))
+    expected_skia_sid = short_uri.GetOrCreatePageState(expected_id_string)
+
+    expected_skia_debug_url = 'https://perf.skia.org/u/?sid=%s' % expected_skia_sid
+    self.assertIn(expected_skia_debug_url, comment)