Break out parts of ReceiveCommits.insertChange()

Break out parts of ReceiveCommits.insertChange() to a new
ChangeInserter class.

Other features in Gerrit use very similar (and redundant code)
for inserting a new change into the db, making maintenance a
burden.

This new class will be used by the new CherryPick feature
(in upcoming changes), but other features in Gerrit should be
able to make use of the code too.

Change-Id: Iabae3b59e875514d6df215921b910ec44231a167
This commit is contained in:
Gustaf Lundh
2013-03-11 15:23:09 +01:00
parent 9e31bef1d4
commit 49e5977df3
3 changed files with 85 additions and 15 deletions

View File

@@ -0,0 +1,76 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.change;
import com.google.gerrit.common.ChangeHooks;
import com.google.gerrit.common.data.LabelTypes;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.PatchSetInfo;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.ApprovalsUtil;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.config.TrackingFooters;
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.FooterLine;
import org.eclipse.jgit.revwalk.RevCommit;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class ChangeInserter {
private final GitReferenceUpdated gitRefUpdated;
private final ChangeHooks hooks;
private final ApprovalsUtil approvalsUtil;
private final TrackingFooters trackingFooters;
@Inject
public ChangeInserter(final GitReferenceUpdated gitRefUpdated,
ChangeHooks hooks, ApprovalsUtil approvalsUtil,
TrackingFooters trackingFooters) {
this.gitRefUpdated = gitRefUpdated;
this.hooks = hooks;
this.approvalsUtil = approvalsUtil;
this.trackingFooters = trackingFooters;
}
public void insertChange(ReviewDb db, Change change, PatchSet ps,
RevCommit commit, LabelTypes labelTypes, List<FooterLine> footerLines,
PatchSetInfo info, Set<Account.Id> reviewers) throws OrmException {
db.changes().beginTransaction(change.getId());
try {
ChangeUtil.insertAncestors(db, ps.getId(), commit);
db.patchSets().insert(Collections.singleton(ps));
db.changes().insert(Collections.singleton(change));
ChangeUtil.updateTrackingIds(db, change, trackingFooters, footerLines);
approvalsUtil.addReviewers(db, labelTypes, change, ps, info, reviewers,
Collections.<Account.Id> emptySet());
db.commit();
} finally {
db.rollback();
}
gitRefUpdated.fire(change.getProject(), ps.getRefName(), ObjectId.zeroId(),
commit);
hooks.doPatchsetCreatedHook(change, ps, db);
}
}

View File

@@ -19,6 +19,7 @@ import static com.google.inject.Scopes.SINGLETON;
import com.google.gerrit.server.ApprovalsUtil;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.RequestCleanup;
import com.google.gerrit.server.change.ChangeInserter;
import com.google.gerrit.server.changedetail.DeleteDraftPatchSet;
import com.google.gerrit.server.changedetail.PublishDraft;
import com.google.gerrit.server.git.BanCommit;
@@ -39,6 +40,7 @@ public class GerritRequestModule extends FactoryModule {
bind(RequestScopedReviewDbProvider.class);
bind(IdentifiedUser.RequestFactory.class).in(SINGLETON);
bind(ApprovalsUtil.class);
bind(ChangeInserter.class);
bind(PerRequestProjectControlCache.class).in(RequestScoped.class);
bind(ChangeControl.Factory.class).in(SINGLETON);

View File

@@ -60,6 +60,7 @@ import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountResolver;
import com.google.gerrit.server.change.ChangeInserter;
import com.google.gerrit.server.config.AllProjectsName;
import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.config.TrackingFooters;
@@ -252,6 +253,7 @@ public class ReceiveCommits {
private final CommitValidators.Factory commitValidatorsFactory;
private final TrackingFooters trackingFooters;
private final TagCache tagCache;
private final ChangeInserter changeInserter;
private final WorkQueue workQueue;
private final ListeningExecutorService changeUpdateExector;
private final RequestScopePropagator requestScopePropagator;
@@ -303,6 +305,7 @@ public class ReceiveCommits {
final GitRepositoryManager repoManager,
final TagCache tagCache,
final ChangeCache changeCache,
final ChangeInserter changeInserter,
final CommitValidators.Factory commitValidatorsFactory,
@CanonicalWebUrl @Nullable final String canonicalWebUrl,
@GerritPersonIdent final PersonIdent gerritIdent,
@@ -333,6 +336,7 @@ public class ReceiveCommits {
this.canonicalWebUrl = canonicalWebUrl;
this.trackingFooters = trackingFooters;
this.tagCache = tagCache;
this.changeInserter = changeInserter;
this.commitValidatorsFactory = commitValidatorsFactory;
this.workQueue = workQueue;
this.changeUpdateExector = changeUpdateExector;
@@ -1461,23 +1465,11 @@ public class ReceiveCommits {
recipients.add(getRecipientsFromFooters(accountResolver, ps, footerLines));
recipients.remove(me);
db.changes().beginTransaction(change.getId());
try {
ChangeUtil.insertAncestors(db, ps.getId(), commit);
db.patchSets().insert(Collections.singleton(ps));
db.changes().insert(Collections.singleton(change));
ChangeUtil.updateTrackingIds(db, change, trackingFooters, footerLines);
approvalsUtil.addReviewers(db, labelTypes, change, ps, info,
recipients.getReviewers(), Collections.<Account.Id> emptySet());
db.commit();
} finally {
db.rollback();
}
changeInserter.insertChange(db, change, ps, commit, labelTypes,
footerLines, info, recipients.getReviewers());
created = true;
gitRefUpdated.fire(project.getNameKey(), ps.getRefName(),
ObjectId.zeroId(), commit);
hooks.doPatchsetCreatedHook(change, ps, db);
workQueue.getDefaultQueue()
.submit(requestScopePropagator.wrap(new Runnable() {
@Override