Skip to content

Commit

Permalink
fix(nexus): get artifacts from nexus events (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
claymccoy authored and Jammy Louie committed Sep 12, 2019
1 parent 500d45b commit 31136d5
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ public String toString() {
// Configuration for artifactory triggers
String artifactorySearchName;

// Configuration for nexus triggers
String nexusSearchName;

// Artifact constraints
List<String> expectedArtifactIds;

Expand Down Expand Up @@ -250,6 +253,10 @@ public Trigger atArtifactorySearchName(final String artifactorySearchName) {
return this.toBuilder().artifactorySearchName(artifactorySearchName).build();
}

public Trigger atNexusSearchName(final String nexusSearchName) {
return this.toBuilder().nexusSearchName(nexusSearchName).build();
}

@JsonPOJOBuilder(withPrefix = "")
public static final class TriggerBuilder {
// When deserializing triggers, always ignore the value of propagateAuth, which should only
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019 Pivotal, 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 com.netflix.spinnaker.kork.artifacts.model.Artifact;
import lombok.Data;
import lombok.EqualsAndHashCode;

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

private Content content;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Content {
private String name;
private Artifact artifact;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2019 Pivotal, 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.NexusEvent;
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;

/**
* Implementation of TriggerEventHandler for events of type {@link NexusEvent}, which occur when an
* artifact is added/modified in anNexus repository.
*/
@Component
public class NexusEventHandler extends BaseTriggerEventHandler<NexusEvent> {
private static final String NEXUS_TRIGGER_TYPE = "nexus";
private static final List<String> supportedTriggerTypes =
Collections.singletonList(NEXUS_TRIGGER_TYPE);

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

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

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

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

@Override
protected boolean isValidTrigger(Trigger trigger) {
return trigger.isEnabled()
&& NEXUS_TRIGGER_TYPE.equals(trigger.getType())
&& trigger.getNexusSearchName() != null;
}

@Override
public boolean isSuccessfulTriggerEvent(NexusEvent event) {
return event != null;
}

@Override
protected Predicate<Trigger> matchTriggerFor(NexusEvent nexusEvent) {
String searchName = nexusEvent.getContent().getName();
return trigger ->
trigger.getType().equals(NEXUS_TRIGGER_TYPE)
&& trigger.getNexusSearchName().equals(searchName);
}

@Override
protected List<Artifact> getArtifactsFromEvent(NexusEvent nexusEvent, Trigger trigger) {
return nexusEvent.getContent() != null && nexusEvent.getContent().getArtifact() != null
? Collections.singletonList(nexusEvent.getContent().getArtifact())
: new ArrayList<>();
}

@Override
protected Function<Trigger, Trigger> buildTrigger(NexusEvent event) {
return trigger ->
trigger.atNexusSearchName(event.getContent().getName()).atEventId(event.getEventId());
}
}

0 comments on commit 31136d5

Please sign in to comment.