use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.
the class StarredChangesUtil method byChange.
public ImmutableMap<Account.Id, StarRef> byChange(Change.Id changeId) throws OrmException {
try (Repository repo = repoManager.openRepository(allUsers)) {
ImmutableMap.Builder<Account.Id, StarRef> builder = ImmutableMap.builder();
for (String refPart : getRefNames(repo, RefNames.refsStarredChangesPrefix(changeId))) {
Integer id = Ints.tryParse(refPart);
if (id == null) {
continue;
}
Account.Id accountId = new Account.Id(id);
builder.put(accountId, readLabels(repo, RefNames.refsStarredChanges(changeId, accountId)));
}
return builder.build();
} catch (IOException e) {
throw new OrmException(String.format("Get accounts that starred change %d failed", changeId.get()), e);
}
}
use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.
the class StarredChangesUtil method deleteRef.
private void deleteRef(Repository repo, String refName, ObjectId oldObjectId) throws IOException, OrmException {
RefUpdate u = repo.updateRef(refName);
u.setForceUpdate(true);
u.setExpectedOldObjectId(oldObjectId);
u.setRefLogIdent(serverIdent);
u.setRefLogMessage("Unstar change", true);
RefUpdate.Result result = u.delete();
switch(result) {
case FORCED:
return;
case NEW:
case NO_CHANGE:
case FAST_FORWARD:
case IO_FAILURE:
case LOCK_FAILURE:
case NOT_ATTEMPTED:
case REJECTED:
case REJECTED_CURRENT_BRANCH:
case RENAMED:
throw new OrmException(String.format("Delete star ref %s failed: %s", refName, result.name()));
}
}
use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.
the class StarredChangesUtil method star.
public ImmutableSortedSet<String> star(Account.Id accountId, Project.NameKey project, Change.Id changeId, Set<String> labelsToAdd, Set<String> labelsToRemove) throws OrmException {
try (Repository repo = repoManager.openRepository(allUsers)) {
String refName = RefNames.refsStarredChanges(changeId, accountId);
StarRef old = readLabels(repo, refName);
Set<String> labels = new HashSet<>(old.labels());
if (labelsToAdd != null) {
labels.addAll(labelsToAdd);
}
if (labelsToRemove != null) {
labels.removeAll(labelsToRemove);
}
if (labels.isEmpty()) {
deleteRef(repo, refName, old.objectId());
} else {
checkMutuallyExclusiveLabels(labels);
updateLabels(repo, refName, old.objectId(), labels);
}
indexer.index(dbProvider.get(), project, changeId);
return ImmutableSortedSet.copyOf(labels);
} catch (IOException e) {
throw new OrmException(String.format("Star change %d for account %d failed", changeId.get(), accountId.get()), e);
}
}
use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.
the class ChangeFinder method find.
/**
* Find changes matching the given identifier.
*
* @param id change identifier, either a numeric ID, a Change-Id, or project~branch~id triplet.
* @param user user to wrap in controls.
* @return possibly-empty list of controls for all matching changes, corresponding to the given
* user; may or may not be visible.
* @throws OrmException if an error occurred querying the database.
*/
public List<ChangeControl> find(String id, CurrentUser user) throws OrmException {
if (id.isEmpty()) {
return Collections.emptyList();
}
// Use the index to search for changes, but don't return any stored fields,
// to force rereading in case the index is stale.
InternalChangeQuery query = queryProvider.get().noFields();
int numTwiddles = 0;
for (char c : id.toCharArray()) {
if (c == '~') {
numTwiddles++;
}
}
if (numTwiddles == 1) {
// Try project~numericChangeId
String project = id.substring(0, id.indexOf('~'));
Integer n = Ints.tryParse(id.substring(project.length() + 1));
if (n != null) {
Change.Id changeId = new Change.Id(n);
try {
return ImmutableList.of(changeControlFactory.controlFor(reviewDb.get(), Project.NameKey.parse(project), changeId, user));
} catch (NoSuchChangeException e) {
return Collections.emptyList();
} catch (IllegalArgumentException e) {
String changeNotFound = String.format("change %s not found in ReviewDb", changeId);
String projectNotFound = String.format("passed project %s when creating ChangeNotes for %s, but actual project is", project, changeId);
if (e.getMessage().equals(changeNotFound) || e.getMessage().startsWith(projectNotFound)) {
return Collections.emptyList();
}
throw e;
} catch (OrmException e) {
// other OrmExceptions (failure in the persistence layer).
if (Throwables.getRootCause(e) instanceof RepositoryNotFoundException) {
return Collections.emptyList();
}
throw e;
}
}
} else if (numTwiddles == 2) {
// Try change triplet
Optional<ChangeTriplet> triplet = ChangeTriplet.parse(id);
if (triplet.isPresent()) {
return asChangeControls(query.byBranchKey(triplet.get().branch(), triplet.get().id()), user);
}
}
// Try numeric changeId
if (id.charAt(0) != '0') {
Integer n = Ints.tryParse(id);
if (n != null) {
return asChangeControls(query.byLegacyChangeId(new Change.Id(n)), user);
}
}
// Try isolated changeId
return asChangeControls(query.byKeyPrefix(id), user);
}
use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.
the class CommentsUtil method deleteCommentByRewritingHistory.
public void deleteCommentByRewritingHistory(ReviewDb db, ChangeUpdate update, Comment.Key commentKey, PatchSet.Id psId, String newMessage) throws OrmException {
if (PrimaryStorage.of(update.getChange()).equals(PrimaryStorage.REVIEW_DB)) {
PatchLineComment.Key key = new PatchLineComment.Key(new Patch.Key(psId, commentKey.filename), commentKey.uuid);
if (db instanceof BatchUpdateReviewDb) {
db = ((BatchUpdateReviewDb) db).unsafeGetDelegate();
}
db = ReviewDbUtil.unwrapDb(db);
PatchLineComment patchLineComment = db.patchComments().get(key);
if (!patchLineComment.getStatus().equals(PUBLISHED)) {
throw new OrmException(String.format("comment %s is not published", key));
}
patchLineComment.setMessage(newMessage);
db.patchComments().upsert(Collections.singleton(patchLineComment));
}
update.deleteCommentByRewritingHistory(commentKey.uuid, newMessage);
}
Aggregations