mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 00:45:44 +01:00
Unite trailer detection and message update
The detection of trailers and removing them from the message should be in one place, not divided between classes.
This commit is contained in:
@@ -27,62 +27,75 @@ package sonia.scm.api.v2.resources;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import sonia.scm.plugin.Extension;
|
||||
import sonia.scm.repository.Changeset;
|
||||
import sonia.scm.repository.ChangesetTrailerProvider;
|
||||
import sonia.scm.repository.ChangesetPreProcessor;
|
||||
import sonia.scm.repository.ChangesetPreProcessorFactory;
|
||||
import sonia.scm.repository.Person;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.Trailer;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Scanner;
|
||||
import java.util.regex.MatchResult;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static java.util.Optional.empty;
|
||||
import static java.util.Optional.of;
|
||||
|
||||
@Extension
|
||||
public class ChangesetDescriptionTrailerProvider implements ChangesetTrailerProvider {
|
||||
public class ChangesetDescriptionTrailerProvider implements ChangesetPreProcessorFactory {
|
||||
|
||||
private static final Collection<String> SUPPORTED_TRAILER_TYPES = ImmutableSet.of("Co-authored-by", "Reviewed-by", "Signed-off-by", "Committed-by");
|
||||
private static final Pattern PERSON_PATTERN = Pattern.compile("^\\W*(.*)\\W+<(.*)>\\W*$");
|
||||
|
||||
@Inject
|
||||
public ChangesetDescriptionTrailerProvider() {}
|
||||
|
||||
@Override
|
||||
public List<Trailer> getTrailers(Repository repository, Changeset changeset) {
|
||||
List<Trailer> trailers = new ArrayList<>();
|
||||
public ChangesetPreProcessor createPreProcessor(Repository repository) {
|
||||
return new TrailerChangesetPreProcessor();
|
||||
}
|
||||
|
||||
try (Scanner scanner = new Scanner(changeset.getDescription())) {
|
||||
while (scanner.hasNextLine()) {
|
||||
String line = scanner.nextLine();
|
||||
private static class TrailerChangesetPreProcessor implements ChangesetPreProcessor {
|
||||
|
||||
String[] typeAndUser = line.split(":\\W");
|
||||
if (typeAndUser.length == 2) {
|
||||
String type = typeAndUser[0];
|
||||
String person = typeAndUser[1];
|
||||
if (SUPPORTED_TRAILER_TYPES.contains(type)) {
|
||||
Optional<Trailer> trailer = createTrailer(type, person);
|
||||
trailer.ifPresent(trailers::add);
|
||||
private final List<Trailer> trailers = new ArrayList<>();
|
||||
private final StringBuilder newDescription = new StringBuilder();
|
||||
|
||||
@Override
|
||||
public void process(Changeset changeset) {
|
||||
|
||||
try (Scanner scanner = new Scanner(changeset.getDescription())) {
|
||||
while (scanner.hasNextLine()) {
|
||||
String line = scanner.nextLine();
|
||||
|
||||
String[] typeAndUser = line.split(":\\W");
|
||||
if (typeAndUser.length == 2) {
|
||||
String type = typeAndUser[0];
|
||||
String person = typeAndUser[1];
|
||||
if (!SUPPORTED_TRAILER_TYPES.contains(type) || !createTrailer(type, person)) {
|
||||
appendLine(scanner, line);
|
||||
}
|
||||
} else {
|
||||
appendLine(scanner, line);
|
||||
}
|
||||
}
|
||||
}
|
||||
changeset.addTrailers(trailers);
|
||||
changeset.setDescription(newDescription.toString());
|
||||
}
|
||||
return trailers;
|
||||
}
|
||||
|
||||
private Optional<Trailer> createTrailer(String type, String person) {
|
||||
Matcher matcher = PERSON_PATTERN.matcher(person.trim());
|
||||
if (matcher.matches()) {
|
||||
MatchResult matchResult = matcher.toMatchResult();
|
||||
return of(new Trailer(type, new Person(matchResult.group(1), matchResult.group(2))));
|
||||
} else {
|
||||
return empty();
|
||||
public void appendLine(Scanner scanner, String line) {
|
||||
newDescription.append(line);
|
||||
if (scanner.hasNextLine()) {
|
||||
newDescription.append('\n');
|
||||
}
|
||||
}
|
||||
|
||||
private boolean createTrailer(String type, String person) {
|
||||
Matcher matcher = PERSON_PATTERN.matcher(person.trim());
|
||||
if (matcher.matches()) {
|
||||
MatchResult matchResult = matcher.toMatchResult();
|
||||
trailers.add(new Trailer(type, new Person(matchResult.group(1), matchResult.group(2))));
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,24 +74,6 @@ public abstract class DefaultChangesetToChangesetDtoMapper extends HalAppenderMa
|
||||
|
||||
abstract PersonDto map(Person person);
|
||||
|
||||
@AfterMapping
|
||||
void removeTrailerFromChangesetDescription(@MappingTarget ChangesetDto target) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
try (Scanner scanner = new Scanner(target.getDescription())) {
|
||||
while (scanner.hasNextLine()) {
|
||||
String line = scanner.nextLine();
|
||||
if (!line.contains("-by:")) {
|
||||
builder.append(line);
|
||||
if (scanner.hasNextLine()) {
|
||||
builder.append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
target.setDescription(builder.toString());
|
||||
}
|
||||
|
||||
@ObjectFactory
|
||||
ChangesetDto createDto(@Context Repository repository, Changeset source) {
|
||||
String namespace = repository.getNamespace();
|
||||
|
||||
Reference in New Issue
Block a user