use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.
the class SubmitRuleEvaluator method evaluate.
/**
* Evaluate the submit rules.
*
* @return List of {@link SubmitRecord} objects returned from the evaluated rules, including any
* errors.
* @param cd ChangeData to evaluate
*/
public List<SubmitRecord> evaluate(ChangeData cd) {
logger.atFine().log("Evaluate submit rules for change %d (caller: %s)", cd.change().getId().get(), callerFinder.findCallerLazy());
try (Timer0.Context ignored = submitRuleEvaluationLatency.start()) {
Change change;
ProjectState projectState;
try {
change = cd.change();
if (change == null) {
throw new StorageException("Change not found");
}
Project.NameKey name = cd.project();
Optional<ProjectState> projectStateOptional = projectCache.get(name);
if (!projectStateOptional.isPresent()) {
throw new NoSuchProjectException(name);
}
projectState = projectStateOptional.get();
} catch (NoSuchProjectException e) {
throw new IllegalStateException("Unable to find project while evaluating submit rule", e);
}
if (change.isClosed() && (!opts.recomputeOnClosedChanges() || OnlineReindexMode.isActive())) {
return cd.notes().getSubmitRecords().stream().map(r -> {
SubmitRecord record = r.deepCopy();
if (record.status == SubmitRecord.Status.OK) {
// Submit records that were OK when they got merged are CLOSED now.
record.status = SubmitRecord.Status.CLOSED;
}
return record;
}).collect(toImmutableList());
}
// and then we collect the results in one list.
return Streams.stream(submitRules).filter(projectState.hasPrologRules() ? rule -> !(rule.get() instanceof DefaultSubmitRule) : rule -> true).map(c -> c.call(s -> {
Optional<SubmitRecord> record = s.evaluate(cd);
if (record.isPresent() && record.get().ruleName == null) {
// Only back-fill the ruleName if it was not populated by the "submit
// rule".
record.get().ruleName = c.getPluginName() + "~" + s.getClass().getSimpleName();
}
return record;
})).filter(Optional::isPresent).map(Optional::get).collect(toImmutableList());
}
}
use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.
the class ChangeData method isMergeable.
@Nullable
public Boolean isMergeable() {
if (mergeable == null) {
Change c = change();
if (c == null) {
return null;
}
if (c.isMerged()) {
mergeable = true;
} else if (c.isAbandoned()) {
return null;
} else if (c.isWorkInProgress()) {
return null;
} else {
if (!lazyload()) {
return null;
}
PatchSet ps = currentPatchSet();
if (ps == null) {
return null;
}
try (Repository repo = repoManager.openRepository(project())) {
Ref ref = repo.getRefDatabase().exactRef(c.getDest().branch());
SubmitTypeRecord str = submitTypeRecord();
if (!str.isOk()) {
// No need to log, as SubmitRuleEvaluator already did it for us.
return false;
}
String mergeStrategy = mergeUtilFactory.create(projectCache.get(project()).orElseThrow(illegalState(project()))).mergeStrategyName();
mergeable = mergeabilityCache.get(ps.commitId(), ref, str.type, mergeStrategy, c.getDest(), repo);
} catch (IOException e) {
throw new StorageException(e);
}
}
}
return mergeable;
}
use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.
the class ChangeData method reloadChange.
public Change reloadChange() {
try {
notes = notesFactory.createChecked(project, legacyId);
} catch (NoSuchChangeException e) {
throw new StorageException("Unable to load change " + legacyId, e);
}
change = notes.getChange();
setPatchSets(null);
return change;
}
use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.
the class ChangeNotesTest method setRevertOfOnChildCommitFails.
@Test
public void setRevertOfOnChildCommitFails() throws Exception {
Change c = newChange();
ChangeUpdate update = newUpdate(c, changeOwner);
update.setRevertOf(newChange().getId().get());
StorageException thrown = assertThrows(StorageException.class, () -> update.commit());
assertThat(thrown).hasMessageThat().contains("Given ChangeUpdate is only allowed on initial commit");
}
use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.
the class ChangeNotesTest method commitChangeNotesUnique.
@Test
public void commitChangeNotesUnique() throws Exception {
// PatchSetId -> ObjectId must be a one to one mapping
Change c = newChange();
ChangeNotes notes = newNotes(c);
PatchSet ps = notes.getCurrentPatchSet();
assertThat(ps).isNotNull();
// new revId for the same patch set, ps1
ChangeUpdate update = newUpdate(c, changeOwner);
RevCommit commit = tr.commit().message("PS1 again").create();
update.setCommit(rw, commit);
update.commit();
StorageException e = assertThrows(StorageException.class, () -> newNotes(c));
assertCause(e, ConfigInvalidException.class, "Multiple revisions parsed for patch set 1:" + " " + commit.name() + " and " + ps.commitId().name());
}
Aggregations