Skip to content

Commit

Permalink
fix(core): Correctly handle artifact matching with multiple packages
Browse files Browse the repository at this point in the history
This appears to have been broken when the global context was removed.
  • Loading branch information
ajordens authored and robfletcher committed Feb 26, 2018
1 parent 86c3398 commit d57d74a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
Expand Up @@ -41,7 +41,7 @@ public class PackageInfo {
private final boolean extractBuildDetails;
private final boolean extractVersion;
private final BuildDetailExtractor buildDetailExtractor;
private final Pattern packageFilePattern;
private final List<Pattern> packageFilePatterns = new ArrayList<>();

public PackageInfo(Stage stage, String packageType, String versionDelimiter, boolean extractBuildDetails, boolean extractVersion, ObjectMapper mapper) {
this.stage = stage;
Expand All @@ -52,7 +52,15 @@ public PackageInfo(Stage stage, String packageType, String versionDelimiter, boo
this.mapper = mapper;
this.buildDetailExtractor = new BuildDetailExtractor();

packageFilePattern = Pattern.compile(format("%s.*\\.%s", stage.getContext().get("package"), packageType));
// can be a space separated set of packages
if (stage.getContext().containsKey("package")) {
String packages = stage.getContext().get("package").toString();
for (String p : packages.split(" ")) {
packageFilePatterns.add(
Pattern.compile(format("%s.*\\.%s", p, packageType))
);
}
}
}

@VisibleForTesting
Expand All @@ -73,7 +81,7 @@ public Map<String, Object> findTargetPackage(boolean allowMissingPackageInstalla
}

if (buildInfo == null || (buildInfo.get("artifacts") != null && !((Collection) buildInfo.get("artifacts")).isEmpty())) {
Map<String, Object> upstreamBuildInfo = findBuildInfoInUpstreamStage(stage, packageFilePattern);
Map<String, Object> upstreamBuildInfo = findBuildInfoInUpstreamStage(stage, packageFilePatterns);
if (!upstreamBuildInfo.isEmpty()) {
buildInfo = upstreamBuildInfo;
}
Expand Down Expand Up @@ -263,23 +271,28 @@ private Map<String, Object> filterRPMArtifacts(List<Map<String, Object>> artifac
.orElse(emptyMap());
}

private static Map<String, Object> findBuildInfoInUpstreamStage(Stage currentStage, Pattern packageFilePattern) {
private static Map<String, Object> findBuildInfoInUpstreamStage(Stage currentStage,
List<Pattern> packageFilePatterns) {
Stage upstreamStage = currentStage
.ancestors()
.stream()
.filter(it -> {
Map<String, Object> buildInfo = (Map<String, Object>) it.getOutputs().get("buildInfo");
return buildInfo != null &&
artifactMatch((List<Map<String, String>>) buildInfo.get("artifacts"), packageFilePattern);
artifactMatch((List<Map<String, String>>) buildInfo.get("artifacts"), packageFilePatterns);
})
.findFirst()
.orElse(null);
return upstreamStage != null ? (Map<String, Object>) upstreamStage.getOutputs().get("buildInfo") : emptyMap();
}

private static boolean artifactMatch(List<Map<String, String>> artifacts, Pattern pattern) {
private static boolean artifactMatch(List<Map<String, String>> artifacts, List<Pattern> patterns) {
return artifacts != null &&
artifacts.stream()
.anyMatch((Map artifact) -> pattern.matcher(String.valueOf(artifact.get("fileName"))).matches());
artifacts
.stream()
.anyMatch((Map artifact) -> patterns
.stream()
.anyMatch(p -> p.matcher(String.valueOf(artifact.get("fileName"))).matches())
);
}
}
Expand Up @@ -44,7 +44,7 @@ class PackageInfoSpec extends Specification {
def artifacts = artifactFilenames.collect { [fileName: it] }

expect:
PackageInfo.artifactMatch(artifacts, pattern) == expectedMatch
PackageInfo.artifactMatch(artifacts, [pattern]) == expectedMatch

where:
artifactPattern || artifactFilenames || expectedMatch
Expand Down Expand Up @@ -309,7 +309,7 @@ class PackageInfoSpec extends Specification {
def pattern = Pattern.compile("api.*")

when:
def buildInfo = packageInfo.findBuildInfoInUpstreamStage(bakeStage, pattern)
def buildInfo = packageInfo.findBuildInfoInUpstreamStage(bakeStage, [pattern])

then:
noExceptionThrown()
Expand Down Expand Up @@ -525,4 +525,43 @@ class PackageInfoSpec extends Specification {
}
]
}

def "should fetch artifacts from upstream stage when not specified on pipeline trigger"() {
given:
def jenkinsTrigger = new JenkinsTrigger("master", "job", 1, "propertyFile")
jenkinsTrigger.buildInfo = new BuildInfo("name", 0, "url", [], [], false, "result")

def pipeline = pipeline {
trigger = jenkinsTrigger // has no artifacts!
stage {
refId = "1"
outputs = [
buildInfo: [
"artifacts": [
["fileName": "spinnaker_0.2.0-114_all.deb"],
["fileName": "spinnakerdeps_0.1.0-114_all.deb"]
]
]
]
}
stage {
id = "2"
requisiteStageRefIds = ["1"]

stage {
id = "3"
context = [
"package": "spinnakerdeps spinnaker"
]
parentStageId = "2"
}
}
}

and:
def packageInfo = new PackageInfo(pipeline.stageById("3"), "deb", "_", false, false, new ObjectMapper())

expect:
packageInfo.findTargetPackage(false).package == "spinnakerdeps_0.1.0-114_all spinnaker_0.2.0-114_all"
}
}

0 comments on commit d57d74a

Please sign in to comment.