前言
最近因为一些特殊的原因接了个gerrit插件开发的任务,本来想着从网上找个现成的demo来借鉴下,结果花了大半天的时间愣是没有找到一个相关的插件案例,国内的相关博客不是停留在Gerrit的搭建教程上,就是使用的说明上,总结起来就是停留在使用的阶段,自我造轮子的几乎没有,还是通过google找到了官方的参考文档,基础的构建工具开发上国内确实还有很长的路要长。在此提供下我开发Gerrit插件的过程总结。
gerrit插件开发流程
1、借助maven工具生成项目基本开发环境
2、根据要开发的工具是否与gerrit强耦合选用扩展开发还是插件开发
3、搭建自己的本地Gerrit服务器(网上这方面资料居多,自己搜索)
4、首先在Gerrit页面上实现一个基础的组件
5、根据自己的需求进一步进行开发
纯java实现topMenu功能
1、第一步编写实现TopMenu的实现类,并定义菜单按钮
import com.google.common.collect.Lists;
import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.extensions.client.MenuItem;
import com.google.gerrit.extensions.webui.TopMenu;
import com.google.inject.Inject;
import java.util.List;
public class HelloTopMenu implements TopMenu {
private final List<MenuEntry> menuEntries;
@Inject
public HelloTopMenu(@PluginName String pluginName) {
String baseUrl = "/plugins/" + pluginName + "/";
List<MenuItem> menuItems = Lists.newArrayListWithCapacity(2);
menuItems.add(new MenuItem("Greeting", "#/x/" + pluginName + "/", ""));
menuItems.add(new MenuItem("Documentation", baseUrl));
menuEntries = Lists.newArrayListWithCapacity(2);
menuEntries.add(new MenuEntry("MITEXTEDIT", menuItems));
menuEntries.add(
new MenuEntry(
"Projects",
Lists.newArrayList(
new MenuItem("Browse Repositories", "https://gerrit.googlesource.com/"))));
}
@Override
public List<MenuEntry> getEntries() {
return menuEntries;
}
}
2、第二步将第一步中实现类绑定到Module类中
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.extensions.webui.TopMenu;
import com.google.inject.AbstractModule;
class Module extends AbstractModule {
@Override
protected void configure() {
//DynamicSet.bind(binder(), ProjectWebLink.class).to(HelloWeblink.class);
DynamicSet.bind(binder(), TopMenu.class).to(HelloTopMenu.class);
}
}
3、第三步在pom.xml中进行相关的配置
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.googlesource.gerrit.plugins.testplugin</groupId>
<artifactId>testplugin</artifactId>
<packaging>jar</packaging>
<version>v3.2.3</version>
<name>CodeEdit</name>
<properties>
<Gerrit-ApiType>plugin</Gerrit-ApiType>
<Gerrit-ApiVersion>3.2.3</Gerrit-ApiVersion>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifestEntries>
<Gerrit-PluginName>CodeEdit</Gerrit-PluginName>
<Gerrit-Module>com.googlesource.gerrit.plugins.testplugin.Module</Gerrit-Module>
<!-- <Gerrit-SshModule>com.googlesource.gerrit.plugins.testplugin.SshModule</Gerrit-SshModule>-->
<!-- <Gerrit-HttpModule>com.googlesource.gerrit.plugins.testplugin.HttpModule</Gerrit-HttpModule>-->
<Implementation-Vendor>Gerrit Code Review</Implementation-Vendor>
<Implementation-URL>http://code.google.com/p/gerrit/</Implementation-URL>
<Implementation-Title>${Gerrit-ApiType} ${project.artifactId}</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
<Gerrit-ApiType>${Gerrit-ApiType}</Gerrit-ApiType>
<Gerrit-ApiVersion>${Gerrit-ApiVersion}</Gerrit-ApiVersion>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.gerrit</groupId>
<artifactId>gerrit-${Gerrit-ApiType}-api</artifactId>
<version>${Gerrit-ApiVersion}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
纯前端实现Gerrit页面按钮跳转功能
gerrit 官方文档上提供的可实现的官方组件是很多的,这里提供从前端实现的思想来实现基本的组件。
/**
* @Desc Gerrit插件开发
* @Auth CoderString
*/
class SomeCiModule extends Polymer.Element {
static get is() {
return "some-ci-module";
}
static get properties() {
return {
realPath: String,
};
}
static get template() {
return Polymer.html`
在线编辑: <a href$="[[realPath]]">插件名称</a>
`;
}
connectedCallback() {
super.connectedCallback();
var url= window.location.href;
var prefix = "https://xxx/flaskcode/gerrit?url=";
var path = prefix + url;
this.realPath = `${path}`;
}
}
customElements.define(SomeCiModule.is, SomeCiModule);
Gerrit.install(plugin => {
plugin.registerCustomComponent('change-view-integration', 'some-ci-module');
});