com.google.gerrit.extensions.api.GerritApi Java Examples

The following examples show how to use com.google.gerrit.extensions.api.GerritApi. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GerritApiBuilder.java    From gerrit-code-review-plugin with Apache License 2.0 6 votes vote down vote up
public GerritApi build() {
  GerritApi gerritApi = null;
  if (verifyParameters()) {
    List<HttpClientBuilderExtension> extensions = new ArrayList<>();
    extensions.add(UserAgentClientBuilderExtension.INSTANCE);
    if (Boolean.TRUE.equals(insecureHttps)) {
      extensions.add(SSLNoVerifyCertificateManagerClientBuilderExtension.INSTANCE);
    }
    gerritApi =
        new GerritRestApiFactory()
            .create(
                new GerritAuthData.Basic(gerritApiUrl.toString(), username, password, true),
                extensions.toArray(new HttpClientBuilderExtension[0]));
  }
  return gerritApi;
}
 
Example #2
Source File: AbstractGerritSCMSource.java    From gerrit-code-review-plugin with Apache License 2.0 5 votes vote down vote up
private ProjectChanges getProjectChanges() throws IOException {
  if (projectChanges == null) {
    GerritURI gerritURI = getGerritURI();
    GerritApi gerritApi = createGerritApi(FakeTaskListener.INSTANCE, gerritURI);
    projectChanges = new ProjectChanges(gerritApi);
  }

  return projectChanges;
}
 
Example #3
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
@Test(enabled = false) // requires running Gerrit instance
public void testBasicRestCallToLocalhostProjectsQuery() throws Exception {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    GerritApi gerritClient = gerritRestApiFactory.create(new GerritAuthData.Basic("http://localhost:8080"));
    List<ProjectInfo> projects = gerritClient.projects().list().withLimit(1).withDescription(true).get();
    System.out.println(String.format("Got %s projects.", projects.size()));
    System.out.println(projects);
}
 
Example #4
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
@Test(enabled = false) // requires running Gerrit instance
public void testBasicRestCallToLocalhostProjects() throws Exception {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    GerritApi gerritClient = gerritRestApiFactory.create(new GerritAuthData.Basic("http://localhost:8080"));
    List<ProjectInfo> projects = gerritClient.projects().list().get();
    System.out.println(String.format("Got %s projects.", projects.size()));
    System.out.println(projects);
}
 
Example #5
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
@Test(enabled = false) // requires running Gerrit instance
public void testBasicRestCallToLocalhost() throws Exception {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    GerritApi gerritClient = gerritRestApiFactory.create(new GerritAuthData.Basic("http://localhost:8080"));
    List<ChangeInfo> changes = gerritClient.changes().query().get();
    System.out.println(String.format("Got %s changes.", changes.size()));
    System.out.println(changes);
}
 
Example #6
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testVersion() throws Exception {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    GerritApi gerritClient = gerritRestApiFactory.create(new GerritAuthData.Basic(jettyUrl));
    String version = gerritClient.config().server().getVersion();
    Truth.assertThat(version).isEqualTo("2.10");
}
 
Example #7
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Tests authentication with an invalid HTTP login (preemptive authentication is assumed). Status 401 expected.
 */
@Test
public void testInvalidUserAuth() throws Exception {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    GerritApi gerritClient = gerritRestApiFactory.create(new GerritAuthData.Basic(jettyUrl, "foox", "bar"));
    boolean catched = false;
    try {
        gerritClient.changes().query().get();
    } catch (HttpStatusException e) {
        catched = true;
        Truth.assertThat(e.getStatusCode()).isEqualTo(401);
        Truth.assertThat(e.getStatusText().toLowerCase()).contains("unauthorized");
    }
    Truth.assertThat(catched).isTrue();
}
 
Example #8
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Tests authentication with a login which us handled by HTTP auth (preemptive authentication is assumed)
 * (path: "/a/changes/" isn't mapped -> status 404).
 */
@Test
public void testUserAuth() throws Exception {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    GerritApi gerritClient = gerritRestApiFactory.create(new GerritAuthData.Basic(jettyUrl, "foo", "bar"));
    boolean catched = false;
    try {
        gerritClient.changes().query().get();
    } catch (HttpStatusException e) {
        catched = true;
        // 404 because this url does not provide a valid response (not set up in this test case)
        Truth.assertThat(e.getStatusCode()).isEqualTo(404);
    }
    Truth.assertThat(catched).isTrue();
}
 
Example #9
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = RestApiException.class)
public void testInvalidHost() throws Exception {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    GerritAuthData.Basic authData = new GerritAuthData.Basic("http://averyinvaliddomainforgerritresttest.com:8089");
    GerritApi gerritClient = gerritRestApiFactory.create(authData);
    gerritClient.changes().query().get();
}
 
Example #10
Source File: GerritCommentStep.java    From gerrit-code-review-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected Void run() throws Exception {
  GerritApi gerritApi =
      new GerritApiBuilder().stepContext(getContext()).requireAuthentication().build();
  if (gerritApi == null) {
    return null;
  }

  GerritChange change = new GerritChange(getContext());
  if (change.valid()) {
    listener
        .getLogger()
        .format(
            "Gerrit review change %d/%d %s=%d (%s)%n",
            change.getChangeId(), change.getRevision(), path, line, message);
    DraftInput draftInput = new DraftInput();
    draftInput.path = path;
    draftInput.line = line;
    draftInput.message = message;
    gerritApi
        .changes()
        .id(change.getChangeId())
        .revision(change.getRevision())
        .createDraft(draftInput);
  }
  return null;
}
 
Example #11
Source File: AbstractGerritSCMSource.java    From gerrit-code-review-plugin with Apache License 2.0 5 votes vote down vote up
private Changes.QueryRequest getOpenChanges(
    GerritApi gerritApi, String project, String changeQueryFilter)
    throws UnsupportedEncodingException {
  String query =
      "p:"
          + project
          + " status:open "
          + OPEN_CHANGES_FILTER
          + (changeQueryFilter == null ? "" : " " + changeQueryFilter);
  return gerritApi
      .changes()
      .query(URLEncoder.encode(query, StandardCharsets.UTF_8.name()))
      .withOption(ListChangesOption.CURRENT_REVISION);
}
 
Example #12
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetChanges() throws Exception {
    GerritApi gerritClient = getGerritApiWithJettyHost();
    List<ChangeInfo> changes = gerritClient.changes().query().get();
    Truth.assertThat(changes.size()).isEqualTo(3);
}
 
Example #13
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetSelfAccount() throws Exception {
    GerritApi gerritClient = getGerritApiWithJettyHost();
    AccountInfo accountInfo = gerritClient.accounts().self().get();
    Truth.assertThat(accountInfo.name).isEqualTo("John Doe");
}
 
Example #14
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions = HttpStatusException.class)
public void testGetInvalidAccount() throws Exception {
    GerritApi gerritClient = getGerritApiWithJettyHost();
    gerritClient.accounts().id("invalid").get();
}
 
Example #15
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetProjects() throws Exception {
    GerritApi gerritClient = getGerritApiWithJettyHost();
    List<ProjectInfo> projects = gerritClient.projects().list().get();
    Truth.assertThat(projects.size()).isEqualTo(3);
}
 
Example #16
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions = HttpStatusException.class)
public void testStarNotLoggedIn() throws Exception {
    GerritApi gerritClient = getGerritApiWithJettyHost();
    gerritClient.accounts().self().starChange("1");
}
 
Example #17
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions = HttpStatusException.class)
public void testUnstarNotLoggedIn() throws Exception {
    GerritApi gerritClient = getGerritApiWithJettyHost();
    gerritClient.accounts().self().unstarChange("1");
}
 
Example #18
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions = HttpStatusException.class)
public void testAbandonNotLoggedIn() throws Exception {
    GerritApi gerritClient = getGerritApiWithJettyHost();
    gerritClient.changes().id(1).abandon();
}
 
Example #19
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions = RestApiException.class)
public void testInvalidJson() throws Exception {
    GerritApi gerritClient = getGerritApiWithJettyHost();
    gerritClient.accounts().id("invalid_json").get();
}
 
Example #20
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions = RestApiException.class)
public void testNullJson() throws Exception {
    GerritApi gerritClient = getGerritApiWithJettyHost();
    gerritClient.accounts().id("null_json").get();
}
 
Example #21
Source File: GerritApiBuilderTest.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void testShouldReturnValidGerritApiWithoutCredentials() throws URISyntaxException {
  GerritApi restApi = getGerritApiBuilderWithUri().credentials(null, null).build();
  assertNotNull(restApi);
}
 
Example #22
Source File: ProjectChanges.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
ProjectChanges(GerritApi gerritApi) {
  this.gerritApi = gerritApi;
}
 
Example #23
Source File: GerritReviewStep.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
@Override
protected Void run() throws Exception {
  GerritApi gerritApi =
      new GerritApiBuilder().stepContext(getContext()).requireAuthentication().build();
  if (gerritApi == null) {
    return null;
  }

  GerritChange change = new GerritChange(getContext());
  if (change.valid()) {
    ReviewInput reviewInput = new ReviewInput().message(message);
    boolean notifyOwner = false;
    if (labels == null && label != null) {
      labels = Collections.singletonMap(label, score);
    }
    listener
        .getLogger()
        .format(
            "Gerrit review change %d/%d labels %s (%s)%n",
            change.getChangeId(), change.getRevision(), labels, message);
    if (labels != null) {
      for (Map.Entry<String, Integer> l : labels.entrySet()) {
        reviewInput.label(l.getKey(), l.getValue());
        if (l.getValue() < 0) {
          notifyOwner = true;
        }
      }
    }
    reviewInput.drafts = ReviewInput.DraftHandling.PUBLISH;
    reviewInput.tag = "autogenerated:jenkins";
    if (notifyOwner) {
      reviewInput.notify = NotifyHandling.OWNER;
    }
    gerritApi
        .changes()
        .id(change.getChangeId())
        .revision(change.getRevision())
        .review(reviewInput);
  }
  return null;
}
 
Example #24
Source File: AbstractGerritSCMSource.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
private GerritApi createGerritApi(@Nonnull TaskListener listener, GerritURI remoteUri)
    throws IOException {
  return setupGerritApiBuilder(listener, remoteUri).build();
}