Allow keeping custom configs in FanoutReplicationConfig Some of the replication plugin default choices may not be suitable for users of GitHub that need some specific replication settings. Historically the defaults of the replication plugin were not suitable for pushing to a non-Gerrit repository, hence the fragility of generating and assuming defaults. With this change, allow to keep any ad-hoc customisation on the generated replication configuration files. Change-Id: Ic451e934b1f8ef062c502eaad3c3f1bf63813556
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/FanoutReplicationConfig.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/FanoutReplicationConfig.java index 412c2ff..4a3bc4b 100644 --- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/FanoutReplicationConfig.java +++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/FanoutReplicationConfig.java
@@ -54,12 +54,19 @@ FS.DETECTED); replicationConf.load(); - replicationConf.setString("remote", null, "url", url); + String currentUrl = replicationConf.getString("remote", null, "url"); + if (currentUrl == null) { + replicationConf.setString("remote", null, "url", url); + } List<String> projects = new ArrayList<>(Arrays.asList(replicationConf.getStringList("remote", null, "projects"))); projects.add(projectName); replicationConf.setStringList("remote", null, "projects", projects); - replicationConf.setString("remote", null, "push", "refs/*:refs/*"); + + String currentPushRefs = replicationConf.getString("remote", null, "push"); + if (currentPushRefs == null) { + replicationConf.setString("remote", null, "push", "refs/*:refs/*"); + } replicationConf.save(); } }
diff --git a/github-plugin/src/test/java/com/googlesource/gerrit/plugins/github/FanoutReplicationConfigTest.java b/github-plugin/src/test/java/com/googlesource/gerrit/plugins/github/FanoutReplicationConfigTest.java new file mode 100644 index 0000000..3613c41 --- /dev/null +++ b/github-plugin/src/test/java/com/googlesource/gerrit/plugins/github/FanoutReplicationConfigTest.java
@@ -0,0 +1,99 @@ +// Copyright (C) 2023 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.googlesource.gerrit.plugins.github; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.gerrit.server.config.SitePaths; +import com.googlesource.gerrit.plugins.github.git.FanoutReplicationConfig; +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.commons.io.FileUtils; +import org.eclipse.jgit.storage.file.FileBasedConfig; +import org.eclipse.jgit.util.FS; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class FanoutReplicationConfigTest { + + private static final String CUSTOM_KEY = "mykey"; + private static final String CUSTOM_VALUE = "myvalue"; + private static final String REMOTE_ENDPOINT = "my-remote-endpoint"; + private static final String TEST_REMOTE_URL = "http://github.com/myurl"; + private static final String TEST_PROJECT_NAME = "myprojectname"; + private Path tempDir; + private SitePaths sitePaths; + + @Before + public void setup() throws Exception { + tempDir = Files.createTempDirectory(getClass().getSimpleName()); + sitePaths = new SitePaths(tempDir); + Files.createDirectories(sitePaths.etc_dir); + } + + @After + public void teardown() throws Exception { + FileUtils.deleteDirectory(tempDir.toFile()); + } + + @Test + public void shoudKeepAdHocSettingsInFanoutReplicationConfig() throws Exception { + FileBasedConfig currConfig = getReplicationConfig(); + currConfig.setString("remote", null, CUSTOM_KEY, CUSTOM_VALUE); + currConfig.save(); + + String url = "http://github.com/myurl"; + FanoutReplicationConfig fanoutReplicationConfig = new FanoutReplicationConfig(sitePaths); + fanoutReplicationConfig.addReplicationRemote(REMOTE_ENDPOINT, url, "myproject"); + + currConfig.load(); + assertThat(currConfig.getString("remote", null, CUSTOM_KEY)).isEqualTo(CUSTOM_VALUE); + } + + @Test + public void shoudKeepCustomUrlInFanoutReplicationConfig() throws Exception { + FileBasedConfig currConfig = getReplicationConfig(); + String customUrl = "http://my-custom-url"; + currConfig.setString("remote", null, "url", customUrl); + currConfig.save(); + + new FanoutReplicationConfig(sitePaths) + .addReplicationRemote(REMOTE_ENDPOINT, TEST_REMOTE_URL, TEST_PROJECT_NAME); + + currConfig.load(); + assertThat(currConfig.getString("remote", null, "url")).isEqualTo(customUrl); + } + + @Test + public void shoudKeepCustomPushRefSpecInFanoutReplicationConfig() throws Exception { + FileBasedConfig currConfig = getReplicationConfig(); + String customPushRefSpec = "+refs/heads/myheads/*:refs/heads/myheads/*"; + currConfig.setString("remote", null, "push", customPushRefSpec); + currConfig.save(); + + new FanoutReplicationConfig(sitePaths) + .addReplicationRemote(REMOTE_ENDPOINT, TEST_REMOTE_URL, TEST_PROJECT_NAME); + + currConfig.load(); + assertThat(currConfig.getString("remote", null, "push")).isEqualTo(customPushRefSpec); + } + + private FileBasedConfig getReplicationConfig() { + return new FileBasedConfig( + sitePaths.etc_dir.resolve("replication").resolve(REMOTE_ENDPOINT + ".config").toFile(), + FS.DETECTED); + } +}