mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 22:45:45 +01:00
Do not try to init non-git repos for git
The initialization to set the default branch in git repositories should not be executed for non-git repositories. Otherwise, this throws exceptions in SVN repositories all the time because the branch command is not supported. Committed-by: Konstantin Schaper <konstantin.schaper@cloudogu.com>
This commit is contained in:
@@ -50,15 +50,17 @@ public class GitRepositoryConfigInitializer {
|
||||
|
||||
@Subscribe
|
||||
public void initConfig(PostReceiveRepositoryHookEvent event) {
|
||||
ConfigurationStore<GitRepositoryConfig> store = storeProvider.get(event.getRepository());
|
||||
GitRepositoryConfig repositoryConfig = store.get();
|
||||
if (repositoryConfig == null || Strings.isNullOrEmpty(repositoryConfig.getDefaultBranch())) {
|
||||
List<String> defaultBranchCandidates = event.getContext().getBranchProvider().getCreatedOrModified();
|
||||
if (GitRepositoryHandler.TYPE_NAME.equals(event.getRepository().getType())) {
|
||||
ConfigurationStore<GitRepositoryConfig> store = storeProvider.get(event.getRepository());
|
||||
GitRepositoryConfig repositoryConfig = store.get();
|
||||
if (repositoryConfig == null || Strings.isNullOrEmpty(repositoryConfig.getDefaultBranch())) {
|
||||
List<String> defaultBranchCandidates = event.getContext().getBranchProvider().getCreatedOrModified();
|
||||
|
||||
String defaultBranch = determineDefaultBranch(defaultBranchCandidates);
|
||||
String defaultBranch = determineDefaultBranch(defaultBranchCandidates);
|
||||
|
||||
GitRepositoryConfig gitRepositoryConfig = new GitRepositoryConfig(defaultBranch);
|
||||
store.set(gitRepositoryConfig);
|
||||
GitRepositoryConfig gitRepositoryConfig = new GitRepositoryConfig(defaultBranch);
|
||||
store.set(gitRepositoryConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -65,113 +65,130 @@ class GitRepositoryConfigInitializerTest {
|
||||
@InjectMocks
|
||||
private GitRepositoryConfigInitializer initializer;
|
||||
|
||||
@BeforeEach
|
||||
void initMocks() {
|
||||
when(storeProvider.get(REPOSITORY)).thenReturn(configStore);
|
||||
when(event.getRepository()).thenReturn(REPOSITORY);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDoNothingIfDefaultBranchAlreadySet() {
|
||||
when(configStore.get()).thenReturn(new GitRepositoryConfig("any"));
|
||||
void shouldSkipNonGitRepositories() {
|
||||
REPOSITORY.setType("svn");
|
||||
|
||||
initializer.initConfig(event);
|
||||
|
||||
verify(event, never()).getContext();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void initEvent() {
|
||||
when(event.getRepository()).thenReturn(REPOSITORY);
|
||||
}
|
||||
|
||||
@Nested
|
||||
class WithGlobalConfig {
|
||||
class ForGitRepositories {
|
||||
|
||||
@BeforeEach
|
||||
void initRepoHandler() {
|
||||
GitConfig gitConfig = new GitConfig();
|
||||
gitConfig.setDefaultBranch("global_default");
|
||||
when(repoHandler.getConfig()).thenReturn(gitConfig);
|
||||
void initMocks() {
|
||||
when(storeProvider.get(REPOSITORY)).thenReturn(configStore);
|
||||
REPOSITORY.setType("git");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSetDefaultBranchIfNoGitConfigYet() {
|
||||
when(configStore.get()).thenReturn(null);
|
||||
initEvent(List.of("main"));
|
||||
void shouldDoNothingIfDefaultBranchAlreadySet() {
|
||||
when(configStore.get()).thenReturn(new GitRepositoryConfig("any"));
|
||||
|
||||
initializer.initConfig(event);
|
||||
|
||||
verify(event).getContext();
|
||||
verify(event, never()).getContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDetermineAndApplyDefaultBranch_GlobalDefault() {
|
||||
initEvent(List.of("global_default", "main", "master"));
|
||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||
@Nested
|
||||
class WithGlobalConfig {
|
||||
|
||||
initializer.initConfig(event);
|
||||
@BeforeEach
|
||||
void initRepoHandler() {
|
||||
GitConfig gitConfig = new GitConfig();
|
||||
gitConfig.setDefaultBranch("global_default");
|
||||
when(repoHandler.getConfig()).thenReturn(gitConfig);
|
||||
}
|
||||
|
||||
verify(configStore).set(argThat(arg -> {
|
||||
assertThat(arg.getDefaultBranch()).isEqualTo("global_default");
|
||||
return true;
|
||||
}));
|
||||
@Test
|
||||
void shouldSetDefaultBranchIfNoGitConfigYet() {
|
||||
when(configStore.get()).thenReturn(null);
|
||||
initEvent(List.of("main"));
|
||||
|
||||
initializer.initConfig(event);
|
||||
|
||||
verify(event).getContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDetermineAndApplyDefaultBranch_GlobalDefault() {
|
||||
initEvent(List.of("global_default", "main", "master"));
|
||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||
|
||||
initializer.initConfig(event);
|
||||
|
||||
verify(configStore).set(argThat(arg -> {
|
||||
assertThat(arg.getDefaultBranch()).isEqualTo("global_default");
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDetermineAndApplyDefaultBranch_Main() {
|
||||
initEvent(List.of("master", "main"));
|
||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||
|
||||
initializer.initConfig(event);
|
||||
|
||||
verify(configStore).set(argThat(arg -> {
|
||||
assertThat(arg.getDefaultBranch()).isEqualTo("main");
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDetermineAndApplyDefaultBranch_Master() {
|
||||
initEvent(List.of("develop", "master"));
|
||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||
|
||||
initializer.initConfig(event);
|
||||
|
||||
verify(configStore).set(argThat(arg -> {
|
||||
assertThat(arg.getDefaultBranch()).isEqualTo("master");
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDetermineAndApplyDefaultBranch_BestMatch() {
|
||||
initEvent(List.of("test/123", "trillian", "dent"));
|
||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||
|
||||
initializer.initConfig(event);
|
||||
|
||||
verify(configStore).set(argThat(arg -> {
|
||||
assertThat(arg.getDefaultBranch()).isEqualTo("dent");
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDetermineAndApplyDefaultBranch_AnyFallback() {
|
||||
initEvent(List.of("test/123", "x/y/z"));
|
||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||
|
||||
initializer.initConfig(event);
|
||||
|
||||
verify(configStore).set(argThat(arg -> {
|
||||
assertThat(arg.getDefaultBranch()).isEqualTo("test/123");
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDetermineAndApplyDefaultBranch_Main() {
|
||||
initEvent(List.of("master", "main"));
|
||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||
|
||||
initializer.initConfig(event);
|
||||
|
||||
verify(configStore).set(argThat(arg -> {
|
||||
assertThat(arg.getDefaultBranch()).isEqualTo("main");
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDetermineAndApplyDefaultBranch_Master() {
|
||||
initEvent(List.of("develop", "master"));
|
||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||
|
||||
initializer.initConfig(event);
|
||||
|
||||
verify(configStore).set(argThat(arg -> {
|
||||
assertThat(arg.getDefaultBranch()).isEqualTo("master");
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDetermineAndApplyDefaultBranch_BestMatch() {
|
||||
initEvent(List.of("test/123", "trillian", "dent"));
|
||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||
|
||||
initializer.initConfig(event);
|
||||
|
||||
verify(configStore).set(argThat(arg -> {
|
||||
assertThat(arg.getDefaultBranch()).isEqualTo("dent");
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDetermineAndApplyDefaultBranch_AnyFallback() {
|
||||
initEvent(List.of("test/123", "x/y/z"));
|
||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||
|
||||
initializer.initConfig(event);
|
||||
|
||||
verify(configStore).set(argThat(arg -> {
|
||||
assertThat(arg.getDefaultBranch()).isEqualTo("test/123");
|
||||
return true;
|
||||
}));
|
||||
private void initEvent(List<String> branches) {
|
||||
HookContext hookContext = mock(HookContext.class);
|
||||
when(event.getContext()).thenReturn(hookContext);
|
||||
HookBranchProvider branchProvider = mock(HookBranchProvider.class);
|
||||
when(hookContext.getBranchProvider()).thenReturn(branchProvider);
|
||||
when(branchProvider.getCreatedOrModified()).thenReturn(branches);
|
||||
}
|
||||
}
|
||||
|
||||
private void initEvent(List<String> branches) {
|
||||
HookContext hookContext = mock(HookContext.class);
|
||||
when(event.getContext()).thenReturn(hookContext);
|
||||
HookBranchProvider branchProvider = mock(HookBranchProvider.class);
|
||||
when(hookContext.getBranchProvider()).thenReturn(branchProvider);
|
||||
when(branchProvider.getCreatedOrModified()).thenReturn(branches);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user