Search in sources :

Example 51 with ResourceNotFoundException

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

the class ListTags method get.

public TagInfo get(ProjectResource resource, IdString id) throws ResourceNotFoundException, IOException {
    try (Repository repo = getRepository(resource.getNameKey());
        RevWalk rw = new RevWalk(repo)) {
        String tagName = id.get();
        if (!tagName.startsWith(Constants.R_TAGS)) {
            tagName = Constants.R_TAGS + tagName;
        }
        Ref ref = repo.getRefDatabase().exactRef(tagName);
        ProjectControl control = resource.getControl();
        if (ref != null && !visibleTags(control, repo, ImmutableMap.of(ref.getName(), ref)).isEmpty()) {
            return createTagInfo(permissionBackend.user(control.getUser()).project(resource.getNameKey()).ref(ref.getName()), ref, rw);
        }
    }
    throw new ResourceNotFoundException(id);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) IdString(com.google.gerrit.extensions.restapi.IdString) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 52 with ResourceNotFoundException

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

the class DashboardsCollection method parse.

private DashboardResource parse(ProjectControl ctl, String ref, String path, ProjectControl myCtl) throws ResourceNotFoundException, IOException, AmbiguousObjectException, IncorrectObjectTypeException, ConfigInvalidException {
    String id = ref + ":" + path;
    if (!ref.startsWith(REFS_DASHBOARDS)) {
        ref = REFS_DASHBOARDS + ref;
    }
    if (!Repository.isValidRefName(ref) || !ctl.controlForRef(ref).isVisible()) {
        throw new ResourceNotFoundException(id);
    }
    try (Repository git = gitManager.openRepository(ctl.getProject().getNameKey())) {
        ObjectId objId = git.resolve(ref + ":" + path);
        if (objId == null) {
            throw new ResourceNotFoundException(id);
        }
        BlobBasedConfig cfg = new BlobBasedConfig(null, git, objId);
        return new DashboardResource(myCtl, ref, path, cfg, false);
    } catch (RepositoryNotFoundException e) {
        throw new ResourceNotFoundException(id);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) IdString(com.google.gerrit.extensions.restapi.IdString) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) BlobBasedConfig(org.eclipse.jgit.lib.BlobBasedConfig)

Example 53 with ResourceNotFoundException

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

the class CreateMergePatchSet method applyImpl.

@Override
protected Response<ChangeInfo> applyImpl(BatchUpdate.Factory updateFactory, ChangeResource rsrc, MergePatchSetInput in) throws OrmException, IOException, InvalidChangeOperationException, RestApiException, UpdateException, PermissionBackendException {
    rsrc.permissions().database(db).check(ChangePermission.ADD_PATCH_SET);
    MergeInput merge = in.merge;
    if (merge == null || Strings.isNullOrEmpty(merge.source)) {
        throw new BadRequestException("merge.source must be non-empty");
    }
    ChangeControl ctl = rsrc.getControl();
    PatchSet ps = psUtil.current(db.get(), ctl.getNotes());
    ProjectControl projectControl = ctl.getProjectControl();
    Change change = ctl.getChange();
    Project.NameKey project = change.getProject();
    Branch.NameKey dest = change.getDest();
    try (Repository git = gitManager.openRepository(project);
        ObjectInserter oi = git.newObjectInserter();
        ObjectReader reader = oi.newReader();
        RevWalk rw = new RevWalk(reader)) {
        RevCommit sourceCommit = MergeUtil.resolveCommit(git, rw, merge.source);
        if (!projectControl.canReadCommit(db.get(), git, sourceCommit)) {
            throw new ResourceNotFoundException("cannot find source commit: " + merge.source + " to merge.");
        }
        RevCommit currentPsCommit = rw.parseCommit(ObjectId.fromString(ps.getRevision().get()));
        Timestamp now = TimeUtil.nowTs();
        IdentifiedUser me = user.get().asIdentifiedUser();
        PersonIdent author = me.newCommitterIdent(now, serverTimeZone);
        RevCommit newCommit = createMergeCommit(in, projectControl, dest, git, oi, rw, currentPsCommit, sourceCommit, author, ObjectId.fromString(change.getKey().get().substring(1)));
        PatchSet.Id nextPsId = ChangeUtil.nextPatchSetId(ps.getId());
        PatchSetInserter psInserter = patchSetInserterFactory.create(ctl, nextPsId, newCommit);
        try (BatchUpdate bu = updateFactory.create(db.get(), project, me, now)) {
            bu.setRepository(git, rw, oi);
            bu.addOp(ctl.getId(), psInserter.setMessage("Uploaded patch set " + nextPsId.get() + ".").setDraft(ps.isDraft()).setNotify(NotifyHandling.NONE).setCheckAddPatchSetPermission(false));
            bu.execute();
        }
        ChangeJson json = jsonFactory.create(ListChangesOption.CURRENT_REVISION);
        return Response.ok(json.format(psInserter.getChange()));
    }
}
Also used : MergeInput(com.google.gerrit.extensions.common.MergeInput) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) RevWalk(org.eclipse.jgit.revwalk.RevWalk) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ProjectControl(com.google.gerrit.server.project.ProjectControl) Timestamp(java.sql.Timestamp) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) ChangeControl(com.google.gerrit.server.project.ChangeControl) Branch(com.google.gerrit.reviewdb.client.Branch) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 54 with ResourceNotFoundException

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

the class CreateMergePatchSet method createMergeCommit.

private RevCommit createMergeCommit(MergePatchSetInput in, ProjectControl projectControl, Branch.NameKey dest, Repository git, ObjectInserter oi, RevWalk rw, RevCommit currentPsCommit, RevCommit sourceCommit, PersonIdent author, ObjectId changeId) throws ResourceNotFoundException, MergeIdenticalTreeException, MergeConflictException, IOException {
    ObjectId parentCommit;
    if (in.inheritParent) {
        // inherit first parent from previous patch set
        parentCommit = currentPsCommit.getParent(0);
    } else {
        // get the current branch tip of destination branch
        Ref destRef = git.getRefDatabase().exactRef(dest.get());
        if (destRef != null) {
            parentCommit = destRef.getObjectId();
        } else {
            throw new ResourceNotFoundException("cannot find destination branch");
        }
    }
    RevCommit mergeTip = rw.parseCommit(parentCommit);
    String commitMsg;
    if (Strings.emptyToNull(in.subject) != null) {
        commitMsg = ChangeIdUtil.insertId(in.subject, changeId);
    } else {
        // reuse previous patch set commit message
        commitMsg = currentPsCommit.getFullMessage();
    }
    String mergeStrategy = MoreObjects.firstNonNull(Strings.emptyToNull(in.merge.strategy), mergeUtilFactory.create(projectControl.getProjectState()).mergeStrategyName());
    return MergeUtil.createMergeCommit(oi, git.getConfig(), mergeTip, sourceCommit, mergeStrategy, author, commitMsg, rw);
}
Also used : Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 55 with ResourceNotFoundException

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

the class DeleteReviewerOp method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws AuthException, ResourceNotFoundException, OrmException {
    Account.Id reviewerId = reviewer.getId();
    if (!approvalsUtil.getReviewers(ctx.getDb(), ctx.getNotes()).all().contains(reviewerId)) {
        throw new ResourceNotFoundException();
    }
    currChange = ctx.getChange();
    currPs = psUtil.current(ctx.getDb(), ctx.getNotes());
    LabelTypes labelTypes = ctx.getControl().getLabelTypes();
    // removing a reviewer will remove all her votes
    for (LabelType lt : labelTypes.getLabelTypes()) {
        newApprovals.put(lt.getName(), (short) 0);
    }
    StringBuilder msg = new StringBuilder();
    msg.append("Removed reviewer " + reviewer.getFullName());
    StringBuilder removedVotesMsg = new StringBuilder();
    removedVotesMsg.append(" with the following votes:\n\n");
    List<PatchSetApproval> del = new ArrayList<>();
    boolean votesRemoved = false;
    for (PatchSetApproval a : approvals(ctx, reviewerId)) {
        if (ctx.getControl().canRemoveReviewer(a)) {
            del.add(a);
            if (a.getPatchSetId().equals(currPs.getId()) && a.getValue() != 0) {
                oldApprovals.put(a.getLabel(), a.getValue());
                removedVotesMsg.append("* ").append(a.getLabel()).append(formatLabelValue(a.getValue())).append(" by ").append(userFactory.create(a.getAccountId()).getNameEmail()).append("\n");
                votesRemoved = true;
            }
        } else {
            throw new AuthException("delete reviewer not permitted");
        }
    }
    if (votesRemoved) {
        msg.append(removedVotesMsg);
    } else {
        msg.append(".");
    }
    ctx.getDb().patchSetApprovals().delete(del);
    ChangeUpdate update = ctx.getUpdate(currPs.getId());
    update.removeReviewer(reviewerId);
    changeMessage = ChangeMessagesUtil.newMessage(ctx, msg.toString(), ChangeMessagesUtil.TAG_DELETE_REVIEWER);
    cmUtil.addChangeMessage(ctx.getDb(), update, changeMessage);
    return true;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) LabelTypes(com.google.gerrit.common.data.LabelTypes) LabelType(com.google.gerrit.common.data.LabelType) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Aggregations

ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)75 IdString (com.google.gerrit.extensions.restapi.IdString)18 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)17 AuthException (com.google.gerrit.extensions.restapi.AuthException)15 Repository (org.eclipse.jgit.lib.Repository)14 Project (com.google.gerrit.reviewdb.client.Project)13 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)12 Account (com.google.gerrit.reviewdb.client.Account)11 RevCommit (org.eclipse.jgit.revwalk.RevCommit)11 RevWalk (org.eclipse.jgit.revwalk.RevWalk)11 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)10 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)10 IOException (java.io.IOException)9 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)9 ObjectId (org.eclipse.jgit.lib.ObjectId)9 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)8 ProjectConfig (com.google.gerrit.server.git.ProjectConfig)8 CurrentUser (com.google.gerrit.server.CurrentUser)7 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)7 ArrayList (java.util.ArrayList)7