Factor out an arguments class for ChangeNotes

We do a semi-manual injection thing with ChangeNotes.Factory that
makes adding new arguments painful. Limit the pain by encapsulating
into an injectable Args class.

This has more side benefits, like making DraftCommentNotes able to use
straight assisted injection, and simplifying TestChanges.

Change-Id: I747aefb8d6e16ad6488b2a686bd23304a0fe24f6
This commit is contained in:
Dave Borowitz
2016-03-23 13:35:03 -04:00
parent eb70d18316
commit c11eae02a7
6 changed files with 84 additions and 114 deletions

View File

@@ -14,10 +14,14 @@
package com.google.gerrit.server.notedb; package com.google.gerrit.server.notedb;
import com.google.common.annotations.VisibleForTesting;
import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
@@ -29,17 +33,35 @@ import java.io.IOException;
/** View of contents at a single ref related to some change. **/ /** View of contents at a single ref related to some change. **/
public abstract class AbstractChangeNotes<T> { public abstract class AbstractChangeNotes<T> {
protected final GitRepositoryManager repoManager; @VisibleForTesting
protected final NotesMigration migration; @Singleton
public static class Args {
final GitRepositoryManager repoManager;
final NotesMigration migration;
final AllUsersName allUsers;
final ChangeNoteUtil noteUtil;
@Inject
Args(
GitRepositoryManager repoManager,
NotesMigration migration,
AllUsersName allUsers,
ChangeNoteUtil noteUtil) {
this.repoManager = repoManager;
this.migration = migration;
this.allUsers = allUsers;
this.noteUtil = noteUtil;
}
}
protected final Args args;
private final Change.Id changeId; private final Change.Id changeId;
private ObjectId revision; private ObjectId revision;
private boolean loaded; private boolean loaded;
AbstractChangeNotes(GitRepositoryManager repoManager, AbstractChangeNotes(Args args, Change.Id changeId) {
NotesMigration migration, Change.Id changeId) { this.args = args;
this.repoManager = repoManager;
this.migration = migration;
this.changeId = changeId; this.changeId = changeId;
} }
@@ -56,11 +78,12 @@ public abstract class AbstractChangeNotes<T> {
if (loaded) { if (loaded) {
return self(); return self();
} }
if (!migration.enabled() || changeId == null) { if (!args.migration.enabled() || changeId == null) {
loadDefaults(); loadDefaults();
return self(); return self();
} }
try (Repository repo = repoManager.openMetadataRepository(getProjectName()); try (Repository repo =
args.repoManager.openMetadataRepository(getProjectName());
RevWalk walk = new RevWalk(repo)) { RevWalk walk = new RevWalk(repo)) {
Ref ref = repo.getRefDatabase().exactRef(getRefName()); Ref ref = repo.getRefDatabase().exactRef(getRefName());
ObjectId id = ref != null ? ref.getObjectId() : null; ObjectId id = ref != null ? ref.getObjectId() : null;
@@ -81,10 +104,11 @@ public abstract class AbstractChangeNotes<T> {
public ObjectId loadRevision() throws OrmException { public ObjectId loadRevision() throws OrmException {
if (loaded) { if (loaded) {
return getRevision(); return getRevision();
} else if (!migration.enabled()) { } else if (!args.migration.enabled()) {
return null; return null;
} }
try (Repository repo = repoManager.openMetadataRepository(getProjectName())) { try (Repository repo =
args.repoManager.openMetadataRepository(getProjectName())) {
Ref ref = repo.getRefDatabase().exactRef(getRefName()); Ref ref = repo.getRefDatabase().exactRef(getRefName());
return ref != null ? ref.getObjectId() : null; return ref != null ? ref.getObjectId() : null;
} catch (IOException e) { } catch (IOException e) {

View File

@@ -53,8 +53,6 @@ import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.reviewdb.client.RevId; import com.google.gerrit.reviewdb.client.RevId;
import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.reviewdb.server.ReviewDbUtil; import com.google.gerrit.reviewdb.server.ReviewDbUtil;
import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.project.NoSuchChangeException; import com.google.gerrit.server.project.NoSuchChangeException;
import com.google.gerrit.server.project.ProjectCache; import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.query.change.ChangeData; import com.google.gerrit.server.query.change.ChangeData;
@@ -113,27 +111,18 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
public static class Factory { public static class Factory {
private static final Logger log = LoggerFactory.getLogger(Factory.class); private static final Logger log = LoggerFactory.getLogger(Factory.class);
private final GitRepositoryManager repoManager; private final Args args;
private final NotesMigration migration;
private final AllUsersName allUsers;
private final Provider<InternalChangeQuery> queryProvider; private final Provider<InternalChangeQuery> queryProvider;
private final ProjectCache projectCache; private final ProjectCache projectCache;
private final ChangeNoteUtil noteUtil;
@VisibleForTesting @VisibleForTesting
@Inject @Inject
public Factory(GitRepositoryManager repoManager, public Factory(Args args,
NotesMigration migration,
AllUsersName allUsers,
Provider<InternalChangeQuery> queryProvider, Provider<InternalChangeQuery> queryProvider,
ProjectCache projectCache, ProjectCache projectCache) {
ChangeNoteUtil noteUtil) { this.args = args;
this.repoManager = repoManager;
this.migration = migration;
this.allUsers = allUsers;
this.queryProvider = queryProvider; this.queryProvider = queryProvider;
this.projectCache = projectCache; this.projectCache = projectCache;
this.noteUtil = noteUtil;
} }
public ChangeNotes createChecked(ReviewDb db, Change c) public ChangeNotes createChecked(ReviewDb db, Change c)
@@ -178,8 +167,7 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
project, changeId, change.getProject()); project, changeId, change.getProject());
// TODO: Throw NoSuchChangeException when the change is not found in the // TODO: Throw NoSuchChangeException when the change is not found in the
// database // database
return new ChangeNotes(repoManager, migration, allUsers, noteUtil, return new ChangeNotes(args, project, change).load();
project, change).load();
} }
/** /**
@@ -191,23 +179,20 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
* @return change notes * @return change notes
*/ */
public ChangeNotes createFromIndexedChange(Change change) { public ChangeNotes createFromIndexedChange(Change change) {
return new ChangeNotes(repoManager, migration, allUsers, noteUtil, return new ChangeNotes(args, change.getProject(), change);
change.getProject(), change);
} }
public ChangeNotes createForNew(Change change) throws OrmException { public ChangeNotes createForNew(Change change) throws OrmException {
return new ChangeNotes(repoManager, migration, allUsers, noteUtil, return new ChangeNotes(args, change.getProject(), change).load();
change.getProject(), change).load();
} }
// TODO(dborowitz): Remove when deleting index schemas <27. // TODO(dborowitz): Remove when deleting index schemas <27.
public ChangeNotes createFromIdOnlyWhenNoteDbDisabled( public ChangeNotes createFromIdOnlyWhenNoteDbDisabled(
ReviewDb db, Change.Id changeId) throws OrmException { ReviewDb db, Change.Id changeId) throws OrmException {
checkState(!migration.readChanges(), "do not call" checkState(!args.migration.readChanges(), "do not call"
+ " createFromIdOnlyWhenNoteDbDisabled when NoteDb is enabled"); + " createFromIdOnlyWhenNoteDbDisabled when NoteDb is enabled");
Change change = unwrap(db).changes().get(changeId); Change change = unwrap(db).changes().get(changeId);
return new ChangeNotes(repoManager, migration, allUsers, noteUtil, return new ChangeNotes(args, change.getProject(), change).load();
change.getProject(), change).load();
} }
// TODO(ekempin): Remove when database backend is deleted // TODO(ekempin): Remove when database backend is deleted
@@ -217,10 +202,9 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
*/ */
private ChangeNotes createFromChangeOnlyWhenNoteDbDisabled(Change change) private ChangeNotes createFromChangeOnlyWhenNoteDbDisabled(Change change)
throws OrmException { throws OrmException {
checkState(!migration.readChanges(), "do not call" checkState(!args.migration.readChanges(), "do not call"
+ " createFromChangeWhenNoteDbDisabled when NoteDb is enabled"); + " createFromChangeWhenNoteDbDisabled when NoteDb is enabled");
return new ChangeNotes(repoManager, migration, allUsers, noteUtil, return new ChangeNotes(args, change.getProject(), change).load();
change.getProject(), change).load();
} }
public CheckedFuture<ChangeNotes, OrmException> createAsync( public CheckedFuture<ChangeNotes, OrmException> createAsync(
@@ -239,8 +223,7 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
"passed project %s when creating ChangeNotes for %s," "passed project %s when creating ChangeNotes for %s,"
+ " but actual project is %s", + " but actual project is %s",
project, changeId, change.getProject()); project, changeId, change.getProject());
return new ChangeNotes(repoManager, migration, allUsers, return new ChangeNotes(args, project, change).load();
noteUtil, project, change).load();
} }
}); });
} }
@@ -258,7 +241,7 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
public List<ChangeNotes> create(ReviewDb db, public List<ChangeNotes> create(ReviewDb db,
Collection<Change.Id> changeIds) throws OrmException { Collection<Change.Id> changeIds) throws OrmException {
List<ChangeNotes> notes = new ArrayList<>(); List<ChangeNotes> notes = new ArrayList<>();
if (migration.enabled()) { if (args.migration.enabled()) {
for (Change.Id changeId : changeIds) { for (Change.Id changeId : changeIds) {
try { try {
notes.add(createChecked(changeId)); notes.add(createChecked(changeId));
@@ -279,7 +262,7 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
Collection<Change.Id> changeIds, Predicate<ChangeNotes> predicate) Collection<Change.Id> changeIds, Predicate<ChangeNotes> predicate)
throws OrmException { throws OrmException {
List<ChangeNotes> notes = new ArrayList<>(); List<ChangeNotes> notes = new ArrayList<>();
if (migration.enabled()) { if (args.migration.enabled()) {
for (Change.Id cid : changeIds) { for (Change.Id cid : changeIds) {
ChangeNotes cn = create(db, project, cid); ChangeNotes cn = create(db, project, cid);
if (cn.getChange() != null && predicate.apply(cn)) { if (cn.getChange() != null && predicate.apply(cn)) {
@@ -303,9 +286,9 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
public ListMultimap<Project.NameKey, ChangeNotes> create(ReviewDb db, public ListMultimap<Project.NameKey, ChangeNotes> create(ReviewDb db,
Predicate<ChangeNotes> predicate) throws IOException, OrmException { Predicate<ChangeNotes> predicate) throws IOException, OrmException {
ListMultimap<Project.NameKey, ChangeNotes> m = ArrayListMultimap.create(); ListMultimap<Project.NameKey, ChangeNotes> m = ArrayListMultimap.create();
if (migration.readChanges()) { if (args.migration.readChanges()) {
for (Project.NameKey project : projectCache.all()) { for (Project.NameKey project : projectCache.all()) {
try (Repository repo = repoManager.openRepository(project)) { try (Repository repo = args.repoManager.openRepository(project)) {
List<ChangeNotes> changes = scanNoteDb(repo, db, project); List<ChangeNotes> changes = scanNoteDb(repo, db, project);
for (ChangeNotes cn : changes) { for (ChangeNotes cn : changes) {
if (predicate.apply(cn)) { if (predicate.apply(cn)) {
@@ -327,7 +310,7 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
public List<ChangeNotes> scan(Repository repo, ReviewDb db, public List<ChangeNotes> scan(Repository repo, ReviewDb db,
Project.NameKey project) throws OrmException, IOException { Project.NameKey project) throws OrmException, IOException {
if (!migration.readChanges()) { if (!args.migration.readChanges()) {
return scanDb(repo, db); return scanDb(repo, db);
} }
@@ -379,7 +362,6 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
} }
} }
private final ChangeNoteUtil noteUtil;
private final Project.NameKey project; private final Project.NameKey project;
private final Change change; private final Change change;
private ImmutableSortedMap<PatchSet.Id, PatchSet> patchSets; private ImmutableSortedMap<PatchSet.Id, PatchSet> patchSets;
@@ -396,16 +378,11 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
// notes easier. // notes easier.
RevisionNoteMap revisionNoteMap; RevisionNoteMap revisionNoteMap;
private final AllUsersName allUsers;
private DraftCommentNotes draftCommentNotes; private DraftCommentNotes draftCommentNotes;
@VisibleForTesting @VisibleForTesting
public ChangeNotes(GitRepositoryManager repoManager, NotesMigration migration, public ChangeNotes(Args args, Project.NameKey project, Change change) {
AllUsersName allUsers, ChangeNoteUtil noteUtil, super(args, change != null ? change.getId() : null);
Project.NameKey project, Change change) {
super(repoManager, migration, change != null ? change.getId() : null);
this.allUsers = allUsers;
this.noteUtil = noteUtil;
this.project = project; this.project = project;
this.change = change != null ? new Change(change) : null; this.change = change != null ? new Change(change) : null;
} }
@@ -502,8 +479,7 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
throws OrmException { throws OrmException {
if (draftCommentNotes == null || if (draftCommentNotes == null ||
!author.equals(draftCommentNotes.getAuthor())) { !author.equals(draftCommentNotes.getAuthor())) {
draftCommentNotes = new DraftCommentNotes(repoManager, migration, draftCommentNotes = new DraftCommentNotes(args, getChangeId(), author);
allUsers, noteUtil, getChangeId(), author);
draftCommentNotes.load(); draftCommentNotes.load();
} }
} }
@@ -550,7 +526,7 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
return; return;
} }
try (ChangeNotesParser parser = new ChangeNotesParser( try (ChangeNotesParser parser = new ChangeNotesParser(
project, change.getId(), rev, walk, repoManager, noteUtil)) { project, change.getId(), rev, walk, args.repoManager, args.noteUtil)) {
parser.parseAll(); parser.parseAll();
if (parser.status != null) { if (parser.status != null) {

View File

@@ -24,10 +24,8 @@ import com.google.gerrit.reviewdb.client.PatchLineComment;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RefNames; import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.reviewdb.client.RevId; import com.google.gerrit.reviewdb.client.RevId;
import com.google.gerrit.server.config.AllUsersName; import com.google.inject.assistedinject.Assisted;
import com.google.gerrit.server.git.GitRepositoryManager; import com.google.inject.assistedinject.AssistedInject;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
@@ -43,45 +41,22 @@ import java.io.IOException;
* its drafts branch. * its drafts branch.
*/ */
public class DraftCommentNotes extends AbstractChangeNotes<DraftCommentNotes> { public class DraftCommentNotes extends AbstractChangeNotes<DraftCommentNotes> {
@Singleton public interface Factory {
public static class Factory { DraftCommentNotes create(Change.Id changeId, Account.Id accountId);
private final GitRepositoryManager repoManager;
private final NotesMigration migration;
private final AllUsersName draftsProject;
private final ChangeNoteUtil noteUtil;
@VisibleForTesting
@Inject
public Factory(GitRepositoryManager repoManager,
NotesMigration migration,
AllUsersName allUsers,
ChangeNoteUtil noteUtil) {
this.repoManager = repoManager;
this.migration = migration;
this.draftsProject = allUsers;
this.noteUtil = noteUtil;
}
public DraftCommentNotes create(Change.Id changeId, Account.Id accountId) {
return new DraftCommentNotes(repoManager, migration, draftsProject,
noteUtil, changeId, accountId);
}
} }
private final AllUsersName draftsProject;
private final ChangeNoteUtil noteUtil;
private final Account.Id author; private final Account.Id author;
private ImmutableListMultimap<RevId, PatchLineComment> comments; private ImmutableListMultimap<RevId, PatchLineComment> comments;
private RevisionNoteMap revisionNoteMap; private RevisionNoteMap revisionNoteMap;
DraftCommentNotes(GitRepositoryManager repoManager, NotesMigration migration, @AssistedInject
AllUsersName draftsProject, ChangeNoteUtil noteUtil, Change.Id changeId, DraftCommentNotes(
Account.Id author) { Args args,
super(repoManager, migration, changeId); @Assisted Change.Id changeId,
this.draftsProject = draftsProject; @Assisted Account.Id author) {
super(args, changeId);
this.author = author; this.author = author;
this.noteUtil = noteUtil;
} }
RevisionNoteMap getRevisionNoteMap() { RevisionNoteMap getRevisionNoteMap() {
@@ -123,7 +98,7 @@ public class DraftCommentNotes extends AbstractChangeNotes<DraftCommentNotes> {
RevCommit tipCommit = walk.parseCommit(rev); RevCommit tipCommit = walk.parseCommit(rev);
ObjectReader reader = walk.getObjectReader(); ObjectReader reader = walk.getObjectReader();
revisionNoteMap = RevisionNoteMap.parse( revisionNoteMap = RevisionNoteMap.parse(
noteUtil, getChangeId(), reader, NoteMap.read(reader, tipCommit), args.noteUtil, getChangeId(), reader, NoteMap.read(reader, tipCommit),
true); true);
Multimap<RevId, PatchLineComment> cs = ArrayListMultimap.create(); Multimap<RevId, PatchLineComment> cs = ArrayListMultimap.create();
for (RevisionNote rn : revisionNoteMap.revisionNotes.values()) { for (RevisionNote rn : revisionNoteMap.revisionNotes.values()) {
@@ -141,7 +116,7 @@ public class DraftCommentNotes extends AbstractChangeNotes<DraftCommentNotes> {
@Override @Override
public Project.NameKey getProjectName() { public Project.NameKey getProjectName() {
return draftsProject; return args.allUsers;
} }
@VisibleForTesting @VisibleForTesting

View File

@@ -21,6 +21,7 @@ public class NoteDbModule extends FactoryModule {
public void configure() { public void configure() {
factory(ChangeUpdate.Factory.class); factory(ChangeUpdate.Factory.class);
factory(ChangeDraftUpdate.Factory.class); factory(ChangeDraftUpdate.Factory.class);
factory(DraftCommentNotes.Factory.class);
factory(NoteDbUpdateManager.Factory.class); factory(NoteDbUpdateManager.Factory.class);
} }
} }

View File

@@ -110,6 +110,9 @@ public abstract class AbstractChangeNotesTest extends GerritBaseTests {
@Inject @Inject
protected ChangeNoteUtil noteUtil; protected ChangeNoteUtil noteUtil;
@Inject
protected AbstractChangeNotes.Args args;
private Injector injector; private Injector injector;
private String systemTimeZone; private String systemTimeZone;
@@ -139,7 +142,7 @@ public abstract class AbstractChangeNotesTest extends GerritBaseTests {
@Override @Override
public void configure() { public void configure() {
install(new GitModule()); install(new GitModule());
factory(NoteDbUpdateManager.Factory.class); install(new NoteDbModule());
bind(AllUsersName.class).toProvider(AllUsersNameProvider.class); bind(AllUsersName.class).toProvider(AllUsersNameProvider.class);
bind(String.class).annotatedWith(GerritServerId.class) bind(String.class).annotatedWith(GerritServerId.class)
.toInstance("gerrit"); .toInstance("gerrit");
@@ -198,14 +201,11 @@ public abstract class AbstractChangeNotesTest extends GerritBaseTests {
protected ChangeUpdate newUpdate(Change c, CurrentUser user) protected ChangeUpdate newUpdate(Change c, CurrentUser user)
throws Exception { throws Exception {
ChangeUpdate update = TestChanges.newUpdate( return TestChanges.newUpdate(injector, c, user);
injector, repoManager, MIGRATION, c, allUsers, user);
return update;
} }
protected ChangeNotes newNotes(Change c) throws OrmException { protected ChangeNotes newNotes(Change c) throws OrmException {
return new ChangeNotes(repoManager, MIGRATION, allUsers, noteUtil, return new ChangeNotes(args, c.getProject(), c).load();
c.getProject(), c).load();
} }
protected static SubmitRecord submitRecord(String status, protected static SubmitRecord submitRecord(String status,

View File

@@ -28,10 +28,8 @@ import com.google.gerrit.reviewdb.client.PatchSetInfo;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RevId; import com.google.gerrit.reviewdb.client.RevId;
import com.google.gerrit.server.CurrentUser; import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.notedb.ChangeDraftUpdate; import com.google.gerrit.server.notedb.AbstractChangeNotes;
import com.google.gerrit.server.notedb.ChangeNoteUtil;
import com.google.gerrit.server.notedb.ChangeNotes; import com.google.gerrit.server.notedb.ChangeNotes;
import com.google.gerrit.server.notedb.ChangeUpdate; import com.google.gerrit.server.notedb.ChangeUpdate;
import com.google.gerrit.server.notedb.NotesMigration; import com.google.gerrit.server.notedb.NotesMigration;
@@ -88,34 +86,33 @@ public class TestChanges {
} }
public static ChangeUpdate newUpdate(Injector injector, public static ChangeUpdate newUpdate(Injector injector,
GitRepositoryManager repoManager, NotesMigration migration, Change c, Change c, final CurrentUser user) throws Exception {
final AllUsersName allUsers, final CurrentUser user)
throws Exception {
injector = injector.createChildInjector(new FactoryModule() { injector = injector.createChildInjector(new FactoryModule() {
@Override @Override
public void configure() { public void configure() {
factory(ChangeUpdate.Factory.class);
factory(ChangeDraftUpdate.Factory.class);
bind(CurrentUser.class).toInstance(user); bind(CurrentUser.class).toInstance(user);
} }
}); });
ChangeUpdate update = injector.getInstance(ChangeUpdate.Factory.class) ChangeUpdate update = injector.getInstance(ChangeUpdate.Factory.class)
.create( .create(
stubChangeControl( stubChangeControl(
repoManager, migration, c, allUsers, injector.getInstance(AbstractChangeNotes.Args.class),
injector.getInstance(ChangeNoteUtil.class), c,
user), user),
TimeUtil.nowTs(), Ordering.<String> natural()); TimeUtil.nowTs(), Ordering.<String> natural());
ChangeNotes notes = update.getChangeNotes(); ChangeNotes notes = update.getChangeNotes();
boolean hasPatchSets = notes.getPatchSets() != null boolean hasPatchSets = notes.getPatchSets() != null
&& !notes.getPatchSets().isEmpty(); && !notes.getPatchSets().isEmpty();
NotesMigration migration = injector.getInstance(NotesMigration.class);
if (hasPatchSets || !migration.readChanges()) { if (hasPatchSets || !migration.readChanges()) {
return update; return update;
} }
// Change doesn't exist yet. NoteDb requires that there be a commit for the // Change doesn't exist yet. NoteDb requires that there be a commit for the
// first patch set, so create one. // first patch set, so create one.
GitRepositoryManager repoManager =
injector.getInstance(GitRepositoryManager.class);
try (Repository repo = repoManager.openRepository(c.getProject())) { try (Repository repo = repoManager.openRepository(c.getProject())) {
TestRepository<Repository> tr = new TestRepository<>(repo); TestRepository<Repository> tr = new TestRepository<>(repo);
PersonIdent ident = user.asIdentifiedUser() PersonIdent ident = user.asIdentifiedUser()
@@ -136,16 +133,13 @@ public class TestChanges {
} }
private static ChangeControl stubChangeControl( private static ChangeControl stubChangeControl(
GitRepositoryManager repoManager, NotesMigration migration, AbstractChangeNotes.Args args,
Change c, AllUsersName allUsers, ChangeNoteUtil noteUtil, Change c, CurrentUser user) throws OrmException {
CurrentUser user) throws OrmException {
ChangeControl ctl = EasyMock.createMock(ChangeControl.class); ChangeControl ctl = EasyMock.createMock(ChangeControl.class);
expect(ctl.getChange()).andStubReturn(c); expect(ctl.getChange()).andStubReturn(c);
expect(ctl.getProject()).andStubReturn(new Project(c.getProject())); expect(ctl.getProject()).andStubReturn(new Project(c.getProject()));
expect(ctl.getUser()).andStubReturn(user); expect(ctl.getUser()).andStubReturn(user);
ChangeNotes notes = ChangeNotes notes = new ChangeNotes(args, c.getProject(), c).load();
new ChangeNotes(repoManager, migration, allUsers, noteUtil,
c.getProject(), c).load();
expect(ctl.getNotes()).andStubReturn(notes); expect(ctl.getNotes()).andStubReturn(notes);
expect(ctl.getId()).andStubReturn(c.getId()); expect(ctl.getId()).andStubReturn(c.getId());
EasyMock.replay(ctl); EasyMock.replay(ctl);