use of com.google.gerrit.extensions.common.RevisionInfo in project gerrit by GerritCodeReview.
the class ChangeJson method toChangeInfoImpl.
private ChangeInfo toChangeInfoImpl(ChangeData cd, Optional<PatchSet.Id> limitToPsId, List<PluginDefinedInfo> pluginInfos) throws PatchListNotAvailableException, GpgException, PermissionBackendException, IOException {
ChangeInfo out = new ChangeInfo();
CurrentUser user = userProvider.get();
if (has(CHECK)) {
out.problems = checkerProvider.get().check(cd.notes(), fix).problems();
// If any problems were fixed, the ChangeData needs to be reloaded.
for (ProblemInfo p : out.problems) {
if (p.status == ProblemInfo.Status.FIXED) {
cd = changeDataFactory.create(cd.project(), cd.getId());
break;
}
}
}
Change in = cd.change();
out.project = in.getProject().get();
out.branch = in.getDest().shortName();
out.topic = in.getTopic();
if (!cd.attentionSet().isEmpty()) {
out.removedFromAttentionSet = removalsOnly(cd.attentionSet()).stream().collect(toImmutableMap(a -> a.account().get(), a -> AttentionSetUtil.createAttentionSetInfo(a, accountLoader)));
out.attentionSet = // This filtering should match GetAttentionSet.
additionsOnly(cd.attentionSet()).stream().collect(toImmutableMap(a -> a.account().get(), a -> AttentionSetUtil.createAttentionSetInfo(a, accountLoader)));
}
out.assignee = in.getAssignee() != null ? accountLoader.get(in.getAssignee()) : null;
out.hashtags = cd.hashtags();
out.changeId = in.getKey().get();
if (in.isNew()) {
SubmitTypeRecord str = cd.submitTypeRecord();
if (str.isOk()) {
out.submitType = str.type;
}
if (includeMergeable) {
out.mergeable = cd.isMergeable();
}
if (has(SUBMITTABLE)) {
out.submittable = submittable(cd);
}
}
if (!has(SKIP_DIFFSTAT)) {
Optional<ChangedLines> changedLines = cd.changedLines();
if (changedLines.isPresent()) {
out.insertions = changedLines.get().insertions;
out.deletions = changedLines.get().deletions;
}
}
out.isPrivate = in.isPrivate() ? true : null;
out.workInProgress = in.isWorkInProgress() ? true : null;
out.hasReviewStarted = in.hasReviewStarted();
out.subject = in.getSubject();
out.status = in.getStatus().asChangeStatus();
out.owner = accountLoader.get(in.getOwner());
out.setCreated(in.getCreatedOn());
out.setUpdated(in.getLastUpdatedOn());
out._number = in.getId().get();
out.totalCommentCount = cd.totalCommentCount();
out.unresolvedCommentCount = cd.unresolvedCommentCount();
if (cd.getRefStates() != null) {
String metaName = RefNames.changeMetaRef(cd.getId());
Optional<RefState> metaState = cd.getRefStates().values().stream().filter(r -> r.ref().equals(metaName)).findAny();
// metaState should always be there, but it doesn't hurt to be extra careful.
metaState.ifPresent(rs -> out.metaRevId = rs.id().getName());
}
if (user.isIdentifiedUser()) {
Collection<String> stars = cd.stars(user.getAccountId());
out.starred = stars.contains(StarredChangesUtil.DEFAULT_LABEL) ? true : null;
if (!stars.isEmpty()) {
out.stars = stars;
}
}
if (in.isNew() && has(REVIEWED) && user.isIdentifiedUser()) {
out.reviewed = cd.isReviewedBy(user.getAccountId()) ? true : null;
}
out.labels = labelsJson.labelsFor(accountLoader, cd, has(LABELS), has(DETAILED_LABELS));
out.requirements = requirementsFor(cd);
out.submitRecords = submitRecordsFor(cd);
if (has(SUBMIT_REQUIREMENTS)) {
out.submitRequirements = submitRequirementsFor(cd);
}
if (out.labels != null && has(DETAILED_LABELS)) {
// list permitted labels, since users can't vote on those patch sets.
if (user.isIdentifiedUser() && (!limitToPsId.isPresent() || limitToPsId.get().equals(in.currentPatchSetId()))) {
out.permittedLabels = !cd.change().isAbandoned() ? labelsJson.permittedLabels(user.getAccountId(), cd) : ImmutableMap.of();
}
}
if (has(LABELS) || has(DETAILED_LABELS)) {
out.reviewers = reviewerMap(cd.reviewers(), cd.reviewersByEmail(), false);
out.pendingReviewers = reviewerMap(cd.pendingReviewers(), cd.pendingReviewersByEmail(), true);
out.removableReviewers = removableReviewers(cd, out);
}
setSubmitter(cd, out);
if (!pluginInfos.isEmpty()) {
out.plugins = pluginInfos;
}
out.revertOf = cd.change().getRevertOf() != null ? cd.change().getRevertOf().get() : null;
out.submissionId = cd.change().getSubmissionId();
out.cherryPickOfChange = cd.change().getCherryPickOf() != null ? cd.change().getCherryPickOf().changeId().get() : null;
out.cherryPickOfPatchSet = cd.change().getCherryPickOf() != null ? cd.change().getCherryPickOf().get() : null;
if (has(REVIEWER_UPDATES)) {
out.reviewerUpdates = reviewerUpdates(cd);
}
boolean needMessages = has(MESSAGES);
boolean needRevisions = has(ALL_REVISIONS) || has(CURRENT_REVISION) || limitToPsId.isPresent();
Map<PatchSet.Id, PatchSet> src;
if (needMessages || needRevisions) {
src = loadPatchSets(cd, limitToPsId);
} else {
src = null;
}
if (needMessages) {
out.messages = messages(cd);
}
finish(out);
// it will be passed to ActionVisitors as-is.
if (needRevisions) {
out.revisions = revisionJson.getRevisions(accountLoader, cd, src, limitToPsId, out);
if (out.revisions != null) {
for (Map.Entry<String, RevisionInfo> entry : out.revisions.entrySet()) {
if (entry.getValue().isCurrent) {
out.currentRevision = entry.getKey();
break;
}
}
}
}
if (has(CURRENT_ACTIONS) || has(CHANGE_ACTIONS)) {
actionJson.addChangeActions(out, cd);
}
if (has(TRACKING_IDS)) {
ListMultimap<String, String> set = trackingFooters.extract(cd.commitFooters());
out.trackingIds = set.entries().stream().map(e -> new TrackingIdInfo(e.getKey(), e.getValue())).collect(toList());
}
return out;
}
use of com.google.gerrit.extensions.common.RevisionInfo 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.extensions.common.RevisionInfo in project gerrit by GerritCodeReview.
the class RevisionJson method getRevisionInfo.
/**
* Returns a {@link RevisionInfo} based on a change and patch set. Reads from the repository
* depending on the options provided when constructing this instance.
*/
public RevisionInfo getRevisionInfo(ChangeData cd, PatchSet in) throws PatchListNotAvailableException, GpgException, IOException, PermissionBackendException {
AccountLoader accountLoader = accountLoaderFactory.create(has(DETAILED_ACCOUNTS));
try (Repository repo = openRepoIfNecessary(cd.project());
RevWalk rw = newRevWalk(repo)) {
RevisionInfo rev = toRevisionInfo(accountLoader, cd, in, repo, rw, true, null);
accountLoader.fill();
return rev;
}
}
use of com.google.gerrit.extensions.common.RevisionInfo in project gerrit by GerritCodeReview.
the class RevisionJson method toRevisionInfo.
private RevisionInfo toRevisionInfo(AccountLoader accountLoader, ChangeData cd, PatchSet in, @Nullable Repository repo, @Nullable RevWalk rw, boolean fillCommit, @Nullable ChangeInfo changeInfo) throws PatchListNotAvailableException, GpgException, IOException, PermissionBackendException {
Change c = cd.change();
RevisionInfo out = new RevisionInfo();
out.isCurrent = in.id().equals(c.currentPatchSetId());
out._number = in.id().get();
out.ref = in.refName();
out.setCreated(in.createdOn());
out.uploader = accountLoader.get(in.uploader());
out.fetch = makeFetchMap(cd, in);
out.kind = changeKindCache.getChangeKind(rw, repo != null ? repo.getConfig() : null, cd, in);
out.description = in.description().orElse(null);
boolean setCommit = has(ALL_COMMITS) || (out.isCurrent && has(CURRENT_COMMIT));
boolean addFooters = out.isCurrent && has(COMMIT_FOOTERS);
if (setCommit || addFooters) {
checkState(rw != null);
checkState(repo != null);
Project.NameKey project = c.getProject();
String rev = in.commitId().name();
RevCommit commit = rw.parseCommit(ObjectId.fromString(rev));
rw.parseBody(commit);
String branchName = cd.change().getDest().branch();
if (setCommit) {
out.commit = getCommitInfo(project, rw, commit, has(WEB_LINKS), fillCommit, branchName);
}
if (addFooters) {
Ref ref = repo.exactRef(branchName);
RevCommit mergeTip = null;
if (ref != null) {
mergeTip = rw.parseCommit(ref.getObjectId());
rw.parseBody(mergeTip);
}
out.commitWithFooters = mergeUtilFactory.create(projectCache.get(project).orElseThrow(illegalState(project))).createCommitMessageOnSubmit(commit, mergeTip, cd.notes(), in.id());
}
}
if (has(ALL_FILES) || (out.isCurrent && has(CURRENT_FILES))) {
try {
out.files = fileInfoJson.getFileInfoMap(c, in);
out.files.remove(Patch.COMMIT_MSG);
out.files.remove(Patch.MERGE_LIST);
} catch (ResourceConflictException e) {
logger.atWarning().withCause(e).log("creating file list failed");
}
}
if (out.isCurrent && has(CURRENT_ACTIONS) && userProvider.get().isIdentifiedUser()) {
actionJson.addRevisionActions(changeInfo, out, new RevisionResource(changeResourceFactory.create(cd, userProvider.get()), in));
}
if (gpgApi.isEnabled() && has(PUSH_CERTIFICATES)) {
if (in.pushCertificate().isPresent()) {
out.pushCertificate = gpgApi.checkPushCertificate(in.pushCertificate().get(), userFactory.create(in.uploader()));
} else {
out.pushCertificate = new PushCertificateInfo();
}
}
return out;
}
use of com.google.gerrit.extensions.common.RevisionInfo in project gerrit by GerritCodeReview.
the class AbstractPushForReview method pushForMasterWithMessageTwiceWithDifferentMessages.
@Test
public void pushForMasterWithMessageTwiceWithDifferentMessages() throws Exception {
enableCreateNewChangeForAllNotInTarget();
PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo, PushOneCommit.SUBJECT, "a.txt", "content");
// %2C is comma; the value below tests that percent decoding happens after splitting.
// All three ways of representing space ("%20", "+", and "_" are also exercised.
PushOneCommit.Result r = push.to("refs/for/master%m=my_test%20+_message%2Cm=");
r.assertOkStatus();
push = pushFactory.create(admin.newIdent(), testRepo, PushOneCommit.SUBJECT, "b.txt", "anotherContent", r.getChangeId());
r = push.to("refs/for/master%m=new_test_message");
r.assertOkStatus();
ChangeInfo ci = get(r.getChangeId(), ALL_REVISIONS);
Collection<RevisionInfo> revisions = ci.revisions.values();
assertThat(revisions).hasSize(2);
for (RevisionInfo ri : revisions) {
if (ri.isCurrent) {
assertThat(ri.description).isEqualTo("new test message");
} else {
assertThat(ri.description).isEqualTo("my test message,m=");
}
}
}
Aggregations