mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 07:25:44 +01:00
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:
@@ -41,10 +41,15 @@ import com.google.inject.Singleton;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.ConfigurationException;
|
||||
import sonia.scm.ContextEntry;
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.installer.HgInstaller;
|
||||
import sonia.scm.installer.HgInstallerFactory;
|
||||
import sonia.scm.io.ExtendedCommand;
|
||||
import sonia.scm.io.INIConfiguration;
|
||||
import sonia.scm.io.INIConfigurationReader;
|
||||
import sonia.scm.io.INIConfigurationWriter;
|
||||
import sonia.scm.io.INISection;
|
||||
import sonia.scm.plugin.Extension;
|
||||
import sonia.scm.repository.spi.HgRepositoryServiceProvider;
|
||||
import sonia.scm.store.ConfigurationStoreFactory;
|
||||
@@ -98,6 +103,8 @@ public class HgRepositoryHandler
|
||||
/** Field description */
|
||||
public static final String PATH_HGRC =
|
||||
".hg".concat(File.separator).concat("hgrc");
|
||||
private static final String CONFIG_SECTION_SCMM = "scmm";
|
||||
private static final String CONFIG_KEY_REPOSITORY_ID = "repositoryid";
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
@@ -322,6 +329,26 @@ public class HgRepositoryHandler
|
||||
protected void postCreate(Repository repository, File directory)
|
||||
throws IOException
|
||||
{
|
||||
File hgrcFile = new File(directory, PATH_HGRC);
|
||||
INIConfiguration hgrc = new INIConfiguration();
|
||||
|
||||
INISection iniSection = new INISection(CONFIG_SECTION_SCMM);
|
||||
iniSection.setParameter(CONFIG_KEY_REPOSITORY_ID, repository.getId());
|
||||
INIConfiguration iniConfiguration = new INIConfiguration();
|
||||
iniConfiguration.addSection(iniSection);
|
||||
hgrc.addSection(iniSection);
|
||||
|
||||
INIConfigurationWriter writer = new INIConfigurationWriter();
|
||||
|
||||
writer.write(hgrc, hgrcFile);
|
||||
}
|
||||
|
||||
public String getRepositoryId(File directory) {
|
||||
try {
|
||||
return new INIConfigurationReader().read(new File(directory, PATH_HGRC)).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);
|
||||
}
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
@@ -62,11 +62,11 @@ public class HgHookChangesetProvider implements HookChangesetProvider
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
public HgHookChangesetProvider(HgRepositoryHandler handler,
|
||||
sonia.scm.repository.Repository repository, HgHookManager hookManager, String startRev,
|
||||
RepositoryHookType type)
|
||||
File repositoryDirectory, HgHookManager hookManager, String startRev,
|
||||
RepositoryHookType type)
|
||||
{
|
||||
this.handler = handler;
|
||||
this.repository = repository;
|
||||
this.repositoryDirectory = repositoryDirectory;
|
||||
this.hookManager = hookManager;
|
||||
this.startRev = startRev;
|
||||
this.type = type;
|
||||
@@ -123,8 +123,6 @@ public class HgHookChangesetProvider implements HookChangesetProvider
|
||||
*/
|
||||
private Repository open()
|
||||
{
|
||||
File repositoryDirectory = handler.getDirectory(repository);
|
||||
|
||||
// use HG_PENDING only for pre receive hooks
|
||||
boolean pending = type == RepositoryHookType.PRE_RECEIVE;
|
||||
|
||||
@@ -142,7 +140,7 @@ public class HgHookChangesetProvider implements HookChangesetProvider
|
||||
private HgHookManager hookManager;
|
||||
|
||||
/** Field description */
|
||||
private sonia.scm.repository.Repository repository;
|
||||
private File repositoryDirectory;
|
||||
|
||||
/** Field description */
|
||||
private HookChangesetResponse response;
|
||||
|
||||
@@ -45,6 +45,7 @@ import sonia.scm.repository.api.HookFeature;
|
||||
import sonia.scm.repository.api.HookMessageProvider;
|
||||
import sonia.scm.repository.api.HookTagProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -68,16 +69,16 @@ public class HgHookContextProvider extends HookContextProvider
|
||||
* Constructs a new instance.
|
||||
*
|
||||
* @param handler mercurial repository handler
|
||||
* @param repository the changed repository
|
||||
* @param repositoryDirectory the directory of the changed repository
|
||||
* @param hookManager mercurial hook manager
|
||||
* @param startRev start revision
|
||||
* @param type type of hook
|
||||
*/
|
||||
public HgHookContextProvider(HgRepositoryHandler handler,
|
||||
Repository repository, HgHookManager hookManager, String startRev,
|
||||
File repositoryDirectory, HgHookManager hookManager, String startRev,
|
||||
RepositoryHookType type)
|
||||
{
|
||||
this.hookChangesetProvider = new HgHookChangesetProvider(handler, repository, hookManager, startRev, type);
|
||||
this.hookChangesetProvider = new HgHookChangesetProvider(handler, repositoryDirectory, hookManager, startRev, type);
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
@@ -44,12 +44,12 @@ import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.ContextEntry;
|
||||
import sonia.scm.NotFoundException;
|
||||
import sonia.scm.repository.HgContext;
|
||||
import sonia.scm.repository.HgHookManager;
|
||||
import sonia.scm.repository.HgRepositoryHandler;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.RepositoryDAO;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.RepositoryHookType;
|
||||
import sonia.scm.repository.api.HgHookMessage;
|
||||
import sonia.scm.repository.api.HgHookMessage.Severity;
|
||||
@@ -118,13 +118,12 @@ public class HgHookCallbackServlet extends HttpServlet
|
||||
@Inject
|
||||
public HgHookCallbackServlet(HookEventFacade hookEventFacade,
|
||||
HgRepositoryHandler handler, HgHookManager hookManager,
|
||||
Provider<HgContext> contextProvider, RepositoryDAO repositoryDAO)
|
||||
Provider<HgContext> contextProvider)
|
||||
{
|
||||
this.hookEventFacade = hookEventFacade;
|
||||
this.handler = handler;
|
||||
this.hookManager = hookManager;
|
||||
this.contextProvider = contextProvider;
|
||||
this.repositoryDAO = repositoryDAO;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
@@ -171,7 +170,7 @@ public class HgHookCallbackServlet extends HttpServlet
|
||||
|
||||
if (m.matches())
|
||||
{
|
||||
Repository repository = getRepositoryId(request);
|
||||
File repositoryPath = getRepositoryPath(request);
|
||||
String type = m.group(1);
|
||||
String challenge = request.getParameter(PARAM_CHALLENGE);
|
||||
|
||||
@@ -188,7 +187,7 @@ public class HgHookCallbackServlet extends HttpServlet
|
||||
authenticate(request, credentials);
|
||||
}
|
||||
|
||||
hookCallback(response, repository, type, challenge, node);
|
||||
hookCallback(response, repositoryPath, type, challenge, node);
|
||||
}
|
||||
else if (logger.isDebugEnabled())
|
||||
{
|
||||
@@ -247,8 +246,7 @@ public class HgHookCallbackServlet extends HttpServlet
|
||||
}
|
||||
}
|
||||
|
||||
private void fireHook(HttpServletResponse response, Repository repository,
|
||||
String node, RepositoryHookType type)
|
||||
private void fireHook(HttpServletResponse response, File repositoryDirectory, String node, RepositoryHookType type)
|
||||
throws IOException
|
||||
{
|
||||
HgHookContextProvider context = null;
|
||||
@@ -260,10 +258,11 @@ public class HgHookCallbackServlet extends HttpServlet
|
||||
contextProvider.get().setPending(true);
|
||||
}
|
||||
|
||||
context = new HgHookContextProvider(handler, repository, hookManager,
|
||||
context = new HgHookContextProvider(handler, repositoryDirectory, hookManager,
|
||||
node, type);
|
||||
|
||||
hookEventFacade.handle(repository).fireHookEvent(type, context);
|
||||
String repositoryId = getRepositoryId(repositoryDirectory);
|
||||
hookEventFacade.handle(repositoryId).fireHookEvent(type, context);
|
||||
|
||||
printMessages(response, context);
|
||||
}
|
||||
@@ -281,7 +280,7 @@ public class HgHookCallbackServlet extends HttpServlet
|
||||
}
|
||||
}
|
||||
|
||||
private void hookCallback(HttpServletResponse response, Repository repository, String typeName, String challenge, String node) throws IOException {
|
||||
private void hookCallback(HttpServletResponse response, File repositoryDirectory, String typeName, String challenge, String node) throws IOException {
|
||||
if (hookManager.isAcceptAble(challenge))
|
||||
{
|
||||
RepositoryHookType type = null;
|
||||
@@ -297,7 +296,7 @@ public class HgHookCallbackServlet extends HttpServlet
|
||||
|
||||
if (type != null)
|
||||
{
|
||||
fireHook(response, repository, node, type);
|
||||
fireHook(response, repositoryDirectory, node, type);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -442,29 +441,21 @@ public class HgHookCallbackServlet extends HttpServlet
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("squid:S2083") // we do nothing with the path given, so this should be no issue
|
||||
private Repository getRepositoryId(HttpServletRequest request)
|
||||
private String getRepositoryId(File repositoryPath)
|
||||
{
|
||||
Repository repository = null;
|
||||
return handler.getRepositoryId(repositoryPath);
|
||||
}
|
||||
|
||||
private File getRepositoryPath(HttpServletRequest request) {
|
||||
String path = request.getParameter(PARAM_REPOSITORYPATH);
|
||||
|
||||
if (Util.isNotEmpty(path)) {
|
||||
repository = repositoryDAO.getRepositoryForDirectory(new File(path));
|
||||
return new File(path);
|
||||
}
|
||||
else if (logger.isWarnEnabled())
|
||||
else
|
||||
{
|
||||
logger.warn("no repository path parameter found");
|
||||
throw new InternalRepositoryException(ContextEntry.ContextBuilder.entity("directory", path), "could not find hgrc in directory");
|
||||
}
|
||||
|
||||
return repository;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
@@ -480,6 +471,4 @@ public class HgHookCallbackServlet extends HttpServlet
|
||||
|
||||
/** Field description */
|
||||
private final HgHookManager hookManager;
|
||||
|
||||
private final RepositoryDAO repositoryDAO;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user