From 8352cf349c36d9e20c87810295f1150d9a043d88 Mon Sep 17 00:00:00 2001 From: Rene Pfeuffer Date: Fri, 8 Sep 2023 13:22:57 +0200 Subject: [PATCH] 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 --- gradle/changelog/git_initialization.yaml | 2 + .../GitRepositoryConfigInitializer.java | 16 +- .../GitRepositoryConfigInitializerTest.java | 187 ++++++++++-------- 3 files changed, 113 insertions(+), 92 deletions(-) create mode 100644 gradle/changelog/git_initialization.yaml diff --git a/gradle/changelog/git_initialization.yaml b/gradle/changelog/git_initialization.yaml new file mode 100644 index 0000000000..23fa52f629 --- /dev/null +++ b/gradle/changelog/git_initialization.yaml @@ -0,0 +1,2 @@ +- type: fixed + description: Exception in SVN repositories due to incorrect git initialization diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitRepositoryConfigInitializer.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitRepositoryConfigInitializer.java index edd56e2e74..3a6f2b31e5 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitRepositoryConfigInitializer.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitRepositoryConfigInitializer.java @@ -50,15 +50,17 @@ public class GitRepositoryConfigInitializer { @Subscribe public void initConfig(PostReceiveRepositoryHookEvent event) { - ConfigurationStore store = storeProvider.get(event.getRepository()); - GitRepositoryConfig repositoryConfig = store.get(); - if (repositoryConfig == null || Strings.isNullOrEmpty(repositoryConfig.getDefaultBranch())) { - List defaultBranchCandidates = event.getContext().getBranchProvider().getCreatedOrModified(); + if (GitRepositoryHandler.TYPE_NAME.equals(event.getRepository().getType())) { + ConfigurationStore store = storeProvider.get(event.getRepository()); + GitRepositoryConfig repositoryConfig = store.get(); + if (repositoryConfig == null || Strings.isNullOrEmpty(repositoryConfig.getDefaultBranch())) { + List 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); + } } } diff --git a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/GitRepositoryConfigInitializerTest.java b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/GitRepositoryConfigInitializerTest.java index d89d8a42a4..2278fc00fe 100644 --- a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/GitRepositoryConfigInitializerTest.java +++ b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/GitRepositoryConfigInitializerTest.java @@ -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 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 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); - } - }