Revert "Remove frontend redirection of legacy URIs without project"

This reverts commit a4f6c029c20aa8241ca686700c6340afdbe5031d.

Reason for revert: chromium-review is still using legacy URIs

Google-Bug-Id: b/307509411
Change-Id: I15b7060384109087dc63c08708dccb7e3ef773a1
diff --git a/polygerrit-ui/app/elements/core/gr-router/gr-router.ts b/polygerrit-ui/app/elements/core/gr-router/gr-router.ts
index e0d4185..ac35264 100644
--- a/polygerrit-ui/app/elements/core/gr-router/gr-router.ts
+++ b/polygerrit-ui/app/elements/core/gr-router/gr-router.ts
@@ -196,6 +196,10 @@
 
   CHANGE_ID_QUERY: /^\/id\/(I[0-9a-f]{40})$/,
 
+  // Matches /c/<changeNum>/[*][/].
+  CHANGE_LEGACY: /^\/c\/(\d+)\/?(.*)$/,
+  CHANGE_NUMBER_LEGACY: /^\/(\d+)\/?/,
+
   // Matches
   // /c/<project>/+/<changeNum>/[<basePatchNum|edit>..][<patchNum|edit>].
   // TODO(kaspern): Migrate completely to project based URLs, with backwards
@@ -845,6 +849,12 @@
     );
 
     this.mapRoute(
+      RoutePattern.CHANGE_NUMBER_LEGACY,
+      'handleChangeNumberLegacyRoute',
+      ctx => this.handleChangeNumberLegacyRoute(ctx)
+    );
+
+    this.mapRoute(
       RoutePattern.DIFF_EDIT,
       'handleDiffEditRoute',
       ctx => this.handleDiffEditRoute(ctx),
@@ -874,6 +884,10 @@
       this.handleChangeRoute(ctx)
     );
 
+    this.mapRoute(RoutePattern.CHANGE_LEGACY, 'handleChangeLegacyRoute', ctx =>
+      this.handleChangeLegacyRoute(ctx)
+    );
+
     this.mapRoute(
       RoutePattern.AGREEMENTS,
       'handleAgreementsRoute',
@@ -1291,6 +1305,14 @@
     this.redirect(ctx.path.replace(LEGACY_QUERY_SUFFIX_PATTERN, ''));
   }
 
+  handleChangeNumberLegacyRoute(ctx: PageContext) {
+    this.redirect(
+      '/c/' +
+        ctx.params[0] +
+        (ctx.querystring.length > 0 ? `?${ctx.querystring}` : '')
+    );
+  }
+
   handleChangeRoute(ctx: PageContext) {
     // Parameter order is based on the regex group number matched.
     const changeNum = Number(ctx.params[1]) as NumericChangeId;
@@ -1442,6 +1464,26 @@
     this.changeViewModel.setState(state);
   }
 
+  handleChangeLegacyRoute(ctx: PageContext) {
+    const changeNum = Number(ctx.params[0]) as NumericChangeId;
+    if (!changeNum) {
+      this.show404();
+      return;
+    }
+    this.restApiService.getFromProjectLookup(changeNum).then(project => {
+      // Show a 404 and terminate if the lookup request failed. Attempting
+      // to redirect after failing to get the project loops infinitely.
+      if (!project) {
+        this.show404();
+        return;
+      }
+      this.redirect(
+        `/c/${project}/+/${changeNum}/${ctx.params[1]}` +
+          (ctx.querystring.length > 0 ? `?${ctx.querystring}` : '')
+      );
+    });
+  }
+
   handleLegacyLinenum(ctx: PageContext) {
     this.redirect(ctx.path.replace(LEGACY_LINENUM_PATTERN, '#$1'));
   }
diff --git a/polygerrit-ui/app/elements/core/gr-router/gr-router_test.ts b/polygerrit-ui/app/elements/core/gr-router/gr-router_test.ts
index c4fb259..234bf95 100644
--- a/polygerrit-ui/app/elements/core/gr-router/gr-router_test.ts
+++ b/polygerrit-ui/app/elements/core/gr-router/gr-router_test.ts
@@ -168,11 +168,13 @@
     const unauthenticatedHandlers = [
       'handleBranchListRoute',
       'handleChangeIdQueryRoute',
+      'handleChangeNumberLegacyRoute',
       'handleChangeRoute',
       'handleCommentRoute',
       'handleCommentsRoute',
       'handleDiffRoute',
       'handleDefaultRoute',
+      'handleChangeLegacyRoute',
       'handleDocumentationRedirectRoute',
       'handleDocumentationSearchRoute',
       'handleDocumentationSearchRedirectRoute',
@@ -851,6 +853,21 @@
     });
 
     suite('CHANGE* / DIFF*', () => {
+      test('CHANGE_NUMBER_LEGACY', async () => {
+        // CHANGE_NUMBER_LEGACY: /^\/(\d+)\/?/,
+        await checkRedirect('/12345', '/c/12345');
+      });
+
+      test('CHANGE_LEGACY', async () => {
+        // CHANGE_LEGACY: /^\/c\/(\d+)\/?(.*)$/,
+        stubRestApi('getFromProjectLookup').resolves('project' as RepoName);
+        await checkRedirect('/c/1234', '/c/project/+/1234/');
+        await checkRedirect(
+          '/c/1234/comment/6789',
+          '/c/project/+/1234/comment/6789'
+        );
+      });
+
       test('DIFF_LEGACY_LINENUM', async () => {
         await checkRedirect(
           '/c/1234/3..8/foo/bar@321',