编译Gerrit2.16版本download与auth插件

发布于 2023年12月20日

1.编译Gerrit插件

在gerrit官网中介绍了有三种编译插件方式

官网链接:https://gerrit-review.googlesource.com/Documentation/dev-build-plugins.html

  • Maven driven
  • Bazel tree driven
  • Bazel standalone

maven编译、bazel树编译、bazel独立编译

如果插件根目录中的这两个两个文件都存在:BUILD、pom.xml

该插件可以使用 Bazel 和 Maven 构建。

1.1 Maven driven build

如果插件包含pom.xml文件,则可以像平常一样使用 Maven 构建它:

mvn clean package
1.2 Bazel standalone

bazel独立构建,如果插件中包含WORKSPACE文件则可以直接进行编译

bazel build <插件名称>
1.3 Bazel tree driven

只能从 Gerrit 树中构建,意思是需要将插件的源码克隆到gerrit/plugins中,进行编译

git clone --recursive https://gerrit.googlesource.com/gerrit
cd gerrit/plugins
git clone https://gerrit.googlesource.com/plugins/download-commands
bazel build download-commands

2.编译download-commands-2.16版本

download-commands是需要进行Bazel tree driven构建的

2.16版本则需要bazel老版本,这边使用bazel-4.0.0版本

2.1 下载bazel容器

直接启动bazel容器,如果没有镜像则直接下载

docker run -dit --name bazel -u root --interactive --entrypoint=/bin/bash gcr.io/bazel-public/bazel:4.0.0
2.2 进入容器内
docker exec -it bazel bash
###克隆gerrit源码
git clone --recursive https://gerrit.googlesource.com/gerrit
###切换分支到2.16版本
cd gerrit && git checkout remotes/origin/stable-2.16
###删除原有的download-commands插件源码
rm -rf plugins/download-commands
###克隆download-commands源码
cd plugins && git clone https://gerrit.googlesource.com/plugins/download-commands && 
cd download-commands && git checkout remotes/origin/stable-2.16
###编译插件
bazel build download-commands

编译完成后会在gerrit/bazel-bin/plugins/download-commands/下出现download-commands的jar包

3.修改HTTP clone命令中用户名称带有的@符号

由于使用的用户名称是邮箱,则会在http clone仓库的时候显示两个@符号

git clone "http://zhangsan@qq.com@10.30.4.54:8200/a/TEST"
###这样是clone不下来的,如果需要使用http clone,则需要将用户名称中的@符号改为%40

上面的教程中已经写了如何编译,这个则是将http clone url更改

###clone下来download-commands的代码是这样的
BUILD  LICENSE  src
cd gerrit/plugins/download-commands/src/main/java/com/googlesource/gerrit/plugins/download/schem
e
###修改HttpScheme.java文件
vim HttpScheme.java ###修改这一段
....
....
  public String getUrl(String project) {
    if (!isEnabled() || !userProvider.get().isIdentifiedUser()) {
      return null;
    }

    final StringBuilder r = new StringBuilder();
    if (gitHttpUrl != null) {
      r.append(gitHttpUrl);
    } else if (canonicalWebUrl != null) {
      String base = canonicalWebUrl;
      int p = base.indexOf("://");
      int s = base.indexOf('/', p + 3);
      if (s < 0) {
        s = base.length();
      }
      String host = base.substring(p + 3, s);
      r.append(base.substring(0, p + 3));
      if (userProvider.get().getUserName().isPresent()) {
        r.append(userProvider.get().getUserName().get());
        r.append("@");
      }
      r.append(host);
      r.append(base.substring(s));
      r.append("a/");
    } else {
      return null;
    }
    r.append(project);
    return r.toString();
  }
....
....
###修改为下面的
...
...
  public String getUrl(String project) {
    if (!isEnabled() || !userProvider.get().isIdentifiedUser()) {
      return null;
    }

    final StringBuilder r = new StringBuilder();
    if (gitHttpUrl != null) {
      r.append(gitHttpUrl);
    } else if (canonicalWebUrl != null) {
      String base = canonicalWebUrl;
      int p = base.indexOf("://");
      int s = base.indexOf('/', p + 3);
      if (s < 0) {
        s = base.length();
      }
      String host = base.substring(p + 3, s);
      r.append(base.substring(0, p + 3));
      if (userProvider.get().getUserName().isPresent()) {
        String userName = userProvider.get().getUserName().get();
	userName = userName.replace("@", "%40");
	r.append(userName);
        r.append("@");
      }
      r.append(host);
      r.append(base.substring(s));
      r.append("a/");
    } else {
      return null;
    }
    r.append(project);
    return r.toString();
  }
...
...

之后进行编译即可

4.删除auth插件内keycloak auth路径

新版本的Keycloak默认请求地址是不带/auth的,如果对接gerrit的时候可以将路径中的auth去掉。

还是使用bazel-4.0.0版本

4.1.bazel独立编译
进入容器内部进行clone gerrit oauth插件
git clone https://gerrit.googlesource.com/plugins/oauth
切换分支到符合gerrit版本的,否则版本不兼容
git branch -a
git checkout remotes/origin/stable-2.16
修改KeycloakApi.java文件,将其中的auth路径去掉
vim src/main/java/com/googlesource/gerrit/plugins/oauth/KeycloakApi.java
在oauth目录中进行编译
命令:bazel build oauth
编译完成会在bazel-bin路径下出现oauth.jar文件
然后将其放置在gerrit plugins下,文件权限要注意,之后重启gerrit