Store repository id in native config file

Hooks can read this repository type dependant config file and handle
the changes for the correct repository id
This commit is contained in:
René Pfeuffer
2018-11-26 17:22:17 +01:00
parent 6b663de7dd
commit 00ab764dab
17 changed files with 204 additions and 175 deletions

View File

@@ -46,6 +46,11 @@ import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.util.SVNDebugLog;
import sonia.scm.ContextEntry;
import sonia.scm.io.INIConfiguration;
import sonia.scm.io.INIConfigurationReader;
import sonia.scm.io.INIConfigurationWriter;
import sonia.scm.io.INISection;
import sonia.scm.logging.SVNKitLogger;
import sonia.scm.plugin.Extension;
import sonia.scm.repository.spi.HookEventFacade;
@@ -54,6 +59,7 @@ import sonia.scm.store.ConfigurationStoreFactory;
import sonia.scm.util.Util;
import java.io.File;
import java.io.IOException;
import static sonia.scm.ContextEntry.ContextBuilder.entity;
@@ -80,6 +86,9 @@ public class SvnRepositoryHandler
public static final RepositoryType TYPE = new RepositoryType(TYPE_NAME,
TYPE_DISPLAYNAME,
SvnRepositoryServiceProvider.COMMANDS);
private static final String CONFIG_FILE_NAME = "scm-manager.conf";
private static final String CONFIG_SECTION_SCMM = "scmm";
private static final String CONFIG_KEY_REPOSITORY_ID = "repositoryid";
private static final Logger logger =
LoggerFactory.getLogger(SvnRepositoryHandler.class);
@@ -87,8 +96,7 @@ public class SvnRepositoryHandler
@Inject
public SvnRepositoryHandler(ConfigurationStoreFactory storeFactory,
HookEventFacade eventFacade,
RepositoryLocationResolver repositoryLocationResolver,
RepositoryDAO repositoryDAO)
RepositoryLocationResolver repositoryLocationResolver)
{
super(storeFactory, repositoryLocationResolver);
@@ -101,7 +109,7 @@ public class SvnRepositoryHandler
// register hook
if (eventFacade != null)
{
FSHooks.registerHook(new SvnRepositoryHook(eventFacade, repositoryDAO));
FSHooks.registerHook(new SvnRepositoryHook(eventFacade, this));
}
else if (logger.isWarnEnabled())
{
@@ -210,4 +218,21 @@ public class SvnRepositoryHandler
{
return SvnConfig.class;
}
@Override
protected void postCreate(Repository repository, File directory) throws IOException {
INISection iniSection = new INISection(CONFIG_SECTION_SCMM);
iniSection.setParameter(CONFIG_KEY_REPOSITORY_ID, repository.getId());
INIConfiguration iniConfiguration = new INIConfiguration();
iniConfiguration.addSection(iniSection);
new INIConfigurationWriter().write(iniConfiguration, new File(directory, CONFIG_FILE_NAME));
}
String getRepositoryId(File directory) {
try {
return new INIConfigurationReader().read(new File(directory, CONFIG_FILE_NAME)).getSection(CONFIG_SECTION_SCMM).getParameter(CONFIG_KEY_REPOSITORY_ID);
} catch (IOException e) {
throw new InternalRepositoryException(ContextEntry.ContextBuilder.entity("directory", directory.toString()), "could not read scm configuration file", e);
}
}
}

View File

@@ -70,10 +70,10 @@ public class SvnRepositoryHook implements FSHook
//~--- constructors ---------------------------------------------------------
public SvnRepositoryHook(HookEventFacade hookEventFacade, RepositoryDAO repositoryDAO)
public SvnRepositoryHook(HookEventFacade hookEventFacade, SvnRepositoryHandler handler)
{
this.hookEventFacade = hookEventFacade;
this.repositoryDAO = repositoryDAO;
this.handler = handler;
}
//~--- methods --------------------------------------------------------------
@@ -154,10 +154,10 @@ public class SvnRepositoryHook implements FSHook
{
try
{
Repository repository = getRepositoryId(directory);
String repositoryId = getRepositoryId(directory);
//J-
hookEventFacade.handle(repository)
hookEventFacade.handle(repositoryId)
.fireHookEvent(
changesetProvider.getType(),
new SvnHookContextProvider(changesetProvider)
@@ -188,11 +188,10 @@ public class SvnRepositoryHook implements FSHook
*
* @throws IOException
*/
private Repository getRepositoryId(File directory)
private String getRepositoryId(File directory)
{
AssertUtil.assertIsNotNull(directory);
return repositoryDAO.getRepositoryForDirectory(directory);
return handler.getRepositoryId(directory);
}
//~--- fields ---------------------------------------------------------------
@@ -200,5 +199,5 @@ public class SvnRepositoryHook implements FSHook
/** Field description */
private HookEventFacade hookEventFacade;
private final RepositoryDAO repositoryDAO;
private final SvnRepositoryHandler handler;
}

View File

@@ -93,7 +93,7 @@ public class SvnRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
protected RepositoryHandler createRepositoryHandler(ConfigurationStoreFactory factory,
File directory) {
repositoryLocationResolver = new RepositoryLocationResolver(repoDao, new InitialRepositoryLocationResolver(contextProvider));
SvnRepositoryHandler handler = new SvnRepositoryHandler(factory, null, repositoryLocationResolver, repositoryDAO);
SvnRepositoryHandler handler = new SvnRepositoryHandler(factory, null, repositoryLocationResolver);
handler.init(contextProvider);
@@ -109,7 +109,7 @@ public class SvnRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
public void getDirectory() {
when(factory.getStore(any(), any())).thenReturn(store);
SvnRepositoryHandler repositoryHandler = new SvnRepositoryHandler(factory,
facade, repositoryLocationResolver, repositoryDAO);
facade, repositoryLocationResolver);
SvnConfig svnConfig = new SvnConfig();
repositoryHandler.setConfig(svnConfig);