Search in sources :

Example 86 with ChangeNotes

use of com.google.gerrit.server.notedb.ChangeNotes in project gerrit by GerritCodeReview.

the class CreateMergePatchSet method findBasePatchSet.

private PatchSet findBasePatchSet(String baseChange) throws PermissionBackendException, UnprocessableEntityException {
    List<ChangeNotes> notes = changeFinder.find(baseChange);
    if (notes.size() != 1) {
        throw new UnprocessableEntityException("Base change not found: " + baseChange);
    }
    ChangeNotes change = Iterables.getOnlyElement(notes);
    try {
        permissionBackend.currentUser().change(change).check(ChangePermission.READ);
    } catch (AuthException e) {
        throw new UnprocessableEntityException("Read not permitted for " + baseChange, e);
    }
    return psUtil.current(change);
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) AuthException(com.google.gerrit.extensions.restapi.AuthException) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes)

Example 87 with ChangeNotes

use of com.google.gerrit.server.notedb.ChangeNotes in project gerrit by GerritCodeReview.

the class ChangesCollection method parse.

public ChangeResource parse(Change.Id id) throws ResourceConflictException, ResourceNotFoundException, PermissionBackendException {
    List<ChangeNotes> notes = changeFinder.find(id);
    if (notes.isEmpty()) {
        throw new ResourceNotFoundException(toIdString(id));
    } else if (notes.size() != 1) {
        throw new ResourceNotFoundException("Multiple changes found for " + id);
    }
    ChangeNotes change = notes.get(0);
    if (!canRead(change)) {
        throw new ResourceNotFoundException(toIdString(id));
    }
    checkProjectStatePermitsRead(change.getProjectName());
    return changeResourceFactory.create(change, user.get());
}
Also used : ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 88 with ChangeNotes

use of com.google.gerrit.server.notedb.ChangeNotes in project gerrit by GerritCodeReview.

the class ChangeFinder method find.

/**
 * Find at most N changes matching the given identifier.
 *
 * @param id change identifier.
 * @param queryLimit maximum number of changes to be returned
 * @return possibly-empty list of notes for all matching changes; may or may not be visible.
 */
public List<ChangeNotes> find(String id, int queryLimit) {
    if (id.isEmpty()) {
        return Collections.emptyList();
    }
    int z = id.lastIndexOf('~');
    int y = id.lastIndexOf('~', z - 1);
    if (y < 0 && z > 0) {
        // Try project~numericChangeId
        Integer n = Ints.tryParse(id.substring(z + 1));
        if (n != null) {
            changeIdCounter.increment(ChangeIdType.PROJECT_NUMERIC_ID);
            return fromProjectNumber(id.substring(0, z), n.intValue());
        }
    }
    if (y < 0 && z < 0) {
        // Try numeric changeId
        Integer n = Ints.tryParse(id);
        if (n != null) {
            changeIdCounter.increment(ChangeIdType.NUMERIC_ID);
            return find(Change.id(n));
        }
    }
    // Use the index to search for changes, but don't return any stored fields,
    // to force rereading in case the index is stale.
    InternalChangeQuery query = queryProvider.get().noFields();
    if (queryLimit > 0) {
        query.setLimit(queryLimit);
    }
    // Try commit hash
    if (id.matches("^([0-9a-fA-F]{" + ObjectIds.ABBREV_STR_LEN + "," + ObjectIds.STR_LEN + "})$")) {
        changeIdCounter.increment(ChangeIdType.COMMIT_HASH);
        return asChangeNotes(query.byCommit(id));
    }
    if (y > 0 && z > 0) {
        // Try change triplet (project~branch~Ihash...)
        Optional<ChangeTriplet> triplet = ChangeTriplet.parse(id, y, z);
        if (triplet.isPresent()) {
            ChangeTriplet t = triplet.get();
            changeIdCounter.increment(ChangeIdType.TRIPLET);
            return asChangeNotes(query.byBranchKey(t.branch(), t.id()));
        }
    }
    // Try isolated Ihash... format ("Change-Id: Ihash").
    List<ChangeNotes> notes = asChangeNotes(query.byKeyPrefix(id));
    if (!notes.isEmpty()) {
        changeIdCounter.increment(ChangeIdType.I_HASH);
    }
    return notes;
}
Also used : InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes)

Example 89 with ChangeNotes

use of com.google.gerrit.server.notedb.ChangeNotes in project gerrit by GerritCodeReview.

the class ChangeJson method checkOnly.

private ChangeInfo checkOnly(ChangeData cd) {
    ChangeNotes notes;
    try {
        notes = cd.notes();
    } catch (StorageException e) {
        String msg = "Error loading change";
        logger.atWarning().withCause(e).log(msg + " %s", cd.getId());
        ChangeInfo info = new ChangeInfo();
        info._number = cd.getId().get();
        ProblemInfo p = new ProblemInfo();
        p.message = msg;
        info.problems = Lists.newArrayList(p);
        return info;
    }
    ConsistencyChecker.Result result = checkerProvider.get().check(notes, fix);
    ChangeInfo info = new ChangeInfo();
    Change c = result.change();
    if (c != null) {
        info.project = c.getProject().get();
        info.branch = c.getDest().shortName();
        info.topic = c.getTopic();
        info.changeId = c.getKey().get();
        info.subject = c.getSubject();
        info.status = c.getStatus().asChangeStatus();
        info.owner = new AccountInfo(c.getOwner().get());
        info.setCreated(c.getCreatedOn());
        info.setUpdated(c.getLastUpdatedOn());
        info._number = c.getId().get();
        info.problems = result.problems();
        info.isPrivate = c.isPrivate() ? true : null;
        info.workInProgress = c.isWorkInProgress() ? true : null;
        info.hasReviewStarted = c.hasReviewStarted();
        finish(info);
    } else {
        info._number = result.id().get();
        info.problems = result.problems();
    }
    return info;
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ProblemInfo(com.google.gerrit.extensions.common.ProblemInfo) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) Change(com.google.gerrit.entities.Change) StorageException(com.google.gerrit.exceptions.StorageException) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

Example 90 with ChangeNotes

use of com.google.gerrit.server.notedb.ChangeNotes in project gerrit by GerritCodeReview.

the class StreamEventsApiListener method onCommentAdded.

@Override
public void onCommentAdded(CommentAddedListener.Event ev) {
    try {
        ChangeNotes notes = getNotes(ev.getChange());
        Change change = notes.getChange();
        PatchSet ps = getPatchSet(notes, ev.getRevision());
        CommentAddedEvent event = new CommentAddedEvent(change);
        event.change = changeAttributeSupplier(change, notes);
        event.author = accountAttributeSupplier(ev.getWho());
        event.patchSet = patchSetAttributeSupplier(change, ps);
        event.comment = ev.getComment();
        event.approvals = approvalsAttributeSupplier(change, ev.getApprovals(), ev.getOldApprovals());
        dispatcher.run(d -> d.postEvent(change, event));
    } catch (StorageException e) {
        logger.atSevere().withCause(e).log("Failed to dispatch event");
    }
}
Also used : PatchSet(com.google.gerrit.entities.PatchSet) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) Change(com.google.gerrit.entities.Change) StorageException(com.google.gerrit.exceptions.StorageException)

Aggregations

ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)134 Test (org.junit.Test)54 Change (com.google.gerrit.entities.Change)47 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)43 PatchSet (com.google.gerrit.entities.PatchSet)42 ObjectId (org.eclipse.jgit.lib.ObjectId)33 StorageException (com.google.gerrit.exceptions.StorageException)22 Change (com.google.gerrit.reviewdb.client.Change)21 Project (com.google.gerrit.entities.Project)17 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)16 FixInput (com.google.gerrit.extensions.api.changes.FixInput)16 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)14 HumanComment (com.google.gerrit.entities.HumanComment)13 TestChanges.newPatchSet (com.google.gerrit.testing.TestChanges.newPatchSet)12 IOException (java.io.IOException)12 RevCommit (org.eclipse.jgit.revwalk.RevCommit)12 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)11 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)10 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)10 AuthException (com.google.gerrit.extensions.restapi.AuthException)9