Remove old property for default branch

This commit is contained in:
René Pfeuffer
2018-12-12 17:30:23 +01:00
parent ddc2cbbfab
commit dbbe467479
22 changed files with 148 additions and 342 deletions

View File

@@ -1,5 +1,9 @@
package sonia.scm.api.v2.resources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.event.ScmEventBus;
import sonia.scm.repository.ClearRepositoryCacheEvent;
import sonia.scm.repository.GitRepositoryConfig;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
@@ -22,15 +26,17 @@ import static sonia.scm.NotFoundException.notFound;
public class GitRepositoryConfigResource {
private static final Logger LOG = LoggerFactory.getLogger(GitRepositoryConfigResource.class);
private final GitRepositoryConfigMapper repositoryConfigMapper;
private final RepositoryManager repositoryManager;
private final ConfigurationStoreFactory configurationStoreFactory;
private final GitRepositoryConfigStoreProvider gitRepositoryConfigStoreProvider;
@Inject
public GitRepositoryConfigResource(GitRepositoryConfigMapper repositoryConfigMapper, RepositoryManager repositoryManager, ConfigurationStoreFactory configurationStoreFactory) {
public GitRepositoryConfigResource(GitRepositoryConfigMapper repositoryConfigMapper, RepositoryManager repositoryManager, GitRepositoryConfigStoreProvider gitRepositoryConfigStoreProvider) {
this.repositoryConfigMapper = repositoryConfigMapper;
this.repositoryManager = repositoryManager;
this.configurationStoreFactory = configurationStoreFactory;
this.gitRepositoryConfigStoreProvider = gitRepositoryConfigStoreProvider;
}
@GET
@@ -55,9 +61,15 @@ public class GitRepositoryConfigResource {
ConfigurationStore<GitRepositoryConfig> repositoryConfigStore = getStore(repository);
GitRepositoryConfig config = repositoryConfigMapper.map(dto);
repositoryConfigStore.set(config);
LOG.info("git default branch of repository {} has changed, sending clear cache event", repository.getNamespaceAndName());
sendClearRepositoryCacheEvent(repository);
return Response.noContent().build();
}
private void sendClearRepositoryCacheEvent(Repository repository) {
ScmEventBus.getInstance().post(new ClearRepositoryCacheEvent(repository));
}
private Repository getRepository(@PathParam("namespace") String namespace, @PathParam("name") String name) {
NamespaceAndName namespaceAndName = new NamespaceAndName(namespace, name);
Repository repository = repositoryManager.get(namespaceAndName);
@@ -68,6 +80,6 @@ public class GitRepositoryConfigResource {
}
private ConfigurationStore<GitRepositoryConfig> getStore(Repository repository) {
return configurationStoreFactory.withType(GitRepositoryConfig.class).withName("gitConfig").forRepository(repository).build();
return gitRepositoryConfigStoreProvider.get(repository);
}
}

View File

@@ -0,0 +1,22 @@
package sonia.scm.api.v2.resources;
import sonia.scm.repository.GitRepositoryConfig;
import sonia.scm.repository.Repository;
import sonia.scm.store.ConfigurationStore;
import sonia.scm.store.ConfigurationStoreFactory;
import javax.inject.Inject;
public class GitRepositoryConfigStoreProvider {
private final ConfigurationStoreFactory configurationStoreFactory;
@Inject
public GitRepositoryConfigStoreProvider(ConfigurationStoreFactory configurationStoreFactory) {
this.configurationStoreFactory = configurationStoreFactory;
}
public ConfigurationStore<GitRepositoryConfig> get(Repository repository) {
return configurationStoreFactory.withType(GitRepositoryConfig.class).withName("gitConfig").forRepository(repository).build();
}
}

View File

@@ -1,49 +0,0 @@
/**
* Copyright (c) 2014, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository;
/**
* Constants for Git.
*
* @author Sebastian Sdorra
* @since 1.50
*/
public final class GitConstants {
/**
* Default branch repository property.
*/
public static final String PROPERTY_DEFAULT_BRANCH = "git.default-branch";
private GitConstants() {
}
}

View File

@@ -8,6 +8,13 @@ import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class GitRepositoryConfig {
public GitRepositoryConfig() {
}
public GitRepositoryConfig(String defaultBranch) {
this.defaultBranch = defaultBranch;
}
private String defaultBranch;
public String getDefaultBranch() {

View File

@@ -1,97 +0,0 @@
/**
* Copyright (c) 2014, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository;
import com.github.legman.Subscribe;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.EagerSingleton;
import sonia.scm.HandlerEventType;
import sonia.scm.event.ScmEventBus;
import sonia.scm.plugin.Extension;
/**
* Repository listener which handles git related repository events.
*
* @author Sebastian Sdorra
* @since 1.50
*/
@Extension
@EagerSingleton
public class GitRepositoryModifyListener {
/**
* the logger for GitRepositoryModifyListener
*/
private static final Logger logger = LoggerFactory.getLogger(GitRepositoryModifyListener.class);
/**
* Receives {@link RepositoryModificationEvent} and fires a {@link ClearRepositoryCacheEvent} if
* the default branch of a git repository was modified.
*
* @param event repository modification event
*/
@Subscribe
public void handleEvent(RepositoryModificationEvent event){
Repository repository = event.getItem();
if ( isModifyEvent(event) &&
isGitRepository(event.getItem()) &&
hasDefaultBranchChanged(event.getItemBeforeModification(), repository))
{
logger.info("git default branch of repository {} has changed, sending clear cache event", repository.getId());
sendClearRepositoryCacheEvent(repository);
}
}
@VisibleForTesting
protected void sendClearRepositoryCacheEvent(Repository repository) {
ScmEventBus.getInstance().post(new ClearRepositoryCacheEvent(repository));
}
private boolean isModifyEvent(RepositoryEvent event) {
return event.getEventType() == HandlerEventType.MODIFY;
}
private boolean isGitRepository(Repository repository) {
return GitRepositoryHandler.TYPE_NAME.equals(repository.getType());
}
private boolean hasDefaultBranchChanged(Repository old, Repository current) {
return !Objects.equal(
old.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH),
current.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH)
);
}
}

View File

@@ -40,7 +40,6 @@ import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.GitConstants;
import sonia.scm.repository.GitUtil;
import java.io.IOException;
@@ -110,7 +109,7 @@ public class AbstractGitCommand
protected Ref getBranchOrDefault(Repository gitRepository, String requestedBranch) throws IOException {
if ( Strings.isNullOrEmpty(requestedBranch) ) {
String defaultBranchName = repository.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH);
String defaultBranchName = context.getConfig().getDefaultBranch();
if (!Strings.isNullOrEmpty(defaultBranchName)) {
return GitUtil.getBranchId(gitRepository, defaultBranchName);
} else {

View File

@@ -37,6 +37,8 @@ package sonia.scm.repository.spi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.api.v2.resources.GitRepositoryConfigStoreProvider;
import sonia.scm.repository.GitRepositoryConfig;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.Repository;
@@ -68,10 +70,11 @@ public class GitContext implements Closeable
* @param directory
* @param repository
*/
public GitContext(File directory, Repository repository)
public GitContext(File directory, Repository repository, GitRepositoryConfigStoreProvider storeProvider)
{
this.directory = directory;
this.repository = repository;
this.storeProvider = storeProvider;
}
//~--- methods --------------------------------------------------------------
@@ -117,11 +120,25 @@ public class GitContext implements Closeable
return directory;
}
GitRepositoryConfig getConfig() {
GitRepositoryConfig config = storeProvider.get(repository).get();
if (config == null) {
return new GitRepositoryConfig();
} else {
return config;
}
}
void setConfig(GitRepositoryConfig newConfig) {
storeProvider.get(repository).set(newConfig);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private final File directory;
private final Repository repository;
private final GitRepositoryConfigStoreProvider storeProvider;
/** Field description */
private org.eclipse.jgit.lib.Repository gitRepository;

View File

@@ -34,6 +34,7 @@
package sonia.scm.repository.spi;
import com.google.common.collect.ImmutableSet;
import sonia.scm.api.v2.resources.GitRepositoryConfigStoreProvider;
import sonia.scm.repository.Feature;
import sonia.scm.repository.GitRepositoryHandler;
import sonia.scm.repository.Repository;
@@ -73,10 +74,10 @@ public class GitRepositoryServiceProvider extends RepositoryServiceProvider
//~--- constructors ---------------------------------------------------------
public GitRepositoryServiceProvider(GitRepositoryHandler handler, Repository repository) {
public GitRepositoryServiceProvider(GitRepositoryHandler handler, Repository repository, GitRepositoryConfigStoreProvider storeProvider) {
this.handler = handler;
this.repository = repository;
this.context = new GitContext(handler.getDirectory(repository.getId()), repository);
this.context = new GitContext(handler.getDirectory(repository.getId()), repository, storeProvider);
}
//~--- methods --------------------------------------------------------------

View File

@@ -35,6 +35,7 @@ package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import sonia.scm.api.v2.resources.GitRepositoryConfigStoreProvider;
import sonia.scm.plugin.Extension;
import sonia.scm.repository.GitRepositoryHandler;
import sonia.scm.repository.Repository;
@@ -47,10 +48,12 @@ import sonia.scm.repository.Repository;
public class GitRepositoryServiceResolver implements RepositoryServiceResolver {
private final GitRepositoryHandler handler;
private final GitRepositoryConfigStoreProvider storeProvider;
@Inject
public GitRepositoryServiceResolver(GitRepositoryHandler handler) {
public GitRepositoryServiceResolver(GitRepositoryHandler handler, GitRepositoryConfigStoreProvider storeProvider) {
this.handler = handler;
this.storeProvider = storeProvider;
}
@Override
@@ -58,7 +61,7 @@ public class GitRepositoryServiceResolver implements RepositoryServiceResolver {
GitRepositoryServiceProvider provider = null;
if (GitRepositoryHandler.TYPE_NAME.equalsIgnoreCase(repository.getType())) {
provider = new GitRepositoryServiceProvider(handler, repository);
provider = new GitRepositoryServiceProvider(handler, repository, storeProvider);
}
return provider;