Search in sources :

Example 6 with BinaryResult

use of com.google.gerrit.extensions.restapi.BinaryResult in project gerrit by GerritCodeReview.

the class RobotCommentsIT method fixInvolvingTwoFilesCanBeApplied.

@Test
public void fixInvolvingTwoFilesCanBeApplied() throws Exception {
    assume().that(notesMigration.readChanges()).isTrue();
    FixReplacementInfo fixReplacementInfo1 = new FixReplacementInfo();
    fixReplacementInfo1.path = FILE_NAME;
    fixReplacementInfo1.range = createRange(2, 0, 3, 0);
    fixReplacementInfo1.replacement = "First modification\n";
    FixReplacementInfo fixReplacementInfo2 = new FixReplacementInfo();
    fixReplacementInfo2.path = FILE_NAME2;
    fixReplacementInfo2.range = createRange(1, 0, 2, 0);
    fixReplacementInfo2.replacement = "Different file modification\n";
    FixSuggestionInfo fixSuggestionInfo = createFixSuggestionInfo(fixReplacementInfo1, fixReplacementInfo2);
    withFixRobotCommentInput.fixSuggestions = ImmutableList.of(fixSuggestionInfo);
    addRobotComment(changeId, withFixRobotCommentInput);
    List<RobotCommentInfo> robotCommentInfos = getRobotComments();
    List<String> fixIds = getFixIds(robotCommentInfos);
    String fixId = Iterables.getOnlyElement(fixIds);
    gApi.changes().id(changeId).current().applyFix(fixId);
    Optional<BinaryResult> file = gApi.changes().id(changeId).edit().getFile(FILE_NAME);
    BinaryResultSubject.assertThat(file).value().asString().isEqualTo("First line\nFirst modification\nThird line\nFourth line\nFifth line\nSixth line\n" + "Seventh line\nEighth line\nNinth line\nTenth line\n");
    Optional<BinaryResult> file2 = gApi.changes().id(changeId).edit().getFile(FILE_NAME2);
    BinaryResultSubject.assertThat(file2).value().asString().isEqualTo("Different file modification\n2nd line\n3rd line\n");
}
Also used : FixReplacementInfo(com.google.gerrit.extensions.common.FixReplacementInfo) FixSuggestionInfo(com.google.gerrit.extensions.common.FixSuggestionInfo) RobotCommentInfo(com.google.gerrit.extensions.common.RobotCommentInfo) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 7 with BinaryResult

use of com.google.gerrit.extensions.restapi.BinaryResult in project gerrit by GerritCodeReview.

the class RobotCommentsIT method fixOnPreviousPatchSetWithExistingChangeEditCanBeApplied.

@Test
public void fixOnPreviousPatchSetWithExistingChangeEditCanBeApplied() throws Exception {
    assume().that(notesMigration.readChanges()).isTrue();
    // Create an empty change edit.
    gApi.changes().id(changeId).edit().create();
    fixReplacementInfo.path = FILE_NAME;
    fixReplacementInfo.replacement = "Modified content";
    fixReplacementInfo.range = createRange(3, 1, 3, 3);
    addRobotComment(changeId, withFixRobotCommentInput);
    List<RobotCommentInfo> robotCommentInfos = getRobotComments();
    // Remember patch set and add another one.
    String previousRevision = gApi.changes().id(changeId).get().currentRevision;
    amendChange(changeId);
    List<String> fixIds = getFixIds(robotCommentInfos);
    String fixId = Iterables.getOnlyElement(fixIds);
    EditInfo editInfo = gApi.changes().id(changeId).revision(previousRevision).applyFix(fixId);
    Optional<BinaryResult> file = gApi.changes().id(changeId).edit().getFile(FILE_NAME);
    BinaryResultSubject.assertThat(file).value().asString().isEqualTo("First line\nSecond line\nTModified contentrd line\nFourth line\nFifth line\n" + "Sixth line\nSeventh line\nEighth line\nNinth line\nTenth line\n");
    assertThat(editInfo).baseRevision().isEqualTo(previousRevision);
}
Also used : EditInfo(com.google.gerrit.extensions.common.EditInfo) RobotCommentInfo(com.google.gerrit.extensions.common.RobotCommentInfo) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 8 with BinaryResult

use of com.google.gerrit.extensions.restapi.BinaryResult in project gerrit by GerritCodeReview.

the class SubmitByMergeIfNecessaryIT method testPreviewSubmitTgz.

@Test
public void testPreviewSubmitTgz() throws Exception {
    Project.NameKey p1 = createProject("project-name");
    TestRepository<?> repo1 = cloneProject(p1);
    PushOneCommit.Result change1 = createChange(repo1, "master", "test", "a.txt", "1", "topic");
    approve(change1.getChangeId());
    // get a preview before submitting:
    File tempfile;
    try (BinaryResult request = submitPreview(change1.getChangeId(), "tgz")) {
        assertThat(request.getContentType()).isEqualTo("application/x-gzip");
        tempfile = File.createTempFile("test", null);
        request.writeTo(Files.newOutputStream(tempfile.toPath()));
    }
    InputStream is = new GZIPInputStream(Files.newInputStream(tempfile.toPath()));
    List<String> untarredFiles = new ArrayList<>();
    try (TarArchiveInputStream tarInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is)) {
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
            untarredFiles.add(entry.getName());
        }
    }
    assertThat(untarredFiles).containsExactly(name("project-name") + ".git");
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) GZIPInputStream(java.util.zip.GZIPInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) Project(com.google.gerrit.reviewdb.client.Project) File(java.io.File) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult) Test(org.junit.Test)

Example 9 with BinaryResult

use of com.google.gerrit.extensions.restapi.BinaryResult in project gerrit by GerritCodeReview.

the class SubmitByMergeIfNecessaryIT method submitChangesAcrossReposBlocked.

@Test
public void submitChangesAcrossReposBlocked() throws Exception {
    Project.NameKey p1 = createProject("project-where-we-submit");
    Project.NameKey p2 = createProject("project-impacted-via-topic");
    Project.NameKey p3 = createProject("project-impacted-indirectly-via-topic");
    TestRepository<?> repo1 = cloneProject(p1);
    TestRepository<?> repo2 = cloneProject(p2);
    TestRepository<?> repo3 = cloneProject(p3);
    RevCommit initialHead1 = getRemoteHead(p1, "master");
    RevCommit initialHead2 = getRemoteHead(p2, "master");
    RevCommit initialHead3 = getRemoteHead(p3, "master");
    PushOneCommit.Result change1a = createChange(repo1, "master", "An ancestor of the change we want to submit", "a.txt", "1", "dependent-topic");
    PushOneCommit.Result change1b = createChange(repo1, "master", "we're interested to submit this change", "a.txt", "2", "topic-to-submit");
    PushOneCommit.Result change2a = createChange(repo2, "master", "indirection level 2a", "a.txt", "1", "topic-indirect");
    PushOneCommit.Result change2b = createChange(repo2, "master", "should go in with first change", "a.txt", "2", "dependent-topic");
    PushOneCommit.Result change3 = createChange(repo3, "master", "indirection level 2b", "a.txt", "1", "topic-indirect");
    // Create a merge conflict for change3 which is only indirectly related
    // via topics.
    repo3.reset(initialHead3);
    PushOneCommit.Result change3Conflict = createChange(repo3, "master", "conflicting change", "a.txt", "2\n2", "conflicting-topic");
    submit(change3Conflict.getChangeId());
    RevCommit tipConflict = getRemoteLog(p3, "master").get(0);
    assertThat(tipConflict.getShortMessage()).isEqualTo(change3Conflict.getCommit().getShortMessage());
    approve(change1a.getChangeId());
    approve(change2a.getChangeId());
    approve(change2b.getChangeId());
    approve(change3.getChangeId());
    if (isSubmitWholeTopicEnabled()) {
        String msg = "Failed to submit 5 changes due to the following problems:\n" + "Change " + change3.getChange().getId() + ": Change could not be " + "merged due to a path conflict. Please rebase the change locally " + "and upload the rebased commit for review.";
        // Get a preview before submitting:
        try (BinaryResult r = submitPreview(change1b.getChangeId())) {
            // We cannot just use the ExpectedException infrastructure as provided
            // by AbstractDaemonTest, as then we'd stop early and not test the
            // actual submit.
            fail("expected failure");
        } catch (RestApiException e) {
            assertThat(e.getMessage()).isEqualTo(msg);
        }
        submitWithConflict(change1b.getChangeId(), msg);
    } else {
        submit(change1b.getChangeId());
    }
    RevCommit tip1 = getRemoteLog(p1, "master").get(0);
    RevCommit tip2 = getRemoteLog(p2, "master").get(0);
    RevCommit tip3 = getRemoteLog(p3, "master").get(0);
    if (isSubmitWholeTopicEnabled()) {
        assertThat(tip1.getShortMessage()).isEqualTo(initialHead1.getShortMessage());
        assertThat(tip2.getShortMessage()).isEqualTo(initialHead2.getShortMessage());
        assertThat(tip3.getShortMessage()).isEqualTo(change3Conflict.getCommit().getShortMessage());
        assertNoSubmitter(change1a.getChangeId(), 1);
        assertNoSubmitter(change2a.getChangeId(), 1);
        assertNoSubmitter(change2b.getChangeId(), 1);
        assertNoSubmitter(change3.getChangeId(), 1);
    } else {
        assertThat(tip1.getShortMessage()).isEqualTo(change1b.getCommit().getShortMessage());
        assertThat(tip2.getShortMessage()).isEqualTo(initialHead2.getShortMessage());
        assertThat(tip3.getShortMessage()).isEqualTo(change3Conflict.getCommit().getShortMessage());
        assertNoSubmitter(change2a.getChangeId(), 1);
        assertNoSubmitter(change2b.getChangeId(), 1);
        assertNoSubmitter(change3.getChangeId(), 1);
    }
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) RevCommit(org.eclipse.jgit.revwalk.RevCommit) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult) Test(org.junit.Test)

Example 10 with BinaryResult

use of com.google.gerrit.extensions.restapi.BinaryResult in project gerrit by GerritCodeReview.

the class ChangeEditApiImpl method getFile.

@Override
public Optional<BinaryResult> getFile(String filePath) throws RestApiException {
    try {
        ChangeEditResource changeEditResource = getChangeEditResource(filePath);
        Response<BinaryResult> fileResponse = changeEditsGet.apply(changeEditResource);
        return fileResponse.isNone() ? Optional.empty() : Optional.of(fileResponse.value());
    } catch (Exception e) {
        throw asRestApiException("Cannot retrieve file of change edit", e);
    }
}
Also used : ChangeEditResource(com.google.gerrit.server.change.ChangeEditResource) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) ApiUtil.asRestApiException(com.google.gerrit.server.api.ApiUtil.asRestApiException) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException) AuthException(com.google.gerrit.extensions.restapi.AuthException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult)

Aggregations

BinaryResult (com.google.gerrit.extensions.restapi.BinaryResult)23 Test (org.junit.Test)12 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)10 OutputStream (java.io.OutputStream)6 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)5 RobotCommentInfo (com.google.gerrit.extensions.common.RobotCommentInfo)5 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)5 IOException (java.io.IOException)5 FixReplacementInfo (com.google.gerrit.extensions.common.FixReplacementInfo)4 FixSuggestionInfo (com.google.gerrit.extensions.common.FixSuggestionInfo)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 CountingOutputStream (com.google.common.io.CountingOutputStream)3 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)3 Project (com.google.gerrit.reviewdb.client.Project)3 ChangeApi (com.google.gerrit.extensions.api.changes.ChangeApi)2 AuthException (com.google.gerrit.extensions.restapi.AuthException)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 IdString (com.google.gerrit.extensions.restapi.IdString)2 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)2