Skip to content

Commit

Permalink
fixed merged error
Browse files Browse the repository at this point in the history
  • Loading branch information
rpaasche committed Feb 17, 2021
1 parent 6ecd46d commit 1e6bd6e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.englishtown.bitbucket.hook;

import com.atlassian.bitbucket.hook.repository.PostRepositoryHookContext;
import com.atlassian.bitbucket.i18n.I18nService;
import com.atlassian.bitbucket.i18n.SimpleI18nService;
import com.atlassian.bitbucket.repository.Repository;
Expand All @@ -8,6 +9,7 @@
import com.atlassian.bitbucket.scm.git.command.GitCommand;
import com.atlassian.bitbucket.scm.git.command.GitScmCommandBuilder;
import com.atlassian.bitbucket.server.ApplicationPropertiesService;
import com.atlassian.bitbucket.setting.Settings;
import com.atlassian.bitbucket.user.DummySecurityService;
import com.atlassian.bitbucket.user.SecurityService;
import org.junit.Before;
Expand All @@ -20,7 +22,9 @@

import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.atlassian.bitbucket.mockito.MockitoUtils.returnArg;
import static com.atlassian.bitbucket.mockito.MockitoUtils.returnFirst;
Expand All @@ -37,8 +41,10 @@ public class MirrorBucketProcessorTest {
private static final String URL_HTTP = "https://bitbucket-mirror.englishtown.com/scm/test/test.git";
private static final String URL_SSH = "ssh://[email protected]/scm/test/test.git";

private static final MirrorSettings SETTINGS = new MirrorSettings() {
{
private static final MirrorSettings SETTINGS = ownSettings(true, true);

private static MirrorSettings ownSettings(boolean pruneFlag, boolean forceFlag) {
return new MirrorSettings() { {
mirrorRepoUrl = URL_SSH;
password = "test-password";
refspec = "+refs/heads/master:refs/heads/master +refs/heads/develop:refs/heads/develop";
Expand All @@ -47,8 +53,12 @@ public class MirrorBucketProcessorTest {
atomic = true;
notes = true;
tags = true;
prune = pruneFlag;
force = forceFlag;
}
};
};
}

private static final MirrorRequest REQUEST = new MirrorRequest(1, SETTINGS);
private static final List<MirrorRequest> REQUESTS = Collections.singletonList(REQUEST);

Expand Down Expand Up @@ -113,6 +123,53 @@ public void testProcess() {
verify(scmService).createBuilder(same(repository));
}


@Test
public void testPruneFlag() throws Exception{
when(repositoryService.getById(eq(1))).thenReturn(repository);

MirrorSettings settings = ownSettings(false, true);
MirrorRequest request = new MirrorRequest(1, settings);

processor.process("ignored", Collections.singletonList(request));

verify(builder).command(eq("push"));
verify(builder).argument(eq("--force"));
verify(builder).argument(eq(URL_SSH));
verify(builder).argument(eq("--atomic"));
verify(builder).argument(eq("+refs/heads/master:refs/heads/master"));
verify(builder).argument(eq("+refs/heads/develop:refs/heads/develop"));
verify(builder).argument(eq("+refs/tags/*:refs/tags/*"));
verify(builder).argument(eq("+refs/notes/*:refs/notes/*"));
verify(command).call();
verify(command).setTimeout(eq(Duration.ofSeconds(120L)));
verify(passwordEncryptor).decrypt(eq(SETTINGS.password));
verify(scmService).createBuilder(same(repository));
}

@Test
public void testForceFlag() throws Exception{
when(repositoryService.getById(eq(1))).thenReturn(repository);

MirrorSettings settings = ownSettings(true, false);
MirrorRequest request = new MirrorRequest(1, settings);

processor.process("ignored", Collections.singletonList(request));

verify(builder).command(eq("push"));
verify(builder).argument(eq("--prune"));
verify(builder).argument(eq(URL_SSH));
verify(builder).argument(eq("--atomic"));
verify(builder).argument(eq("+refs/heads/master:refs/heads/master"));
verify(builder).argument(eq("+refs/heads/develop:refs/heads/develop"));
verify(builder).argument(eq("+refs/tags/*:refs/tags/*"));
verify(builder).argument(eq("+refs/notes/*:refs/notes/*"));
verify(command).call();
verify(command).setTimeout(eq(Duration.ofSeconds(120L)));
verify(passwordEncryptor).decrypt(eq(SETTINGS.password));
verify(scmService).createBuilder(same(repository));
}

@Test
public void testProcessWithDeletedRepository() {
processor.process("ignored", REQUESTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import com.atlassian.bitbucket.hook.repository.*;
import com.atlassian.bitbucket.project.Project;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.CommandErrorHandler;
import com.atlassian.bitbucket.scm.CommandExitHandler;
import com.atlassian.bitbucket.scm.CommandOutputHandler;
import com.atlassian.bitbucket.scm.git.GitScm;
import com.atlassian.bitbucket.scm.git.command.GitScmCommandBuilder;
import com.atlassian.bitbucket.scope.Scope;
import com.atlassian.bitbucket.scope.Scopes;
import com.atlassian.bitbucket.server.ApplicationPropertiesService;
Expand Down Expand Up @@ -64,7 +68,7 @@ public class MirrorRepositoryHookTest {

@Before
public void setup() {
doReturn(bucketedExecutor).when(concurrencyService).getBucketedExecutor(anyString(), any());
doReturn(bucketedExecutor).when(concurrencyService).getBucketedExecutor(anyString(), any());

when(propertiesService.getPluginProperty(eq(PROP_ATTEMPTS), anyInt())).thenAnswer(returnArg(1));
when(propertiesService.getPluginProperty(eq(PROP_THREADS), anyInt())).thenAnswer(returnArg(1));
Expand Down Expand Up @@ -238,56 +242,8 @@ public void testValidateForProject() {
verifyZeroInteractions(bucketedExecutor, errors, settings);
}

@Test
public void testPruneFlag() throws Exception{
when(passwordEncryptor.decrypt(anyString())).thenReturn(password);

Repository repo = mock(Repository.class);

RepositoryHookContext context = mock(RepositoryHookContext.class);
Settings settings = ownSettings(false, true);
when(context.getSettings()).thenReturn(settings);
when(context.getRepository()).thenReturn(repo);

hook.postReceive(context, new ArrayList<>());

verify(executor).submit(argumentCaptor.capture());
Runnable runnable = argumentCaptor.getValue();
runnable.run();

verify(builder, times(1)).command(eq("push"));
verify(builder, never()).argument(eq("--prune"));
verify(builder, times(1)).argument(eq("--atomic"));
verify(builder, times(1)).argument(eq(repository));
verify(builder, times(1)).argument(eq("--force"));
}

@Test
public void testForceFlag() throws Exception{
when(passwordEncryptor.decrypt(anyString())).thenReturn(password);

Repository repo = mock(Repository.class);

RepositoryHookContext context = mock(RepositoryHookContext.class);
Settings settings = ownSettings(true, false);
when(context.getSettings()).thenReturn(settings);
when(context.getRepository()).thenReturn(repo);

hook.postReceive(context, new ArrayList<>());

verify(executor).submit(argumentCaptor.capture());
Runnable runnable = argumentCaptor.getValue();
runnable.run();

verify(builder, times(1)).command(eq("push"));
verify(builder, times(1)).argument(eq("--prune"));
verify(builder, times(1)).argument(eq("--atomic"));
verify(builder, times(1)).argument(eq(repository));
verify(builder, never()).argument(eq("--force"));
}

private PostRepositoryHookContext buildContext() {
RepositoryHookContext context = mock(RepositoryHookContext.class);
Settings settings = defaultSettings();

PostRepositoryHookContext context = mock(PostRepositoryHookContext.class);
Expand Down Expand Up @@ -320,22 +276,4 @@ private Settings defaultSettings() {
when(settings.getBoolean(eq(MirrorRepositoryHook.SETTING_FORCE), eq(true))).thenReturn(true);
return settings;
}

private Settings ownSettings(boolean pruneFlag, boolean forceFlag) {
Map<String, Object> map = new HashMap<String, Object>();
map.put(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL, "");

Settings settings = mock(Settings.class);
when(settings.asMap()).thenReturn(map);
when(settings.getString(eq(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL), eq(""))).thenReturn(mirrorRepoUrlHttp);
when(settings.getString(eq(MirrorRepositoryHook.SETTING_USERNAME), eq(""))).thenReturn(username);
when(settings.getString(eq(MirrorRepositoryHook.SETTING_PASSWORD), eq(""))).thenReturn(password);
when(settings.getString(eq(MirrorRepositoryHook.SETTING_REFSPEC), eq(""))).thenReturn(refspec);
when(settings.getBoolean(eq(MirrorRepositoryHook.SETTING_TAGS), eq(true))).thenReturn(true);
when(settings.getBoolean(eq(MirrorRepositoryHook.SETTING_NOTES), eq(true))).thenReturn(true);
when(settings.getBoolean(eq(MirrorRepositoryHook.SETTING_ATOMIC), eq(true))).thenReturn(true);
when(settings.getBoolean(eq(MirrorRepositoryHook.SETTING_PRUNE), eq(pruneFlag))).thenReturn(pruneFlag);
when(settings.getBoolean(eq(MirrorRepositoryHook.SETTING_FORCE), eq(forceFlag))).thenReturn(forceFlag);
return settings;
}
}

0 comments on commit 1e6bd6e

Please sign in to comment.