Search in sources :

Example 1 with PatchSetAttribute

use of com.google.gerrit.server.data.PatchSetAttribute in project gerrit by GerritCodeReview.

the class OutputStreamQuery method buildChangeAttribute.

private ChangeAttribute buildChangeAttribute(ChangeData d, Map<Project.NameKey, Repository> repos, Map<Project.NameKey, RevWalk> revWalks) throws IOException {
    LabelTypes labelTypes = d.getLabelTypes();
    ChangeAttribute c = eventFactory.asChangeAttribute(d.change(), d.notes());
    eventFactory.extend(c, d.change());
    if (!trackingFooters.isEmpty()) {
        eventFactory.addTrackingIds(c, d.trackingFooters());
    }
    if (includeAllReviewers) {
        eventFactory.addAllReviewers(c, d.notes());
    }
    if (includeSubmitRecords) {
        SubmitRuleOptions options = SubmitRuleOptions.builder().recomputeOnClosedChanges(true).build();
        eventFactory.addSubmitRecords(c, submitRuleEvaluatorFactory.create(options).evaluate(d));
    }
    if (includeCommitMessage) {
        eventFactory.addCommitMessage(c, d.commitMessage());
    }
    RevWalk rw = null;
    if (includePatchSets || includeCurrentPatchSet || includeDependencies) {
        Project.NameKey p = d.change().getProject();
        rw = revWalks.get(p);
        // Cache and reuse repos and revwalks.
        if (rw == null) {
            Repository repo = repoManager.openRepository(p);
            checkState(repos.put(p, repo) == null);
            rw = new RevWalk(repo);
            revWalks.put(p, rw);
        }
    }
    if (includePatchSets) {
        eventFactory.addPatchSets(rw, c, d.patchSets(), includeApprovals ? d.approvals().asMap() : null, includeFiles, d.change(), labelTypes);
    }
    if (includeCurrentPatchSet) {
        PatchSet current = d.currentPatchSet();
        if (current != null) {
            c.currentPatchSet = eventFactory.asPatchSetAttribute(rw, d.change(), current);
            eventFactory.addApprovals(c.currentPatchSet, d.currentApprovals(), labelTypes);
            if (includeFiles) {
                eventFactory.addPatchSetFileNames(c.currentPatchSet, d.change(), d.currentPatchSet());
            }
            if (includeComments) {
                eventFactory.addPatchSetComments(c.currentPatchSet, d.publishedComments());
            }
        }
    }
    if (includeComments) {
        eventFactory.addComments(c, d.messages());
        if (includePatchSets) {
            eventFactory.addPatchSets(rw, c, d.patchSets(), includeApprovals ? d.approvals().asMap() : null, includeFiles, d.change(), labelTypes);
            for (PatchSetAttribute attribute : c.patchSets) {
                eventFactory.addPatchSetComments(attribute, d.publishedComments());
            }
        }
    }
    if (includeDependencies) {
        eventFactory.addDependencies(rw, c, d.change(), d.currentPatchSet());
    }
    List<PluginDefinedInfo> pluginInfos = pluginInfosByChange.get(d.getId());
    if (!pluginInfos.isEmpty()) {
        c.plugins = pluginInfos;
    }
    return c;
}
Also used : Project(com.google.gerrit.entities.Project) LabelTypes(com.google.gerrit.entities.LabelTypes) Repository(org.eclipse.jgit.lib.Repository) ChangeAttribute(com.google.gerrit.server.data.ChangeAttribute) SubmitRuleOptions(com.google.gerrit.server.project.SubmitRuleOptions) PatchSetAttribute(com.google.gerrit.server.data.PatchSetAttribute) PatchSet(com.google.gerrit.entities.PatchSet) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PluginDefinedInfo(com.google.gerrit.extensions.common.PluginDefinedInfo)

Example 2 with PatchSetAttribute

use of com.google.gerrit.server.data.PatchSetAttribute in project gerrit by GerritCodeReview.

the class EventFactory method asPatchSetAttribute.

/**
 * Create a PatchSetAttribute for the given patchset suitable for serialization to JSON.
 */
public PatchSetAttribute asPatchSetAttribute(RevWalk revWalk, Change change, PatchSet patchSet) {
    PatchSetAttribute p = new PatchSetAttribute();
    p.revision = patchSet.commitId().name();
    p.number = patchSet.number();
    p.ref = patchSet.refName();
    p.uploader = asAccountAttribute(patchSet.uploader());
    p.createdOn = patchSet.createdOn().getEpochSecond();
    PatchSet.Id pId = patchSet.id();
    try {
        p.parents = new ArrayList<>();
        RevCommit c = revWalk.parseCommit(ObjectId.fromString(p.revision));
        for (RevCommit parent : c.getParents()) {
            p.parents.add(parent.name());
        }
        UserIdentity author = emails.toUserIdentity(c.getAuthorIdent());
        if (author.getAccount() == null) {
            p.author = new AccountAttribute();
            p.author.email = author.getEmail();
            p.author.name = author.getName();
            p.author.username = "";
        } else {
            p.author = asAccountAttribute(author.getAccount());
        }
        Map<String, FileDiffOutput> modifiedFiles = diffOperations.listModifiedFilesAgainstParent(change.getProject(), patchSet.commitId(), /* parentNum= */
        0, DiffOptions.DEFAULTS);
        for (FileDiffOutput fileDiff : modifiedFiles.values()) {
            p.sizeDeletions += fileDiff.deletions();
            p.sizeInsertions += fileDiff.insertions();
        }
        p.kind = changeKindCache.getChangeKind(change, patchSet);
    } catch (IOException | StorageException e) {
        logger.atSevere().withCause(e).log("Cannot load patch set data for %s", patchSet.id());
    } catch (DiffNotAvailableException e) {
        logger.atSevere().withCause(e).log("Cannot get size information for %s.", pId);
    }
    return p;
}
Also used : AccountAttribute(com.google.gerrit.server.data.AccountAttribute) UserIdentity(com.google.gerrit.entities.UserIdentity) PatchSet(com.google.gerrit.entities.PatchSet) IOException(java.io.IOException) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) PatchSetAttribute(com.google.gerrit.server.data.PatchSetAttribute) StorageException(com.google.gerrit.exceptions.StorageException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 3 with PatchSetAttribute

use of com.google.gerrit.server.data.PatchSetAttribute in project gerrit by GerritCodeReview.

the class EventFactory method addPatchSets.

public void addPatchSets(ReviewDb db, RevWalk revWalk, ChangeAttribute ca, Collection<PatchSet> ps, Map<PatchSet.Id, Collection<PatchSetApproval>> approvals, boolean includeFiles, Change change, LabelTypes labelTypes) {
    if (!ps.isEmpty()) {
        ca.patchSets = new ArrayList<>(ps.size());
        for (PatchSet p : ps) {
            PatchSetAttribute psa = asPatchSetAttribute(db, revWalk, change, p);
            if (approvals != null) {
                addApprovals(psa, p.getId(), approvals, labelTypes);
            }
            ca.patchSets.add(psa);
            if (includeFiles) {
                addPatchSetFileNames(psa, change, p);
            }
        }
    }
}
Also used : PatchSetAttribute(com.google.gerrit.server.data.PatchSetAttribute) PatchSet(com.google.gerrit.reviewdb.client.PatchSet)

Example 4 with PatchSetAttribute

use of com.google.gerrit.server.data.PatchSetAttribute in project gerrit by GerritCodeReview.

the class EventFactory method asPatchSetAttribute.

/**
   * Create a PatchSetAttribute for the given patchset suitable for serialization to JSON.
   *
   * @param db Review database
   * @param patchSet
   * @return object suitable for serialization to JSON
   */
public PatchSetAttribute asPatchSetAttribute(ReviewDb db, RevWalk revWalk, Change change, PatchSet patchSet) {
    PatchSetAttribute p = new PatchSetAttribute();
    p.revision = patchSet.getRevision().get();
    p.number = patchSet.getPatchSetId();
    p.ref = patchSet.getRefName();
    p.uploader = asAccountAttribute(patchSet.getUploader());
    p.createdOn = patchSet.getCreatedOn().getTime() / 1000L;
    p.isDraft = patchSet.isDraft();
    PatchSet.Id pId = patchSet.getId();
    try {
        p.parents = new ArrayList<>();
        RevCommit c = revWalk.parseCommit(ObjectId.fromString(p.revision));
        for (RevCommit parent : c.getParents()) {
            p.parents.add(parent.name());
        }
        UserIdentity author = toUserIdentity(c.getAuthorIdent());
        if (author.getAccount() == null) {
            p.author = new AccountAttribute();
            p.author.email = author.getEmail();
            p.author.name = author.getName();
            p.author.username = "";
        } else {
            p.author = asAccountAttribute(author.getAccount());
        }
        List<Patch> list = patchListCache.get(change, patchSet).toPatchList(pId);
        for (Patch pe : list) {
            if (!Patch.isMagic(pe.getFileName())) {
                p.sizeDeletions -= pe.getDeletions();
                p.sizeInsertions += pe.getInsertions();
            }
        }
        p.kind = changeKindCache.getChangeKind(db, change, patchSet);
    } catch (IOException e) {
        log.error("Cannot load patch set data for " + patchSet.getId(), e);
    } catch (PatchListNotAvailableException e) {
        log.error(String.format("Cannot get size information for %s.", pId), e);
    }
    return p;
}
Also used : AccountAttribute(com.google.gerrit.server.data.AccountAttribute) UserIdentity(com.google.gerrit.reviewdb.client.UserIdentity) PatchListNotAvailableException(com.google.gerrit.server.patch.PatchListNotAvailableException) PatchSetAttribute(com.google.gerrit.server.data.PatchSetAttribute) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) IOException(java.io.IOException) Patch(com.google.gerrit.reviewdb.client.Patch) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 5 with PatchSetAttribute

use of com.google.gerrit.server.data.PatchSetAttribute in project gerrit by GerritCodeReview.

the class EventFactory method addPatchSets.

public void addPatchSets(RevWalk revWalk, ChangeAttribute ca, Collection<PatchSet> ps, Map<PatchSet.Id, Collection<PatchSetApproval>> approvals, boolean includeFiles, Change change, LabelTypes labelTypes) {
    if (!ps.isEmpty()) {
        ca.patchSets = new ArrayList<>(ps.size());
        for (PatchSet p : ps) {
            PatchSetAttribute psa = asPatchSetAttribute(revWalk, change, p);
            if (approvals != null) {
                addApprovals(psa, p.id(), approvals, labelTypes);
            }
            ca.patchSets.add(psa);
            if (includeFiles) {
                addPatchSetFileNames(psa, change, p);
            }
        }
    }
}
Also used : PatchSetAttribute(com.google.gerrit.server.data.PatchSetAttribute) PatchSet(com.google.gerrit.entities.PatchSet)

Aggregations

PatchSetAttribute (com.google.gerrit.server.data.PatchSetAttribute)5 PatchSet (com.google.gerrit.entities.PatchSet)3 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)2 AccountAttribute (com.google.gerrit.server.data.AccountAttribute)2 IOException (java.io.IOException)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 LabelTypes (com.google.gerrit.entities.LabelTypes)1 Project (com.google.gerrit.entities.Project)1 UserIdentity (com.google.gerrit.entities.UserIdentity)1 StorageException (com.google.gerrit.exceptions.StorageException)1 PluginDefinedInfo (com.google.gerrit.extensions.common.PluginDefinedInfo)1 Patch (com.google.gerrit.reviewdb.client.Patch)1 UserIdentity (com.google.gerrit.reviewdb.client.UserIdentity)1 ChangeAttribute (com.google.gerrit.server.data.ChangeAttribute)1 DiffNotAvailableException (com.google.gerrit.server.patch.DiffNotAvailableException)1 PatchListNotAvailableException (com.google.gerrit.server.patch.PatchListNotAvailableException)1 FileDiffOutput (com.google.gerrit.server.patch.filediff.FileDiffOutput)1 SubmitRuleOptions (com.google.gerrit.server.project.SubmitRuleOptions)1 Repository (org.eclipse.jgit.lib.Repository)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1