Do not fail resume if there are new patch sets in target system
Resuming an import after doing changes in the target system is not
supported, still we can be nice and handle a few cases.
At the moment resuming the import fails if a change in the target
system has a new patch set. It fails because we try to fetch inline
comments for this patch set from the source system and this request
fails with '404 Not Found'. Recognize this case and continue the
import.
Change-Id: I547c9f6061b487be3dfd63eb4df6fcaca009a54d
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/RemoteApi.java b/src/main/java/com/googlesource/gerrit/plugins/importer/RemoteApi.java
index f8be23a..2ec93e5 100755
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/RemoteApi.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/RemoteApi.java
@@ -86,11 +86,26 @@
}
}
+ /**
+ * Retrieves inline comments of a patch set.
+ *
+ * @param changeId numeric change ID
+ * @param rev the revision
+ * @return Iterable that provides the inline comments, or {@code null} if the
+ * revision does not exist
+ * @throws IOException thrown if sending the request fails
+ * @throws BadRequestException thrown if the response is neither
+ * {@code 200 OK} nor {@code 404 Not Found}
+ */
public Iterable<CommentInfo> getComments(int changeId, String rev)
throws IOException, BadRequestException {
String endPoint = "/changes/" + changeId + "/revisions/" + rev + "/comments";
Map<String, List<CommentInfo>> result;
- try (RestResponse r = checkedGet(endPoint)) {
+ try (RestResponse r = restSession.get(endPoint)) {
+ if (r.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
+ return null;
+ }
+ assertOK(HttpMethod.GET, endPoint, r);
result = newGson().fromJson(r.getReader(),
new TypeToken<Map<String, List<CommentInfo>>>() {}.getType());
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/ReplayInlineCommentsStep.java b/src/main/java/com/googlesource/gerrit/plugins/importer/ReplayInlineCommentsStep.java
index 1e2fa12..6f54117 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/ReplayInlineCommentsStep.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/ReplayInlineCommentsStep.java
@@ -22,6 +22,7 @@
import com.google.common.collect.Multimap;
import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.common.errors.NoSuchAccountException;
+import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.extensions.client.Side;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.CommentInfo;
@@ -44,6 +45,9 @@
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
@@ -59,6 +63,9 @@
RemoteApi api, boolean resume);
}
+ private static final Logger log = LoggerFactory
+ .getLogger(ReplayInlineCommentsStep.class);
+
private final AccountUtil accountUtil;
private final ReviewDb db;
private final IdentifiedUser.GenericFactory genericUserFactory;
@@ -66,6 +73,7 @@
private final ChangeUpdate.Factory updateFactory;
private final PatchLineCommentsUtil plcUtil;
private final PatchListCache patchListCache;
+ private final String pluginName;
private final Change change;
private final ChangeInfo changeInfo;
private final RemoteApi api;
@@ -79,6 +87,7 @@
ChangeUpdate.Factory updateFactory,
PatchLineCommentsUtil plcUtil,
PatchListCache patchListCache,
+ @PluginName String pluginName,
@Assisted Change change,
@Assisted ChangeInfo changeInfo,
@Assisted RemoteApi api,
@@ -90,6 +99,7 @@
this.updateFactory = updateFactory;
this.plcUtil = plcUtil;
this.patchListCache = patchListCache;
+ this.pluginName = pluginName;
this.change = change;
this.changeInfo = changeInfo;
this.api = api;
@@ -102,6 +112,18 @@
Iterable<CommentInfo> comments = api.getComments(
changeInfo._number, ps.getRevision().get());
if (resume) {
+ if (comments == null) {
+ // the revision does not exist in the source system,
+ // it must be a revision that was created in the target system after
+ // the initial import
+ log.warn(String.format(
+ "[%s] Project %s was modified in target system: "
+ + "Skip replay inline comments for patch set %s"
+ + " which doesn't exist in the source system.",
+ pluginName, change.getProject().get(), ps.getId().toString()));
+ continue;
+ }
+
comments = filterComments(ps, comments);
}