mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 02:31:14 +01:00
Added ChangesetTrailerExtractor
This commit is contained in:
committed by
René Pfeuffer
parent
d5e3279032
commit
bb1126befc
@@ -32,6 +32,7 @@ import lombok.NoArgsConstructor;
|
|||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@@ -58,6 +59,8 @@ public class ChangesetDto extends HalRepresentation {
|
|||||||
*/
|
*/
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
|
private Map<String, PersonDto> trailerPersons;
|
||||||
|
|
||||||
public ChangesetDto(Links links, Embedded embedded) {
|
public ChangesetDto(Links links, Embedded embedded) {
|
||||||
super(links, embedded);
|
super(links, embedded);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* 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.repository;
|
||||||
|
|
||||||
|
import sonia.scm.plugin.ExtensionPoint;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@ExtensionPoint
|
||||||
|
public interface ChangesetTrailerTypes {
|
||||||
|
|
||||||
|
List<String> getTrailerTypes();
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
/*
|
||||||
|
* 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 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.regex.Pattern;
|
||||||
|
|
||||||
|
public class ChangesetTrailerExtractor {
|
||||||
|
|
||||||
|
private final UserDisplayManager userDisplayManager;
|
||||||
|
private final ChangesetTrailerTypes changesetTrailerTypes;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public ChangesetTrailerExtractor(UserDisplayManager userDisplayManager, ChangesetTrailerTypes changesetTrailerTypes) {
|
||||||
|
this.userDisplayManager = userDisplayManager;
|
||||||
|
this.changesetTrailerTypes = changesetTrailerTypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, PersonDto> extractTrailersFromCommitMessage(String commitMessage) {
|
||||||
|
|
||||||
|
HashMap<String, PersonDto> persons = new HashMap<>();
|
||||||
|
|
||||||
|
try (Scanner scanner = new Scanner(commitMessage)) {
|
||||||
|
scanner.useDelimiter(Pattern.compile("[\\n;]"));
|
||||||
|
while (scanner.hasNext()) {
|
||||||
|
String line = scanner.next();
|
||||||
|
|
||||||
|
for (String trailerType : changesetTrailerTypes.getTrailerTypes()) {
|
||||||
|
if (line.contains(trailerType)) {
|
||||||
|
String mail = line.split("<|>")[1];
|
||||||
|
persons.put(trailerType, createPersonDtoFromUser(mail));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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());
|
||||||
|
return personDto;
|
||||||
|
}
|
||||||
|
|
||||||
|
static class TrailerTypes implements ChangesetTrailerTypes {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getTrailerTypes() {
|
||||||
|
return ImmutableList.of("Co-authored-by", "Reviewed-by", "Signed-off-by", "Committed-by");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,10 +26,7 @@ package sonia.scm.api.v2.resources;
|
|||||||
|
|
||||||
import de.otto.edison.hal.Embedded;
|
import de.otto.edison.hal.Embedded;
|
||||||
import de.otto.edison.hal.Links;
|
import de.otto.edison.hal.Links;
|
||||||
import org.mapstruct.Context;
|
import org.mapstruct.*;
|
||||||
import org.mapstruct.Mapper;
|
|
||||||
import org.mapstruct.Mapping;
|
|
||||||
import org.mapstruct.ObjectFactory;
|
|
||||||
import sonia.scm.repository.Branch;
|
import sonia.scm.repository.Branch;
|
||||||
import sonia.scm.repository.Changeset;
|
import sonia.scm.repository.Changeset;
|
||||||
import sonia.scm.repository.Repository;
|
import sonia.scm.repository.Repository;
|
||||||
@@ -66,10 +63,16 @@ public abstract class DefaultChangesetToChangesetDtoMapper extends HalAppenderMa
|
|||||||
@Inject
|
@Inject
|
||||||
private TagCollectionToDtoMapper tagCollectionToDtoMapper;
|
private TagCollectionToDtoMapper tagCollectionToDtoMapper;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private ChangesetTrailerExtractor changesetTrailerExtractor;
|
||||||
|
|
||||||
@Mapping(target = "attributes", ignore = true) // We do not map HAL attributes
|
@Mapping(target = "attributes", ignore = true) // We do not map HAL attributes
|
||||||
public abstract ChangesetDto map(Changeset changeset, @Context Repository repository);
|
public abstract ChangesetDto map(Changeset changeset, @Context Repository repository);
|
||||||
|
|
||||||
|
@AfterMapping
|
||||||
|
void appendTrailerPersons(Changeset changeset, @MappingTarget ChangesetDto target) {
|
||||||
|
target.setTrailerPersons(changesetTrailerExtractor.extractTrailersFromCommitMessage(changeset.getDescription()));
|
||||||
|
}
|
||||||
|
|
||||||
@ObjectFactory
|
@ObjectFactory
|
||||||
ChangesetDto createDto(@Context Repository repository, Changeset source) {
|
ChangesetDto createDto(@Context Repository repository, Changeset source) {
|
||||||
|
|||||||
@@ -0,0 +1,142 @@
|
|||||||
|
/*
|
||||||
|
* 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 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 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReturnEmptyMap() {
|
||||||
|
String commitMessage = "zaphod beetlebrox";
|
||||||
|
|
||||||
|
Map<String, PersonDto> map = extractor.extractTrailersFromCommitMessage(commitMessage);
|
||||||
|
|
||||||
|
assertThat(map).isInstanceOf(Map.class);
|
||||||
|
assertThat(map).isNotNull();
|
||||||
|
assertThat(map.keySet()).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReturnMapWithCoAuthors() {
|
||||||
|
DisplayUser displayUser = mockDisplayUser("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);
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReturnMapWithReviewers() {
|
||||||
|
DisplayUser displayUser = mockDisplayUser("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);
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReturnMapWithSigner() {
|
||||||
|
DisplayUser displayUser = mockDisplayUser("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);
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReturnMapWithCommitter() {
|
||||||
|
DisplayUser displayUser = mockDisplayUser("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);
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private DisplayUser mockDisplayUser(String name, String mail) {
|
||||||
|
DisplayUser displayUser = DisplayUser.from(new User(name, name, mail));
|
||||||
|
when(userDisplayManager.autocomplete(mail)).thenReturn(ImmutableList.of(displayUser));
|
||||||
|
return displayUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
private PersonDto createPersonDto(DisplayUser displayUser) {
|
||||||
|
PersonDto personDto = new PersonDto();
|
||||||
|
personDto.setName(displayUser.getDisplayName());
|
||||||
|
personDto.setMail(displayUser.getMail());
|
||||||
|
return personDto;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user