Merge "ChangeNotes.Factory: Add more methods to create change notes"
This commit is contained in:
		| @@ -54,6 +54,7 @@ import com.google.gerrit.server.change.Revisions; | ||||
| import com.google.gerrit.server.change.SubmittedTogether; | ||||
| import com.google.gerrit.server.change.SuggestReviewers; | ||||
| import com.google.gerrit.server.git.UpdateException; | ||||
| import com.google.gerrit.server.project.NoSuchChangeException; | ||||
| import com.google.gwtorm.server.OrmException; | ||||
| import com.google.inject.Inject; | ||||
| import com.google.inject.Provider; | ||||
| @@ -219,7 +220,8 @@ class ChangeApiImpl implements ChangeApi { | ||||
|   public ChangeApi revert(RevertInput in) throws RestApiException { | ||||
|     try { | ||||
|       return changeApi.id(revert.apply(change, in)._number); | ||||
|     } catch (OrmException | IOException | UpdateException e) { | ||||
|     } catch (OrmException | IOException | UpdateException | ||||
|         | NoSuchChangeException e) { | ||||
|       throw new RestApiException("Cannot revert change", e); | ||||
|     } | ||||
|   } | ||||
|   | ||||
| @@ -59,6 +59,7 @@ 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.git.UpdateException; | ||||
| import com.google.gerrit.server.project.NoSuchChangeException; | ||||
| import com.google.gwtorm.server.OrmException; | ||||
| import com.google.inject.Inject; | ||||
| import com.google.inject.Provider; | ||||
| @@ -214,7 +215,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) { | ||||
|     } catch (OrmException | EmailException | UpdateException | IOException | ||||
|         | NoSuchChangeException e) { | ||||
|       throw new RestApiException("Cannot rebase ps", e); | ||||
|     } | ||||
|   } | ||||
|   | ||||
| @@ -100,6 +100,7 @@ import com.google.gerrit.server.notedb.ChangeNotes; | ||||
| import com.google.gerrit.server.notedb.ReviewerStateInternal; | ||||
| import com.google.gerrit.server.patch.PatchListNotAvailableException; | ||||
| import com.google.gerrit.server.project.ChangeControl; | ||||
| import com.google.gerrit.server.project.NoSuchChangeException; | ||||
| import com.google.gerrit.server.project.ProjectCache; | ||||
| import com.google.gerrit.server.project.SubmitRuleEvaluator; | ||||
| import com.google.gerrit.server.query.change.ChangeData; | ||||
| @@ -157,7 +158,7 @@ public class ChangeJson { | ||||
|   private final Provider<ConsistencyChecker> checkerProvider; | ||||
|   private final ActionJson actionJson; | ||||
|   private final GpgApiAdapter gpgApi; | ||||
|   private final ChangeNotes.Factory changeNotesFactory; | ||||
|   private final ChangeNotes.Factory notesFactory; | ||||
|  | ||||
|   private AccountLoader accountLoader; | ||||
|   private Map<Change.Id, List<SubmitRecord>> submitRecords; | ||||
| @@ -183,7 +184,7 @@ public class ChangeJson { | ||||
|       Provider<ConsistencyChecker> checkerProvider, | ||||
|       ActionJson actionJson, | ||||
|       GpgApiAdapter gpgApi, | ||||
|       ChangeNotes.Factory changeNotesFactory, | ||||
|       ChangeNotes.Factory notesFactory, | ||||
|       @Assisted Set<ListChangesOption> options) { | ||||
|     this.db = db; | ||||
|     this.labelNormalizer = ln; | ||||
| @@ -203,7 +204,7 @@ public class ChangeJson { | ||||
|     this.checkerProvider = checkerProvider; | ||||
|     this.actionJson = actionJson; | ||||
|     this.gpgApi = gpgApi; | ||||
|     this.changeNotesFactory = changeNotesFactory; | ||||
|     this.notesFactory = notesFactory; | ||||
|     this.options = options.isEmpty() | ||||
|         ? EnumSet.noneOf(ListChangesOption.class) | ||||
|         : EnumSet.copyOf(options); | ||||
| @@ -223,17 +224,17 @@ public class ChangeJson { | ||||
|   } | ||||
|  | ||||
|   public ChangeInfo format(Project.NameKey project, Change.Id id) | ||||
|       throws OrmException { | ||||
|     ChangeNotes notes; | ||||
|       throws OrmException, NoSuchChangeException { | ||||
|     Change c; | ||||
|     try { | ||||
|       notes = changeNotesFactory.create(db.get(), project, id); | ||||
|     } catch (OrmException e) { | ||||
|       c = notesFactory.createChecked(db.get(), project, id).getChange(); | ||||
|     } catch (OrmException | NoSuchChangeException e) { | ||||
|       if (!has(CHECK)) { | ||||
|         throw e; | ||||
|       } | ||||
|       return checkOnly(changeDataFactory.create(db.get(), project, id)); | ||||
|     } | ||||
|     return format(changeDataFactory.create(db.get(), notes.getChange())); | ||||
|     return format(changeDataFactory.create(db.get(), c)); | ||||
|   } | ||||
|  | ||||
|   public ChangeInfo format(ChangeData cd) throws OrmException { | ||||
|   | ||||
| @@ -426,13 +426,12 @@ public class ConsistencyChecker { | ||||
|           continue; | ||||
|         } | ||||
|         try { | ||||
|           Change c = notesFactory | ||||
|               .create(db.get(), change.getProject(), psId.getParentKey()) | ||||
|               .getChange(); | ||||
|           if (c == null || !c.getDest().equals(change.getDest())) { | ||||
|           Change c = notesFactory.createChecked( | ||||
|               db.get(), change.getProject(), psId.getParentKey()).getChange(); | ||||
|           if (!c.getDest().equals(change.getDest())) { | ||||
|             continue; | ||||
|           } | ||||
|         } catch (OrmException e) { | ||||
|         } catch (OrmException | NoSuchChangeException e) { | ||||
|           warn(e); | ||||
|           // Include this patch set; should cause an error below, which is good. | ||||
|         } | ||||
| @@ -593,12 +592,8 @@ public class ConsistencyChecker { | ||||
|     try { | ||||
|       db.changes().beginTransaction(cid); | ||||
|       try { | ||||
|         ChangeNotes notes = notesFactory.create(db, project, cid); | ||||
|         ChangeNotes notes = notesFactory.createChecked(db, project, cid); | ||||
|         Change c = notes.getChange(); | ||||
|         if (c == null) { | ||||
|           throw new OrmException("Change missing: " + cid); | ||||
|         } | ||||
|  | ||||
|         if (psId.equals(c.currentPatchSetId())) { | ||||
|           List<PatchSet> all = Lists.newArrayList(db.patchSets().byChange(cid)); | ||||
|           if (all.size() == 1 && all.get(0).getId().equals(psId)) { | ||||
| @@ -638,7 +633,8 @@ public class ConsistencyChecker { | ||||
|       } finally { | ||||
|         db.rollback(); | ||||
|       } | ||||
|     } catch (PatchSetInfoNotAvailableException | OrmException e) { | ||||
|     } catch (PatchSetInfoNotAvailableException | OrmException | ||||
|         | NoSuchChangeException e) { | ||||
|       String msg = "Error deleting patch set"; | ||||
|       log.warn(msg + ' ' + psId, e); | ||||
|       p.status = Status.FIX_FAILED; | ||||
|   | ||||
| @@ -233,7 +233,7 @@ public class CreateChange implements | ||||
|           bu.execute(); | ||||
|         } | ||||
|         ChangeJson json = jsonFactory.create(ChangeJson.NO_OPTIONS); | ||||
|         return Response.created(json.format(project, changeId)); | ||||
|         return Response.created(json.format(ins.getChange())); | ||||
|       } | ||||
|  | ||||
|     } | ||||
|   | ||||
| @@ -38,6 +38,7 @@ import com.google.gerrit.server.git.UpdateException; | ||||
| import com.google.gerrit.server.git.validators.CommitValidators; | ||||
| import com.google.gerrit.server.notedb.ChangeNotes; | ||||
| import com.google.gerrit.server.project.ChangeControl; | ||||
| import com.google.gerrit.server.project.NoSuchChangeException; | ||||
| import com.google.gerrit.server.query.change.ChangeData; | ||||
| import com.google.gerrit.server.query.change.InternalChangeQuery; | ||||
| import com.google.gwtorm.server.OrmException; | ||||
| @@ -98,7 +99,7 @@ public class Rebase implements RestModifyView<RevisionResource, RebaseInput>, | ||||
|   @Override | ||||
|   public ChangeInfo apply(RevisionResource rsrc, RebaseInput input) | ||||
|       throws EmailException, OrmException, UpdateException, RestApiException, | ||||
|       IOException { | ||||
|       IOException, NoSuchChangeException { | ||||
|     ChangeControl control = rsrc.getControl(); | ||||
|     Change change = rsrc.getChange(); | ||||
|     try (Repository repo = repoManager.openRepository(change.getProject()); | ||||
| @@ -297,7 +298,7 @@ public class Rebase implements RestModifyView<RevisionResource, RebaseInput>, | ||||
|     @Override | ||||
|     public ChangeInfo apply(ChangeResource rsrc, RebaseInput input) | ||||
|         throws EmailException, OrmException, UpdateException, RestApiException, | ||||
|         IOException { | ||||
|         IOException, NoSuchChangeException { | ||||
|       PatchSet ps = | ||||
|           rebase.dbProvider.get().patchSets() | ||||
|               .get(rsrc.getChange().currentPatchSetId()); | ||||
|   | ||||
| @@ -58,7 +58,7 @@ public class Revert implements RestModifyView<ChangeResource, RevertInput>, | ||||
|   @Override | ||||
|   public ChangeInfo apply(ChangeResource req, RevertInput input) | ||||
|       throws IOException, OrmException, RestApiException, | ||||
|       UpdateException { | ||||
|       UpdateException, NoSuchChangeException { | ||||
|     RefControl refControl = req.getControl().getRefControl(); | ||||
|     Change change = req.getChange(); | ||||
|     if (!refControl.canUpload()) { | ||||
|   | ||||
| @@ -45,8 +45,12 @@ import com.google.gerrit.reviewdb.server.ReviewDbUtil; | ||||
| import com.google.gerrit.server.config.AllUsersName; | ||||
| import com.google.gerrit.server.config.AllUsersNameProvider; | ||||
| import com.google.gerrit.server.git.GitRepositoryManager; | ||||
| import com.google.gerrit.server.project.NoSuchChangeException; | ||||
| import com.google.gerrit.server.query.change.ChangeData; | ||||
| import com.google.gerrit.server.query.change.InternalChangeQuery; | ||||
| import com.google.gwtorm.server.OrmException; | ||||
| import com.google.inject.Inject; | ||||
| import com.google.inject.Provider; | ||||
| import com.google.inject.Singleton; | ||||
|  | ||||
| import org.eclipse.jgit.errors.ConfigInvalidException; | ||||
| @@ -55,9 +59,12 @@ import org.eclipse.jgit.lib.ObjectId; | ||||
| import org.eclipse.jgit.lib.PersonIdent; | ||||
| import org.eclipse.jgit.notes.NoteMap; | ||||
| import org.eclipse.jgit.revwalk.RevWalk; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| import java.io.IOException; | ||||
| import java.sql.Timestamp; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| /** View of a single {@link Change} based on the log of its notes branch. */ | ||||
| @@ -103,18 +110,56 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> { | ||||
|  | ||||
|   @Singleton | ||||
|   public static class Factory { | ||||
|     private static final Logger log = LoggerFactory.getLogger(Factory.class); | ||||
|  | ||||
|     private final GitRepositoryManager repoManager; | ||||
|     private final NotesMigration migration; | ||||
|     private final AllUsersNameProvider allUsersProvider; | ||||
|     private final Provider<InternalChangeQuery> queryProvider; | ||||
|  | ||||
|     @VisibleForTesting | ||||
|     @Inject | ||||
|     public Factory(GitRepositoryManager repoManager, | ||||
|         NotesMigration migration, | ||||
|         AllUsersNameProvider allUsersProvider) { | ||||
|         AllUsersNameProvider allUsersProvider, | ||||
|         Provider<InternalChangeQuery> queryProvider) { | ||||
|       this.repoManager = repoManager; | ||||
|       this.migration = migration; | ||||
|       this.allUsersProvider = allUsersProvider; | ||||
|       this.queryProvider = queryProvider; | ||||
|     } | ||||
|  | ||||
|     public ChangeNotes createChecked(ReviewDb db, Change c) | ||||
|         throws OrmException, NoSuchChangeException { | ||||
|       ChangeNotes notes = create(db, c.getProject(), c.getId()); | ||||
|       if (notes.getChange() == null) { | ||||
|         throw new NoSuchChangeException(c.getId()); | ||||
|       } | ||||
|       return notes; | ||||
|     } | ||||
|  | ||||
|     public ChangeNotes createChecked(ReviewDb db, Project.NameKey project, | ||||
|         Change.Id changeId) throws OrmException, NoSuchChangeException { | ||||
|       ChangeNotes notes = create(db, project, changeId); | ||||
|       if (notes.getChange() == null) { | ||||
|         throw new NoSuchChangeException(changeId); | ||||
|       } | ||||
|       return notes; | ||||
|     } | ||||
|  | ||||
|     public ChangeNotes createChecked(Change.Id changeId) | ||||
|         throws OrmException, NoSuchChangeException { | ||||
|       InternalChangeQuery query = queryProvider.get().noFields(); | ||||
|       List<ChangeData> changes = query.byLegacyChangeId(changeId); | ||||
|       if (changes.isEmpty()) { | ||||
|         throw new NoSuchChangeException(changeId); | ||||
|       } | ||||
|       if (changes.size() != 1) { | ||||
|         log.error( | ||||
|             String.format("Multiple changes found for %d", changeId.get())); | ||||
|         throw new NoSuchChangeException(changeId); | ||||
|       } | ||||
|       return changes.get(0).notes(); | ||||
|     } | ||||
|  | ||||
|     public ChangeNotes create(ReviewDb db, Project.NameKey project, | ||||
|   | ||||
| @@ -29,7 +29,6 @@ import com.google.gerrit.reviewdb.client.PatchSetApproval; | ||||
| import com.google.gerrit.reviewdb.client.Project; | ||||
| import com.google.gerrit.reviewdb.server.ReviewDb; | ||||
| import com.google.gerrit.server.ApprovalsUtil; | ||||
| import com.google.gerrit.server.ChangeFinder; | ||||
| import com.google.gerrit.server.CurrentUser; | ||||
| import com.google.gerrit.server.notedb.ChangeNotes; | ||||
| import com.google.gerrit.server.query.change.ChangeData; | ||||
| @@ -48,16 +47,13 @@ public class ChangeControl { | ||||
|   public static class GenericFactory { | ||||
|     private final ProjectControl.GenericFactory projectControl; | ||||
|     private final ChangeNotes.Factory notesFactory; | ||||
|     private final ChangeFinder changeFinder; | ||||
|  | ||||
|     @Inject | ||||
|     GenericFactory( | ||||
|         ProjectControl.GenericFactory p, | ||||
|         ChangeNotes.Factory n, | ||||
|         ChangeFinder f) { | ||||
|         ChangeNotes.Factory n) { | ||||
|       projectControl = p; | ||||
|       notesFactory = n; | ||||
|       changeFinder = f; | ||||
|     } | ||||
|  | ||||
|     public ChangeControl controlFor(ReviewDb db, Project.NameKey project, | ||||
| @@ -92,13 +88,12 @@ public class ChangeControl { | ||||
|  | ||||
|     public ChangeControl validateFor(ReviewDb db, Change.Id changeId, | ||||
|         CurrentUser user) throws NoSuchChangeException, OrmException { | ||||
|       ChangeControl ctl = changeFinder.findOne(changeId, user); | ||||
|       return validateFor(db, ctl.getChange(), user); | ||||
|       return validateFor(db, notesFactory.createChecked(changeId), user); | ||||
|     } | ||||
|  | ||||
|     public ChangeControl validateFor(ReviewDb db, Change change, | ||||
|     public ChangeControl validateFor(ReviewDb db, ChangeNotes notes, | ||||
|         CurrentUser user) throws NoSuchChangeException, OrmException { | ||||
|       ChangeControl c = controlFor(db, change, user); | ||||
|       ChangeControl c = controlFor(notes, user); | ||||
|       if (!c.isVisible(db)) { | ||||
|         throw new NoSuchChangeException(c.getId()); | ||||
|       } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Edwin Kempin
					Edwin Kempin