diff --git a/scm-core/src/main/java/sonia/scm/repository/Branch.java b/scm-core/src/main/java/sonia/scm/repository/Branch.java index 038775d716..2c65e74908 100644 --- a/scm-core/src/main/java/sonia/scm/repository/Branch.java +++ b/scm-core/src/main/java/sonia/scm/repository/Branch.java @@ -125,15 +125,8 @@ public final class Branch implements Serializable, Validateable { return new Branch(name, revision, true, lastCommitDate); } - @SuppressWarnings("java:S3655") // we check "isPresent" for both dates, but due to the third check sonar does not get it - public void markAsStaleDependingOn(Branch defaultBranch) { - if (!isDefaultBranch() && getLastCommitDate().isPresent() && defaultBranch.getLastCommitDate().isPresent()) { - Instant defaultCommitDate = ofEpochMilli(defaultBranch.getLastCommitDate().get()); - Instant thisCommitDate = ofEpochMilli(getLastCommitDate().get()); - stale = thisCommitDate.plus(30, ChronoUnit.DAYS).isBefore(defaultCommitDate); - } else { - stale = false; - } + public void setStale(boolean stale) { + this.stale = stale; } @Override diff --git a/scm-core/src/main/java/sonia/scm/repository/api/BranchXDaysOlderThanDefaultStaleComputer.java b/scm-core/src/main/java/sonia/scm/repository/api/BranchXDaysOlderThanDefaultStaleComputer.java new file mode 100644 index 0000000000..cd05bad1b0 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/repository/api/BranchXDaysOlderThanDefaultStaleComputer.java @@ -0,0 +1,61 @@ +/* + * 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.api; + +import sonia.scm.repository.Branch; +import sonia.scm.repository.spi.BranchStaleComputer; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; + +import static java.time.Instant.ofEpochMilli; + +public class BranchXDaysOlderThanDefaultStaleComputer implements BranchStaleComputer { + + public static final int DEFAULT_AMOUNT_OF_DAYS = 30; + + private final int amountOfDays; + + public BranchXDaysOlderThanDefaultStaleComputer() { + this(DEFAULT_AMOUNT_OF_DAYS); + } + + public BranchXDaysOlderThanDefaultStaleComputer(int amountOfDays) { + this.amountOfDays = amountOfDays; + } + + @Override + @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()) { + Instant defaultCommitDate = ofEpochMilli(defaultBranch.getLastCommitDate().get()); + Instant thisCommitDate = ofEpochMilli(branch.getLastCommitDate().get()); + return thisCommitDate.plus(amountOfDays, ChronoUnit.DAYS).isBefore(defaultCommitDate); + } else { + return false; + } + } +} diff --git a/scm-core/src/main/java/sonia/scm/repository/api/BranchesCommandBuilder.java b/scm-core/src/main/java/sonia/scm/repository/api/BranchesCommandBuilder.java index 908dcdbc7a..af784f287b 100644 --- a/scm-core/src/main/java/sonia/scm/repository/api/BranchesCommandBuilder.java +++ b/scm-core/src/main/java/sonia/scm/repository/api/BranchesCommandBuilder.java @@ -165,7 +165,7 @@ public final class BranchesCommandBuilder private Branches getBranchesFromCommand() throws IOException { - return new Branches(branchesCommand.getBranchesWithStaleFlags()); + return new Branches(branchesCommand.getBranchesWithStaleFlags(new BranchXDaysOlderThanDefaultStaleComputer())); } //~--- inner classes -------------------------------------------------------- diff --git a/scm-core/src/main/java/sonia/scm/repository/spi/BranchStaleComputer.java b/scm-core/src/main/java/sonia/scm/repository/spi/BranchStaleComputer.java new file mode 100644 index 0000000000..c6324fa1cf --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/repository/spi/BranchStaleComputer.java @@ -0,0 +1,38 @@ +/* + * 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.spi; + +import lombok.Data; +import sonia.scm.repository.Branch; + +public interface BranchStaleComputer { + + boolean computeStale(Branch branch, StaleContext context); + + @Data + class StaleContext { + private Branch defaultBranch; + } +} diff --git a/scm-core/src/main/java/sonia/scm/repository/spi/BranchesCommand.java b/scm-core/src/main/java/sonia/scm/repository/spi/BranchesCommand.java index 921818c4c1..becb32850c 100644 --- a/scm-core/src/main/java/sonia/scm/repository/spi/BranchesCommand.java +++ b/scm-core/src/main/java/sonia/scm/repository/spi/BranchesCommand.java @@ -24,14 +24,11 @@ package sonia.scm.repository.spi; -//~--- non-JDK imports -------------------------------------------------------- - import sonia.scm.repository.Branch; import java.io.IOException; import java.util.List; - -//~--- JDK imports ------------------------------------------------------------ +import java.util.function.Function; /** * @@ -41,22 +38,19 @@ import java.util.List; public interface BranchesCommand { - /** - * Method description - * - * - * @return - * - * @throws IOException - */ List getBranches() throws IOException; - default List getBranchesWithStaleFlags() throws IOException { + default List getBranchesWithStaleFlags(BranchStaleComputer computer) throws IOException { + Function createContext = branch -> { + BranchStaleComputer.StaleContext staleContext = new BranchStaleComputer.StaleContext(); + staleContext.setDefaultBranch(branch); + return staleContext; + }; List branches = getBranches(); branches.stream() .filter(Branch::isDefaultBranch) .findFirst() - .ifPresent(defaultBranch -> branches.forEach(branch -> branch.markAsStaleDependingOn(defaultBranch))); + .ifPresent(defaultBranch -> branches.forEach(branch -> branch.setStale(computer.computeStale(branch, createContext.apply(defaultBranch))))); return branches; } } diff --git a/scm-core/src/test/java/sonia/scm/repository/BranchTest.java b/scm-core/src/test/java/sonia/scm/repository/api/BranchXDaysOlderThanDefaultStaleComputerTest.java similarity index 72% rename from scm-core/src/test/java/sonia/scm/repository/BranchTest.java rename to scm-core/src/test/java/sonia/scm/repository/api/BranchXDaysOlderThanDefaultStaleComputerTest.java index 89828d2f65..12fb269162 100644 --- a/scm-core/src/test/java/sonia/scm/repository/BranchTest.java +++ b/scm-core/src/test/java/sonia/scm/repository/api/BranchXDaysOlderThanDefaultStaleComputerTest.java @@ -22,9 +22,11 @@ * SOFTWARE. */ -package sonia.scm.repository; +package sonia.scm.repository.api; import org.junit.jupiter.api.Test; +import sonia.scm.repository.Branch; +import sonia.scm.repository.spi.BranchStaleComputer; import java.time.Instant; import java.time.temporal.ChronoUnit; @@ -34,11 +36,14 @@ import static org.assertj.core.api.Assertions.assertThat; import static sonia.scm.repository.Branch.defaultBranch; import static sonia.scm.repository.Branch.normalBranch; -class BranchTest { +class BranchXDaysOlderThanDefaultStaleComputerTest { + + Instant now = now(); + + BranchXDaysOlderThanDefaultStaleComputer computer = new BranchXDaysOlderThanDefaultStaleComputer(30); @Test void shouldTagOldBranchAsStale() { - Instant now = now(); long staleTime = now .minus(30, ChronoUnit.DAYS) @@ -46,14 +51,13 @@ class BranchTest { .toEpochMilli(); Branch branch = normalBranch("hog", "42", staleTime); - branch.markAsStaleDependingOn(defaultBranch("default", "23", now.toEpochMilli())); + boolean stale = computer.computeStale(branch, createStaleContext()); - assertThat(branch.isStale()).isTrue(); + assertThat(stale).isTrue(); } @Test void shouldNotTagNotSoOldBranchAsStale() { - Instant now = now(); long activeTime = now .minus(30, ChronoUnit.DAYS) @@ -61,14 +65,13 @@ class BranchTest { .toEpochMilli(); Branch branch = normalBranch("hog", "42", activeTime); - branch.markAsStaleDependingOn(defaultBranch("default", "23", now.toEpochMilli())); + boolean stale = computer.computeStale(branch, createStaleContext()); - assertThat(branch.isStale()).isFalse(); + assertThat(stale).isFalse(); } @Test void shouldNotTagDefaultBranchAsStale() { - Instant now = now(); long staleTime = now .minus(30, ChronoUnit.DAYS) @@ -76,8 +79,14 @@ class BranchTest { .toEpochMilli(); Branch branch = defaultBranch("hog", "42", staleTime); - branch.markAsStaleDependingOn(defaultBranch("default", "23", now.toEpochMilli())); + boolean stale = computer.computeStale(branch, createStaleContext()); - assertThat(branch.isStale()).isFalse(); + assertThat(stale).isFalse(); + } + + BranchStaleComputer.StaleContext createStaleContext() { + BranchStaleComputer.StaleContext staleContext = new BranchStaleComputer.StaleContext(); + staleContext.setDefaultBranch(defaultBranch("default", "23", now.toEpochMilli())); + return staleContext; } } diff --git a/scm-core/src/test/java/sonia/scm/repository/spi/BranchesCommandTest.java b/scm-core/src/test/java/sonia/scm/repository/spi/BranchesCommandTest.java index 2abb2c2326..4a0f9326c0 100644 --- a/scm-core/src/test/java/sonia/scm/repository/spi/BranchesCommandTest.java +++ b/scm-core/src/test/java/sonia/scm/repository/spi/BranchesCommandTest.java @@ -27,6 +27,7 @@ package sonia.scm.repository.spi; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import sonia.scm.repository.Branch; +import sonia.scm.repository.api.BranchXDaysOlderThanDefaultStaleComputer; import java.io.IOException; import java.time.Instant; @@ -64,7 +65,7 @@ class BranchesCommandTest { public List getBranches() { return branches; } - }.getBranchesWithStaleFlags(); + }.getBranchesWithStaleFlags(new BranchXDaysOlderThanDefaultStaleComputer()); Assertions.assertThat(branchesWithStaleFlags) .extracting("stale") diff --git a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgBranchesCommand.java b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgBranchesCommand.java index 1bb9772b98..a2285bad11 100644 --- a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgBranchesCommand.java +++ b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgBranchesCommand.java @@ -27,7 +27,6 @@ package sonia.scm.repository.spi; //~--- non-JDK imports -------------------------------------------------------- import com.aragost.javahg.Changeset; -import com.google.common.base.Function; import com.google.common.collect.Lists; import sonia.scm.repository.Branch; @@ -63,14 +62,8 @@ public class HgBranchesCommand extends AbstractCommand List hgBranches = com.aragost.javahg.commands.BranchesCommand.on(open()).execute(); - List branches = Lists.transform(hgBranches, - new Function() - { - - @Override - public Branch apply(com.aragost.javahg.Branch hgBranch) - { + return Lists.transform(hgBranches, + hgBranch -> { String node = null; Changeset changeset = hgBranch.getBranchTip(); @@ -85,9 +78,6 @@ public class HgBranchesCommand extends AbstractCommand } else { return Branch.normalBranch(hgBranch.getName(), node, lastCommitDate); } - } - }); - - return branches; + }); } }