Gerrit plug-in development case

preface

Recently, I took on the task of Gerrit plug-in development for some special reasons. I originally wanted to find a ready-made demo from the Internet for reference. As a result, I spent most of the time stunned and didn't find a relevant plug-in case. The relevant domestic blogs either stayed in Gerrit's construction tutorial or in the instructions for use. To sum up, they stayed in the stage of use, There are few self-made wheels, but the official reference documents have been found through google. There is a long way to go in the development of basic construction tools in China. Here is a summary of my process of developing Gerrit plug-in.

gerrit plug-in development process

1. Generate the basic development environment of the project with maven tools

2. According to whether the tools to be developed are strongly coupled with gerrit, choose extension development or plug-in development

3. Set up your own local Gerrit server (most of this information on the Internet, search by yourself)

4. First, implement a basic component on the Gerrit page

5. Further development according to their own needs

topMenu function implemented in pure java

1. The first step is to define and implement the top menu class

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. The second step is to bind the implementation class in the first step to the Module class

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. The third step is in 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>

Pure front-end implementation of Gerrit page button jump function

There are many realizable official components provided in the official document of gerrit. Here we provide the idea of front-end implementation to realize the basic components.

/**
 * @Desc Gerrit Plug in development
 * @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`
         Online editing: <a href$="[[realPath]]">MiText</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');
});

Reference documents

Keywords: git

Added by jeremy0 on Wed, 09 Feb 2022 01:11:42 +0200