Skip to content

Commit

Permalink
feat(plugins): Add new plugin trigger type (#884)
Browse files Browse the repository at this point in the history
  • Loading branch information
robzienert committed May 6, 2020
1 parent 46127ea commit 669a9f3
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public enum Type {
WEBHOOK("webhook"),
PUBSUB("pubsub"),
DRYRUN("dryrun"),
PIPELINE("pipeline");
PIPELINE("pipeline"),
PLUGIN("plugin");

private final String type;

Expand Down Expand Up @@ -157,6 +158,14 @@ public String toString() {
// Artifact constraints
List<String> expectedArtifactIds;

// Configuration for plugin triggers
String pluginId;
String version;
String releaseDate;
List<Map<String, String>> requires;
String binaryUrl;
boolean preferred;

/** Field to use for custom triggers involving artifacts */
String artifactName;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2020 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.echo.model.trigger;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;
import java.util.Map;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
@JsonIgnoreProperties(ignoreUnknown = true)
public class PluginEvent extends TriggerEvent {
public static final String TYPE = "plugin";

private Content content;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Content {
private String pluginId;
private String version;
private String releaseDate;
private List<Map<String, String>> requires;
private String binaryUrl;
private boolean preferred;
private String lastModified;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2020 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.echo.pipelinetriggers.eventhandlers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.echo.model.Trigger;
import com.netflix.spinnaker.echo.model.trigger.PluginEvent;
import com.netflix.spinnaker.fiat.shared.FiatPermissionEvaluator;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import org.springframework.stereotype.Component;

@Component
public class PluginEventHandler extends BaseTriggerEventHandler<PluginEvent> {

private static final String PLUGIN_TRIGGER_TYPE = "plugin";
private static final List<String> SUPPORTED_TYPES =
Collections.singletonList(PLUGIN_TRIGGER_TYPE);

public PluginEventHandler(
Registry registry,
ObjectMapper objectMapper,
FiatPermissionEvaluator fiatPermissionEvaluator) {
super(registry, objectMapper, fiatPermissionEvaluator);
}

@Override
protected Predicate<Trigger> matchTriggerFor(PluginEvent event) {
return trigger -> trigger.getType().equals(PLUGIN_TRIGGER_TYPE);
}

@Override
protected Function<Trigger, Trigger> buildTrigger(PluginEvent event) {
return trigger ->
trigger
.toBuilder()
.pluginId(event.getContent().getPluginId())
.version(event.getContent().getVersion())
.releaseDate(event.getContent().getReleaseDate())
.requires(event.getContent().getRequires())
.binaryUrl(event.getContent().getBinaryUrl())
.build();
}

@Override
protected boolean isValidTrigger(Trigger trigger) {
return trigger.isEnabled()
&& trigger.getType().equals(PLUGIN_TRIGGER_TYPE)
&& trigger.isPreferred();
}

@Override
protected Class<PluginEvent> getEventType() {
return PluginEvent.class;
}

@Override
protected List<Artifact> getArtifactsFromEvent(PluginEvent event, Trigger trigger) {
// TODO(rz): Add artifact support someday
return new ArrayList<>();
}

@Override
public List<String> supportedTriggerTypes() {
return SUPPORTED_TYPES;
}

@Override
public boolean handleEventType(String eventType) {
return eventType.equalsIgnoreCase(PluginEvent.TYPE);
}
}

0 comments on commit 669a9f3

Please sign in to comment.