Improve URLmatching to match real Urls
Previously the UrlMatch string needed to match the templated value in
the replication.config file, now it can additionally match a real URL
with the project name already in it.
Change-Id: Ib766f162a9f9106b7763ed26347b4639a9041d6d
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java b/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java
index 48694bf..966248a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java
@@ -653,33 +653,35 @@
List<URIish> getURIs(Project.NameKey project, String urlMatch) {
List<URIish> r = Lists.newArrayListWithCapacity(config.getRemoteConfig().getURIs().size());
- for (URIish uri : config.getRemoteConfig().getURIs()) {
- if (matches(uri, urlMatch)) {
- String name = project.get();
- if (needsUrlEncoding(uri)) {
- name = encode(name);
- }
- String remoteNameStyle = config.getRemoteNameStyle();
- if (remoteNameStyle.equals("dash")) {
- name = name.replace("/", "-");
- } else if (remoteNameStyle.equals("underscore")) {
- name = name.replace("/", "_");
- } else if (remoteNameStyle.equals("basenameOnly")) {
- name = FilenameUtils.getBaseName(name);
- } else if (!remoteNameStyle.equals("slash")) {
- repLog.debug("Unknown remoteNameStyle: {}, falling back to slash", remoteNameStyle);
- }
- String replacedPath =
- ReplicationQueue.replaceName(uri.getPath(), name, isSingleProjectMatch());
- if (replacedPath != null) {
- uri = uri.setPath(replacedPath);
- r.add(uri);
- }
+ for (URIish configUri : config.getRemoteConfig().getURIs()) {
+ URIish uri = getURI(configUri, project);
+ if (matches(configUri, urlMatch) || matches(uri, urlMatch)) {
+ r.add(uri);
}
}
return r;
}
+ URIish getURI(URIish template, Project.NameKey project) {
+ String name = project.get();
+ if (needsUrlEncoding(template)) {
+ name = encode(name);
+ }
+ String remoteNameStyle = config.getRemoteNameStyle();
+ if (remoteNameStyle.equals("dash")) {
+ name = name.replace("/", "-");
+ } else if (remoteNameStyle.equals("underscore")) {
+ name = name.replace("/", "_");
+ } else if (remoteNameStyle.equals("basenameOnly")) {
+ name = FilenameUtils.getBaseName(name);
+ } else if (!remoteNameStyle.equals("slash")) {
+ repLog.debug("Unknown remoteNameStyle: {}, falling back to slash", remoteNameStyle);
+ }
+ String replacedPath =
+ ReplicationQueue.replaceName(template.getPath(), name, isSingleProjectMatch());
+ return (replacedPath != null) ? template.setPath(replacedPath) : template;
+ }
+
static boolean needsUrlEncoding(URIish uri) {
return "http".equalsIgnoreCase(uri.getScheme())
|| "https".equalsIgnoreCase(uri.getScheme())
diff --git a/src/main/resources/Documentation/cmd-start.md b/src/main/resources/Documentation/cmd-start.md
index 6af73af..8291421 100644
--- a/src/main/resources/Documentation/cmd-start.md
+++ b/src/main/resources/Documentation/cmd-start.md
@@ -97,10 +97,10 @@
: Schedule replication for all projects.
`--url <PATTERN>`
-: Replicate only to replication destinations whose URL contains
- the substring `PATTERN`. This can be useful to replicate
- only to a previously down node, which has been brought back
- online.
+: Replicate only to replication destinations whose configuration
+ URL contains the substring `PATTERN`, or whose expanded project
+ URL contains `PATTERN`. This can be useful to replicate only to
+ a previously down node, which has been brought back online.
EXAMPLES
--------
@@ -136,6 +136,12 @@
$ ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ start --url slave1 ^(|.*/)vendor(|/.*)
```
+Replicate to only one specific destination URL:
+
+```
+ $ ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ start --url https://example.com/tools/gerrit.git
+```
+
SEE ALSO
--------
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java
index 060150c..cec57a7 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java
@@ -217,6 +217,49 @@
}
@Test
+ public void shouldMatchTemplatedURL() throws Exception {
+ createTestProject("projectreplica");
+
+ setReplicationDestination("foo", "replica", ALL_PROJECTS);
+ reloadConfig();
+
+ String urlMatch = gitPath.resolve("${name}" + "replica" + ".git").toString();
+ String expectedURI = gitPath.resolve(project + "replica" + ".git").toString();
+
+ plugin
+ .getSysInjector()
+ .getInstance(ReplicationQueue.class)
+ .scheduleFullSync(project, urlMatch, new ReplicationState(NO_OP), true);
+
+ assertThat(listReplicationTasks(".*all.*")).hasSize(1);
+ for (ReplicationTasksStorage.ReplicateRefUpdate task : tasksStorage.list()) {
+ assertThat(task.uri).isEqualTo(expectedURI);
+ }
+ }
+
+ @Test
+ public void shouldMatchRealURL() throws Exception {
+ createTestProject("projectreplica");
+
+ setReplicationDestination("foo", "replica", ALL_PROJECTS);
+ reloadConfig();
+
+ String urlMatch = gitPath.resolve(project + "replica" + ".git").toString();
+ String expectedURI = urlMatch;
+
+ plugin
+ .getSysInjector()
+ .getInstance(ReplicationQueue.class)
+ .scheduleFullSync(project, urlMatch, new ReplicationState(NO_OP), true);
+
+ assertThat(listReplicationTasks(".*")).hasSize(1);
+ for (ReplicationTasksStorage.ReplicateRefUpdate task : tasksStorage.list()) {
+ assertThat(task.uri).isEqualTo(expectedURI);
+ }
+ assertThat(tasksStorage.list()).isNotEmpty();
+ }
+
+ @Test
public void shouldReplicateHeadUpdate() throws Exception {
setReplicationDestination("foo", "replica", ALL_PROJECTS);
reloadConfig();