-
Notifications
You must be signed in to change notification settings - Fork 811
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add SpEL Function Helpers for Resolved Trigger Artifacts (#…
- Loading branch information
1 parent
0d0a1ba
commit 8e53833
Showing
4 changed files
with
160 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...lix/spinnaker/orca/pipeline/expressions/functions/ArtifactExpressionFunctionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2019 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.orca.pipeline.expressions.functions; | ||
|
||
import static java.lang.String.format; | ||
|
||
import com.netflix.spinnaker.kork.artifacts.model.Artifact; | ||
import com.netflix.spinnaker.kork.artifacts.model.ExpectedArtifact; | ||
import com.netflix.spinnaker.kork.expressions.ExpressionFunctionProvider; | ||
import com.netflix.spinnaker.kork.expressions.SpelHelperFunctionException; | ||
import com.netflix.spinnaker.orca.pipeline.model.Execution; | ||
import java.util.function.Predicate; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ArtifactExpressionFunctionProvider implements ExpressionFunctionProvider { | ||
@Nullable | ||
@Override | ||
public String getNamespace() { | ||
return null; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Functions getFunctions() { | ||
return new Functions( | ||
new FunctionDefinition( | ||
"triggerResolvedArtifact", | ||
new FunctionParameter( | ||
Execution.class, "execution", "The execution to search for artifacts"), | ||
new FunctionParameter(String.class, "name", "The name of the resolved artifact")), | ||
new FunctionDefinition( | ||
"triggerResolvedArtifactByType", | ||
new FunctionParameter( | ||
Execution.class, "execution", "The execution to search for artifacts"), | ||
new FunctionParameter(String.class, "type", "The type of the resolved artifact"))); | ||
} | ||
|
||
public static Artifact triggerResolvedArtifact(Execution execution, String name) { | ||
return triggerResolvedArtifactBy(execution, name, artifact -> name.equals(artifact.getName())); | ||
} | ||
|
||
public static Artifact triggerResolvedArtifactByType(Execution execution, String type) { | ||
return triggerResolvedArtifactBy(execution, type, artifact -> type.equals(artifact.getType())); | ||
} | ||
|
||
private static Artifact triggerResolvedArtifactBy( | ||
Execution execution, String nameOrType, Predicate<Artifact> predicate) { | ||
return execution.getTrigger().getResolvedExpectedArtifacts().stream() | ||
.filter( | ||
expectedArtifact -> | ||
expectedArtifact.getBoundArtifact() != null | ||
&& predicate.test(expectedArtifact.getBoundArtifact())) | ||
.findFirst() | ||
.map(ExpectedArtifact::getBoundArtifact) | ||
.orElseThrow( | ||
() -> | ||
new SpelHelperFunctionException( | ||
format( | ||
"Unable to locate resolved artifact %s in trigger execution %s.", | ||
nameOrType, execution.getId()))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...innaker/orca/pipeline/expressions/functions/ArtifactExpressionFunctionProviderSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2019 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.orca.pipeline.expressions.functions | ||
|
||
import com.netflix.spinnaker.kork.artifacts.model.Artifact | ||
import com.netflix.spinnaker.kork.artifacts.model.ExpectedArtifact | ||
import com.netflix.spinnaker.kork.expressions.SpelHelperFunctionException | ||
import com.netflix.spinnaker.orca.pipeline.model.DefaultTrigger | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
import static com.netflix.spinnaker.orca.pipeline.expressions.functions.ArtifactExpressionFunctionProvider.triggerResolvedArtifact | ||
import static com.netflix.spinnaker.orca.pipeline.expressions.functions.ArtifactExpressionFunctionProvider.triggerResolvedArtifactByType | ||
import static com.netflix.spinnaker.orca.test.model.ExecutionBuilder.pipeline | ||
|
||
|
||
class ArtifactExpressionFunctionProviderSpec extends Specification { | ||
@Shared | ||
def pipeline1 = pipeline { | ||
def matchArtifact1 = new Artifact(type: "docker/image", "name": "artifact1") | ||
def boundArtifact = new Artifact(type: "docker/image", "name": "artifact1", "reference": "artifact1") | ||
|
||
trigger = new DefaultTrigger("manual", "artifact1") | ||
trigger.resolvedExpectedArtifacts = [ | ||
new ExpectedArtifact(matchArtifact: matchArtifact1, boundArtifact: boundArtifact), | ||
] | ||
} | ||
|
||
def "triggerResolvedArtifact returns resolved trigger artifact by name"() { | ||
when: | ||
def artifact = triggerResolvedArtifact(pipeline1, "artifact1") | ||
|
||
then: | ||
artifact.type == "docker/image" | ||
artifact.name == "artifact1" | ||
} | ||
|
||
def "triggerResolvedArtifactByType returns resolved trigger artifact by type"() { | ||
when: | ||
def artifact = triggerResolvedArtifactByType(pipeline1, "docker/image") | ||
|
||
then: | ||
artifact.type == "docker/image" | ||
artifact.name == "artifact1" | ||
} | ||
|
||
|
||
def "triggerResolvedArtifact throws when artifact is not found"() { | ||
when: | ||
triggerResolvedArtifact(pipeline1, "somename") | ||
|
||
then: | ||
thrown(SpelHelperFunctionException) | ||
} | ||
|
||
def "triggerResolvedArtifactByType throws if artifact is not found"() { | ||
when: | ||
triggerResolvedArtifactByType(pipeline1, "s3/object") | ||
|
||
then: | ||
thrown(SpelHelperFunctionException) | ||
} | ||
} |