Browse Source

Merge branch 'UniversityofWarwick-feature/bitbucket-6-support' into develop

develop
Adrian Gonzalez 6 years ago
parent
commit
7539cd8e5c
  1. 33
      src/main/java/com/englishtown/bitbucket/hook/MirrorRepositoryHook.java
  2. 20
      src/test/java/com/englishtown/bitbucket/hook/MirrorRepositoryHookTest.java

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

@ -4,9 +4,7 @@ import com.atlassian.bitbucket.concurrent.BucketedExecutor;
import com.atlassian.bitbucket.concurrent.BucketedExecutorSettings; import com.atlassian.bitbucket.concurrent.BucketedExecutorSettings;
import com.atlassian.bitbucket.concurrent.ConcurrencyPolicy; import com.atlassian.bitbucket.concurrent.ConcurrencyPolicy;
import com.atlassian.bitbucket.concurrent.ConcurrencyService; import com.atlassian.bitbucket.concurrent.ConcurrencyService;
import com.atlassian.bitbucket.hook.repository.PostRepositoryHook; import com.atlassian.bitbucket.hook.repository.*;
import com.atlassian.bitbucket.hook.repository.PostRepositoryHookContext;
import com.atlassian.bitbucket.hook.repository.RepositoryHookRequest;
import com.atlassian.bitbucket.repository.Repository; import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.git.GitScm; import com.atlassian.bitbucket.scm.git.GitScm;
import com.atlassian.bitbucket.scope.RepositoryScope; import com.atlassian.bitbucket.scope.RepositoryScope;
@ -16,15 +14,13 @@ import com.atlassian.bitbucket.server.ApplicationPropertiesService;
import com.atlassian.bitbucket.setting.Settings; import com.atlassian.bitbucket.setting.Settings;
import com.atlassian.bitbucket.setting.SettingsValidationErrors; import com.atlassian.bitbucket.setting.SettingsValidationErrors;
import com.atlassian.bitbucket.setting.SettingsValidator; import com.atlassian.bitbucket.setting.SettingsValidator;
import com.google.common.collect.ImmutableSet;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.net.URI; import java.net.URI;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class MirrorRepositoryHook implements PostRepositoryHook<RepositoryHookRequest>, SettingsValidator { public class MirrorRepositoryHook implements PostRepositoryHook<RepositoryHookRequest>, SettingsValidator {
@ -40,6 +36,14 @@ public class MirrorRepositoryHook implements PostRepositoryHook<RepositoryHookRe
static final String SETTING_NOTES = "notes"; static final String SETTING_NOTES = "notes";
static final String SETTING_ATOMIC = "atomic"; static final String SETTING_ATOMIC = "atomic";
/**
* Trigger types that don't cause a mirror to happen
*/
private static Set<RepositoryHookTrigger> TRIGGERS_TO_IGNORE =
ImmutableSet.of(
StandardRepositoryHookTrigger.UNKNOWN
);
private final PasswordEncryptor passwordEncryptor; private final PasswordEncryptor passwordEncryptor;
private final SettingsReflectionHelper settingsReflectionHelper; private final SettingsReflectionHelper settingsReflectionHelper;
private final BucketedExecutor<MirrorRequest> pushExecutor; private final BucketedExecutor<MirrorRequest> pushExecutor;
@ -72,11 +76,16 @@ public class MirrorRepositoryHook implements PostRepositoryHook<RepositoryHookRe
/** /**
* Schedules pushes to apply the latest changes to any configured mirrors. * Schedules pushes to apply the latest changes to any configured mirrors.
* *
* @param context provides any settings which have been configured for the hook * @param context provides hook settings and a way to obtain the commits added/removed
* @param request describes the repository and refs which were updated * @param request provides details about the refs that have been updated
*/ */
@Override @Override
public void postUpdate(@Nonnull PostRepositoryHookContext context, @Nonnull RepositoryHookRequest request) { public void postUpdate(@Nonnull PostRepositoryHookContext context, @Nonnull RepositoryHookRequest request) {
if (TRIGGERS_TO_IGNORE.contains(request.getTrigger())) {
logger.trace("MirrorRepositoryHook: skipping trigger {}", request.getTrigger());
return;
}
Repository repository = request.getRepository(); Repository repository = request.getRepository();
if (!GitScm.ID.equalsIgnoreCase(repository.getScmId())) { if (!GitScm.ID.equalsIgnoreCase(repository.getScmId())) {
return; return;
@ -93,11 +102,11 @@ public class MirrorRepositoryHook implements PostRepositoryHook<RepositoryHookRe
} }
/** /**
* Validates hook settings before they are persisted, and encrypts any user-supplied password. * Validate the given {@code settings} before they are persisted., and encrypts any user-supplied password.
* *
* @param settings to be validated * @param settings to be validated
* @param errors callback for reporting validation errors * @param errors callback for reporting validation errors.
* @param scope the scope for which the hook has been configured * @param scope the context {@code Repository} the settings will be associated with
*/ */
@Override @Override
public void validate(@Nonnull Settings settings, @Nonnull SettingsValidationErrors errors, @Nonnull Scope scope) { public void validate(@Nonnull Settings settings, @Nonnull SettingsValidationErrors errors, @Nonnull Scope scope) {

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

@ -2,8 +2,7 @@ package com.englishtown.bitbucket.hook;
import com.atlassian.bitbucket.concurrent.BucketedExecutor; import com.atlassian.bitbucket.concurrent.BucketedExecutor;
import com.atlassian.bitbucket.concurrent.ConcurrencyService; import com.atlassian.bitbucket.concurrent.ConcurrencyService;
import com.atlassian.bitbucket.hook.repository.PostRepositoryHookContext; import com.atlassian.bitbucket.hook.repository.*;
import com.atlassian.bitbucket.hook.repository.RepositoryPushHookRequest;
import com.atlassian.bitbucket.project.Project; import com.atlassian.bitbucket.project.Project;
import com.atlassian.bitbucket.repository.Repository; import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.git.GitScm; import com.atlassian.bitbucket.scm.git.GitScm;
@ -24,7 +23,6 @@ import org.mockito.junit.MockitoRule;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static com.atlassian.bitbucket.mockito.MockitoUtils.returnArg; import static com.atlassian.bitbucket.mockito.MockitoUtils.returnArg;
@ -118,6 +116,15 @@ public class MirrorRepositoryHookTest {
verifyZeroInteractions(bucketedExecutor); verifyZeroInteractions(bucketedExecutor);
} }
@Test
public void testUnwantedEventsIgnored() {
Repository repo = mock(Repository.class);
hook.postUpdate(buildContext(), buildRequest(StandardRepositoryHookTrigger.UNKNOWN, repo));
verify(bucketedExecutor, never()).submit(any());
}
@Test @Test
public void testValidate() { public void testValidate() {
Settings settings = mock(Settings.class); Settings settings = mock(Settings.class);
@ -239,6 +246,13 @@ public class MirrorRepositoryHookTest {
return context; return context;
} }
private RepositoryHookRequest buildRequest(RepositoryHookTrigger trigger, Repository repo) {
RepositoryHookRequest request = mock(RepositoryHookRequest.class);
when(request.getTrigger()).thenReturn(trigger);
when(request.getRepository()).thenReturn(repo);
return request;
}
private Settings defaultSettings() { private Settings defaultSettings() {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL, ""); map.put(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL, "");

Loading…
Cancel
Save