Browse Source

Fix regression in hook triggers.

- Switched from RepositoryPushHookRequest to RepositoryHookRequest so
  the MirrorRepositoryHook will be triggered after any ref change
- Added some more unit tests and logging
pull/63/head
Bryan Turner 7 years ago
parent
commit
e0deef7c62
  1. 26
      src/main/java/com/englishtown/bitbucket/hook/MirrorRepositoryHook.java
  2. 2
      src/test/java/com/englishtown/bitbucket/hook/MirrorBucketProcessorTest.java
  3. 53
      src/test/java/com/englishtown/bitbucket/hook/MirrorRepositoryHookTest.java

26
src/main/java/com/englishtown/bitbucket/hook/MirrorRepositoryHook.java

@ -6,7 +6,7 @@ import com.atlassian.bitbucket.concurrent.ConcurrencyPolicy;
import com.atlassian.bitbucket.concurrent.ConcurrencyService;
import com.atlassian.bitbucket.hook.repository.PostRepositoryHook;
import com.atlassian.bitbucket.hook.repository.PostRepositoryHookContext;
import com.atlassian.bitbucket.hook.repository.RepositoryPushHookRequest;
import com.atlassian.bitbucket.hook.repository.RepositoryHookRequest;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.git.GitScm;
import com.atlassian.bitbucket.scope.RepositoryScope;
@ -27,7 +27,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class MirrorRepositoryHook implements PostRepositoryHook<RepositoryPushHookRequest>, SettingsValidator {
public class MirrorRepositoryHook implements PostRepositoryHook<RepositoryHookRequest>, SettingsValidator {
static final String PROP_PREFIX = "plugin.com.englishtown.stash-hook-mirror.push.";
static final String PROP_ATTEMPTS = PROP_PREFIX + "attempts";
@ -73,19 +73,23 @@ public class MirrorRepositoryHook implements PostRepositoryHook<RepositoryPushHo
* Schedules pushes to apply the latest changes to any configured mirrors.
*
* @param context provides any settings which have been configured for the hook
* @param request describes the push
* @param request describes the repository and refs which were updated
*/
@Override
public void postUpdate(@Nonnull PostRepositoryHookContext context,
@Nonnull RepositoryPushHookRequest request) {
public void postUpdate(@Nonnull PostRepositoryHookContext context, @Nonnull RepositoryHookRequest request) {
Repository repository = request.getRepository();
if (!GitScm.ID.equalsIgnoreCase(repository.getScmId())) {
return;
}
logger.debug("{}: MirrorRepositoryHook: postReceive started.", repository);
getMirrorSettings(context.getSettings())
.forEach(settings -> pushExecutor.schedule(new MirrorRequest(repository, settings), 5L, TimeUnit.SECONDS));
List<MirrorSettings> mirrorSettings = getMirrorSettings(context.getSettings());
if (mirrorSettings.isEmpty()) {
logger.debug("{}: Mirroring is not configured", repository);
} else {
logger.debug("{}: Scheduling pushes for {} remote(s) after {}",
repository, mirrorSettings.size(), request.getTrigger());
schedulePushes(repository, mirrorSettings);
}
}
/**
@ -122,7 +126,7 @@ public class MirrorRepositoryHook implements PostRepositoryHook<RepositoryPushHo
// If no errors, run the mirror command
if (ok) {
updateSettings(mirrorSettings, settings);
mirrorSettings.forEach(ms -> pushExecutor.schedule(new MirrorRequest(repository, ms), 5L, TimeUnit.SECONDS));
schedulePushes(repository, mirrorSettings);
}
} catch (Exception e) {
logger.error("Error running MirrorRepositoryHook validate.", e);
@ -160,6 +164,10 @@ public class MirrorRepositoryHook implements PostRepositoryHook<RepositoryPushHo
return results;
}
private void schedulePushes(Repository repository, List<MirrorSettings> list) {
list.forEach(settings -> pushExecutor.schedule(new MirrorRequest(repository, settings), 5L, TimeUnit.SECONDS));
}
private boolean validate(MirrorSettings ms, SettingsValidationErrors errors) {
boolean result = true;
boolean isHttp = false;

2
src/test/java/com/englishtown/bitbucket/hook/MirrorBucketProcessorTest.java

@ -30,7 +30,7 @@ import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
/**
* Unit tests for {@link MirrorRepositoryHook}
* Unit tests for {@link MirrorBucketProcessor}.
*/
public class MirrorBucketProcessorTest {

53
src/test/java/com/englishtown/bitbucket/hook/MirrorRepositoryHookTest.java

@ -4,6 +4,7 @@ import com.atlassian.bitbucket.concurrent.BucketedExecutor;
import com.atlassian.bitbucket.concurrent.ConcurrencyService;
import com.atlassian.bitbucket.hook.repository.PostRepositoryHookContext;
import com.atlassian.bitbucket.hook.repository.RepositoryPushHookRequest;
import com.atlassian.bitbucket.project.Project;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.git.GitScm;
import com.atlassian.bitbucket.scope.Scope;
@ -20,8 +21,10 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static com.atlassian.bitbucket.mockito.MockitoUtils.returnArg;
@ -31,7 +34,7 @@ import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
/**
* Unit tests for {@link MirrorRepositoryHook}
* Unit tests for {@link MirrorRepositoryHook}.
*/
public class MirrorRepositoryHookTest {
@ -72,7 +75,7 @@ public class MirrorRepositoryHookTest {
}
@Test
public void testPostReceive() {
public void testPostUpdate() {
when(passwordEncryptor.decrypt(anyString())).thenReturn(password);
Repository repo = mock(Repository.class);
@ -89,6 +92,32 @@ public class MirrorRepositoryHookTest {
assertEquals(1, request.getRepositoryId());
}
@Test
public void testPostUpdateForHgRepository() {
Repository repo = mock(Repository.class);
when(repo.getScmId()).thenReturn("hg");
hook.postUpdate(buildContext(), new RepositoryPushHookRequest.Builder(repo).build());
verifyZeroInteractions(bucketedExecutor);
}
@Test
public void testPostUpdateUnconfigured() {
Repository repo = mock(Repository.class);
when(repo.getScmId()).thenReturn(GitScm.ID);
Settings settings = mock(Settings.class);
when(settings.asMap()).thenReturn(Collections.emptyMap());
PostRepositoryHookContext context = mock(PostRepositoryHookContext.class);
when(context.getSettings()).thenReturn(settings);
hook.postUpdate(context, new RepositoryPushHookRequest.Builder(repo).build());
verifyZeroInteractions(bucketedExecutor);
}
@Test
public void testValidate() {
Settings settings = mock(Settings.class);
@ -178,7 +207,27 @@ public class MirrorRepositoryHookTest {
hook.validate(settings, errors, scope);
verify(errors, never()).addFormError(anyString());
verify(errors, never()).addFieldError(anyString(), anyString());
}
@Test
public void testValidateForGlobal() {
SettingsValidationErrors errors = mock(SettingsValidationErrors.class);
Settings settings = mock(Settings.class);
hook.validate(settings, errors, Scopes.global());
verifyZeroInteractions(bucketedExecutor, errors, settings);
}
@Test
public void testValidateForProject() {
SettingsValidationErrors errors = mock(SettingsValidationErrors.class);
Project project = mock(Project.class);
Settings settings = mock(Settings.class);
hook.validate(settings, errors, Scopes.project(project));
verifyZeroInteractions(bucketedExecutor, errors, settings);
}
private PostRepositoryHookContext buildContext() {

Loading…
Cancel
Save