2024-07-07 20:12:28 +02:00
|
|
|
From ca2df6d7f53441d443d42908e30bf60fbfc15392 Mon Sep 17 00:00:00 2001
|
2020-07-03 00:28:39 +02:00
|
|
|
From: Luke Granger-Brown <git@lukegb.com>
|
|
|
|
Date: Thu, 2 Jul 2020 23:03:02 +0100
|
2022-09-11 18:34:47 +02:00
|
|
|
Subject: [PATCH 3/3] Add titles to CLs over HTTP
|
2020-07-03 00:28:39 +02:00
|
|
|
|
|
|
|
---
|
2024-07-07 20:12:28 +02:00
|
|
|
.../gerrit/httpd/raw/IndexHtmlUtil.java | 12 +++-
|
2021-04-06 11:27:20 +02:00
|
|
|
.../google/gerrit/httpd/raw/IndexServlet.java | 8 ++-
|
2022-09-11 18:34:47 +02:00
|
|
|
.../google/gerrit/httpd/raw/StaticModule.java | 5 +-
|
2020-07-03 00:28:39 +02:00
|
|
|
.../gerrit/httpd/raw/TitleComputer.java | 67 +++++++++++++++++++
|
|
|
|
.../gerrit/httpd/raw/PolyGerritIndexHtml.soy | 4 +-
|
2024-07-07 20:12:28 +02:00
|
|
|
5 files changed, 88 insertions(+), 8 deletions(-)
|
2020-07-03 00:28:39 +02:00
|
|
|
create mode 100644 java/com/google/gerrit/httpd/raw/TitleComputer.java
|
|
|
|
|
2020-06-14 20:38:15 +02:00
|
|
|
diff --git a/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java b/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java
|
2024-07-07 20:12:28 +02:00
|
|
|
index a92dd18f04..f87c46d321 100644
|
2020-06-14 20:38:15 +02:00
|
|
|
--- a/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java
|
|
|
|
+++ b/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java
|
2022-11-05 03:18:41 +01:00
|
|
|
@@ -41,6 +41,7 @@ import java.util.Collections;
|
2020-06-14 20:38:15 +02:00
|
|
|
import java.util.HashMap;
|
2022-11-05 03:18:41 +01:00
|
|
|
import java.util.HashSet;
|
2020-06-14 20:38:15 +02:00
|
|
|
import java.util.Map;
|
|
|
|
+import java.util.Optional;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.function.Function;
|
2020-11-27 18:39:35 +01:00
|
|
|
|
2024-07-07 20:12:28 +02:00
|
|
|
@@ -63,13 +64,14 @@ public class IndexHtmlUtil {
|
2020-06-14 20:38:15 +02:00
|
|
|
String faviconPath,
|
|
|
|
Map<String, String[]> urlParameterMap,
|
|
|
|
Function<String, SanitizedContent> urlInScriptTagOrdainer,
|
|
|
|
- String requestedURL)
|
|
|
|
+ String requestedURL,
|
|
|
|
+ TitleComputer titleComputer)
|
|
|
|
throws URISyntaxException, RestApiException {
|
|
|
|
ImmutableMap.Builder<String, Object> data = ImmutableMap.builder();
|
|
|
|
data.putAll(
|
2020-11-27 18:39:35 +01:00
|
|
|
staticTemplateData(
|
|
|
|
canonicalURL, cdnPath, faviconPath, urlParameterMap, urlInScriptTagOrdainer))
|
2024-07-07 20:12:28 +02:00
|
|
|
- .putAll(dynamicTemplateData(gerritApi, requestedURL, canonicalURL));
|
|
|
|
+ .putAll(dynamicTemplateData(gerritApi, requestedURL, canonicalURL, titleComputer));
|
2022-11-05 03:18:41 +01:00
|
|
|
Set<String> enabledExperiments = new HashSet<>();
|
|
|
|
enabledExperiments.addAll(experimentFeatures.getEnabledExperimentFeatures());
|
|
|
|
// Add all experiments enabled through url
|
2024-07-07 20:12:28 +02:00
|
|
|
@@ -82,7 +84,7 @@ public class IndexHtmlUtil {
|
2020-06-14 20:38:15 +02:00
|
|
|
|
|
|
|
/** Returns dynamic parameters of {@code index.html}. */
|
2020-11-27 18:39:35 +01:00
|
|
|
public static ImmutableMap<String, Object> dynamicTemplateData(
|
2024-07-07 20:12:28 +02:00
|
|
|
- GerritApi gerritApi, String requestedURL, String canonicalURL)
|
|
|
|
+ GerritApi gerritApi, String requestedURL, String canonicalURL, TitleComputer titleComputer)
|
|
|
|
throws RestApiException, URISyntaxException {
|
2020-06-14 20:38:15 +02:00
|
|
|
ImmutableMap.Builder<String, Object> data = ImmutableMap.builder();
|
|
|
|
Map<String, SanitizedContent> initialData = new HashMap<>();
|
2024-07-07 20:12:28 +02:00
|
|
|
@@ -141,6 +143,10 @@ public class IndexHtmlUtil {
|
2020-06-14 20:38:15 +02:00
|
|
|
}
|
|
|
|
|
2022-09-11 18:34:47 +02:00
|
|
|
data.put("gerritInitialData", initialData);
|
|
|
|
+
|
2020-06-14 20:38:15 +02:00
|
|
|
+ Optional<String> title = titleComputer.computeTitle(requestedURL);
|
|
|
|
+ title.ifPresent(s -> data.put("title", s));
|
|
|
|
+
|
|
|
|
return data.build();
|
|
|
|
}
|
2022-09-11 18:34:47 +02:00
|
|
|
|
2020-06-14 20:38:15 +02:00
|
|
|
diff --git a/java/com/google/gerrit/httpd/raw/IndexServlet.java b/java/com/google/gerrit/httpd/raw/IndexServlet.java
|
2022-11-05 03:18:41 +01:00
|
|
|
index fcb821e5ae..e1464b992b 100644
|
2020-06-14 20:38:15 +02:00
|
|
|
--- a/java/com/google/gerrit/httpd/raw/IndexServlet.java
|
|
|
|
+++ b/java/com/google/gerrit/httpd/raw/IndexServlet.java
|
2022-09-11 18:34:47 +02:00
|
|
|
@@ -48,13 +48,15 @@ public class IndexServlet extends HttpServlet {
|
2021-04-06 11:27:20 +02:00
|
|
|
private final ExperimentFeatures experimentFeatures;
|
2020-06-14 20:38:15 +02:00
|
|
|
private final SoySauce soySauce;
|
|
|
|
private final Function<String, SanitizedContent> urlOrdainer;
|
|
|
|
+ private TitleComputer titleComputer;
|
|
|
|
|
|
|
|
IndexServlet(
|
|
|
|
@Nullable String canonicalUrl,
|
|
|
|
@Nullable String cdnPath,
|
|
|
|
@Nullable String faviconPath,
|
2021-04-06 11:27:20 +02:00
|
|
|
GerritApi gerritApi,
|
|
|
|
- ExperimentFeatures experimentFeatures) {
|
|
|
|
+ ExperimentFeatures experimentFeatures,
|
2020-06-14 20:38:15 +02:00
|
|
|
+ TitleComputer titleComputer) {
|
|
|
|
this.canonicalUrl = canonicalUrl;
|
|
|
|
this.cdnPath = cdnPath;
|
|
|
|
this.faviconPath = faviconPath;
|
2022-09-11 18:34:47 +02:00
|
|
|
@@ -69,6 +71,7 @@ public class IndexServlet extends HttpServlet {
|
2020-06-14 20:38:15 +02:00
|
|
|
(s) ->
|
|
|
|
UnsafeSanitizedContentOrdainer.ordainAsSafe(
|
|
|
|
s, SanitizedContent.ContentKind.TRUSTED_RESOURCE_URI);
|
|
|
|
+ this.titleComputer = titleComputer;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-09-11 18:34:47 +02:00
|
|
|
@@ -86,7 +89,8 @@ public class IndexServlet extends HttpServlet {
|
2021-04-06 11:27:20 +02:00
|
|
|
faviconPath,
|
|
|
|
parameterMap,
|
|
|
|
urlOrdainer,
|
2022-09-11 18:34:47 +02:00
|
|
|
- getRequestUrl(req));
|
|
|
|
+ getRequestUrl(req),
|
2022-11-05 03:18:41 +01:00
|
|
|
+ titleComputer);
|
2020-06-14 20:38:15 +02:00
|
|
|
renderer = soySauce.renderTemplate("com.google.gerrit.httpd.raw.Index").setData(templateData);
|
|
|
|
} catch (URISyntaxException | RestApiException e) {
|
|
|
|
throw new IOException(e);
|
|
|
|
diff --git a/java/com/google/gerrit/httpd/raw/StaticModule.java b/java/com/google/gerrit/httpd/raw/StaticModule.java
|
2024-07-07 20:12:28 +02:00
|
|
|
index b00294f73e..f1c1aae12c 100644
|
2020-06-14 20:38:15 +02:00
|
|
|
--- a/java/com/google/gerrit/httpd/raw/StaticModule.java
|
|
|
|
+++ b/java/com/google/gerrit/httpd/raw/StaticModule.java
|
2024-07-07 20:12:28 +02:00
|
|
|
@@ -224,10 +224,11 @@ public class StaticModule extends ServletModule {
|
2020-06-14 20:38:15 +02:00
|
|
|
@CanonicalWebUrl @Nullable String canonicalUrl,
|
|
|
|
@GerritServerConfig Config cfg,
|
2021-04-06 11:27:20 +02:00
|
|
|
GerritApi gerritApi,
|
|
|
|
- ExperimentFeatures experimentFeatures) {
|
|
|
|
+ ExperimentFeatures experimentFeatures,
|
2022-11-05 03:18:41 +01:00
|
|
|
+ TitleComputer titleComputer) {
|
2024-07-07 20:12:28 +02:00
|
|
|
String cdnPath = options.devCdn().orElseGet(() -> cfg.getString("gerrit", null, "cdnPath"));
|
2020-06-14 20:38:15 +02:00
|
|
|
String faviconPath = cfg.getString("gerrit", null, "faviconPath");
|
2021-04-06 11:27:20 +02:00
|
|
|
- return new IndexServlet(canonicalUrl, cdnPath, faviconPath, gerritApi, experimentFeatures);
|
2022-09-11 18:34:47 +02:00
|
|
|
+ return new IndexServlet(canonicalUrl, cdnPath, faviconPath, gerritApi, experimentFeatures, titleComputer);
|
2020-06-14 20:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Provides
|
|
|
|
diff --git a/java/com/google/gerrit/httpd/raw/TitleComputer.java b/java/com/google/gerrit/httpd/raw/TitleComputer.java
|
|
|
|
new file mode 100644
|
2020-07-03 00:28:39 +02:00
|
|
|
index 0000000000..8fd2053ad0
|
2020-06-14 20:38:15 +02:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/java/com/google/gerrit/httpd/raw/TitleComputer.java
|
|
|
|
@@ -0,0 +1,67 @@
|
|
|
|
+package com.google.gerrit.httpd.raw;
|
|
|
|
+
|
|
|
|
+import com.google.common.flogger.FluentLogger;
|
|
|
|
+import com.google.gerrit.entities.Change;
|
|
|
|
+import com.google.gerrit.extensions.restapi.ResourceConflictException;
|
|
|
|
+import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
|
|
|
+import com.google.gerrit.server.change.ChangeResource;
|
|
|
|
+import com.google.gerrit.server.permissions.PermissionBackendException;
|
|
|
|
+import com.google.gerrit.server.restapi.change.ChangesCollection;
|
|
|
|
+import com.google.inject.Inject;
|
|
|
|
+import com.google.inject.Provider;
|
|
|
|
+import com.google.inject.Singleton;
|
|
|
|
+
|
|
|
|
+import java.net.MalformedURLException;
|
|
|
|
+import java.net.URL;
|
|
|
|
+import java.util.Optional;
|
|
|
|
+import java.util.regex.Matcher;
|
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
+
|
|
|
|
+@Singleton
|
|
|
|
+public class TitleComputer {
|
|
|
|
+ private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
|
|
|
+
|
|
|
|
+ @Inject
|
|
|
|
+ public TitleComputer(Provider<ChangesCollection> changes) {
|
|
|
|
+ this.changes = changes;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Optional<String> computeTitle(String requestedURI) {
|
|
|
|
+ URL url = null;
|
|
|
|
+ try {
|
|
|
|
+ url = new URL(requestedURI);
|
|
|
|
+ } catch (MalformedURLException e) {
|
|
|
|
+ logger.atWarning().log("Failed to turn %s into a URL.", requestedURI);
|
|
|
|
+ return Optional.empty();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Try to turn this into a change.
|
|
|
|
+ Optional<Change.Id> changeId = tryExtractChange(url.getPath());
|
|
|
|
+ if (changeId.isPresent()) {
|
|
|
|
+ return titleFromChangeId(changeId.get());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return Optional.empty();
|
|
|
|
+ }
|
|
|
|
+
|
2020-07-03 00:28:39 +02:00
|
|
|
+ private static final Pattern extractChangeIdRegex = Pattern.compile("^/(?:c/.*/\\+/)?(?<changeId>[0-9]+)(?:/[0-9]+)?(?:/.*)?$");
|
2020-06-14 20:38:15 +02:00
|
|
|
+ private final Provider<ChangesCollection> changes;
|
|
|
|
+
|
|
|
|
+ private Optional<Change.Id> tryExtractChange(String path) {
|
|
|
|
+ Matcher m = extractChangeIdRegex.matcher(path);
|
|
|
|
+ if (!m.matches()) {
|
|
|
|
+ return Optional.empty();
|
|
|
|
+ }
|
|
|
|
+ return Change.Id.tryParse(m.group("changeId"));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private Optional<String> titleFromChangeId(Change.Id changeId) {
|
|
|
|
+ ChangesCollection changesCollection = changes.get();
|
|
|
|
+ try {
|
|
|
|
+ ChangeResource changeResource = changesCollection.parse(changeId);
|
|
|
|
+ return Optional.of(changeResource.getChange().getSubject());
|
|
|
|
+ } catch (ResourceConflictException | ResourceNotFoundException | PermissionBackendException e) {
|
|
|
|
+ return Optional.empty();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
diff --git a/resources/com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy b/resources/com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy
|
2024-07-07 20:12:28 +02:00
|
|
|
index 5ff1822cd9..81c3cdf0e1 100644
|
2020-06-14 20:38:15 +02:00
|
|
|
--- a/resources/com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy
|
|
|
|
+++ b/resources/com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy
|
2022-11-05 03:18:41 +01:00
|
|
|
@@ -33,10 +33,12 @@
|
2020-11-27 18:39:35 +01:00
|
|
|
{@param? defaultDashboardHex: ?}
|
|
|
|
{@param? dashboardQuery: ?}
|
2020-06-14 20:38:15 +02:00
|
|
|
{@param? userIsAuthenticated: ?}
|
|
|
|
+ {@param? title: ?}
|
|
|
|
<!DOCTYPE html>{\n}
|
|
|
|
<html lang="en">{\n}
|
|
|
|
<meta charset="utf-8">{\n}
|
|
|
|
- <meta name="description" content="Gerrit Code Review">{\n}
|
|
|
|
+ {if $title}<title>{$title} · Gerrit Code Review</title>{\n}{/if}
|
|
|
|
+ <meta name="description" content="{if $title}{$title} · {/if}Gerrit Code Review">{\n}
|
|
|
|
<meta name="referrer" content="never">{\n}
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">{\n}
|
|
|
|
|
2020-07-03 00:28:39 +02:00
|
|
|
--
|
2024-07-07 20:12:28 +02:00
|
|
|
2.45.1
|
2020-07-03 00:28:39 +02:00
|
|
|
|