mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-05-07 07:36:33 +02:00
Improve readability
This commit is contained in:
@@ -50,7 +50,7 @@ public class BranchXDaysOlderThanDefaultStaleComputer implements BranchStaleComp
|
||||
@SuppressWarnings("java:S3655") // we check "isPresent" for both dates, but due to the third check sonar does not get it
|
||||
public boolean computeStale(Branch branch, StaleContext context) {
|
||||
Branch defaultBranch = context.getDefaultBranch();
|
||||
if (!branch.isDefaultBranch() && branch.getLastCommitDate().isPresent() && defaultBranch.getLastCommitDate().isPresent()) {
|
||||
if (shouldCompute(branch, defaultBranch)) {
|
||||
Instant defaultCommitDate = ofEpochMilli(defaultBranch.getLastCommitDate().get());
|
||||
Instant thisCommitDate = ofEpochMilli(branch.getLastCommitDate().get());
|
||||
return thisCommitDate.plus(amountOfDays, ChronoUnit.DAYS).isBefore(defaultCommitDate);
|
||||
@@ -58,4 +58,8 @@ public class BranchXDaysOlderThanDefaultStaleComputer implements BranchStaleComp
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean shouldCompute(Branch branch, Branch defaultBranch) {
|
||||
return !branch.isDefaultBranch() && branch.getLastCommitDate().isPresent() && defaultBranch.getLastCommitDate().isPresent();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,29 +28,45 @@ import sonia.scm.repository.Branch;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
* @since 1.18
|
||||
*/
|
||||
public interface BranchesCommand
|
||||
{
|
||||
public interface BranchesCommand {
|
||||
|
||||
List<Branch> getBranches() throws IOException;
|
||||
|
||||
default List<Branch> getBranchesWithStaleFlags(BranchStaleComputer computer) throws IOException {
|
||||
Function<Branch, BranchStaleComputer.StaleContext> createContext = branch -> {
|
||||
BranchStaleComputer.StaleContext staleContext = new BranchStaleComputer.StaleContext();
|
||||
staleContext.setDefaultBranch(branch);
|
||||
return staleContext;
|
||||
};
|
||||
List<Branch> branches = getBranches();
|
||||
branches.stream()
|
||||
.filter(Branch::isDefaultBranch)
|
||||
.findFirst()
|
||||
.ifPresent(defaultBranch -> branches.forEach(branch -> branch.setStale(computer.computeStale(branch, createContext.apply(defaultBranch)))));
|
||||
new StaleProcessor(computer, branches).process();
|
||||
return branches;
|
||||
}
|
||||
|
||||
final class StaleProcessor {
|
||||
|
||||
private final BranchStaleComputer computer;
|
||||
private final List<Branch> branches;
|
||||
|
||||
private StaleProcessor(BranchStaleComputer computer, List<Branch> branches) {
|
||||
this.computer = computer;
|
||||
this.branches = branches;
|
||||
}
|
||||
|
||||
private void process() {
|
||||
Optional<Branch> defaultBranch = branches.stream()
|
||||
.filter(Branch::isDefaultBranch)
|
||||
.findFirst();
|
||||
|
||||
defaultBranch.ifPresent(this::process);
|
||||
}
|
||||
|
||||
private void process(Branch defaultBranch) {
|
||||
BranchStaleComputer.StaleContext staleContext = new BranchStaleComputer.StaleContext();
|
||||
staleContext.setDefaultBranch(defaultBranch);
|
||||
|
||||
branches.forEach(branch -> branch.setStale(computer.computeStale(branch, staleContext)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user