diff options
author | Fabio Ponciroli <ponch78@gmail.com> | 2023-10-20 16:57:36 +0100 |
---|---|---|
committer | Luca Milanesio <luca.milanesio@gmail.com> | 2023-10-26 21:12:14 +0000 |
commit | 8c845f9afc46ab2d8d99579d0dc03228bef39230 (patch) | |
tree | 9a7c680c47ab13bbfd2230e3ed02ab4817996e03 | |
parent | 21da56b7bfb376e090c6b236ec5fde8550964d03 (diff) |
Redirect comment links URIs without project
Change I4ccff233 introduced a regression in the comment links
URI without project.
A URI like `/c/test/+/1/comment/ff3303fd_8341647b/` would now
return a 404.
Handle it backend side the same way as any URIs without project.
Bug: Issue 306564466
Release-Notes: skip
Change-Id: Id06504eb88896fc52010a15e528f564d35d1918a
(cherry picked from commit 201e47b10fa90983171fe8819ea898048ea6223a)
3 files changed, 47 insertions, 2 deletions
diff --git a/java/com/google/gerrit/httpd/NumericChangeIdRedirectServlet.java b/java/com/google/gerrit/httpd/NumericChangeIdRedirectServlet.java index 77c53810ac..d9f1c09ee5 100644 --- a/java/com/google/gerrit/httpd/NumericChangeIdRedirectServlet.java +++ b/java/com/google/gerrit/httpd/NumericChangeIdRedirectServlet.java @@ -29,7 +29,17 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -/** Redirects {@code domain.tld/123} to {@code domain.tld/c/project/+/123}. */ +/** + * Redirects: + * + * <ul> + * <li>{@code domain.tld/123} to {@code domain.tld/c/project/+/123} + * <li/> + * <li>{@code domain.tld/123/comment/bc630c55_3e265b44} to {@code + * domain.tld/c/project/+/123/comment/bc630c55_3e265b44/} + * <li/> + * </ul> + */ @Singleton public class NumericChangeIdRedirectServlet extends HttpServlet { private static final long serialVersionUID = 1L; @@ -43,7 +53,11 @@ public class NumericChangeIdRedirectServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException { - String idString = req.getPathInfo(); + String uriPath = req.getPathInfo(); + // Check if we are processing a comment url, like "/c/1/comment/ff3303fd_8341647b/". + int commentIdx = uriPath.indexOf("/comment"); + String idString = commentIdx == -1 ? uriPath : uriPath.substring(0, commentIdx); + if (idString.endsWith("/")) { idString = idString.substring(0, idString.length() - 1); } @@ -64,6 +78,10 @@ public class NumericChangeIdRedirectServlet extends HttpServlet { } String path = PageLinks.toChange(changeResource.getProject(), changeResource.getChange().getId()); + if (commentIdx > -1) { + // path already contain a trailing /, hence we start from "commentIdx + 1" + path = path + uriPath.substring(commentIdx + 1); + } UrlModule.toGerrit(path, req, rsp); } } diff --git a/java/com/google/gerrit/httpd/UrlModule.java b/java/com/google/gerrit/httpd/UrlModule.java index 5a898a1189..aad6b57ad8 100644 --- a/java/com/google/gerrit/httpd/UrlModule.java +++ b/java/com/google/gerrit/httpd/UrlModule.java @@ -73,6 +73,7 @@ class UrlModule extends ServletModule { serveRegex("^/register$").with(registerScreen(false)); serveRegex("^/register/(.+)$").with(registerScreen(true)); serveRegex("^(?:/c)?/([1-9][0-9]*)/?$").with(NumericChangeIdRedirectServlet.class); + serveRegex("^(?:/c)?/([1-9][0-9]*)/comment/\\w+/?$").with(NumericChangeIdRedirectServlet.class); serveRegex("^/p/(.*)$").with(queryProjectNew()); serveRegex("^/r/(.+)/?$").with(DirectChangeByCommit.class); diff --git a/javatests/com/google/gerrit/acceptance/rest/RestApiServletIT.java b/javatests/com/google/gerrit/acceptance/rest/RestApiServletIT.java index 1143e893ba..0393f2b1c5 100644 --- a/javatests/com/google/gerrit/acceptance/rest/RestApiServletIT.java +++ b/javatests/com/google/gerrit/acceptance/rest/RestApiServletIT.java @@ -436,6 +436,32 @@ public class RestApiServletIT extends AbstractDaemonTest { } @Test + public void testCommentLinkWithPrefixRedirects() throws Exception { + int changeNumber = createChange().getChange().getId().get(); + String commentId = "ff3303fd_8341647b"; + + String redirectUri = + String.format("/c/%s/+/%d/comment/%s", project.get(), changeNumber, commentId); + + anonymousRestSession + .get(String.format("/c/%s/comment/%s", changeNumber, commentId)) + .assertTemporaryRedirect(redirectUri); + } + + @Test + public void testCommentLinkWithoutPrefixRedirects() throws Exception { + int changeNumber = createChange().getChange().getId().get(); + String commentId = "ff3303fd_8341647b"; + + String redirectUri = + String.format("/c/%s/+/%d/comment/%s", project.get(), changeNumber, commentId); + + anonymousRestSession + .get(String.format("/%s/comment/%s", changeNumber, commentId)) + .assertTemporaryRedirect(redirectUri); + } + + @Test public void testNumericChangeIdRedirectWithoutPrefix() throws Exception { int changeNumber = createChange().getChange().getId().get(); |