use of com.google.gerrit.server.project.InvalidChangeOperationException in project gerrit by GerritCodeReview.
the class ChangeEditModifier method createNewTree.
private static ObjectId createNewTree(Repository repository, RevCommit baseCommit, List<TreeModification> treeModifications) throws BadRequestException, IOException, InvalidChangeOperationException {
if (treeModifications.isEmpty()) {
return baseCommit.getTree();
}
ObjectId newTreeId;
try {
TreeCreator treeCreator = TreeCreator.basedOn(baseCommit);
treeCreator.addTreeModifications(treeModifications);
newTreeId = treeCreator.createNewTreeAndGetId(repository);
} catch (InvalidPathException e) {
throw new BadRequestException(e.getMessage());
}
if (ObjectId.isEqual(newTreeId, baseCommit.getTree())) {
throw new InvalidChangeOperationException("no changes were made");
}
return newTreeId;
}
use of com.google.gerrit.server.project.InvalidChangeOperationException in project gerrit by GerritCodeReview.
the class ChangeEditModifier method createEdit.
/**
* Creates a new change edit.
*
* @param repository the affected Git repository
* @param notes the {@link ChangeNotes} of the change for which the change edit should be created
* @throws AuthException if the user isn't authenticated or not allowed to use change edits
* @throws InvalidChangeOperationException if a change edit already existed for the change
*/
public void createEdit(Repository repository, ChangeNotes notes) throws AuthException, IOException, InvalidChangeOperationException, PermissionBackendException, ResourceConflictException {
assertCanEdit(notes);
Optional<ChangeEdit> changeEdit = lookupChangeEdit(notes);
if (changeEdit.isPresent()) {
throw new InvalidChangeOperationException(String.format("A change edit already exists for change %s", notes.getChangeId()));
}
PatchSet currentPatchSet = lookupCurrentPatchSet(notes);
ObjectId patchSetCommitId = currentPatchSet.commitId();
noteDbEdits.createEdit(repository, notes, currentPatchSet, patchSetCommitId, TimeUtil.now());
}
use of com.google.gerrit.server.project.InvalidChangeOperationException in project gerrit by GerritCodeReview.
the class SubmitWithStickyApprovalDiff method apply.
public String apply(ChangeNotes notes, CurrentUser currentUser) throws AuthException, IOException, PermissionBackendException, InvalidChangeOperationException {
PatchSet currentPatchset = notes.getCurrentPatchSet();
PatchSet.Id latestApprovedPatchsetId = getLatestApprovedPatchsetId(notes);
if (latestApprovedPatchsetId.get() == currentPatchset.id().get()) {
// If the latest approved patchset is the current patchset, no need to return anything.
return "";
}
StringBuilder diff = new StringBuilder(String.format("\n\n%d is the latest approved patch-set.\n", latestApprovedPatchsetId.get()));
Map<String, FileDiffOutput> modifiedFiles = listModifiedFiles(notes.getProjectName(), currentPatchset, notes.getPatchSets().get(latestApprovedPatchsetId));
// To make the message a bit more concise, we skip the magic files.
List<FileDiffOutput> modifiedFilesList = modifiedFiles.values().stream().filter(p -> !Patch.isMagic(p.newPath().orElse(""))).collect(Collectors.toList());
if (modifiedFilesList.isEmpty()) {
diff.append("No files were changed between the latest approved patch-set and the submitted one.\n");
return diff.toString();
}
diff.append("The change was submitted with unreviewed changes in the following files:\n\n");
TemporaryBuffer.Heap buffer = new TemporaryBuffer.Heap(Math.min(HEAP_EST_SIZE, DEFAULT_POST_SUBMIT_SIZE_LIMIT), DEFAULT_POST_SUBMIT_SIZE_LIMIT);
try (Repository repository = repositoryManager.openRepository(notes.getProjectName());
DiffFormatter formatter = new DiffFormatter(buffer)) {
formatter.setRepository(repository);
formatter.setDetectRenames(true);
boolean isDiffTooLarge = false;
List<String> formatterResult = null;
try {
formatter.format(modifiedFilesList.get(0).oldCommitId(), modifiedFilesList.get(0).newCommitId());
// This returns the diff for all the files.
formatterResult = Arrays.stream(RawParseUtils.decode(buffer.toByteArray()).split("\n")).collect(Collectors.toList());
} catch (IOException e) {
if (JGitText.get().inMemoryBufferLimitExceeded.equals(e.getMessage())) {
isDiffTooLarge = true;
} else {
throw e;
}
}
if (formatterResult != null) {
int addedBytes = formatterResult.stream().mapToInt(String::length).sum();
if (!CommentCumulativeSizeValidator.isEnoughSpace(notes, addedBytes, maxCumulativeSize)) {
isDiffTooLarge = true;
}
}
for (FileDiffOutput fileDiff : modifiedFilesList) {
diff.append(getDiffForFile(notes, currentPatchset.id(), latestApprovedPatchsetId, fileDiff, currentUser, formatterResult, isDiffTooLarge));
}
}
return diff.toString();
}
use of com.google.gerrit.server.project.InvalidChangeOperationException in project gerrit by GerritCodeReview.
the class GetFixPreview method apply.
@Override
public Response<Map<String, DiffInfo>> apply(FixResource resource) throws PermissionBackendException, ResourceNotFoundException, ResourceConflictException, AuthException, IOException, InvalidChangeOperationException {
Map<String, DiffInfo> result = new HashMap<>();
PatchSet patchSet = resource.getRevisionResource().getPatchSet();
ChangeNotes notes = resource.getRevisionResource().getNotes();
Change change = notes.getChange();
ProjectState state = projectCache.get(change.getProject()).orElseThrow(illegalState(change.getProject()));
Map<String, List<FixReplacement>> fixReplacementsPerFilePath = resource.getFixReplacements().stream().collect(groupingBy(fixReplacement -> fixReplacement.path));
try {
try (Repository git = repoManager.openRepository(notes.getProjectName())) {
for (Map.Entry<String, List<FixReplacement>> entry : fixReplacementsPerFilePath.entrySet()) {
String fileName = entry.getKey();
DiffInfo diffInfo = getFixPreviewForSingleFile(git, patchSet, state, notes, fileName, ImmutableList.copyOf(entry.getValue()));
result.put(fileName, diffInfo);
}
}
} catch (NoSuchChangeException e) {
throw new ResourceNotFoundException(e.getMessage(), e);
} catch (LargeObjectException e) {
throw new ResourceConflictException(e.getMessage(), e);
}
return Response.ok(result);
}
use of com.google.gerrit.server.project.InvalidChangeOperationException in project gerrit by GerritCodeReview.
the class BatchUpdate method wrapAndThrowException.
private static void wrapAndThrowException(Exception e) throws UpdateException, RestApiException {
// exception types.
if (e instanceof InvalidChangeOperationException || e instanceof LimitExceededException) {
throw new ResourceConflictException(e.getMessage(), e);
} else if (e instanceof NoSuchChangeException || e instanceof NoSuchRefException || e instanceof NoSuchProjectException) {
throw new ResourceNotFoundException(e.getMessage(), e);
} else if (e instanceof CommentsRejectedException) {
// status code and it's isolated in monitoring.
throw new BadRequestException(e.getMessage(), e);
}
Throwables.throwIfUnchecked(e);
// Propagate REST API exceptions thrown by operations; they commonly throw exceptions like
// ResourceConflictException to indicate an atomic update failure.
Throwables.throwIfInstanceOf(e, UpdateException.class);
Throwables.throwIfInstanceOf(e, RestApiException.class);
// Otherwise, wrap in a generic UpdateException, which does not include a user-visible message.
throw new UpdateException(e);
}
Aggregations