mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
adjust ChangesetTrailerExtractor so that we don't check if user already exists
This commit is contained in:
committed by
René Pfeuffer
parent
bb1126befc
commit
e451bb618e
@@ -32,7 +32,7 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@@ -59,7 +59,7 @@ public class ChangesetDto extends HalRepresentation {
|
||||
*/
|
||||
private String description;
|
||||
|
||||
private Map<String, PersonDto> trailerPersons;
|
||||
private List<TrailerPersonDto> trailerPersons;
|
||||
|
||||
public ChangesetDto(Links links, Embedded embedded) {
|
||||
super(links, embedded);
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class TrailerPersonDto extends PersonDto {
|
||||
private String trailerType;
|
||||
}
|
||||
@@ -26,27 +26,24 @@ package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import sonia.scm.repository.ChangesetTrailerTypes;
|
||||
import sonia.scm.user.DisplayUser;
|
||||
import sonia.scm.user.UserDisplayManager;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ChangesetTrailerExtractor {
|
||||
|
||||
private final UserDisplayManager userDisplayManager;
|
||||
private final ChangesetTrailerTypes changesetTrailerTypes;
|
||||
|
||||
@Inject
|
||||
public ChangesetTrailerExtractor(UserDisplayManager userDisplayManager, ChangesetTrailerTypes changesetTrailerTypes) {
|
||||
this.userDisplayManager = userDisplayManager;
|
||||
public ChangesetTrailerExtractor(ChangesetTrailerTypes changesetTrailerTypes) {
|
||||
this.changesetTrailerTypes = changesetTrailerTypes;
|
||||
}
|
||||
|
||||
Map<String, PersonDto> extractTrailersFromCommitMessage(String commitMessage) {
|
||||
|
||||
HashMap<String, PersonDto> persons = new HashMap<>();
|
||||
List<TrailerPersonDto> extractTrailersFromCommitMessage(String commitMessage) {
|
||||
List<TrailerPersonDto> persons = new ArrayList<>();
|
||||
|
||||
try (Scanner scanner = new Scanner(commitMessage)) {
|
||||
scanner.useDelimiter(Pattern.compile("[\\n;]"));
|
||||
@@ -55,30 +52,29 @@ public class ChangesetTrailerExtractor {
|
||||
|
||||
for (String trailerType : changesetTrailerTypes.getTrailerTypes()) {
|
||||
if (line.contains(trailerType)) {
|
||||
String mail = line.split("<|>")[1];
|
||||
persons.put(trailerType, createPersonDtoFromUser(mail));
|
||||
TrailerPersonDto personDto = createPersonDtoFromUser(line);
|
||||
personDto.setTrailerType(trailerType);
|
||||
persons.add(personDto);
|
||||
}
|
||||
}
|
||||
|
||||
/* if (line.contains("Co-authored-by")) {
|
||||
persons.put("Co-authored-by", createPersonDtoFromUser(mail));
|
||||
}
|
||||
if (line.contains("Reviewed-by")) {
|
||||
persons.put("Reviewed-by", createPersonDtoFromUser(mail));
|
||||
}*/
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return persons;
|
||||
}
|
||||
|
||||
private PersonDto createPersonDtoFromUser(String mail) {
|
||||
DisplayUser displayUser = userDisplayManager.autocomplete(mail).iterator().next();
|
||||
PersonDto personDto = new PersonDto();
|
||||
personDto.setName(displayUser.getDisplayName());
|
||||
personDto.setMail(displayUser.getMail());
|
||||
private TrailerPersonDto createPersonDtoFromUser(String line) {
|
||||
TrailerPersonDto personDto = new TrailerPersonDto();
|
||||
|
||||
String[] splittedTrailer = line.split("[:<>]");
|
||||
|
||||
if (splittedTrailer.length > 1) {
|
||||
personDto.setName(splittedTrailer[1].trim());
|
||||
if (splittedTrailer.length > 2) {
|
||||
personDto.setMail(splittedTrailer[2]);
|
||||
}
|
||||
}
|
||||
|
||||
return personDto;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ package sonia.scm.api.v2.resources;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.servlet.ServletScopes;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
import sonia.scm.repository.ChangesetTrailerTypes;
|
||||
import sonia.scm.web.api.RepositoryToHalMapper;
|
||||
|
||||
public class MapperModule extends AbstractModule {
|
||||
@@ -77,6 +78,7 @@ public class MapperModule extends AbstractModule {
|
||||
bind(MeDtoFactory.class);
|
||||
bind(UIPluginDtoMapper.class);
|
||||
bind(UIPluginDtoCollectionMapper.class);
|
||||
bind(ChangesetTrailerTypes.class).to(ChangesetTrailerExtractor.TrailerTypes.class);
|
||||
|
||||
bind(ScmPathInfoStore.class).in(ServletScopes.REQUEST);
|
||||
|
||||
|
||||
@@ -24,111 +24,104 @@
|
||||
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.api.v2.resources.ChangesetTrailerExtractor.TrailerTypes;
|
||||
import sonia.scm.user.DisplayUser;
|
||||
import sonia.scm.user.User;
|
||||
import sonia.scm.user.UserDisplayManager;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class ChangesetTrailerExtractorTest {
|
||||
|
||||
@Mock
|
||||
private UserDisplayManager userDisplayManager;
|
||||
|
||||
private ChangesetTrailerExtractor extractor;
|
||||
|
||||
@BeforeEach
|
||||
void initExtractor() {
|
||||
TrailerTypes trailerTypes = new TrailerTypes();
|
||||
extractor = new ChangesetTrailerExtractor(userDisplayManager, trailerTypes);
|
||||
extractor = new ChangesetTrailerExtractor(trailerTypes);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnEmptyMap() {
|
||||
void shouldReturnEmptyList() {
|
||||
String commitMessage = "zaphod beetlebrox";
|
||||
|
||||
Map<String, PersonDto> map = extractor.extractTrailersFromCommitMessage(commitMessage);
|
||||
List<TrailerPersonDto> trailerPersons = extractor.extractTrailersFromCommitMessage(commitMessage);
|
||||
|
||||
assertThat(map).isInstanceOf(Map.class);
|
||||
assertThat(map).isNotNull();
|
||||
assertThat(map.keySet()).isEmpty();
|
||||
assertThat(trailerPersons).isNotNull();
|
||||
assertThat(trailerPersons).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnMapWithCoAuthors() {
|
||||
DisplayUser displayUser = mockDisplayUser("Arthur Dent", "dent@hitchhiker.org");
|
||||
void shouldReturnTrailerPersonsWithCoAuthors() {
|
||||
DisplayUser displayUser = createDisplayUser("Arthur Dent", "dent@hitchhiker.org");
|
||||
String commitMessage = "zaphod beetlebrox\n\nCo-authored-by: Arthur Dent <dent@hitchhiker.org>";
|
||||
|
||||
PersonDto personDto = createPersonDto(displayUser);
|
||||
|
||||
Map<String, PersonDto> map = extractor.extractTrailersFromCommitMessage(commitMessage);
|
||||
List<TrailerPersonDto> trailerPersons = extractor.extractTrailersFromCommitMessage(commitMessage);
|
||||
|
||||
assertThat(map.keySet().iterator().next()).isEqualTo("Co-authored-by");
|
||||
PersonDto person = map.values().iterator().next();
|
||||
assertThat(person.getName()).isEqualTo(personDto.getName());
|
||||
assertThat(person.getMail()).isEqualTo(personDto.getMail());
|
||||
TrailerPersonDto trailerPersonDto = trailerPersons.get(0);
|
||||
assertThat(trailerPersonDto.getTrailerType()).isEqualTo("Co-authored-by");
|
||||
assertThat(trailerPersonDto.getName()).isEqualTo(personDto.getName());
|
||||
assertThat(trailerPersonDto.getMail()).isEqualTo(personDto.getMail());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnMapWithReviewers() {
|
||||
DisplayUser displayUser = mockDisplayUser("Tricia McMillan", "trillian@hitchhiker.org");
|
||||
void shouldReturnTrailerPersonsWithReviewers() {
|
||||
DisplayUser displayUser = createDisplayUser("Tricia McMillan", "trillian@hitchhiker.org");
|
||||
String commitMessage = "zaphod beetlebrox\nReviewed-by: Tricia McMillan <trillian@hitchhiker.org>";
|
||||
|
||||
PersonDto personDto = createPersonDto(displayUser);
|
||||
|
||||
Map<String, PersonDto> map = extractor.extractTrailersFromCommitMessage(commitMessage);
|
||||
List<TrailerPersonDto> trailerPersons = extractor.extractTrailersFromCommitMessage(commitMessage);
|
||||
|
||||
assertThat(map.keySet().iterator().next()).isEqualTo("Reviewed-by");
|
||||
PersonDto person = map.values().iterator().next();
|
||||
assertThat(person.getName()).isEqualTo(personDto.getName());
|
||||
assertThat(person.getMail()).isEqualTo(personDto.getMail());
|
||||
TrailerPersonDto trailerPersonDto = trailerPersons.get(0);
|
||||
|
||||
assertThat(trailerPersonDto.getTrailerType()).isEqualTo("Reviewed-by");
|
||||
assertThat(trailerPersonDto.getName()).isEqualTo(personDto.getName());
|
||||
assertThat(trailerPersonDto.getMail()).isEqualTo(personDto.getMail());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnMapWithSigner() {
|
||||
DisplayUser displayUser = mockDisplayUser("Tricia McMillan", "trillian@hitchhiker.org");
|
||||
void shouldReturnTrailerPersonsWithSigner() {
|
||||
DisplayUser displayUser = createDisplayUser("Tricia McMillan", "trillian@hitchhiker.org");
|
||||
String commitMessage = "zaphod beetlebrox\nSigned-off-by: Tricia McMillan <trillian@hitchhiker.org>";
|
||||
|
||||
PersonDto personDto = createPersonDto(displayUser);
|
||||
|
||||
Map<String, PersonDto> map = extractor.extractTrailersFromCommitMessage(commitMessage);
|
||||
List<TrailerPersonDto> trailerPersons = extractor.extractTrailersFromCommitMessage(commitMessage);
|
||||
|
||||
assertThat(map.keySet().iterator().next()).isEqualTo("Signed-off-by");
|
||||
PersonDto person = map.values().iterator().next();
|
||||
assertThat(person.getName()).isEqualTo(personDto.getName());
|
||||
assertThat(person.getMail()).isEqualTo(personDto.getMail());
|
||||
TrailerPersonDto trailerPersonDto = trailerPersons.get(0);
|
||||
|
||||
assertThat(trailerPersonDto.getTrailerType()).isEqualTo("Signed-off-by");
|
||||
assertThat(trailerPersonDto.getName()).isEqualTo(personDto.getName());
|
||||
assertThat(trailerPersonDto.getMail()).isEqualTo(personDto.getMail());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnMapWithCommitter() {
|
||||
DisplayUser displayUser = mockDisplayUser("Tricia McMillan", "trillian@hitchhiker.org");
|
||||
void shouldReturnTrailerPersonsWithCommitter() {
|
||||
DisplayUser displayUser = createDisplayUser("Tricia McMillan", "trillian@hitchhiker.org");
|
||||
String commitMessage = "zaphod beetlebrox\nCommitted-by: Tricia McMillan <trillian@hitchhiker.org>";
|
||||
|
||||
PersonDto personDto = createPersonDto(displayUser);
|
||||
|
||||
Map<String, PersonDto> map = extractor.extractTrailersFromCommitMessage(commitMessage);
|
||||
List<TrailerPersonDto> trailerPersons = extractor.extractTrailersFromCommitMessage(commitMessage);
|
||||
|
||||
assertThat(map.keySet().iterator().next()).isEqualTo("Committed-by");
|
||||
PersonDto person = map.values().iterator().next();
|
||||
assertThat(person.getName()).isEqualTo(personDto.getName());
|
||||
assertThat(person.getMail()).isEqualTo(personDto.getMail());
|
||||
TrailerPersonDto trailerPersonDto = trailerPersons.get(0);
|
||||
|
||||
assertThat(trailerPersonDto.getTrailerType()).isEqualTo("Committed-by");
|
||||
assertThat(trailerPersonDto.getName()).isEqualTo(personDto.getName());
|
||||
assertThat(trailerPersonDto.getMail()).isEqualTo(personDto.getMail());
|
||||
}
|
||||
|
||||
|
||||
private DisplayUser mockDisplayUser(String name, String mail) {
|
||||
private DisplayUser createDisplayUser(String name, String mail) {
|
||||
DisplayUser displayUser = DisplayUser.from(new User(name, name, mail));
|
||||
when(userDisplayManager.autocomplete(mail)).thenReturn(ImmutableList.of(displayUser));
|
||||
return displayUser;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user