Skip to content

Commit

Permalink
fix(triggers): fix handling of blank regex in docker registry triggers (
Browse files Browse the repository at this point in the history
#166)

When you remove a previously existing regex from the trigger in the
UI, deck keeps a tag field with an empty string for the value in the
json. This arrives as non null to the DockerEventMonitor, which  causes
the trigger to not fire for any new tag until you manually update the
pipeline json to remove the field. This change makes the logic more
resilient to malformed input. Blank or empty tag regex doesn't make
sense so it should behave the same as when the regex is null.
  • Loading branch information
chris-h-phillips authored and jtk54 committed Aug 31, 2017
1 parent 1b158fa commit 9aedf00
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.netflix.spinnaker.echo.pipelinetriggers.PipelineCache;
import lombok.NonNull;
import lombok.val;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import rx.Observable;
Expand Down Expand Up @@ -100,15 +101,22 @@ private boolean matchTags(String suppliedTag, String incomingTag) {

@Override
protected Predicate<Trigger> matchTriggerFor(final TriggerEvent event) {
DockerEvent dockerEvent = (DockerEvent) event;
return trigger -> isMatchingTrigger((DockerEvent)event, trigger);
}

private boolean isMatchingTrigger(DockerEvent dockerEvent, Trigger trigger) {
String account = dockerEvent.getContent().getAccount();
String repository = dockerEvent.getContent().getRepository();
String tag = dockerEvent.getContent().getTag();
return trigger -> trigger.getType().equals(TRIGGER_TYPE) &&
trigger.getRepository().equals(repository) &&
trigger.getAccount().equals(account) &&
((trigger.getTag() == null && !tag.equals("latest"))
|| trigger.getTag() != null && matchTags(trigger.getTag(), tag));
String eventTag = dockerEvent.getContent().getTag();
String triggerTagPattern = null;
if (StringUtils.isNotBlank(trigger.getTag())) {
triggerTagPattern = trigger.getTag().trim();
}
return trigger.getType().equals(TRIGGER_TYPE) &&
trigger.getRepository().equals(repository) &&
trigger.getAccount().equals(account) &&
((triggerTagPattern == null && !eventTag.equals("latest"))
|| triggerTagPattern != null && matchTags(triggerTagPattern, eventTag));
}

protected void onMatchingPipeline(Pipeline pipeline) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,44 @@ class DockerEventMonitorSpec extends Specification implements RetrofitStubs {
event = createDockerEvent("2")
}

@Unroll
def "triggers a pipeline that has an enabled docker trigger with empty string for regex"() {
given:
def pipeline = createPipelineWith(trigger)
pipelineCache.getPipelines() >> [pipeline]

when:
monitor.processEvent(objectMapper.convertValue(event, Event))

then:
1 * subscriber.call({ it.id == pipeline.id })

where:
trigger | field
enabledDockerTrigger.withTag("") | "regex tag"

event = createDockerEvent("2")
}

@Unroll
def "triggers a pipeline that has an enabled docker trigger with only whitespace for regex"() {
given:
def pipeline = createPipelineWith(trigger)
pipelineCache.getPipelines() >> [pipeline]

when:
monitor.processEvent(objectMapper.convertValue(event, Event))

then:
1 * subscriber.call({ it.id == pipeline.id })

where:
trigger | field
enabledDockerTrigger.withTag(" \t") | "regex tag"

event = createDockerEvent("2")
}

@Unroll
def "does not trigger a pipeline that has an enabled docker trigger with regex"() {
given:
Expand Down

0 comments on commit 9aedf00

Please sign in to comment.