use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class RelatedChangesSorter method sort.
public List<PatchSetData> sort(List<ChangeData> in, PatchSet startPs) throws IOException, PermissionBackendException {
checkArgument(!in.isEmpty(), "Input may not be empty");
// Map of all patch sets, keyed by commit SHA-1.
Map<ObjectId, PatchSetData> byId = collectById(in);
PatchSetData start = byId.get(startPs.commitId());
requireNonNull(start, () -> String.format("commit %s of patch set %s not found in %s", startPs.commitId().name(), startPs.id(), byId.entrySet().stream().collect(toMap(e -> e.getKey().name(), e -> e.getValue().patchSet().id()))));
// Map of patch set -> immediate parent.
ListMultimap<PatchSetData, PatchSetData> parents = MultimapBuilder.hashKeys(in.size()).arrayListValues(3).build();
// Map of patch set -> immediate children.
ListMultimap<PatchSetData, PatchSetData> children = MultimapBuilder.hashKeys(in.size()).arrayListValues(3).build();
// All other patch sets of the same change as startPs.
List<PatchSetData> otherPatchSetsOfStart = new ArrayList<>();
for (ChangeData cd : in) {
for (PatchSet ps : cd.patchSets()) {
PatchSetData thisPsd = requireNonNull(byId.get(ps.commitId()));
if (cd.getId().equals(start.id()) && !ps.id().equals(start.psId())) {
otherPatchSetsOfStart.add(thisPsd);
}
for (RevCommit p : thisPsd.commit().getParents()) {
PatchSetData parentPsd = byId.get(p);
if (parentPsd != null) {
parents.put(thisPsd, parentPsd);
children.put(parentPsd, thisPsd);
}
}
}
}
Collection<PatchSetData> ancestors = walkAncestors(parents, start);
List<PatchSetData> descendants = walkDescendants(children, start, otherPatchSetsOfStart, ancestors);
List<PatchSetData> result = new ArrayList<>(ancestors.size() + descendants.size() - 1);
result.addAll(Lists.reverse(descendants));
result.addAll(ancestors);
return result;
}
use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class RebaseUtil method parseBase.
public Base parseBase(RevisionResource rsrc, String base) {
// Try parsing the base as a ref string.
PatchSet.Id basePatchSetId = PatchSet.Id.fromRef(base);
if (basePatchSetId != null) {
Change.Id baseChangeId = basePatchSetId.changeId();
ChangeNotes baseNotes = notesFor(rsrc, baseChangeId);
if (baseNotes != null) {
return Base.create(notesFor(rsrc, basePatchSetId.changeId()), psUtil.get(baseNotes, basePatchSetId));
}
}
// Try parsing base as a change number (assume current patch set).
Integer baseChangeId = Ints.tryParse(base);
if (baseChangeId != null) {
ChangeNotes baseNotes = notesFor(rsrc, Change.id(baseChangeId));
if (baseNotes != null) {
return Base.create(baseNotes, psUtil.current(baseNotes));
}
}
// Try parsing as SHA-1.
Base ret = null;
for (ChangeData cd : queryProvider.get().byProjectCommit(rsrc.getProject(), base)) {
for (PatchSet ps : cd.patchSets()) {
if (!ObjectIds.matchesAbbreviation(ps.commitId(), base)) {
continue;
}
if (ret == null || ret.patchSet().id().get() < ps.id().get()) {
ret = Base.create(cd.notes(), ps);
}
}
}
return ret;
}
use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class ReviewerJson method format.
public ReviewerInfo format(ReviewerInfo out, Account.Id reviewerAccountId, ChangeData cd, Iterable<PatchSetApproval> approvals) throws PermissionBackendException {
LabelTypes labelTypes = cd.getLabelTypes();
out.approvals = new TreeMap<>(labelTypes.nameComparator());
for (PatchSetApproval ca : approvals) {
Optional<LabelType> at = labelTypes.byLabel(ca.labelId());
at.ifPresent(lt -> out.approvals.put(lt.getName(), formatValue(ca.value())));
}
// Add dummy approvals for all permitted labels for the user even if they
// do not exist in the DB.
PatchSet ps = cd.currentPatchSet();
if (ps != null) {
PermissionBackend.ForChange perm = permissionBackend.absentUser(reviewerAccountId).change(cd);
for (SubmitRecord rec : cd.submitRecords(SubmitRuleOptions.defaults())) {
if (rec.labels == null) {
continue;
}
for (SubmitRecord.Label label : rec.labels) {
String name = label.label;
Optional<LabelType> type = labelTypes.byLabel(name);
if (out.approvals.containsKey(name) || !type.isPresent()) {
continue;
}
if (perm.test(new LabelPermission(type.get()))) {
out.approvals.put(name, formatValue((short) 0));
}
}
}
}
if (out.approvals.isEmpty()) {
out.approvals = null;
}
return out;
}
use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class RevisionJson method getRevisions.
/**
* Returns multiple {@link RevisionInfo}s for a single change. Uses the provided {@link
* AccountLoader} to lazily populate accounts. Callers have to call {@link AccountLoader#fill()}
* afterwards to populate all accounts in the returned {@link RevisionInfo}s.
*/
Map<String, RevisionInfo> getRevisions(AccountLoader accountLoader, ChangeData cd, Map<PatchSet.Id, PatchSet> map, Optional<PatchSet.Id> limitToPsId, ChangeInfo changeInfo) throws PatchListNotAvailableException, GpgException, IOException, PermissionBackendException {
Map<String, RevisionInfo> res = new LinkedHashMap<>();
try (Repository repo = openRepoIfNecessary(cd.project());
RevWalk rw = newRevWalk(repo)) {
for (PatchSet in : map.values()) {
PatchSet.Id id = in.id();
boolean want;
if (has(ALL_REVISIONS)) {
want = true;
} else if (limitToPsId.isPresent()) {
want = id.equals(limitToPsId.get());
} else {
want = id.equals(cd.change().currentPatchSetId());
}
if (want) {
res.put(in.commitId().name(), toRevisionInfo(accountLoader, cd, in, repo, rw, false, changeInfo));
}
}
return res;
}
}
use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class ProjectsConsistencyChecker method executeQueryAndAutoCloseChanges.
private ImmutableList<ChangeInfo> executeQueryAndAutoCloseChanges(Predicate<ChangeData> basePredicate, Set<Change.Id> seenChanges, List<Predicate<ChangeData>> predicates, boolean fix, Map<Change.Key, ObjectId> changeIdToMergedSha1, List<ObjectId> mergedSha1s) {
if (predicates.isEmpty()) {
return ImmutableList.of();
}
try {
List<ChangeData> queryResult = retryHelper.changeIndexQuery("projectsConsistencyCheckerQueryChanges", q -> q.setRequestedFields(ChangeField.CHANGE, ChangeField.PATCH_SET).query(and(basePredicate, or(predicates)))).call();
// Result for this query that we want to return to the client.
ImmutableList.Builder<ChangeInfo> autoCloseableChangesByBranch = ImmutableList.builder();
for (ChangeData autoCloseableChange : queryResult) {
// earlier queries.
if (seenChanges.add(autoCloseableChange.getId())) {
retryHelper.changeUpdate("projectsConsistencyCheckerAutoCloseChanges", () -> {
// Auto-close by change
if (changeIdToMergedSha1.containsKey(autoCloseableChange.change().getKey())) {
autoCloseableChangesByBranch.add(changeJson(fix, changeIdToMergedSha1.get(autoCloseableChange.change().getKey())).format(autoCloseableChange));
return null;
}
// Auto-close by commit
for (ObjectId patchSetSha1 : autoCloseableChange.patchSets().stream().map(PatchSet::commitId).collect(toSet())) {
if (mergedSha1s.contains(patchSetSha1)) {
autoCloseableChangesByBranch.add(changeJson(fix, patchSetSha1).format(autoCloseableChange));
break;
}
}
return null;
}).call();
}
}
return autoCloseableChangesByBranch.build();
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new StorageException(e);
}
}
Aggregations