Skip to content

Commit

Permalink
Merge pull request #6049 from reckart/issues/6048
Browse files Browse the repository at this point in the history
Issue #6048: Ability to configure threshold for semantic versioning failures
  • Loading branch information
pkriens committed Mar 18, 2024
2 parents 9377a7d + 55ffba5 commit 104ad99
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 15 deletions.
33 changes: 27 additions & 6 deletions biz.aQute.bndlib.tests/test/test/baseline/BaselineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.TreeSet;
import java.util.regex.Pattern;

import aQute.bnd.osgi.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

Expand All @@ -32,12 +33,6 @@
import aQute.bnd.differ.Baseline.BundleInfo;
import aQute.bnd.differ.Baseline.Info;
import aQute.bnd.differ.DiffPluginImpl;
import aQute.bnd.osgi.Analyzer;
import aQute.bnd.osgi.Builder;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.Verifier;
import aQute.bnd.service.RepositoryPlugin;
import aQute.bnd.service.diff.Delta;
import aQute.bnd.service.diff.Diff;
Expand Down Expand Up @@ -627,6 +622,32 @@ public void testMinorChange() throws Exception {
}
}

@Test
public void testMinorChangeSuppressedByThreshold() throws Exception {
Processor processor = new Processor();

DiffPluginImpl differ = new DiffPluginImpl();
Baseline baseline = new Baseline(processor, differ);

try (Jar older = new Jar(IO.getFile("testresources/minor-and-removed-change-1.0.0.jar"));
Jar newer = new Jar(IO.getFile("testresources/minor-change-1.0.1.jar"));) {

baseline.baseline(newer, older, new Instructions("*;threshold=MAJOR"));

BundleInfo bundleInfo = baseline.getBundleInfo();

assertFalse(bundleInfo.mismatch);
assertEquals(bundleInfo.newerVersion.toString(), bundleInfo.suggestedVersion.toString());

baseline.baseline(newer, older, new Instructions("*;threshold=MINOR"));

bundleInfo = baseline.getBundleInfo();

assertTrue(bundleInfo.mismatch);
assertEquals("1.1.0", bundleInfo.suggestedVersion.toString());
}
}

// Adding a method to an exported class and unexporting a package produces a
// MINOR bump (1.0.0 -> 1.1.0)
@Test
Expand Down
44 changes: 36 additions & 8 deletions biz.aQute.bndlib/src/aQute/bnd/differ/Baseline.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@
import static aQute.bnd.service.diff.Delta.UNCHANGED;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Formatter;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.jar.Manifest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import aQute.bnd.header.Attrs;
import aQute.bnd.header.OSGiHeader;
import aQute.bnd.header.Parameters;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Descriptors;
import aQute.bnd.osgi.Instruction;
import aQute.bnd.osgi.Instructions;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Processor;
Expand All @@ -33,7 +31,7 @@
import aQute.bnd.unmodifiable.Sets;
import aQute.bnd.version.Version;
import aQute.libg.generics.Create;
import aQute.service.reporter.Reporter;
import aQute.service.reporter.Reporter;;

/**
* This class maintains
Expand Down Expand Up @@ -133,9 +131,12 @@ public Set<Info> baseline(Tree n, Parameters nExports, Tree o, Parameters oExpor
.startsWith("java."))
continue;

if (!packageFilters.matches(pdiff.getName()))
var matcher = packageFilters.matcher(pdiff.getName());
if (!packageFilters.isEmpty() && (matcher == null || matcher.isNegated()))
continue;

var threshold = getThreshold(packageFilters, matcher);

final Info info = new Info();
infos.add(info);
info.reason = getRootCauses(pdiff);
Expand Down Expand Up @@ -170,6 +171,11 @@ public Set<Info> baseline(Tree n, Parameters nExports, Tree o, Parameters oExpor
default :
break;
}

if (threshold != null && diff.getDelta().compareTo(threshold) < 0) {
return true;
}

switch (diff.getType()) {
case PACKAGE :
case INTERFACE :
Expand Down Expand Up @@ -244,6 +250,12 @@ public Set<Info> baseline(Tree n, Parameters nExports, Tree o, Parameters oExpor
case REMOVED -> MAJOR;
default -> MAJOR;
};


if (threshold != null && content.compareTo(threshold) < 0) {
content = UNCHANGED;
}

if (content.compareTo(highestDelta) > 0) {
highestDelta = content;
}
Expand Down Expand Up @@ -285,6 +297,22 @@ public Set<Info> baseline(Tree n, Parameters nExports, Tree o, Parameters oExpor
return infos;
}

private Delta getThreshold(Instructions packageFilters, Instruction matcher) {
if (matcher == null)
return null;
var attrs = packageFilters.get(matcher);
assert attrs != null : "guaranteed by the matcher != null";
var threshold = attrs.getOrDefault(Constants.DIFFPACKAGES_THRESHOLD, "MICRO")
.toUpperCase();
try {
return Delta.valueOf(threshold);
}
catch (IllegalArgumentException e) {
bnd.error("baseline.threshold baseline threshold specified as [%s] but does not correspond to a Delta enum - ignoring", threshold);
}
return null;
}

/**
* "Major version zero (0.y.z) is for initial development. Anything may
* change at any time. The public API should not be considered stable."
Expand Down Expand Up @@ -367,7 +395,7 @@ public void setReleaseRepository(String releaseRepository) {

private Version bump(Delta delta, Version last, int offset, int base) {
return switch (delta) {
case UNCHANGED -> last;
case UNCHANGED, IGNORED -> last;
case MINOR -> new Version(last.getMajor(), last.getMinor() + offset, base);
case MAJOR -> new Version(last.getMajor() + 1, base, base);
case ADDED -> last;
Expand Down
2 changes: 1 addition & 1 deletion biz.aQute.bndlib/src/aQute/bnd/differ/package-info.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This package provides baseline support.
*/
@Version("2.0.0")
@Version("2.1.0")
package aQute.bnd.differ;

import org.osgi.annotation.versioning.Version;
1 change: 1 addition & 0 deletions biz.aQute.bndlib/src/aQute/bnd/osgi/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public interface Constants {
String COMPRESSION = "-compression";
String DIFFIGNORE = "-diffignore";
String DIFFPACKAGES = "-diffpackages";
String DIFFPACKAGES_THRESHOLD = "threshold";
String DEPENDSON = "-dependson";
String DEPLOY = "-deploy";
String DEPLOYREPO = "-deployrepo";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import aQute.bnd.osgi.Instructions;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Processor;
import aQute.bnd.service.diff.Delta;
import aQute.bnd.service.diff.Diff;
import aQute.bnd.version.MavenVersion;
import aQute.lib.io.IO;
Expand Down

0 comments on commit 104ad99

Please sign in to comment.