Fix broken build on Gerrit master (2.13-SNAPSHOT)
Since the merge of I2d961f8d it is not possible to find patch-sets
by SHA1 anymore. We need to leverage the Lucene index to check
if a pull-request commit has been already imported or not.
MenuItem is now imported from com.google.gerrit.extensions.client to
be able to compile against the latest Gerrit master.
Change-Id: I9868b3a0c1c58bb2ea027be58a561252773277fa
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GitHubTopMenu.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GitHubTopMenu.java
index 12a9b30..d05b6d6 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GitHubTopMenu.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GitHubTopMenu.java
@@ -15,6 +15,7 @@
import com.google.gerrit.extensions.annotations.Listen;
import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.client.MenuItem;
import com.google.gerrit.extensions.webui.TopMenu;
import com.google.gerrit.reviewdb.client.AuthType;
import com.google.gerrit.server.CurrentUser;
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
index d72d450..eaa7169 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
@@ -24,11 +24,11 @@
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.Project;
-import com.google.gerrit.reviewdb.client.RevId;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.Sequences;
import com.google.gerrit.server.change.ChangeInserter;
import com.google.gerrit.server.change.PatchSetInserter;
import com.google.gerrit.server.git.BatchUpdate;
@@ -40,10 +40,13 @@
import com.google.gerrit.server.project.NoSuchProjectException;
import com.google.gerrit.server.project.ProjectControl;
import com.google.gerrit.server.project.RefControl;
+import com.google.gerrit.server.query.QueryParseException;
import com.google.gerrit.server.query.change.ChangeData;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
import com.google.gerrit.server.query.change.InternalChangeQuery;
+import com.google.gerrit.server.query.change.QueryProcessor;
+import com.google.gerrit.server.query.change.QueryResult;
import com.google.gwtorm.server.OrmException;
-import com.google.gwtorm.server.ResultSet;
import com.google.inject.Inject;
import com.google.inject.Provider;
@@ -60,6 +63,7 @@
import org.slf4j.LoggerFactory;
import java.io.IOException;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@@ -74,6 +78,8 @@
private final GenericFactory userFactory;
private final Provider<InternalChangeQuery> queryProvider;
private final BatchUpdate.Factory updateFactory;
+ private final QueryProcessor qp;
+ private final ChangeQueryBuilder changeQuery;
@Inject
PullRequestCreateChange(ChangeInserter.Factory changeInserterFactory,
@@ -81,13 +87,17 @@
ProjectControl.Factory projectControlFactory,
IdentifiedUser.GenericFactory userFactory,
Provider<InternalChangeQuery> queryProvider,
- BatchUpdate.Factory batchUpdateFactory) {
+ BatchUpdate.Factory batchUpdateFactory,
+ QueryProcessor qp,
+ ChangeQueryBuilder changeQuery) {
this.changeInserterFactory = changeInserterFactory;
this.patchSetInserterFactory = patchSetInserterFactory;
this.projectControlFactory = projectControlFactory;
this.userFactory = userFactory;
this.queryProvider = queryProvider;
this.updateFactory = batchUpdateFactory;
+ this.qp = qp;
+ this.changeQuery = changeQuery;
}
public Change.Id addCommitToChange(ReviewDb db, final Project project,
@@ -118,7 +128,7 @@
throw new InvalidChangeOperationException(
"Destination branch cannot be null or empty");
}
- Ref destRef = repo.getRef(destinationBranch);
+ Ref destRef = repo.findRef(destinationBranch);
if (destRef == null) {
throw new InvalidChangeOperationException("Branch " + destinationBranch
+ " does not exist.");
@@ -129,14 +139,10 @@
destinationBranch);
String pullRequestSha1 = pullRequestCommit.getId().getName();
- ResultSet<PatchSet> existingPatchSet =
- db.patchSets().byRevision(new RevId(pullRequestSha1));
- Iterator<PatchSet> patchSetIterator = existingPatchSet.iterator();
- if (patchSetIterator.hasNext()) {
- PatchSet patchSet = patchSetIterator.next();
+ List<ChangeData> existingChanges = queryChangesForSha1(pullRequestSha1);
+ if (!existingChanges.isEmpty()) {
LOG.debug("Pull request commit ID " + pullRequestSha1
- + " has been already uploaded as PatchSetID="
- + patchSet.getPatchSetId() + " in ChangeID=" + patchSet.getId());
+ + " has been already uploaded as Change-Id=" + existingChanges.get(0).getId());
return null;
}
@@ -186,6 +192,18 @@
topic);
}
+ private List<ChangeData> queryChangesForSha1(String pullRequestSha1) {
+ QueryResult results;
+ try {
+ results = qp.queryChanges(changeQuery.commit(pullRequestSha1));
+ return results.changes();
+ } catch (OrmException | QueryParseException e) {
+ LOG.error("Invalid SHA1 " + pullRequestSha1
+ + ": cannot query changes for this pull request", e);
+ return Collections.emptyList();
+ }
+ }
+
private void insertPatchSet(BatchUpdate bu, Repository git, Change change,
RevCommit cherryPickCommit, RefControl refControl,
String pullRequestMessage) throws IOException, UpdateException,
@@ -217,7 +235,10 @@
change.setTopic(topic);
}
ChangeInserter ins =
- changeInserterFactory.create(refControl, change, pullRequestCommit);
+ changeInserterFactory.create(
+ change.getId(),
+ pullRequestCommit,
+ refControl.getRefName());
ins.setMessage(pullRequestMessage);
bu.insertChange(ins);
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
index ef90d04..4fd0d66 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
@@ -17,18 +17,18 @@
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
-import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.Project.NameKey;
-import com.google.gerrit.reviewdb.client.RevId;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.project.ProjectCache;
+import com.google.gerrit.server.query.QueryParseException;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.QueryProcessor;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gwtorm.server.OrmException;
-import com.google.gwtorm.server.ResultSet;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
@@ -70,15 +70,22 @@
private final ProjectCache projectsCache;
private final GitRepositoryManager repoMgr;
private final Provider<ReviewDb> schema;
+ private final QueryProcessor qp;
+ private final ChangeQueryBuilder changeQuery;
@Inject
- public PullRequestListController(final ProjectCache projectsCache,
- final GitRepositoryManager repoMgr, final Provider<ReviewDb> schema,
- final GitHubConfig config) {
+ public PullRequestListController(ProjectCache projectsCache,
+ GitRepositoryManager repoMgr,
+ Provider<ReviewDb> schema,
+ GitHubConfig config,
+ QueryProcessor qp,
+ ChangeQueryBuilder changeQuery) {
this.projectsCache = projectsCache;
this.repoMgr = repoMgr;
this.schema = schema;
this.config = config;
+ this.qp = qp;
+ this.changeQuery = changeQuery;
}
@Override
@@ -192,31 +199,18 @@
Repository gitRepo, GHPullRequest ghPullRequest)
throws IncorrectObjectTypeException, IOException {
boolean pullRequestToImport = false;
- try (RevWalk gitWalk = new RevWalk(gitRepo)) {
+ try {
for (GHPullRequestCommitDetail pullRequestCommit : ghPullRequest
.listCommits()) {
- ObjectId pullRequestHeadObjectId =
- ObjectId.fromString(pullRequestCommit.getSha());
-
- try {
- gitWalk.parseCommit(pullRequestHeadObjectId);
-
- ResultSet<PatchSet> patchSets;
- try {
- patchSets =
- db.patchSets().byRevision(new RevId(pullRequestCommit.getSha()));
- } catch (OrmException e) {
- LOG.error("Error whilst fetching patch-sets from DB associated to commit "
- + pullRequestCommit.getSha());
- return false;
- }
- pullRequestToImport = !patchSets.iterator().hasNext();
- patchSets.close();
- } catch (MissingObjectException e) {
- pullRequestToImport = true;
- }
+ pullRequestToImport |=
+ qp.queryChanges(changeQuery.commit(pullRequestCommit.getSha()))
+ .changes().isEmpty();
}
return pullRequestToImport;
+ } catch (OrmException | QueryParseException e) {
+ LOG.error("Unable to query Gerrit changes for pull-request "
+ + ghPullRequest.getNumber(), e);
+ return false;
}
}
}