Merge branch 'main' into avm99963-monorail

Merged commit 34d8229ae2b51fb1a15bd208e6fe6185c94f6266

GitOrigin-RevId: 7ee0917f93a577e475f8e09526dd144d245593f4
diff --git a/static_src/reducers/issueV0.js b/static_src/reducers/issueV0.js
index 8f670c9..5ffafd9 100644
--- a/static_src/reducers/issueV0.js
+++ b/static_src/reducers/issueV0.js
@@ -1,4 +1,4 @@
-// Copyright 2019 The Chromium Authors. All rights reserved.
+// Copyright 2019 The Chromium Authors
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -30,6 +30,13 @@
 
 /** @typedef {import('redux').AnyAction} AnyAction */
 
+
+const RESTRICT_VIEW_PREFIX = 'restrict-view-';
+const RESTRICT_EDIT_PREFIX = 'restrict-editissue-';
+const RESTRICT_COMMENT_PREFIX = 'restrict-addissuecomment-';
+const MIGRATED_BUGANIZER_ISSUE_PREFIXES = ['migrated-to-b-', 'copybara-migration-complete-', 'cob-migrated-to-b-'];
+const MIGRATED_LAUNCH_ISSUE_PREFIXES = ['migrated-to-launch-'];
+
 // Actions
 export const VIEW_ISSUE = 'VIEW_ISSUE';
 
@@ -490,12 +497,6 @@
 });
 
 // Selectors
-const RESTRICT_VIEW_PREFIX = 'restrict-view-';
-const RESTRICT_EDIT_PREFIX = 'restrict-editissue-';
-const RESTRICT_COMMENT_PREFIX = 'restrict-addissuecomment-';
-const MIGRATED_ISSUE_PREFIX = 'migrated-to-';
-const MIGRATED_BUGANIZER_ISSUE_PREFIX = 'migrated-to-b-';
-const MIGRATED_LAUNCH_ISSUE_PREFIX = 'migrated-to-launch-';
 
 /**
  * Selector to retrieve all normalized Issue data in the Redux store,
@@ -706,48 +707,60 @@
     },
 );
 
+/**
+ * Helper to find the issue ID for a migration label if one exists, otherwise
+ * returns undefined.
+ * @param {Array<LabelRef>} labelRefs
+ * @param {Array<string>} validPrefixes
+ * @return {string?} issue referenced in label or undefined if none found.
+ */
+function extractIdFromLabels(labelRefs, validPrefixes) {
+  // Assume that there's only one migrated-to-* label. Or at least drop any
+  // labels besides the first one.
+  const migrationLabel = labelRefs.find((labelRef) => validPrefixes.find(
+    (prefix) => labelRef.label.toLowerCase().startsWith(prefix)));
+
+  if (!migrationLabel) return undefined;
+
+  const {label} = migrationLabel;
+
+  const matchedPrefix = validPrefixes.find((prefix) => label.startsWith(prefix));
+
+  return migrationLabel.label.substring(matchedPrefix.length);
+}
+
 // Gets the Issue Tracker or Launch ID of a moved issue.
 export const migratedId = createSelector(
+  viewedIssue,
   labelRefs,
-  (labelRefs) => {
+  (issue, labelRefs) => {
+    if (issue && issue.migratedId) return issue.migratedId;
     if (!labelRefs) return '';
 
-    // Assume that there's only one migrated-to-* label. Or at least drop any
-    // labels besides the first one.
-    const migrationLabel = labelRefs.find((labelRef) => {
-      return labelRef.label.toLowerCase().startsWith(MIGRATED_ISSUE_PREFIX);
-    });
-    
-    if (migrationLabel) {
-      if (migrationLabel.label.toLowerCase().startsWith(MIGRATED_BUGANIZER_ISSUE_PREFIX)) {
-        return migrationLabel.label.substring(MIGRATED_BUGANIZER_ISSUE_PREFIX.length);
-      } else if (migrationLabel.label.toLowerCase().startsWith(MIGRATED_LAUNCH_ISSUE_PREFIX)) {
-        return migrationLabel.label.substring(MIGRATED_LAUNCH_ISSUE_PREFIX.length);
-      }
-    }
+    const launchIssue = extractIdFromLabels(labelRefs, MIGRATED_LAUNCH_ISSUE_PREFIXES);
+    if (launchIssue) return launchIssue;
+
+    const bIssue = extractIdFromLabels(labelRefs, MIGRATED_BUGANIZER_ISSUE_PREFIXES);
+    if (bIssue) return bIssue;
+
     return '';
   },
 );
 
 // Gets the Issue Migrated Type of a moved issue.
 export const migratedType = createSelector(
+  viewedIssue,
   labelRefs,
-  (labelRefs) => {
+  (issue, labelRefs) => {
+    if (issue && issue.migratedId) return migratedTypes.BUGANIZER_TYPE;
     if (!labelRefs) return migratedTypes.NONE;
 
-    // Assume that there's only one migrated-to-* label. Or at least drop any
-    // labels besides the first one.
-    const migrationLabel = labelRefs.find((labelRef) => {
-      return labelRef.label.toLowerCase().startsWith(MIGRATED_ISSUE_PREFIX);
-    });
+    const launchIssue = extractIdFromLabels(labelRefs, MIGRATED_LAUNCH_ISSUE_PREFIXES);
+    if (launchIssue) return migratedTypes.LAUNCH_TYPE;
 
-    if (migrationLabel) {
-      if (migrationLabel.label.toLowerCase().startsWith(MIGRATED_BUGANIZER_ISSUE_PREFIX)) {
-        return migratedTypes.BUGANIZER_TYPE;
-      } else if (migrationLabel.label.toLowerCase().startsWith(MIGRATED_LAUNCH_ISSUE_PREFIX)) {
-        return migratedTypes.LAUNCH_TYPE;
-      }
-    }
+    const bIssue = extractIdFromLabels(labelRefs, MIGRATED_BUGANIZER_ISSUE_PREFIXES);
+    if (bIssue) return migratedTypes.BUGANIZER_TYPE;
+
     return migratedTypes.NONE;
   },
 );