Search in sources :

Example 6 with BadRequestException

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

the class PutDraftComment method applyImpl.

@Override
protected Response<CommentInfo> applyImpl(BatchUpdate.Factory updateFactory, DraftCommentResource rsrc, DraftInput in) throws RestApiException, UpdateException, OrmException {
    if (in == null || in.message == null || in.message.trim().isEmpty()) {
        return delete.applyImpl(updateFactory, rsrc, null);
    } else if (in.id != null && !rsrc.getId().equals(in.id)) {
        throw new BadRequestException("id must match URL");
    } else if (in.line != null && in.line < 0) {
        throw new BadRequestException("line must be >= 0");
    } else if (in.line != null && in.range != null && in.line != in.range.endLine) {
        throw new BadRequestException("range endLine must be on the same line as the comment");
    }
    try (BatchUpdate bu = updateFactory.create(db.get(), rsrc.getChange().getProject(), rsrc.getControl().getUser(), TimeUtil.nowTs())) {
        Op op = new Op(rsrc.getComment().key, in);
        bu.addOp(rsrc.getChange().getId(), op);
        bu.execute();
        return Response.ok(commentJson.get().setFillAccounts(false).newCommentFormatter().format(op.comment));
    }
}
Also used : BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Example 7 with BadRequestException

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

the class FileContentUtil method getContent.

/**
   * Get the content of a file at a specific commit or one of it's parent commits.
   *
   * @param project A {@code Project} that this request refers to.
   * @param revstr An {@code ObjectId} specifying the commit.
   * @param path A string specifying the filepath.
   * @param parent A 1-based parent index to get the content from instead. Null if the content
   *     should be obtained from {@code revstr} instead.
   * @return Content of the file as {@code BinaryResult}.
   * @throws ResourceNotFoundException
   * @throws IOException
   */
public BinaryResult getContent(ProjectState project, ObjectId revstr, String path, @Nullable Integer parent) throws BadRequestException, ResourceNotFoundException, IOException {
    try (Repository repo = openRepository(project);
        RevWalk rw = new RevWalk(repo)) {
        if (parent != null) {
            RevCommit revCommit = rw.parseCommit(revstr);
            if (revCommit == null) {
                throw new ResourceNotFoundException("commit not found");
            }
            if (parent > revCommit.getParentCount()) {
                throw new BadRequestException("invalid parent");
            }
            revstr = rw.parseCommit(revstr).getParent(Integer.max(0, parent - 1)).toObjectId();
        }
        return getContent(repo, project, revstr, path);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 8 with BadRequestException

use of com.google.gerrit.extensions.restapi.BadRequestException 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 9 with BadRequestException

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

the class GetMergeList method apply.

@Override
public Response<List<CommitInfo>> apply(RevisionResource rsrc) throws BadRequestException, IOException {
    Project.NameKey p = rsrc.getChange().getProject();
    try (Repository repo = repoManager.openRepository(p);
        RevWalk rw = new RevWalk(repo)) {
        String rev = rsrc.getPatchSet().getRevision().get();
        RevCommit commit = rw.parseCommit(ObjectId.fromString(rev));
        rw.parseBody(commit);
        if (uninterestingParent < 1 || uninterestingParent > commit.getParentCount()) {
            throw new BadRequestException("No such parent: " + uninterestingParent);
        }
        if (commit.getParentCount() < 2) {
            return createResponse(rsrc, ImmutableList.<CommitInfo>of());
        }
        List<RevCommit> commits = MergeListBuilder.build(rw, commit, uninterestingParent);
        List<CommitInfo> result = new ArrayList<>(commits.size());
        ChangeJson changeJson = json.noOptions();
        for (RevCommit c : commits) {
            result.add(changeJson.toCommit(rsrc.getControl(), rw, c, addLinks, true));
        }
        return createResponse(rsrc, result);
    }
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) ArrayList(java.util.ArrayList) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) CommitInfo(com.google.gerrit.extensions.common.CommitInfo) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 10 with BadRequestException

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

the class PutOwner method apply.

@Override
public GroupInfo apply(GroupResource resource, Input input) throws ResourceNotFoundException, MethodNotAllowedException, AuthException, BadRequestException, UnprocessableEntityException, OrmException, IOException {
    AccountGroup group = resource.toAccountGroup();
    if (group == null) {
        throw new MethodNotAllowedException();
    } else if (!resource.getControl().isOwner()) {
        throw new AuthException("Not group owner");
    }
    if (input == null || Strings.isNullOrEmpty(input.owner)) {
        throw new BadRequestException("owner is required");
    }
    group = db.get().accountGroups().get(group.getId());
    if (group == null) {
        throw new ResourceNotFoundException();
    }
    GroupDescription.Basic owner = groupsCollection.parse(input.owner);
    if (!group.getOwnerGroupUUID().equals(owner.getGroupUUID())) {
        group.setOwnerGroupUUID(owner.getGroupUUID());
        db.get().accountGroups().update(Collections.singleton(group));
        groupCache.evict(group);
    }
    return json.format(owner);
}
Also used : GroupDescription(com.google.gerrit.common.data.GroupDescription) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Aggregations

BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)65 AuthException (com.google.gerrit.extensions.restapi.AuthException)22 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)21 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)15 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)13 Repository (org.eclipse.jgit.lib.Repository)13 Project (com.google.gerrit.reviewdb.client.Project)12 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)11 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)10 RevCommit (org.eclipse.jgit.revwalk.RevCommit)10 Account (com.google.gerrit.reviewdb.client.Account)9 Change (com.google.gerrit.reviewdb.client.Change)8 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)8 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)8 Map (java.util.Map)8 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)7 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)7 RevWalk (org.eclipse.jgit.revwalk.RevWalk)7 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)6