Search in sources :

Example 1 with InternalChangeQuery

use of com.google.gerrit.server.query.change.InternalChangeQuery in project gerrit by GerritCodeReview.

the class ChangeFinder method find.

/**
   * Find changes matching the given identifier.
   *
   * @param id change identifier, either a numeric ID, a Change-Id, or project~branch~id triplet.
   * @param user user to wrap in controls.
   * @return possibly-empty list of controls for all matching changes, corresponding to the given
   *     user; may or may not be visible.
   * @throws OrmException if an error occurred querying the database.
   */
public List<ChangeControl> find(String id, CurrentUser user) throws OrmException {
    if (id.isEmpty()) {
        return Collections.emptyList();
    }
    // 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();
    int numTwiddles = 0;
    for (char c : id.toCharArray()) {
        if (c == '~') {
            numTwiddles++;
        }
    }
    if (numTwiddles == 1) {
        // Try project~numericChangeId
        String project = id.substring(0, id.indexOf('~'));
        Integer n = Ints.tryParse(id.substring(project.length() + 1));
        if (n != null) {
            Change.Id changeId = new Change.Id(n);
            try {
                return ImmutableList.of(changeControlFactory.controlFor(reviewDb.get(), Project.NameKey.parse(project), changeId, user));
            } catch (NoSuchChangeException e) {
                return Collections.emptyList();
            } catch (IllegalArgumentException e) {
                String changeNotFound = String.format("change %s not found in ReviewDb", changeId);
                String projectNotFound = String.format("passed project %s when creating ChangeNotes for %s, but actual project is", project, changeId);
                if (e.getMessage().equals(changeNotFound) || e.getMessage().startsWith(projectNotFound)) {
                    return Collections.emptyList();
                }
                throw e;
            } catch (OrmException e) {
                // other OrmExceptions (failure in the persistence layer).
                if (Throwables.getRootCause(e) instanceof RepositoryNotFoundException) {
                    return Collections.emptyList();
                }
                throw e;
            }
        }
    } else if (numTwiddles == 2) {
        // Try change triplet
        Optional<ChangeTriplet> triplet = ChangeTriplet.parse(id);
        if (triplet.isPresent()) {
            return asChangeControls(query.byBranchKey(triplet.get().branch(), triplet.get().id()), user);
        }
    }
    // Try numeric changeId
    if (id.charAt(0) != '0') {
        Integer n = Ints.tryParse(id);
        if (n != null) {
            return asChangeControls(query.byLegacyChangeId(new Change.Id(n)), user);
        }
    }
    // Try isolated changeId
    return asChangeControls(query.byKeyPrefix(id), user);
}
Also used : Optional(java.util.Optional) Change(com.google.gerrit.reviewdb.client.Change) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) OrmException(com.google.gwtorm.server.OrmException)

Example 2 with InternalChangeQuery

use of com.google.gerrit.server.query.change.InternalChangeQuery in project gerrit by GerritCodeReview.

the class PatchSetParser method parsePatchSet.

public PatchSet parsePatchSet(String token, ProjectControl projectControl, String branch) throws UnloggedFailure, OrmException {
    //
    if (token.matches("^([0-9a-fA-F]{4," + RevId.LEN + "})$")) {
        InternalChangeQuery query = queryProvider.get();
        List<ChangeData> cds;
        if (projectControl != null) {
            Project.NameKey p = projectControl.getProject().getNameKey();
            if (branch != null) {
                cds = query.byBranchCommit(p.get(), branch, token);
            } else {
                cds = query.byProjectCommit(p, token);
            }
        } else {
            cds = query.byCommit(token);
        }
        List<PatchSet> matches = new ArrayList<>(cds.size());
        for (ChangeData cd : cds) {
            Change c = cd.change();
            if (!(inProject(c, projectControl) && inBranch(c, branch))) {
                continue;
            }
            for (PatchSet ps : cd.patchSets()) {
                if (ps.getRevision().matches(token)) {
                    matches.add(ps);
                }
            }
        }
        switch(matches.size()) {
            case 1:
                return matches.iterator().next();
            case 0:
                throw error("\"" + token + "\" no such patch set");
            default:
                throw error("\"" + token + "\" matches multiple patch sets");
        }
    }
    //
    if (token.matches("^[1-9][0-9]*,[1-9][0-9]*$")) {
        PatchSet.Id patchSetId;
        try {
            patchSetId = PatchSet.Id.parse(token);
        } catch (IllegalArgumentException e) {
            throw error("\"" + token + "\" is not a valid patch set");
        }
        ChangeNotes notes = getNotes(projectControl, patchSetId.getParentKey());
        PatchSet patchSet = psUtil.get(db.get(), notes, patchSetId);
        if (patchSet == null) {
            throw error("\"" + token + "\" no such patch set");
        }
        if (projectControl != null || branch != null) {
            Change change = notes.getChange();
            if (!inProject(change, projectControl)) {
                throw error("change " + change.getId() + " not in project " + projectControl.getProject().getName());
            }
            if (!inBranch(change, branch)) {
                throw error("change " + change.getId() + " not in branch " + branch);
            }
        }
        return patchSet;
    }
    throw error("\"" + token + "\" is not a valid patch set");
}
Also used : InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) Project(com.google.gerrit.reviewdb.client.Project) ArrayList(java.util.ArrayList) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Aggregations

Change (com.google.gerrit.reviewdb.client.Change)2 InternalChangeQuery (com.google.gerrit.server.query.change.InternalChangeQuery)2 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)1 Project (com.google.gerrit.reviewdb.client.Project)1 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)1 NoSuchChangeException (com.google.gerrit.server.project.NoSuchChangeException)1 ChangeData (com.google.gerrit.server.query.change.ChangeData)1 OrmException (com.google.gwtorm.server.OrmException)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)1