Search in sources :

Example 1 with BinaryResult

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

the class PreviewSubmit method getBundles.

private BinaryResult getBundles(BatchUpdate.Factory updateFactory, RevisionResource rsrc, ArchiveFormat f) throws OrmException, RestApiException {
    ReviewDb db = dbProvider.get();
    ChangeControl control = rsrc.getControl();
    IdentifiedUser caller = control.getUser().asIdentifiedUser();
    Change change = rsrc.getChange();
    // Returned BinaryResult takes ownership and handles closing.
    @SuppressWarnings("resource") MergeOp op = mergeOpFactory.create(updateFactory);
    try {
        op.merge(db, change, caller, false, new SubmitInput(), true);
        BinaryResult bin = new SubmitPreviewResult(op, f, maxBundleSize);
        bin.disableGzip().setContentType(f.getMimeType()).setAttachmentName("submit-preview-" + change.getChangeId() + "." + format);
        return bin;
    } catch (OrmException | RestApiException | RuntimeException e) {
        op.close();
        throw e;
    }
}
Also used : MergeOp(com.google.gerrit.server.git.MergeOp) Change(com.google.gerrit.reviewdb.client.Change) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) SubmitInput(com.google.gerrit.extensions.api.changes.SubmitInput) OrmException(com.google.gwtorm.server.OrmException) ChangeControl(com.google.gerrit.server.project.ChangeControl) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult)

Example 2 with BinaryResult

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

the class FileContentUtil method zipBlob.

@SuppressWarnings("resource")
private BinaryResult zipBlob(final String path, final ObjectLoader obj, RevCommit commit, @Nullable final String suffix) {
    final String commitName = commit.getName();
    final long when = commit.getCommitTime() * 1000L;
    return new BinaryResult() {

        @Override
        public void writeTo(OutputStream os) throws IOException {
            try (ZipOutputStream zipOut = new ZipOutputStream(os)) {
                String decoration = randSuffix();
                if (!Strings.isNullOrEmpty(suffix)) {
                    decoration = suffix + '-' + decoration;
                }
                ZipEntry e = new ZipEntry(safeFileName(path, decoration));
                e.setComment(commitName + ":" + path);
                e.setSize(obj.getSize());
                e.setTime(when);
                zipOut.putNextEntry(e);
                obj.copyTo(zipOut);
                zipOut.closeEntry();
            }
        }
    }.setContentType(ZIP_TYPE).setAttachmentName(safeFileName(path, suffix) + ".zip").disableGzip();
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult)

Example 3 with BinaryResult

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

the class GetArchive method apply.

@Override
public BinaryResult apply(RevisionResource rsrc) throws BadRequestException, IOException, MethodNotAllowedException {
    if (Strings.isNullOrEmpty(format)) {
        throw new BadRequestException("format is not specified");
    }
    final ArchiveFormat f = allowedFormats.extensions.get("." + format);
    if (f == null) {
        throw new BadRequestException("unknown archive format");
    }
    if (f == ArchiveFormat.ZIP) {
        throw new MethodNotAllowedException("zip format is disabled");
    }
    boolean close = true;
    final Repository repo = repoManager.openRepository(rsrc.getControl().getProject().getNameKey());
    try {
        final RevCommit commit;
        String name;
        try (RevWalk rw = new RevWalk(repo)) {
            commit = rw.parseCommit(ObjectId.fromString(rsrc.getPatchSet().getRevision().get()));
            name = name(f, rw, commit);
        }
        BinaryResult bin = new BinaryResult() {

            @Override
            public void writeTo(OutputStream out) throws IOException {
                try {
                    new ArchiveCommand(repo).setFormat(f.name()).setTree(commit.getTree()).setOutputStream(out).call();
                } catch (GitAPIException e) {
                    throw new IOException(e);
                }
            }

            @Override
            public void close() throws IOException {
                repo.close();
            }
        };
        bin.disableGzip().setContentType(f.getMimeType()).setAttachmentName(name);
        close = false;
        return bin;
    } finally {
        if (close) {
            repo.close();
        }
    }
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ArchiveCommand(org.eclipse.jgit.api.ArchiveCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Repository(org.eclipse.jgit.lib.Repository) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult)

Example 4 with BinaryResult

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

the class RevisionIT method patchWithPath.

@Test
public void patchWithPath() throws Exception {
    PushOneCommit.Result r = createChange();
    ChangeApi changeApi = gApi.changes().id(r.getChangeId());
    BinaryResult bin = changeApi.revision(r.getCommit().name()).patch(FILE_NAME);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    bin.writeTo(os);
    String res = new String(os.toByteArray(), UTF_8);
    assertThat(res).isEqualTo(PATCH_FILE_ONLY);
    exception.expect(ResourceNotFoundException.class);
    exception.expectMessage("File not found: nonexistent-file.");
    changeApi.revision(r.getCommit().name()).patch("nonexistent-file");
}
Also used : ChangeApi(com.google.gerrit.extensions.api.changes.ChangeApi) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 5 with BinaryResult

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

the class RevisionIT method patch.

@Test
public void patch() throws Exception {
    PushOneCommit.Result r = createChange();
    ChangeApi changeApi = gApi.changes().id(r.getChangeId());
    BinaryResult bin = changeApi.revision(r.getCommit().name()).patch();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    bin.writeTo(os);
    String res = new String(os.toByteArray(), UTF_8);
    ChangeInfo change = changeApi.get();
    RevisionInfo rev = change.revisions.get(change.currentRevision);
    DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
    String date = df.format(rev.commit.author.date);
    assertThat(res).isEqualTo(String.format(PATCH, r.getCommit().name(), date, r.getChangeId()));
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) RevisionInfo(com.google.gerrit.extensions.common.RevisionInfo) ChangeApi(com.google.gerrit.extensions.api.changes.ChangeApi) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleDateFormat(java.text.SimpleDateFormat) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

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