Fix accidental overwrite of existing url and push specs
When importing additional repositories on an existing organisation,
the GitHub plugin preserves the legacy 'url' and 'push' specs
for allowing custom ad-hoc settings, such as the use of stable SSH
keys for replication or more restrictive push specs.
The abstraction of the replication configuration update implemented
with I9bde7e1148 has accidental broken the current behaviour causing
a regression in how the remotes are configured.
Reintroduce the correct behaviour by reading the existing config
when updating a remote and avoid overwriting the url and push
replication settings.
Change-Id: Idd7113f6c0549d752b1dd31bf52d960aee1f77b1
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/ReplicationRemoteConfigBuilder.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/ReplicationRemoteConfigBuilder.java
index bef109c..fc808d8 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/ReplicationRemoteConfigBuilder.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/ReplicationRemoteConfigBuilder.java
@@ -13,6 +13,7 @@
// limitations under the License.
package com.googlesource.gerrit.plugins.github.git;
+import com.google.common.base.Strings;
import com.google.gerrit.extensions.registration.DynamicItem;
import com.google.inject.Inject;
import com.googlesource.gerrit.plugins.github.GitHubURL;
@@ -44,23 +45,30 @@
}
Config build(String repositoryName) {
- Config remoteConfig = new Config();
+ Config remoteConfig = replicationConfigItem.get().get(username);
remoteConfig.setString("remote", username, "username", username);
remoteConfig.setString("remote", username, "password", authToken);
- remoteConfig.setString("remote", username, "url", gitHubUrl + "/${name}.git");
+ setRemoteConfigIfNotSet(remoteConfig, "url", gitHubUrl + "/${name}.git");
String[] existingProjects = getProjects();
List<String> projects = new ArrayList<>(List.of(existingProjects));
projects.add(repositoryName);
remoteConfig.setStringList("remote", username, "projects", projects);
- remoteConfig.setString("remote", username, "push", "refs/*:refs/*");
+ setRemoteConfigIfNotSet(remoteConfig, "push", "refs/*:refs/*");
return remoteConfig;
}
+ private void setRemoteConfigIfNotSet(Config remoteConfig, String key, String value) {
+ String existingValue = remoteConfig.getString("remote", username, key);
+ if (Strings.isNullOrEmpty(existingValue)) {
+ remoteConfig.setString("remote", username, key, value);
+ }
+ }
+
private String[] getProjects() {
ReplicationRemotesApi config = replicationConfigItem.get();
if (config != null) {
diff --git a/github-plugin/src/test/java/com/googlesource/gerrit/plugins/github/git/ReplicationRemoteConfigBuilderTest.java b/github-plugin/src/test/java/com/googlesource/gerrit/plugins/github/git/ReplicationRemoteConfigBuilderTest.java
index c6023e0..015b52e 100644
--- a/github-plugin/src/test/java/com/googlesource/gerrit/plugins/github/git/ReplicationRemoteConfigBuilderTest.java
+++ b/github-plugin/src/test/java/com/googlesource/gerrit/plugins/github/git/ReplicationRemoteConfigBuilderTest.java
@@ -61,6 +61,21 @@
assertThat(actual.getString("remote", username, "push")).isEqualTo("refs/*:refs/*");
}
+ @Test
+ public void shoudKeepCustomUrlAndPushRefSpecInRemoteConfig() throws Exception {
+ Config currentConfig = new Config();
+ String customPushRefSpec = "+refs/heads/myheads/*:refs/heads/myheads/*";
+ String customUrl = "ssh://github@github.com/myuser/${name}.git";
+ currentConfig.setString("remote", username, "push", customPushRefSpec);
+ currentConfig.setString("remote", username, "url", customUrl);
+
+ ReplicationRemoteConfigBuilder builder = newReplicationRemoteConfigBuilder(currentConfig);
+ Config actual = builder.build(repoName);
+
+ assertThat(actual.getString("remote", username, "push")).isEqualTo(customPushRefSpec);
+ assertThat(actual.getString("remote", username, "url")).isEqualTo(customUrl);
+ }
+
private ReplicationRemoteConfigBuilder newReplicationRemoteConfigBuilder() throws Exception {
return newReplicationRemoteConfigBuilder(new Config());
}