diff options
Diffstat (limited to 'gerrit-server/src/main/java/com/google/gerrit/server/api/changes/RevisionApiImpl.java')
-rw-r--r-- | gerrit-server/src/main/java/com/google/gerrit/server/api/changes/RevisionApiImpl.java | 266 |
1 files changed, 180 insertions, 86 deletions
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/RevisionApiImpl.java b/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/RevisionApiImpl.java index 43be8df9fd..2eb55a5e12 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/RevisionApiImpl.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/RevisionApiImpl.java @@ -14,8 +14,12 @@ package com.google.gerrit.server.api.changes; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.gerrit.server.api.ApiUtil.asRestApiException; + import com.google.common.collect.ImmutableSet; -import com.google.gerrit.common.errors.EmailException; +import com.google.common.collect.ListMultimap; +import com.google.common.collect.MultimapBuilder.ListMultimapBuilder; import com.google.gerrit.extensions.api.changes.ChangeApi; import com.google.gerrit.extensions.api.changes.Changes; import com.google.gerrit.extensions.api.changes.CherryPickInput; @@ -24,15 +28,19 @@ import com.google.gerrit.extensions.api.changes.DraftApi; import com.google.gerrit.extensions.api.changes.DraftInput; import com.google.gerrit.extensions.api.changes.FileApi; import com.google.gerrit.extensions.api.changes.RebaseInput; +import com.google.gerrit.extensions.api.changes.RelatedChangesInfo; import com.google.gerrit.extensions.api.changes.ReviewInput; +import com.google.gerrit.extensions.api.changes.ReviewResult; import com.google.gerrit.extensions.api.changes.RevisionApi; import com.google.gerrit.extensions.api.changes.RevisionReviewerApi; import com.google.gerrit.extensions.api.changes.RobotCommentApi; import com.google.gerrit.extensions.api.changes.SubmitInput; import com.google.gerrit.extensions.client.SubmitType; import com.google.gerrit.extensions.common.ActionInfo; +import com.google.gerrit.extensions.common.ApprovalInfo; import com.google.gerrit.extensions.common.CommentInfo; import com.google.gerrit.extensions.common.CommitInfo; +import com.google.gerrit.extensions.common.EditInfo; import com.google.gerrit.extensions.common.FileInfo; import com.google.gerrit.extensions.common.MergeableInfo; import com.google.gerrit.extensions.common.RobotCommentInfo; @@ -41,16 +49,25 @@ import com.google.gerrit.extensions.restapi.BinaryResult; import com.google.gerrit.extensions.restapi.IdString; import com.google.gerrit.extensions.restapi.RestApiException; import com.google.gerrit.extensions.restapi.RestModifyView; +import com.google.gerrit.reviewdb.client.PatchSetApproval; +import com.google.gerrit.reviewdb.server.ReviewDb; +import com.google.gerrit.server.ApprovalsUtil; +import com.google.gerrit.server.account.AccountDirectory.FillOptions; +import com.google.gerrit.server.account.AccountLoader; +import com.google.gerrit.server.change.ApplyFix; +import com.google.gerrit.server.change.ChangeJson; import com.google.gerrit.server.change.CherryPick; import com.google.gerrit.server.change.Comments; import com.google.gerrit.server.change.CreateDraftComment; -import com.google.gerrit.server.change.DeleteDraftPatchSet; import com.google.gerrit.server.change.DraftComments; import com.google.gerrit.server.change.FileResource; import com.google.gerrit.server.change.Files; +import com.google.gerrit.server.change.Fixes; +import com.google.gerrit.server.change.GetCommit; import com.google.gerrit.server.change.GetDescription; import com.google.gerrit.server.change.GetMergeList; import com.google.gerrit.server.change.GetPatch; +import com.google.gerrit.server.change.GetRelated; import com.google.gerrit.server.change.GetRevisionActions; import com.google.gerrit.server.change.ListRevisionComments; import com.google.gerrit.server.change.ListRevisionDrafts; @@ -58,7 +75,6 @@ import com.google.gerrit.server.change.ListRobotComments; import com.google.gerrit.server.change.Mergeable; import com.google.gerrit.server.change.PostReview; import com.google.gerrit.server.change.PreviewSubmit; -import com.google.gerrit.server.change.PublishDraftPatchSet; import com.google.gerrit.server.change.PutDescription; import com.google.gerrit.server.change.Rebase; import com.google.gerrit.server.change.RebaseUtil; @@ -69,13 +85,10 @@ import com.google.gerrit.server.change.RobotComments; import com.google.gerrit.server.change.Submit; import com.google.gerrit.server.change.TestSubmitType; import com.google.gerrit.server.git.GitRepositoryManager; -import com.google.gerrit.server.patch.PatchListNotAvailableException; -import com.google.gerrit.server.update.UpdateException; -import com.google.gwtorm.server.OrmException; import com.google.inject.Inject; import com.google.inject.Provider; import com.google.inject.assistedinject.Assisted; -import java.io.IOException; +import java.util.EnumSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -92,23 +105,24 @@ class RevisionApiImpl implements RevisionApi { private final RevisionReviewers revisionReviewers; private final RevisionReviewerApiImpl.Factory revisionReviewerApi; private final CherryPick cherryPick; - private final DeleteDraftPatchSet deleteDraft; private final Rebase rebase; private final RebaseUtil rebaseUtil; private final Submit submit; private final PreviewSubmit submitPreview; - private final PublishDraftPatchSet publish; private final Reviewed.PutReviewed putReviewed; private final Reviewed.DeleteReviewed deleteReviewed; private final RevisionResource revision; private final Files files; private final Files.ListFiles listFiles; + private final GetCommit getCommit; private final GetPatch getPatch; private final PostReview review; private final Mergeable mergeable; private final FileApiImpl.Factory fileApi; private final ListRevisionComments listComments; private final ListRobotComments listRobotComments; + private final ApplyFix applyFix; + private final Fixes fixes; private final ListRevisionDrafts listDrafts; private final CreateDraftComment createDraft; private final DraftComments drafts; @@ -121,8 +135,12 @@ class RevisionApiImpl implements RevisionApi { private final TestSubmitType testSubmitType; private final TestSubmitType.Get getSubmitType; private final Provider<GetMergeList> getMergeList; + private final GetRelated getRelated; private final PutDescription putDescription; private final GetDescription getDescription; + private final ApprovalsUtil approvalsUtil; + private final Provider<ReviewDb> db; + private final AccountLoader.Factory accountLoaderFactory; @Inject RevisionApiImpl( @@ -131,22 +149,23 @@ class RevisionApiImpl implements RevisionApi { RevisionReviewers revisionReviewers, RevisionReviewerApiImpl.Factory revisionReviewerApi, CherryPick cherryPick, - DeleteDraftPatchSet deleteDraft, Rebase rebase, RebaseUtil rebaseUtil, Submit submit, PreviewSubmit submitPreview, - PublishDraftPatchSet publish, Reviewed.PutReviewed putReviewed, Reviewed.DeleteReviewed deleteReviewed, Files files, Files.ListFiles listFiles, + GetCommit getCommit, GetPatch getPatch, PostReview review, Mergeable mergeable, FileApiImpl.Factory fileApi, ListRevisionComments listComments, ListRobotComments listRobotComments, + ApplyFix applyFix, + Fixes fixes, ListRevisionDrafts listDrafts, CreateDraftComment createDraft, DraftComments drafts, @@ -159,31 +178,36 @@ class RevisionApiImpl implements RevisionApi { TestSubmitType testSubmitType, TestSubmitType.Get getSubmitType, Provider<GetMergeList> getMergeList, + GetRelated getRelated, PutDescription putDescription, GetDescription getDescription, + ApprovalsUtil approvalsUtil, + Provider<ReviewDb> db, + AccountLoader.Factory accountLoaderFactory, @Assisted RevisionResource r) { this.repoManager = repoManager; this.changes = changes; this.revisionReviewers = revisionReviewers; this.revisionReviewerApi = revisionReviewerApi; this.cherryPick = cherryPick; - this.deleteDraft = deleteDraft; this.rebase = rebase; this.rebaseUtil = rebaseUtil; this.review = review; this.submit = submit; this.submitPreview = submitPreview; - this.publish = publish; this.files = files; this.putReviewed = putReviewed; this.deleteReviewed = deleteReviewed; this.listFiles = listFiles; + this.getCommit = getCommit; this.getPatch = getPatch; this.mergeable = mergeable; this.fileApi = fileApi; this.listComments = listComments; this.robotComments = robotComments; this.listRobotComments = listRobotComments; + this.applyFix = applyFix; + this.fixes = fixes; this.listDrafts = listDrafts; this.createDraft = createDraft; this.drafts = drafts; @@ -195,17 +219,21 @@ class RevisionApiImpl implements RevisionApi { this.testSubmitType = testSubmitType; this.getSubmitType = getSubmitType; this.getMergeList = getMergeList; + this.getRelated = getRelated; this.putDescription = putDescription; this.getDescription = getDescription; + this.approvalsUtil = approvalsUtil; + this.db = db; + this.accountLoaderFactory = accountLoaderFactory; this.revision = r; } @Override - public void review(ReviewInput in) throws RestApiException { + public ReviewResult review(ReviewInput in) throws RestApiException { try { - review.apply(revision, in); - } catch (OrmException | UpdateException | IOException e) { - throw new RestApiException("Cannot post review", e); + return review.apply(revision, in).value(); + } catch (Exception e) { + throw asRestApiException("Cannot post review", e); } } @@ -219,8 +247,8 @@ class RevisionApiImpl implements RevisionApi { public void submit(SubmitInput in) throws RestApiException { try { submit.apply(revision, in); - } catch (OrmException | IOException e) { - throw new RestApiException("Cannot submit change", e); + } catch (Exception e) { + throw asRestApiException("Cannot submit change", e); } } @@ -234,27 +262,19 @@ class RevisionApiImpl implements RevisionApi { try { submitPreview.setFormat(format); return submitPreview.apply(revision); - } catch (OrmException e) { - throw new RestApiException("Cannot get submit preview", e); + } catch (Exception e) { + throw asRestApiException("Cannot get submit preview", e); } } @Override public void publish() throws RestApiException { - try { - publish.apply(revision, new PublishDraftPatchSet.Input()); - } catch (UpdateException e) { - throw new RestApiException("Cannot publish draft patch set", e); - } + throw new UnsupportedOperationException("draft workflow is discontinued"); } @Override public void delete() throws RestApiException { - try { - deleteDraft.apply(revision, null); - } catch (UpdateException e) { - throw new RestApiException("Cannot delete draft ps", e); - } + throw new UnsupportedOperationException("draft workflow is discontinued"); } @Override @@ -267,8 +287,8 @@ class RevisionApiImpl implements RevisionApi { public ChangeApi rebase(RebaseInput in) throws RestApiException { try { return changes.id(rebase.apply(revision, in)._number); - } catch (OrmException | EmailException | UpdateException | IOException e) { - throw new RestApiException("Cannot rebase ps", e); + } catch (Exception e) { + throw asRestApiException("Cannot rebase ps", e); } } @@ -277,8 +297,8 @@ class RevisionApiImpl implements RevisionApi { try (Repository repo = repoManager.openRepository(revision.getProject()); RevWalk rw = new RevWalk(repo)) { return rebaseUtil.canRebase(revision.getPatchSet(), revision.getChange().getDest(), repo, rw); - } catch (IOException e) { - throw new RestApiException("Cannot check if rebase is possible", e); + } catch (Exception e) { + throw asRestApiException("Cannot check if rebase is possible", e); } } @@ -286,8 +306,8 @@ class RevisionApiImpl implements RevisionApi { public ChangeApi cherryPick(CherryPickInput in) throws RestApiException { try { return changes.id(cherryPick.apply(revision, in)._number); - } catch (OrmException | IOException | UpdateException e) { - throw new RestApiException("Cannot cherry pick", e); + } catch (Exception e) { + throw asRestApiException("Cannot cherry pick", e); } } @@ -296,8 +316,8 @@ class RevisionApiImpl implements RevisionApi { try { return revisionReviewerApi.create( revisionReviewers.parse(revision, IdString.fromDecoded(id))); - } catch (OrmException e) { - throw new RestApiException("Cannot parse reviewer", e); + } catch (Exception e) { + throw asRestApiException("Cannot parse reviewer", e); } } @@ -312,7 +332,7 @@ class RevisionApiImpl implements RevisionApi { } view.apply(files.parse(revision, IdString.fromDecoded(path)), new Reviewed.Input()); } catch (Exception e) { - throw new RestApiException("Cannot update reviewed flag", e); + throw asRestApiException("Cannot update reviewed flag", e); } } @@ -322,8 +342,8 @@ class RevisionApiImpl implements RevisionApi { try { return ImmutableSet.copyOf( (Iterable<String>) listFiles.setReviewed(true).apply(revision).value()); - } catch (OrmException | IOException | PatchListNotAvailableException e) { - throw new RestApiException("Cannot list reviewed files", e); + } catch (Exception e) { + throw asRestApiException("Cannot list reviewed files", e); } } @@ -331,8 +351,8 @@ class RevisionApiImpl implements RevisionApi { public MergeableInfo mergeable() throws RestApiException { try { return mergeable.apply(revision); - } catch (OrmException | IOException e) { - throw new RestApiException("Cannot check mergeability", e); + } catch (Exception e) { + throw asRestApiException("Cannot check mergeability", e); } } @@ -341,8 +361,8 @@ class RevisionApiImpl implements RevisionApi { try { mergeable.setOtherBranches(true); return mergeable.apply(revision); - } catch (OrmException | IOException e) { - throw new RestApiException("Cannot check mergeability", e); + } catch (Exception e) { + throw asRestApiException("Cannot check mergeability", e); } } @@ -351,8 +371,8 @@ class RevisionApiImpl implements RevisionApi { public Map<String, FileInfo> files() throws RestApiException { try { return (Map<String, FileInfo>) listFiles.apply(revision).value(); - } catch (OrmException | IOException | PatchListNotAvailableException e) { - throw new RestApiException("Cannot retrieve files", e); + } catch (Exception e) { + throw asRestApiException("Cannot retrieve files", e); } } @@ -361,8 +381,8 @@ class RevisionApiImpl implements RevisionApi { public Map<String, FileInfo> files(String base) throws RestApiException { try { return (Map<String, FileInfo>) listFiles.setBase(base).apply(revision).value(); - } catch (OrmException | IOException | PatchListNotAvailableException e) { - throw new RestApiException("Cannot retrieve files", e); + } catch (Exception e) { + throw asRestApiException("Cannot retrieve files", e); } } @@ -371,8 +391,19 @@ class RevisionApiImpl implements RevisionApi { public Map<String, FileInfo> files(int parentNum) throws RestApiException { try { return (Map<String, FileInfo>) listFiles.setParent(parentNum).apply(revision).value(); - } catch (OrmException | IOException | PatchListNotAvailableException e) { - throw new RestApiException("Cannot retrieve files", e); + } catch (Exception e) { + throw asRestApiException("Cannot retrieve files", e); + } + } + + @SuppressWarnings("unchecked") + @Override + public List<String> queryFiles(String query) throws RestApiException { + try { + checkArgument(query != null, "no query provided"); + return (List<String>) listFiles.setQuery(query).apply(revision).value(); + } catch (Exception e) { + throw asRestApiException("Cannot retrieve files", e); } } @@ -382,11 +413,20 @@ class RevisionApiImpl implements RevisionApi { } @Override + public CommitInfo commit(boolean addLinks) throws RestApiException { + try { + return getCommit.setAddLinks(addLinks).apply(revision).value(); + } catch (Exception e) { + throw asRestApiException("Cannot retrieve commit", e); + } + } + + @Override public Map<String, List<CommentInfo>> comments() throws RestApiException { try { return listComments.apply(revision); - } catch (OrmException e) { - throw new RestApiException("Cannot retrieve comments", e); + } catch (Exception e) { + throw asRestApiException("Cannot retrieve comments", e); } } @@ -394,8 +434,8 @@ class RevisionApiImpl implements RevisionApi { public Map<String, List<RobotCommentInfo>> robotComments() throws RestApiException { try { return listRobotComments.apply(revision); - } catch (OrmException e) { - throw new RestApiException("Cannot retrieve robot comments", e); + } catch (Exception e) { + throw asRestApiException("Cannot retrieve robot comments", e); } } @@ -403,8 +443,8 @@ class RevisionApiImpl implements RevisionApi { public List<CommentInfo> commentsAsList() throws RestApiException { try { return listComments.getComments(revision); - } catch (OrmException e) { - throw new RestApiException("Cannot retrieve comments", e); + } catch (Exception e) { + throw asRestApiException("Cannot retrieve comments", e); } } @@ -412,8 +452,8 @@ class RevisionApiImpl implements RevisionApi { public Map<String, List<CommentInfo>> drafts() throws RestApiException { try { return listDrafts.apply(revision); - } catch (OrmException e) { - throw new RestApiException("Cannot retrieve drafts", e); + } catch (Exception e) { + throw asRestApiException("Cannot retrieve drafts", e); } } @@ -421,8 +461,17 @@ class RevisionApiImpl implements RevisionApi { public List<RobotCommentInfo> robotCommentsAsList() throws RestApiException { try { return listRobotComments.getComments(revision); - } catch (OrmException e) { - throw new RestApiException("Cannot retrieve robot comments", e); + } catch (Exception e) { + throw asRestApiException("Cannot retrieve robot comments", e); + } + } + + @Override + public EditInfo applyFix(String fixId) throws RestApiException { + try { + return applyFix.apply(fixes.parse(revision, IdString.fromDecoded(fixId)), null).value(); + } catch (Exception e) { + throw asRestApiException("Cannot apply fix", e); } } @@ -430,8 +479,8 @@ class RevisionApiImpl implements RevisionApi { public List<CommentInfo> draftsAsList() throws RestApiException { try { return listDrafts.getComments(revision); - } catch (OrmException e) { - throw new RestApiException("Cannot retrieve drafts", e); + } catch (Exception e) { + throw asRestApiException("Cannot retrieve drafts", e); } } @@ -439,8 +488,8 @@ class RevisionApiImpl implements RevisionApi { public DraftApi draft(String id) throws RestApiException { try { return draftFactory.create(drafts.parse(revision, IdString.fromDecoded(id))); - } catch (OrmException e) { - throw new RestApiException("Cannot retrieve draft", e); + } catch (Exception e) { + throw asRestApiException("Cannot retrieve draft", e); } } @@ -453,8 +502,8 @@ class RevisionApiImpl implements RevisionApi { .id(revision.getChange().getId().get()) .revision(revision.getPatchSet().getId().get()) .draft(id); - } catch (UpdateException | OrmException e) { - throw new RestApiException("Cannot create draft", e); + } catch (Exception e) { + throw asRestApiException("Cannot create draft", e); } } @@ -462,8 +511,8 @@ class RevisionApiImpl implements RevisionApi { public CommentApi comment(String id) throws RestApiException { try { return commentFactory.create(comments.parse(revision, IdString.fromDecoded(id))); - } catch (OrmException e) { - throw new RestApiException("Cannot retrieve comment", e); + } catch (Exception e) { + throw asRestApiException("Cannot retrieve comment", e); } } @@ -471,8 +520,8 @@ class RevisionApiImpl implements RevisionApi { public RobotCommentApi robotComment(String id) throws RestApiException { try { return robotCommentFactory.create(robotComments.parse(revision, IdString.fromDecoded(id))); - } catch (OrmException e) { - throw new RestApiException("Cannot retrieve robot comment", e); + } catch (Exception e) { + throw asRestApiException("Cannot retrieve robot comment", e); } } @@ -480,8 +529,8 @@ class RevisionApiImpl implements RevisionApi { public BinaryResult patch() throws RestApiException { try { return getPatch.apply(revision); - } catch (IOException e) { - throw new RestApiException("Cannot get patch", e); + } catch (Exception e) { + throw asRestApiException("Cannot get patch", e); } } @@ -489,8 +538,8 @@ class RevisionApiImpl implements RevisionApi { public BinaryResult patch(String path) throws RestApiException { try { return getPatch.setPath(path).apply(revision); - } catch (IOException e) { - throw new RestApiException("Cannot get patch", e); + } catch (Exception e) { + throw asRestApiException("Cannot get patch", e); } } @@ -498,8 +547,8 @@ class RevisionApiImpl implements RevisionApi { public Map<String, ActionInfo> actions() throws RestApiException { try { return revisionActions.apply(revision).value(); - } catch (OrmException e) { - throw new RestApiException("Cannot get actions", e); + } catch (Exception e) { + throw asRestApiException("Cannot get actions", e); } } @@ -507,8 +556,8 @@ class RevisionApiImpl implements RevisionApi { public SubmitType submitType() throws RestApiException { try { return getSubmitType.apply(revision); - } catch (OrmException e) { - throw new RestApiException("Cannot get submit type", e); + } catch (Exception e) { + throw asRestApiException("Cannot get submit type", e); } } @@ -516,8 +565,8 @@ class RevisionApiImpl implements RevisionApi { public SubmitType testSubmitType(TestSubmitRuleInput in) throws RestApiException { try { return testSubmitType.apply(revision, in); - } catch (OrmException e) { - throw new RestApiException("Cannot test submit type", e); + } catch (Exception e) { + throw asRestApiException("Cannot test submit type", e); } } @@ -531,21 +580,66 @@ class RevisionApiImpl implements RevisionApi { gml.setUninterestingParent(getUninterestingParent()); gml.setAddLinks(getAddLinks()); return gml.apply(revision).value(); - } catch (IOException e) { - throw new RestApiException("Cannot get merge list", e); + } catch (Exception e) { + throw asRestApiException("Cannot get merge list", e); } } }; } @Override + public RelatedChangesInfo related() throws RestApiException { + try { + return getRelated.apply(revision); + } catch (Exception e) { + throw asRestApiException("Cannot get related changes", e); + } + } + + @Override + public ListMultimap<String, ApprovalInfo> votes() throws RestApiException { + ListMultimap<String, ApprovalInfo> result = + ListMultimapBuilder.treeKeys().arrayListValues().build(); + try { + Iterable<PatchSetApproval> approvals = + approvalsUtil.byPatchSet( + db.get(), + revision.getNotes(), + revision.getChangeResource().getUser(), + revision.getPatchSet().getId(), + null, + null); + AccountLoader accountLoader = + accountLoaderFactory.create( + EnumSet.of( + FillOptions.ID, FillOptions.NAME, FillOptions.EMAIL, FillOptions.USERNAME)); + for (PatchSetApproval approval : approvals) { + String label = approval.getLabel(); + ApprovalInfo info = + ChangeJson.getApprovalInfo( + approval.getAccountId(), + Integer.valueOf(approval.getValue()), + null, + approval.getTag(), + approval.getGranted()); + accountLoader.put(info); + result.get(label).add(info); + } + accountLoader.fill(); + } catch (Exception e) { + throw asRestApiException("Cannot get votes", e); + } + return result; + } + + @Override public void description(String description) throws RestApiException { PutDescription.Input in = new PutDescription.Input(); in.description = description; try { putDescription.apply(revision, in); - } catch (UpdateException e) { - throw new RestApiException("Cannot set description", e); + } catch (Exception e) { + throw asRestApiException("Cannot set description", e); } } |