Skip to content

Commit fac627f

Browse files
author
eheimbuch
authored
Merge pull request #2 from scm-manager/feature/contextualize_error
Contextualize error in scm protocol
2 parents 62f6940 + bf57483 commit fac627f

File tree

3 files changed

+56
-20
lines changed

3 files changed

+56
-20
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
### Added
9+
- 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))
10+
711
## 1.0.0 - 2020-10-09
8-
## Added
12+
### Added
913
- Initial implementation with `Custom RegEx Validator`

src/main/java/com/cloudogu/scm/commitmessagechecker/CommitMessageCheckerHook.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import sonia.scm.repository.PreReceiveRepositoryHookEvent;
3434
import sonia.scm.repository.Repository;
3535
import sonia.scm.repository.api.HookContext;
36+
import sonia.scm.repository.api.HookFeature;
3637

3738
import javax.inject.Inject;
3839
import java.util.List;
@@ -59,10 +60,21 @@ public void onEvent(PreReceiveRepositoryHookEvent event) {
5960
.ifPresent(
6061
configuration -> {
6162
List<Validation> validations = configuration.getValidations();
62-
validate(context, event.getRepository(), validations);
63+
try {
64+
validate(context, event.getRepository(), validations);
65+
} catch (RuntimeException e) {
66+
contextualizeError(event);
67+
throw e;
68+
}
6369
});
6470
}
6571

72+
private void contextualizeError(PreReceiveRepositoryHookEvent event) {
73+
if (event.getContext().isFeatureSupported(HookFeature.MESSAGE_PROVIDER)) {
74+
event.getContext().getMessageProvider().sendError("Commit message validation:");
75+
}
76+
}
77+
6678
private void validate(HookContext context, Repository repository, List<Validation> validations) {
6779
if (validations.isEmpty()) {
6880
log.debug("No validations found");

src/test/java/com/cloudogu/scm/commitmessagechecker/CommitMessageCheckerHookTest.java

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,19 @@
4242
import sonia.scm.repository.RepositoryTestData;
4343
import sonia.scm.repository.api.HookChangesetBuilder;
4444
import sonia.scm.repository.api.HookContext;
45+
import sonia.scm.repository.api.HookFeature;
46+
import sonia.scm.repository.api.HookMessageProvider;
4547

46-
import java.util.Collections;
4748
import java.util.Optional;
4849

50+
import static java.util.Arrays.asList;
51+
import static java.util.Collections.emptyList;
4952
import static org.assertj.core.api.Assertions.assertThat;
53+
import static org.junit.jupiter.api.Assertions.assertThrows;
5054
import static org.mockito.ArgumentMatchers.anyString;
5155
import static org.mockito.ArgumentMatchers.eq;
56+
import static org.mockito.Mockito.doThrow;
57+
import static org.mockito.Mockito.mock;
5258
import static org.mockito.Mockito.never;
5359
import static org.mockito.Mockito.verify;
5460
import static org.mockito.Mockito.when;
@@ -77,7 +83,7 @@ class CommitMessageCheckerHookTest {
7783
@Test
7884
void shouldNotValidateIfNoValidationsFound() {
7985
when(configurationProvider.evaluateConfiguration(REPOSITORY))
80-
.thenReturn(Optional.of(new Configuration(true, Collections.emptyList())));
86+
.thenReturn(Optional.of(new Configuration(true, emptyList())));
8187

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

8894
@Test
8995
void shouldNotValidateIfConfigurationIsDisabled() {
90-
when(configurationProvider.evaluateConfiguration(REPOSITORY)).thenReturn(Optional.of(new Configuration(false, Collections.emptyList())));
96+
when(configurationProvider.evaluateConfiguration(REPOSITORY)).thenReturn(Optional.of(new Configuration(false, emptyList())));
9197

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

98104
@Test
99105
void shouldValidate() {
100-
Object configuration = new Object();
101-
when(configurationProvider.evaluateConfiguration(REPOSITORY))
102-
.thenReturn(Optional.of(new Configuration(true, ImmutableList.of(new Validation("TestValidator", configuration)))));
103-
when(hookContext.getChangesetProvider()).thenReturn(hookChangesetBuilder);
104-
Changeset changeset = new Changeset("1", 1L, null, "awesome commit");
105-
changeset.setBranches(ImmutableList.of("master"));
106-
when(hookChangesetBuilder.getChangesetList()).thenReturn(ImmutableList.of(changeset));
107-
when(availableValidators.validatorFor("TestValidator")).thenReturn(validator);
106+
Object configuration = prepareValidator("master");
108107

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

120119
@Test
121120
void shouldValidateChangesetWithoutBranches() {
122-
Object configuration = new Object();
123-
when(configurationProvider.evaluateConfiguration(REPOSITORY))
124-
.thenReturn(Optional.of(new Configuration(true, ImmutableList.of(new Validation("TestValidator", configuration)))));
125-
when(hookContext.getChangesetProvider()).thenReturn(hookChangesetBuilder);
126-
Changeset changeset = new Changeset("1", 1L, null, "awesome commit");
127-
when(hookChangesetBuilder.getChangesetList()).thenReturn(ImmutableList.of(changeset));
128-
when(availableValidators.validatorFor("TestValidator")).thenReturn(validator);
121+
Object configuration = prepareValidator();
129122

130123
PreReceiveRepositoryHookEvent event = new PreReceiveRepositoryHookEvent(new RepositoryHookEvent(hookContext, REPOSITORY, RepositoryHookType.PRE_RECEIVE));
131124
hook.onEvent(event);
@@ -137,4 +130,31 @@ void shouldValidateChangesetWithoutBranches() {
137130
assertThat(context.getRepository()).isEqualTo(REPOSITORY);
138131
assertThat(context.getConfiguration()).isEqualTo(configuration);
139132
}
133+
134+
@Test
135+
void shouldSendContextMessage() {
136+
when(hookContext.isFeatureSupported(HookFeature.MESSAGE_PROVIDER)).thenReturn(true);
137+
HookMessageProvider messageProvider = mock(HookMessageProvider.class);
138+
when(hookContext.getMessageProvider()).thenReturn(messageProvider);
139+
140+
prepareValidator("master");
141+
doThrow(RuntimeException.class).when(validator).validate(contextCaptor.capture(), eq("awesome commit"));
142+
143+
PreReceiveRepositoryHookEvent event = new PreReceiveRepositoryHookEvent(new RepositoryHookEvent(hookContext, REPOSITORY, RepositoryHookType.PRE_RECEIVE));
144+
assertThrows(RuntimeException.class, () -> hook.onEvent(event));
145+
146+
verify(messageProvider).sendError("Commit message validation:");
147+
}
148+
149+
private Object prepareValidator(String... branches) {
150+
Object configuration = new Object();
151+
when(configurationProvider.evaluateConfiguration(REPOSITORY))
152+
.thenReturn(Optional.of(new Configuration(true, ImmutableList.of(new Validation("TestValidator", configuration)))));
153+
when(hookContext.getChangesetProvider()).thenReturn(hookChangesetBuilder);
154+
Changeset changeset = new Changeset("1", 1L, null, "awesome commit");
155+
when(hookChangesetBuilder.getChangesetList()).thenReturn(ImmutableList.of(changeset));
156+
when(availableValidators.validatorFor("TestValidator")).thenReturn(validator);
157+
changeset.setBranches(asList(branches));
158+
return configuration;
159+
}
140160
}

0 commit comments

Comments
 (0)