ChangeNotes: Add special factory method for new changes

When a new change is created the Change instance must be passed into
ChangeNotes, because there is no notes branch yet from which the
change can be read. Add an extra factory method for this so that this
case can be differentiated from instantiating ChangeNotes for
existing changes. Then in a later step we can change the method
signature of the factory method for existing changes to require a
project name + a change ID instead of a change.

Change-Id: I3adc5c5814c4201541834d25f48b50a1d886895a
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2016-01-29 14:37:34 +01:00
parent 1523e7d3bf
commit 416dd77400
6 changed files with 28 additions and 4 deletions

View File

@@ -397,6 +397,7 @@ public class BatchUpdate implements AutoCloseable {
private final GitRepositoryManager repoManager;
private final ChangeIndexer indexer;
private final ChangeControl.GenericFactory changeControlFactory;
private final ChangeNotes.Factory changeNotesFactory;
private final ChangeUpdate.Factory changeUpdateFactory;
private final GitReferenceUpdated gitRefUpdated;
private final NotesMigration notesMigration;
@@ -424,6 +425,7 @@ public class BatchUpdate implements AutoCloseable {
BatchUpdate(GitRepositoryManager repoManager,
ChangeIndexer indexer,
ChangeControl.GenericFactory changeControlFactory,
ChangeNotes.Factory changeNotesFactory,
ChangeUpdate.Factory changeUpdateFactory,
GitReferenceUpdated gitRefUpdated,
NotesMigration notesMigration,
@@ -437,6 +439,7 @@ public class BatchUpdate implements AutoCloseable {
this.repoManager = repoManager;
this.indexer = indexer;
this.changeControlFactory = changeControlFactory;
this.changeNotesFactory = changeNotesFactory;
this.changeUpdateFactory = changeUpdateFactory;
this.gitRefUpdated = gitRefUpdated;
this.notesMigration = notesMigration;
@@ -628,8 +631,9 @@ public class BatchUpdate implements AutoCloseable {
// Pass in preloaded change to controlFor, to avoid:
// - reading from a db that does not belong to this update
// - attempting to read a change that doesn't exist yet
ChangeNotes notes = changeNotesFactory.createForNew(c);
ChangeContext ctx = new ChangeContext(
changeControlFactory.controlFor(c, user), new BatchUpdateReviewDb(db));
changeControlFactory.controlFor(notes, user), new BatchUpdateReviewDb(db));
if (notesMigration.readChanges()) {
ctx.getNotes().load();
}

View File

@@ -89,7 +89,7 @@ public abstract class AbstractChangeNotes<T> extends VersionedMetaData {
* @return the NameKey for the project where the notes should be stored,
* which is not necessarily the same as the change's project.
*/
protected abstract Project.NameKey getProjectName();
public abstract Project.NameKey getProjectName();
@SuppressWarnings("unchecked")
protected final T self() {

View File

@@ -118,6 +118,10 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
public ChangeNotes create(Change change) {
return new ChangeNotes(repoManager, migration, allUsersProvider, change);
}
public ChangeNotes createForNew(Change change) {
return new ChangeNotes(repoManager, migration, allUsersProvider, change);
}
}
private final Change change;
@@ -345,7 +349,7 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
}
@Override
protected Project.NameKey getProjectName() {
public Project.NameKey getProjectName() {
return getChange().getProject();
}
}

View File

@@ -134,7 +134,7 @@ public class DraftCommentNotes extends AbstractChangeNotes<DraftCommentNotes> {
}
@Override
protected Project.NameKey getProjectName() {
public Project.NameKey getProjectName() {
return draftsProject;
}
}

View File

@@ -77,6 +77,16 @@ public class ChangeControl {
}
}
public ChangeControl controlFor(ChangeNotes notes, CurrentUser user)
throws NoSuchChangeException {
try {
return projectControl.controlFor(notes.getProjectName(), user)
.controlFor(notes);
} catch (NoSuchProjectException | IOException e) {
throw new NoSuchChangeException(notes.getChangeId(), e);
}
}
public ChangeControl validateFor(Change.Id changeId, CurrentUser user)
throws NoSuchChangeException, OrmException {
Change change = db.get().changes().get(changeId);

View File

@@ -43,6 +43,7 @@ import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.TagCache;
import com.google.gerrit.server.git.VisibleRefFilter;
import com.google.gerrit.server.group.SystemGroupBackend;
import com.google.gerrit.server.notedb.ChangeNotes;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted;
@@ -196,6 +197,11 @@ public class ProjectControl {
return changeControlFactory.create(controlForRef(change.getDest()), change);
}
public ChangeControl controlFor(ChangeNotes notes) {
return changeControlFactory
.create(controlForRef(notes.getChange().getDest()), notes);
}
public RefControl controlForRef(Branch.NameKey ref) {
return controlForRef(ref.get());
}