gerrit-rest-java-client

repository·master·Indexed 41 days ago

https://github.com/uwolfer/gerrit-rest-java-client

A type-safe Java implementation of the Gerrit Code Review Tool REST API. It implements the com.google.gerrit.extensions.api.GerritApi interface and provides tools to interact with Gerrit services, including change management via FileApi, CommentApi, and ChangeMessageApi, server configuration through the Server interface, and plugin management via the Plugins interface. Supports multiple authentication methods including HTTP basic, digest, and LDAP.

Tokens
5.2K
Snippets
27
Records
37
Agent score
51%

What's inside gerrit-rest-java-client

  1. Install gerrit-rest-java-client via Maven

    master

    Add the following dependency to your pom.xml to include the Gerrit REST API client in your Java project.

    <dependency>
        <groupId>com.urswolfer.gerrit.client.rest</groupId>
        <artifactId>gerrit-rest-java-client</artifactId>
        <version>0.9.8</version>
    </dependency>
  2. Use the GerritApi to interact with Gerrit

    master

    The library implements the com.google.gerrit.extensions.api.GerritApi interface. To use it, instantiate a GerritRestApiFactory, provide authentication data via GerritAuthData.Basic, and then create the GerritApi instance.

    Supported authentication methods include HTTP basic, HTTP digest, LDAP with form, and HTTP password from Gerrit settings.

    Note: If an interface is not yet implemented, the library will throw a com.google.gerrit.extensions.restapi.NotImplementedException.

    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    // Use only the base URL
    GerritAuthData.Basic authData = new GerritAuthData.Basic("http://localhost:8080");
    // Or provide full credentials: authData = new GerritAuthData.Basic("https://example.com/gerrit", "user", "password");
    
    GerritApi gerritApi = gerritRestApiFactory.create(authData);
    
    // Example: Querying changes
    List<ChangeInfo> changes = gerritApi.changes().query("status:merged").withLimit(10).get();
  3. List server experiments using ListExperimentsRequest

    master

    To list available experiments on the Gerrit server, use listExperiments(). This returns a ListExperimentsRequest object which follows a builder-like pattern to filter results.

    Use the .enabledOnly() method on the request object to filter the results to only include experiments that are currently active/enabled. Finally, call .get() to execute the request and retrieve an ImmutableMap<String, ExperimentInfo>.

    // Example: List only enabled experiments
    ImmutableMap<String, ExperimentInfo> experiments = server.listExperiments()
        .enabledOnly()
        .get();
  4. Use FlowApi to manage change flows

    master

    The FlowApi interface provides methods to interact with Gerrit REST endpoints related to change flows. It allows you to retrieve information about a specific flow or delete an existing flow.

    Methods:

    • get(): Retrieves the FlowInfo for the current flow. Throws RestApiException if the request fails.
    • delete(): Deletes the current flow. Throws RestApiException if the request fails.
    // Example usage of FlowApi
    FlowApi flowApi = // ... obtained from the client
    
    // Get flow information
    FlowInfo info = flowApi.get();
    
    // Delete the flow
    flowApi.delete();
  5. Suggest reviewers or CCs with BranchApi

    master

    The BranchApi provides methods to suggest reviewers for changes related to the branch:

    • suggestReviewers(): Returns a SuggestedReviewersRequest to find potential reviewers.
    • suggestReviewers(String query): Returns a request filtered by a specific query string.
    • suggestCcs(String query): Returns a request specifically for finding potential CCs (Copy-Condition) using a query string.
    // Suggest reviewers based on a query
    SuggestedReviewersRequest request = branchApi.suggestReviewers("user@example.com");
    
    // Suggest CCs based on a query
    SuggestedReviewersRequest ccRequest = branchApi.suggestCcs("project-team");
  6. Manage project submit requirements with SubmitRequirementApi

    master

    The SubmitRequirementApi interface provides methods to perform CRUD (Create, Read, Update, Delete) operations on Gerrit project submit requirements.

    Methods:

    • create(SubmitRequirementInput input): Creates a new submit requirement using the provided input. Returns the SubmitRequirementApi instance (allowing for chaining) or the created object depending on implementation context.
    • get(): Retrieves the existing submit requirement information as a SubmitRequirementInfo object.
    • update(SubmitRequirementInput input): Updates the existing submit requirement with the provided input. Returns the updated SubmitRequirementInfo.
    • delete(): Removes the existing submit requirement.

    All methods throw RestApiException if the operation fails.

    // Example usage pattern for SubmitRequirementApi
    SubmitRequirementApi api = // ... obtain instance via client
    
    // Create
    api.create(new SubmitRequirementInput().setName("My Requirement"));
    
    // Get
    SubmitRequirementInfo info = api.get();
    
    // Update
    api.update(new SubmitRequirementInput().setName("Updated Name"));
    
    // Delete
    api.delete();
  7. Remove an attention set from a change using AttentionSetApi

    master

    The AttentionSetApi interface provides methods for managing the attention set of a Gerrit change. To remove an entry from the attention set, use the remove(AttentionSetInput input) method. This method may throw a RestApiException if the operation fails.

    AttentionSetApi attentionSetApi;
    // ... initialization ...
    attentionSetApi.remove(input);
  8. Manage files within a change using FileApi

    master

    The FileApi interface provides methods to interact with specific files within a Gerrit change. You can retrieve file content, generate diffs, check blame information, and update the review status of a file.

    Key Operations

    • Retrieve Content: Use content() to get the file's binary content.
    • Generate Diffs:
      • diff(): Diff against the revision's parent.
      • diff(String base): Diff against a specific revision ID.
      • diff(int parent): Diff against a specific 1-based parent number.
      • diffRequest(): Returns a DiffRequest for advanced configuration.
    • Blame Information: Use blameRequest() to get a BlameRequest object for line-level history.
    • Update Review Status: Use setReviewed(boolean reviewed) to mark a file as reviewed or not.
  9. Configure diffs using DiffRequest

    master

    The DiffRequest class allows you to customize how diff information is retrieved. It follows a builder-like pattern where you configure options and then call .get() to execute the request.

    Configuration Options

    • withBase(String base): Sets the revision ID to use as the diff base.
    • withIntraline(boolean intraline): Configures whether to show intra-line changes.
    • withWhitespace(Whitespace whitespace): Sets whitespace handling preferences.
    • withParent(int parent): Sets a 1-based parent number to diff against.
    • withContext(int context): (Deprecated) Used for setting context lines.

    Execution

    Call .get() on the DiffRequest instance to retrieve the DiffInfo.

    DiffInfo info = fileApi.diffRequest()
        .withBase("some-revision-id")
        .withIntraline(true)
        .withWhitespace(Whitespace.IGNORE)
        .get();
  10. Manage project commits with CommitApi

    master

    The CommitApi interface provides methods to retrieve information about a specific commit, perform cherry-picks, and inspect file changes. All methods throw a RestApiException if the operation fails.

    // Example usage of CommitApi methods
    CommitInfo info = commitApi.get();
    Map<String, FileInfo> files = commitApi.files(1);
    Map<String, FileInfo> diff = commitApi.diffFiles("base-sha1-40-chars", true);
    ChangeApi newChange = commitApi.cherryPick(cherryPickInput);