Make stale state computation configurable

This commit is contained in:
René Pfeuffer
2020-11-24 08:41:03 +01:00
parent 518a1118fc
commit 7a47e68707
8 changed files with 135 additions and 49 deletions

View File

@@ -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

View File

@@ -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;
}
}
}

View File

@@ -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 --------------------------------------------------------

View File

@@ -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;
}
}

View File

@@ -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<Branch> getBranches() throws IOException;
default List<Branch> getBranchesWithStaleFlags() 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.markAsStaleDependingOn(defaultBranch)));
.ifPresent(defaultBranch -> branches.forEach(branch -> branch.setStale(computer.computeStale(branch, createContext.apply(defaultBranch)))));
return branches;
}
}

View File

@@ -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;
}
}

View File

@@ -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<Branch> getBranches() {
return branches;
}
}.getBranchesWithStaleFlags();
}.getBranchesWithStaleFlags(new BranchXDaysOlderThanDefaultStaleComputer());
Assertions.assertThat(branchesWithStaleFlags)
.extracting("stale")

View File

@@ -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<com.aragost.javahg.Branch> hgBranches =
com.aragost.javahg.commands.BranchesCommand.on(open()).execute();
List<Branch> branches = Lists.transform(hgBranches,
new Function<com.aragost.javahg.Branch,
Branch>()
{
@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;
});
}
}