Skip to content

Commit

Permalink
Merge pull request #2 from scm-manager/feature/contextualize_error
Browse files Browse the repository at this point in the history
Contextualize error in scm protocol
  • Loading branch information
eheimbuch authored Nov 12, 2020
2 parents 62f6940 + bf57483 commit fac627f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- Small additional message to be shown in scm clients in case of an validation error ([#2](https://github.com/scm-manager/scm-commit-message-checker-plugin/pull/2))

## 1.0.0 - 2020-10-09
## Added
### Added
- Initial implementation with `Custom RegEx Validator`
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import sonia.scm.repository.PreReceiveRepositoryHookEvent;
import sonia.scm.repository.Repository;
import sonia.scm.repository.api.HookContext;
import sonia.scm.repository.api.HookFeature;

import javax.inject.Inject;
import java.util.List;
Expand All @@ -59,10 +60,21 @@ public void onEvent(PreReceiveRepositoryHookEvent event) {
.ifPresent(
configuration -> {
List<Validation> validations = configuration.getValidations();
validate(context, event.getRepository(), validations);
try {
validate(context, event.getRepository(), validations);
} catch (RuntimeException e) {
contextualizeError(event);
throw e;
}
});
}

private void contextualizeError(PreReceiveRepositoryHookEvent event) {
if (event.getContext().isFeatureSupported(HookFeature.MESSAGE_PROVIDER)) {
event.getContext().getMessageProvider().sendError("Commit message validation:");
}
}

private void validate(HookContext context, Repository repository, List<Validation> validations) {
if (validations.isEmpty()) {
log.debug("No validations found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@
import sonia.scm.repository.RepositoryTestData;
import sonia.scm.repository.api.HookChangesetBuilder;
import sonia.scm.repository.api.HookContext;
import sonia.scm.repository.api.HookFeature;
import sonia.scm.repository.api.HookMessageProvider;

import java.util.Collections;
import java.util.Optional;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -77,7 +83,7 @@ class CommitMessageCheckerHookTest {
@Test
void shouldNotValidateIfNoValidationsFound() {
when(configurationProvider.evaluateConfiguration(REPOSITORY))
.thenReturn(Optional.of(new Configuration(true, Collections.emptyList())));
.thenReturn(Optional.of(new Configuration(true, emptyList())));

PreReceiveRepositoryHookEvent event = new PreReceiveRepositoryHookEvent(new RepositoryHookEvent(hookContext, REPOSITORY, RepositoryHookType.PRE_RECEIVE));
hook.onEvent(event);
Expand All @@ -87,7 +93,7 @@ void shouldNotValidateIfNoValidationsFound() {

@Test
void shouldNotValidateIfConfigurationIsDisabled() {
when(configurationProvider.evaluateConfiguration(REPOSITORY)).thenReturn(Optional.of(new Configuration(false, Collections.emptyList())));
when(configurationProvider.evaluateConfiguration(REPOSITORY)).thenReturn(Optional.of(new Configuration(false, emptyList())));

PreReceiveRepositoryHookEvent event = new PreReceiveRepositoryHookEvent(new RepositoryHookEvent(hookContext, REPOSITORY, RepositoryHookType.PRE_RECEIVE));
hook.onEvent(event);
Expand All @@ -97,14 +103,7 @@ void shouldNotValidateIfConfigurationIsDisabled() {

@Test
void shouldValidate() {
Object configuration = new Object();
when(configurationProvider.evaluateConfiguration(REPOSITORY))
.thenReturn(Optional.of(new Configuration(true, ImmutableList.of(new Validation("TestValidator", configuration)))));
when(hookContext.getChangesetProvider()).thenReturn(hookChangesetBuilder);
Changeset changeset = new Changeset("1", 1L, null, "awesome commit");
changeset.setBranches(ImmutableList.of("master"));
when(hookChangesetBuilder.getChangesetList()).thenReturn(ImmutableList.of(changeset));
when(availableValidators.validatorFor("TestValidator")).thenReturn(validator);
Object configuration = prepareValidator("master");

PreReceiveRepositoryHookEvent event = new PreReceiveRepositoryHookEvent(new RepositoryHookEvent(hookContext, REPOSITORY, RepositoryHookType.PRE_RECEIVE));
hook.onEvent(event);
Expand All @@ -119,13 +118,7 @@ void shouldValidate() {

@Test
void shouldValidateChangesetWithoutBranches() {
Object configuration = new Object();
when(configurationProvider.evaluateConfiguration(REPOSITORY))
.thenReturn(Optional.of(new Configuration(true, ImmutableList.of(new Validation("TestValidator", configuration)))));
when(hookContext.getChangesetProvider()).thenReturn(hookChangesetBuilder);
Changeset changeset = new Changeset("1", 1L, null, "awesome commit");
when(hookChangesetBuilder.getChangesetList()).thenReturn(ImmutableList.of(changeset));
when(availableValidators.validatorFor("TestValidator")).thenReturn(validator);
Object configuration = prepareValidator();

PreReceiveRepositoryHookEvent event = new PreReceiveRepositoryHookEvent(new RepositoryHookEvent(hookContext, REPOSITORY, RepositoryHookType.PRE_RECEIVE));
hook.onEvent(event);
Expand All @@ -137,4 +130,31 @@ void shouldValidateChangesetWithoutBranches() {
assertThat(context.getRepository()).isEqualTo(REPOSITORY);
assertThat(context.getConfiguration()).isEqualTo(configuration);
}

@Test
void shouldSendContextMessage() {
when(hookContext.isFeatureSupported(HookFeature.MESSAGE_PROVIDER)).thenReturn(true);
HookMessageProvider messageProvider = mock(HookMessageProvider.class);
when(hookContext.getMessageProvider()).thenReturn(messageProvider);

prepareValidator("master");
doThrow(RuntimeException.class).when(validator).validate(contextCaptor.capture(), eq("awesome commit"));

PreReceiveRepositoryHookEvent event = new PreReceiveRepositoryHookEvent(new RepositoryHookEvent(hookContext, REPOSITORY, RepositoryHookType.PRE_RECEIVE));
assertThrows(RuntimeException.class, () -> hook.onEvent(event));

verify(messageProvider).sendError("Commit message validation:");
}

private Object prepareValidator(String... branches) {
Object configuration = new Object();
when(configurationProvider.evaluateConfiguration(REPOSITORY))
.thenReturn(Optional.of(new Configuration(true, ImmutableList.of(new Validation("TestValidator", configuration)))));
when(hookContext.getChangesetProvider()).thenReturn(hookChangesetBuilder);
Changeset changeset = new Changeset("1", 1L, null, "awesome commit");
when(hookChangesetBuilder.getChangesetList()).thenReturn(ImmutableList.of(changeset));
when(availableValidators.validatorFor("TestValidator")).thenReturn(validator);
changeset.setBranches(asList(branches));
return configuration;
}
}

0 comments on commit fac627f

Please sign in to comment.