Skip to content

Commit

Permalink
Fixed ManualEventHandlerSpec UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-agrawal committed May 23, 2024
1 parent 4455614 commit a961b13
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ public List<Pipeline> getMatchingPipelines(ManualEvent event, PipelineCache pipe
.map(Optional::get)
.collect(Collectors.toList());
}

// If there's no pipeline in the cache with a matching trigger, query for
// it. This is expected if PipelineCache is only caching (enabled)
// pipelines with (enabled) triggers of specific types.
if (pipelines.isEmpty() && pipelineCache.isFilterFront50Pipelines()) {
Optional<Pipeline> pipeline = getPipelineForEvent(event, pipelineCache);
return pipeline.map(p -> List.of(p)).orElse(List.of());
}

log.debug("End of the get matching Pipelines - ManualTriggerEventHandler");
return pipelines;
}
Expand All @@ -132,6 +141,65 @@ private boolean isJenkinsBuildTriggerAndUnstableBuild(ManualEvent event) {
&& trigger.isUnstableBuild();
}

/**
* Query front50 (via the pipeline cache) for a pipeline by id, and if necessary name,
* corresponding to a manual trigger event.
*/
private Optional<Pipeline> getPipelineForEvent(ManualEvent event, PipelineCache pipelineCache) {
Content content = event.getContent();
String application = content.getApplication();
String pipelineNameOrId = content.getPipelineNameOrId();
try {
// Manual execution in deck specifies the pipeline name, so try that first. If that doesn't
// work, query by id.
Optional<Pipeline> pipeline =
pipelineCache
.getPipelineByName(application, pipelineNameOrId)
.or(() -> pipelineCache.getPipelineById(pipelineNameOrId));

if (pipeline.isEmpty()) {
// no luck, we queried, but still can't find the pipeline
return Optional.empty();
}

Pipeline actualPipeline = pipeline.get();

// It's a bit of belt-and-suspenders, but since we have logic that
// verifies that a pipeline from the cache matches a trigger (see
// withMatchingTrigger call pipelineMatches), let's verify that this
// pipeline matches the trigger as well. This also handles the disabled
// check.
if (!pipelineMatches(application, pipelineNameOrId, actualPipeline)) {
log.debug(
"pipeline from front50 doesn't match trigger. trigger: {}, pipeline id {}, pipeline application, pipeline name",
event,
actualPipeline.getId(),
actualPipeline.getApplication(),
actualPipeline.getName());
return Optional.empty();
}

return Optional.of(buildTrigger(actualPipeline, content.getTrigger()));
} catch (Exception e) {
// This isn't my favorite way of doing error handling, but it's what
// buildTrigger does, and what TriggerMonitor.triggerMatchingPipelines
// expects.
log.error(
String.format(
"exception querying for pipeline in application %s with nameOrId %s",
application, pipelineNameOrId),
e);
// We don't know whether we have a pipeline name or id...but name is a
// required field in a pipeline, so use it there.
return Optional.of(
Pipeline.builder()
.application(application)
.name(pipelineNameOrId)
.errorMessage(e.toString())
.build());
}
}

private boolean pipelineMatches(String application, String nameOrId, Pipeline pipeline) {
return !pipeline.isDisabled()
&& pipeline.getApplication().equals(application)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package com.netflix.spinnaker.echo.pipelinetriggers.eventhandlers

import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spinnaker.echo.artifacts.ArtifactInfoService
import com.netflix.spinnaker.echo.build.BuildInfoService
import com.netflix.spinnaker.echo.jackson.EchoObjectMapper
Expand Down

0 comments on commit a961b13

Please sign in to comment.