diff options
Diffstat (limited to 'third_party/gerrit')
-rw-r--r-- | third_party/gerrit/0001-Syntax-highlight-nix.patch | 37 | ||||
-rw-r--r-- | third_party/gerrit/0002-Syntax-highlight-rules.pl.patch | 37 | ||||
-rw-r--r-- | third_party/gerrit/0003-Add-titles-to-CLs-over-HTTP.patch | 215 | ||||
-rw-r--r-- | third_party/gerrit/default.nix | 158 | ||||
-rw-r--r-- | third_party/gerrit/detzip.go | 97 |
5 files changed, 544 insertions, 0 deletions
diff --git a/third_party/gerrit/0001-Syntax-highlight-nix.patch b/third_party/gerrit/0001-Syntax-highlight-nix.patch new file mode 100644 index 000000000000..bdc3fd3b5510 --- /dev/null +++ b/third_party/gerrit/0001-Syntax-highlight-nix.patch @@ -0,0 +1,37 @@ +From 084e4f92fb58f7cd85303ba602fb3c40133c8fcc Mon Sep 17 00:00:00 2001 +From: Luke Granger-Brown <git@lukegb.com> +Date: Thu, 2 Jul 2020 23:02:32 +0100 +Subject: [PATCH 1/3] Syntax highlight nix + +--- + .../app/embed/diff/gr-syntax-layer/gr-syntax-layer-worker.ts | 1 + + resources/com/google/gerrit/server/mime/mime-types.properties | 1 + + 2 files changed, 2 insertions(+) + +diff --git a/polygerrit-ui/app/embed/diff/gr-syntax-layer/gr-syntax-layer-worker.ts b/polygerrit-ui/app/embed/diff/gr-syntax-layer/gr-syntax-layer-worker.ts +index a9f88bdd81..385249f280 100644 +--- a/polygerrit-ui/app/embed/diff/gr-syntax-layer/gr-syntax-layer-worker.ts ++++ b/polygerrit-ui/app/embed/diff/gr-syntax-layer/gr-syntax-layer-worker.ts +@@ -93,6 +93,7 @@ const LANGUAGE_MAP = new Map<string, string>([ + ['text/x-vhdl', 'vhdl'], + ['text/x-yaml', 'yaml'], + ['text/vbscript', 'vbscript'], ++ ['text/x-nix', 'nix'], + ]); + + const CLASS_PREFIX = 'gr-diff gr-syntax gr-syntax-'; +diff --git a/resources/com/google/gerrit/server/mime/mime-types.properties b/resources/com/google/gerrit/server/mime/mime-types.properties +index 2f9561ba2e..739818ec05 100644 +--- a/resources/com/google/gerrit/server/mime/mime-types.properties ++++ b/resources/com/google/gerrit/server/mime/mime-types.properties +@@ -149,6 +149,7 @@ mscin = text/x-mscgen + msgenny = text/x-msgenny + nb = text/x-mathematica + nginx.conf = text/x-nginx-conf ++nix = text/x-nix + nsh = text/x-nsis + nsi = text/x-nsis + nt = text/n-triples +-- +2.37.3 + diff --git a/third_party/gerrit/0002-Syntax-highlight-rules.pl.patch b/third_party/gerrit/0002-Syntax-highlight-rules.pl.patch new file mode 100644 index 000000000000..4b91e2c3541f --- /dev/null +++ b/third_party/gerrit/0002-Syntax-highlight-rules.pl.patch @@ -0,0 +1,37 @@ +From aedf8ac8fa5113843bcd83ff85e2d9f3bffdb16c Mon Sep 17 00:00:00 2001 +From: Luke Granger-Brown <git@lukegb.com> +Date: Thu, 2 Jul 2020 23:02:43 +0100 +Subject: [PATCH 2/3] Syntax highlight rules.pl + +--- + .../app/embed/diff/gr-syntax-layer/gr-syntax-layer-worker.ts | 1 + + resources/com/google/gerrit/server/mime/mime-types.properties | 1 + + 2 files changed, 2 insertions(+) + +diff --git a/polygerrit-ui/app/embed/diff/gr-syntax-layer/gr-syntax-layer-worker.ts b/polygerrit-ui/app/embed/diff/gr-syntax-layer/gr-syntax-layer-worker.ts +index 385249f280..7cb3068494 100644 +--- a/polygerrit-ui/app/embed/diff/gr-syntax-layer/gr-syntax-layer-worker.ts ++++ b/polygerrit-ui/app/embed/diff/gr-syntax-layer/gr-syntax-layer-worker.ts +@@ -68,6 +68,7 @@ const LANGUAGE_MAP = new Map<string, string>([ + ['text/x-perl', 'perl'], + ['text/x-pgsql', 'pgsql'], // postgresql + ['text/x-php', 'php'], ++ ['text/x-prolog', 'prolog'], + ['text/x-properties', 'properties'], + ['text/x-protobuf', 'protobuf'], + ['text/x-puppet', 'puppet'], +diff --git a/resources/com/google/gerrit/server/mime/mime-types.properties b/resources/com/google/gerrit/server/mime/mime-types.properties +index 739818ec05..58eb727bf9 100644 +--- a/resources/com/google/gerrit/server/mime/mime-types.properties ++++ b/resources/com/google/gerrit/server/mime/mime-types.properties +@@ -200,6 +200,7 @@ rq = application/sparql-query + rs = text/x-rustsrc + rss = application/xml + rst = text/x-rst ++rules.pl = text/x-prolog + README.md = text/x-gfm + s = text/x-gas + sas = text/x-sas +-- +2.37.3 + diff --git a/third_party/gerrit/0003-Add-titles-to-CLs-over-HTTP.patch b/third_party/gerrit/0003-Add-titles-to-CLs-over-HTTP.patch new file mode 100644 index 000000000000..c4edee3a40c3 --- /dev/null +++ b/third_party/gerrit/0003-Add-titles-to-CLs-over-HTTP.patch @@ -0,0 +1,215 @@ +From f49c50ca9a84ca374b7bd91c171bbea0457f2c7a Mon Sep 17 00:00:00 2001 +From: Luke Granger-Brown <git@lukegb.com> +Date: Thu, 2 Jul 2020 23:03:02 +0100 +Subject: [PATCH 3/3] Add titles to CLs over HTTP + +--- + .../gerrit/httpd/raw/IndexHtmlUtil.java | 13 +++- + .../google/gerrit/httpd/raw/IndexServlet.java | 8 ++- + .../google/gerrit/httpd/raw/StaticModule.java | 5 +- + .../gerrit/httpd/raw/TitleComputer.java | 67 +++++++++++++++++++ + .../gerrit/httpd/raw/PolyGerritIndexHtml.soy | 4 +- + 5 files changed, 89 insertions(+), 8 deletions(-) + create mode 100644 java/com/google/gerrit/httpd/raw/TitleComputer.java + +diff --git a/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java b/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java +index 72bfe40c3b..439bd73b44 100644 +--- a/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java ++++ b/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java +@@ -41,6 +41,7 @@ import java.util.Collections; + import java.util.HashMap; + import java.util.HashSet; + import java.util.Map; ++import java.util.Optional; + import java.util.Set; + import java.util.function.Function; + +@@ -62,13 +63,14 @@ public class IndexHtmlUtil { + 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( + staticTemplateData( + canonicalURL, cdnPath, faviconPath, urlParameterMap, urlInScriptTagOrdainer)) +- .putAll(dynamicTemplateData(gerritApi, requestedURL)); ++ .putAll(dynamicTemplateData(gerritApi, requestedURL, titleComputer)); + Set<String> enabledExperiments = new HashSet<>(); + enabledExperiments.addAll(experimentFeatures.getEnabledExperimentFeatures()); + // Add all experiments enabled through url +@@ -81,7 +83,8 @@ public class IndexHtmlUtil { + + /** Returns dynamic parameters of {@code index.html}. */ + public static ImmutableMap<String, Object> dynamicTemplateData( +- GerritApi gerritApi, String requestedURL) throws RestApiException, URISyntaxException { ++ GerritApi gerritApi, String requestedURL, TitleComputer titleComputer) ++ throws RestApiException, URISyntaxException { + ImmutableMap.Builder<String, Object> data = ImmutableMap.builder(); + Map<String, SanitizedContent> initialData = new HashMap<>(); + Server serverApi = gerritApi.config().server(); +@@ -129,6 +132,10 @@ public class IndexHtmlUtil { + } + + data.put("gerritInitialData", initialData); ++ ++ Optional<String> title = titleComputer.computeTitle(requestedURL); ++ title.ifPresent(s -> data.put("title", s)); ++ + return data.build(); + } + +diff --git a/java/com/google/gerrit/httpd/raw/IndexServlet.java b/java/com/google/gerrit/httpd/raw/IndexServlet.java +index fcb821e5ae..e1464b992b 100644 +--- a/java/com/google/gerrit/httpd/raw/IndexServlet.java ++++ b/java/com/google/gerrit/httpd/raw/IndexServlet.java +@@ -48,13 +48,15 @@ public class IndexServlet extends HttpServlet { + private final ExperimentFeatures experimentFeatures; + private final SoySauce soySauce; + private final Function<String, SanitizedContent> urlOrdainer; ++ private TitleComputer titleComputer; + + IndexServlet( + @Nullable String canonicalUrl, + @Nullable String cdnPath, + @Nullable String faviconPath, + GerritApi gerritApi, +- ExperimentFeatures experimentFeatures) { ++ ExperimentFeatures experimentFeatures, ++ TitleComputer titleComputer) { + this.canonicalUrl = canonicalUrl; + this.cdnPath = cdnPath; + this.faviconPath = faviconPath; +@@ -69,6 +71,7 @@ public class IndexServlet extends HttpServlet { + (s) -> + UnsafeSanitizedContentOrdainer.ordainAsSafe( + s, SanitizedContent.ContentKind.TRUSTED_RESOURCE_URI); ++ this.titleComputer = titleComputer; + } + + @Override +@@ -86,7 +89,8 @@ public class IndexServlet extends HttpServlet { + faviconPath, + parameterMap, + urlOrdainer, +- getRequestUrl(req)); ++ getRequestUrl(req), ++ titleComputer); + 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 +index 15dcf42e0e..9f56bf33ce 100644 +--- a/java/com/google/gerrit/httpd/raw/StaticModule.java ++++ b/java/com/google/gerrit/httpd/raw/StaticModule.java +@@ -241,10 +241,11 @@ public class StaticModule extends ServletModule { + @CanonicalWebUrl @Nullable String canonicalUrl, + @GerritServerConfig Config cfg, + GerritApi gerritApi, +- ExperimentFeatures experimentFeatures) { ++ ExperimentFeatures experimentFeatures, ++ TitleComputer titleComputer) { + String cdnPath = options.devCdn().orElse(cfg.getString("gerrit", null, "cdnPath")); + String faviconPath = cfg.getString("gerrit", null, "faviconPath"); +- return new IndexServlet(canonicalUrl, cdnPath, faviconPath, gerritApi, experimentFeatures); ++ return new IndexServlet(canonicalUrl, cdnPath, faviconPath, gerritApi, experimentFeatures, titleComputer); + } + + @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 +index 0000000000..8fd2053ad0 +--- /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(); ++ } ++ ++ private static final Pattern extractChangeIdRegex = Pattern.compile("^/(?:c/.*/\\+/)?(?<changeId>[0-9]+)(?:/[0-9]+)?(?:/.*)?$"); ++ 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 +index dbfef44dfe..347ee75aab 100644 +--- a/resources/com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy ++++ b/resources/com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy +@@ -33,10 +33,12 @@ + {@param? defaultDashboardHex: ?} + {@param? dashboardQuery: ?} + {@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} + +-- +2.37.3 + diff --git a/third_party/gerrit/default.nix b/third_party/gerrit/default.nix new file mode 100644 index 000000000000..b228e600fdc9 --- /dev/null +++ b/third_party/gerrit/default.nix @@ -0,0 +1,158 @@ +{ depot, pkgs, ... }: + +let + bazelRunScript = pkgs.writeShellScriptBin "bazel-run" '' + yarn config set cache-folder "$bazelOut/external/yarn_cache" + export HOME="$bazelOut/external/home" + mkdir -p "$bazelOut/external/home" + exec /bin/bazel "$@" + ''; + bazelTop = pkgs.buildFHSUserEnv { + name = "bazel"; + targetPkgs = pkgs: [ + (pkgs.bazel_5.override { enableNixHacks = true; }) + pkgs.jdk11_headless + pkgs.zlib + pkgs.python3 + pkgs.curl + pkgs.nodejs + pkgs.yarn + pkgs.git + bazelRunScript + ]; + runScript = "/bin/bazel-run"; + }; + bazel = bazelTop // { override = x: bazelTop; }; + version = "3.7.0-rc4"; +in +pkgs.lib.makeOverridable pkgs.buildBazelPackage { + pname = "gerrit"; + inherit version; + + src = pkgs.fetchgit { + url = "https://gerrit.googlesource.com/gerrit"; + rev = "3e445c7833c4acf49f1171fe4c82ceb32e93c780"; + branchName = "v${version}"; + sha256 = "sha256:002aw2bfifyla66v8khyiq4m9qj6ahs6r1dzb5kjk8xqpf6c6q9p"; + fetchSubmodules = true; + }; + + patches = [ + ./0001-Syntax-highlight-nix.patch + ./0002-Syntax-highlight-rules.pl.patch + ./0003-Add-titles-to-CLs-over-HTTP.patch + ]; + + bazelTargets = [ "release" "api-skip-javadoc" ]; + inherit bazel; + + bazelFlags = [ + "--repository_cache=" + "--disk_cache=" + ]; + + removeRulesCC = false; + fetchConfigured = true; + + fetchAttrs = { + sha256 = "sha256:1a31bkl723dyd906h0m54j2mnvgs82j3xr85ca07g35vncmh3lm2"; + preBuild = '' + rm .bazelversion + ''; + + installPhase = '' + runHook preInstall + + # Remove all built in external workspaces, Bazel will recreate them when building + rm -rf $bazelOut/external/{bazel_tools,\@bazel_tools.marker} + rm -rf $bazelOut/external/{embedded_jdk,\@embedded_jdk.marker} + rm -rf $bazelOut/external/{local_config_cc,\@local_config_cc.marker} + rm -rf $bazelOut/external/{local_*,\@local_*.marker} + + # Clear markers + find $bazelOut/external -name '@*\.marker' -exec sh -c 'echo > {}' \; + + # Remove all vcs files + rm -rf $(find $bazelOut/external -type d -name .git) + rm -rf $(find $bazelOut/external -type d -name .svn) + rm -rf $(find $bazelOut/external -type d -name .hg) + + # Removing top-level symlinks along with their markers. + # This is needed because they sometimes point to temporary paths (?). + # For example, in Tensorflow-gpu build: + # platforms -> NIX_BUILD_TOP/tmp/install/35282f5123611afa742331368e9ae529/_embedded_binaries/platforms + find $bazelOut/external -maxdepth 1 -type l | while read symlink; do + name="$(basename "$symlink")" + rm -rf "$symlink" "$bazelOut/external/@$name.marker" + done + + # Patching symlinks to remove build directory reference + find $bazelOut/external -type l | while read symlink; do + new_target="$(readlink "$symlink" | sed "s,$NIX_BUILD_TOP,NIX_BUILD_TOP,")" + rm "$symlink" + ln -sf "$new_target" "$symlink" + done + + echo '${bazel.name}' > $bazelOut/external/.nix-bazel-version + + # Gerrit fixups: + # Normalize permissions on .yarn-{tarball,metadata} files + find $bazelOut/external/yarn_cache \( -name .yarn-tarball.tgz -or -name .yarn-metadata.json \) -exec chmod 644 {} + + + mkdir $bazelOut/_bits/ + find . -name node_modules -prune -print | while read d; do + echo "$d" "$(dirname $d)" + mkdir -p $bazelOut/_bits/$(dirname $d) + cp -R "$d" "$bazelOut/_bits/$(dirname $d)/node_modules" + done + + (cd $bazelOut/ && tar czf $out --sort=name --mtime='@1' --owner=0 --group=0 --numeric-owner external/ _bits/) + + runHook postInstall + ''; + }; + + buildAttrs = { + preConfigure = '' + rm .bazelversion + + cp -R $bazelOut/_bits/* ./ + ''; + postPatch = '' + # Disable all errorprone checks, since we might be using a different version. + sed -i \ + -e '/-Xep:/d' \ + -e '/-XepExcludedPaths:/a "-XepDisableAllChecks",' \ + tools/BUILD + ''; + installPhase = '' + mkdir -p "$out"/webapps/ "$out"/share/api/ + cp bazel-bin/release.war "$out"/webapps/gerrit-${version}.war + unzip bazel-bin/api-skip-javadoc.zip -d "$out"/share/api + ''; + + nativeBuildInputs = with pkgs; [ + unzip + ]; + }; + + passthru = { + # A list of plugins that are part of the gerrit.war file. + # Use `java -jar gerrit.war ls | grep -Po '(?<=plugins/)[^.]+' | sed -e 's,^,",' -e 's,$,",' | sort` to generate that list. + plugins = [ + "codemirror-editor" + "commit-message-length-validator" + "delete-project" + "download-commands" + "gitiles" + "hooks" + "plugin-manager" + "replication" + "reviewnotes" + "singleusergroup" + "webhooks" + ]; + }; + + meta.ci.targets = [ "deps" ]; +} diff --git a/third_party/gerrit/detzip.go b/third_party/gerrit/detzip.go new file mode 100644 index 000000000000..511c18ecfe17 --- /dev/null +++ b/third_party/gerrit/detzip.go @@ -0,0 +1,97 @@ +package main + +import ( + "archive/zip" + "flag" + "fmt" + "io" + "log" + "os" + "path/filepath" + "sort" + "strings" +) + +var ( + exclude = flag.String("exclude", "", "comma-separated list of filenames to exclude (in any directory)") +) + +func init() { + flag.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s [zip file] [directory]:\n", os.Args[0]) + flag.PrintDefaults() + } +} + +func listToMap(ss []string) map[string]bool { + m := make(map[string]bool) + for _, s := range ss { + m[s] = true + } + return m +} + +func main() { + flag.Parse() + if flag.NArg() != 2 { + flag.Usage() + os.Exit(1) + } + + outPath := flag.Arg(0) + dirPath := flag.Arg(1) + + excludeFiles := listToMap(strings.Split(*exclude, ",")) + + // Aggregate all files first. + var files []string + filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { + return nil + } + if excludeFiles[info.Name()] { + return nil + } + files = append(files, path) + return nil + }) + + // Create zip + outW, err := os.Create(outPath) + if err != nil { + log.Fatalf("Create(%q): %v", outPath, err) + } + + zipW := zip.NewWriter(outW) + + // Output files in alphabetical order + sort.Strings(files) + for _, f := range files { + fw, err := zipW.CreateHeader(&zip.FileHeader{ + Name: f, + Method: zip.Store, + }) + if err != nil { + log.Fatalf("creating %q in zip: %v", f, err) + } + + ff, err := os.Open(f) + if err != nil { + log.Fatalf("opening %q: %v", f, err) + } + if _, err := io.Copy(fw, ff); err != nil { + log.Fatalf("copying %q to zip: %v", f, err) + } + ff.Close() + } + + if err := zipW.Close(); err != nil { + log.Fatalf("writing ZIP central directory: %v", err) + } + if err := outW.Close(); err != nil { + log.Fatalf("closing ZIP file: %v", err) + } +} |