use of com.google.gerrit.extensions.common.ChangeInfo in project gerrit by GerritCodeReview.
the class ChangesImpl method get.
private List<ChangeInfo> get(QueryRequest q) throws RestApiException {
try (DynamicOptions dynamicOptions = new DynamicOptions(injector, dynamicBeans)) {
QueryChanges qc = queryProvider.get();
if (q.getQuery() != null) {
qc.addQuery(q.getQuery());
}
qc.setLimit(q.getLimit());
qc.setStart(q.getStart());
qc.setNoLimit(q.getNoLimit());
for (ListChangesOption option : q.getOptions()) {
qc.addOption(option);
}
dynamicOptionParser.parseDynamicOptions(qc, q.getPluginOptions(), dynamicOptions);
try {
List<?> result = qc.apply(TopLevelResource.INSTANCE).value();
if (result.isEmpty()) {
return ImmutableList.of();
}
// Check type safety of result; the extension API should be safer than the
// REST API in this case, since it's intended to be used in Java.
Object first = requireNonNull(result.iterator().next());
checkState(first instanceof ChangeInfo);
@SuppressWarnings("unchecked") List<ChangeInfo> infos = (List<ChangeInfo>) result;
return ImmutableList.copyOf(infos);
} catch (Exception e) {
throw asRestApiException("Cannot query changes", e);
}
}
}
use of com.google.gerrit.extensions.common.ChangeInfo in project gerrit by GerritCodeReview.
the class ChangeKindCreator method cherryPick.
/**
* Creates a cherry pick of the provided change with the given {@link ChangeKind} and returns the
* change id.
*/
public String cherryPick(String changeId, ChangeKind changeKind, TestRepository<InMemoryRepository> testRepo, TestAccount user, Project.NameKey project) throws Exception {
switch(changeKind) {
case REWORK:
case TRIVIAL_REBASE:
break;
case NO_CODE_CHANGE:
case NO_CHANGE:
case MERGE_FIRST_PARENT_UPDATE:
default:
assertWithMessage("unexpected change kind: " + changeKind).fail();
}
testRepo.reset(projectOperations.project(project).getHead("master"));
PushOneCommit.Result r = pushFactory.create(user.newIdent(), testRepo, PushOneCommit.SUBJECT, "other.txt", "new content " + System.nanoTime()).to("refs/for/master");
r.assertOkStatus();
vote(user, r.getChangeId(), 2, 1);
merge(r);
String subject = ChangeKind.TRIVIAL_REBASE.equals(changeKind) ? PushOneCommit.SUBJECT : "Reworked change " + System.nanoTime();
CherryPickInput in = new CherryPickInput();
in.destination = "master";
in.message = String.format("%s\n\nChange-Id: %s", subject, changeId);
ChangeInfo c = gApi.changes().id(changeId).current().cherryPick(in).get();
return c.changeId;
}
use of com.google.gerrit.extensions.common.ChangeInfo in project gerrit by GerritCodeReview.
the class ChangeKindCreator method updateSecondParent.
/**
* Update the second parent of a merge.
*/
public void updateSecondParent(String changeId, TestRepository<InMemoryRepository> testRepo, TestAccount user) throws Exception {
ChangeInfo c = detailedChange(changeId);
List<CommitInfo> parents = c.revisions.get(c.currentRevision).commit.parents;
String parent1 = parents.get(0).commit;
String parent2 = parents.get(1).commit;
RevCommit commitParent1 = testRepo.getRevWalk().parseCommit(ObjectId.fromString(parent1));
testRepo.reset(parent2);
PushOneCommit.Result newParent2 = createChange("new parent 2", "p2-2.txt", "content 2-2", testRepo, user);
PushOneCommit merge = pushFactory.create(user.newIdent(), testRepo, changeId);
merge.setParents(ImmutableList.of(commitParent1, newParent2.getCommit()));
PushOneCommit.Result result = merge.to("refs/for/master");
result.assertOkStatus();
assertThat(getChangeKind(changeId)).isEqualTo(ChangeKind.REWORK);
}
use of com.google.gerrit.extensions.common.ChangeInfo in project gerrit by GerritCodeReview.
the class ChangeJson method format.
private ChangeInfo format(ChangeData cd, Optional<PatchSet.Id> limitToPsId, boolean fillAccountLoader, List<PluginDefinedInfo> pluginInfosForChange) {
try {
if (fillAccountLoader) {
accountLoader = accountLoaderFactory.create(has(DETAILED_ACCOUNTS));
ChangeInfo res = toChangeInfo(cd, limitToPsId, pluginInfosForChange);
accountLoader.fill();
return res;
}
return toChangeInfo(cd, limitToPsId, pluginInfosForChange);
} catch (PatchListNotAvailableException | GpgException | IOException | PermissionBackendException | RuntimeException e) {
if (!has(CHECK)) {
Throwables.throwIfInstanceOf(e, StorageException.class);
throw new StorageException(e);
}
return checkOnly(cd);
}
}
use of com.google.gerrit.extensions.common.ChangeInfo in project gerrit by GerritCodeReview.
the class ChangeJson method toChangeInfos.
private List<ChangeInfo> toChangeInfos(List<ChangeData> changes, Map<Change.Id, ChangeInfo> cache, ImmutableListMultimap<Change.Id, PluginDefinedInfo> pluginInfosByChange) {
try (Timer0.Context ignored = metrics.toChangeInfosLatency.start()) {
List<ChangeInfo> changeInfos = new ArrayList<>(changes.size());
for (int i = 0; i < changes.size(); i++) {
// We can only cache and re-use an entity if it's not the last in the list. The last entity
// may later get _moreChanges set. If it was cached or re-used, that setting would propagate
// to the original entity yielding wrong results.
// This problem has two sides where 'last in the list' has to be respected:
// (1) Caching
// (2) Reusing
boolean isCacheable = i != changes.size() - 1;
ChangeData cd = changes.get(i);
ChangeInfo info = cache.get(cd.getId());
if (info != null && isCacheable) {
changeInfos.add(info);
continue;
}
// Compute and cache if possible
try {
ensureLoaded(Collections.singleton(cd));
info = format(cd, Optional.empty(), false, pluginInfosByChange.get(cd.getId()));
changeInfos.add(info);
if (isCacheable) {
cache.put(Change.id(info._number), info);
}
} catch (RuntimeException e) {
Optional<RequestCancelledException> requestCancelledException = RequestCancelledException.getFromCausalChain(e);
if (requestCancelledException.isPresent()) {
throw e;
}
logger.atWarning().withCause(e).log("Omitting corrupt change %s from results", cd.getId());
}
}
return changeInfos;
}
}
Aggregations