All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.spinnaker.echo.pipelinetriggers.eventhandlers.WebhookEventHandler Maven / Gradle / Ivy

The newest version!
/*
 * 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 static com.netflix.spinnaker.echo.pipelinetriggers.artifacts.ArtifactMatcher.isJsonPathConstraintInPayload;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.echo.model.Pipeline;
import com.netflix.spinnaker.echo.model.Trigger;
import com.netflix.spinnaker.echo.model.trigger.WebhookEvent;
import com.netflix.spinnaker.fiat.shared.FiatPermissionEvaluator;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Implementation of TriggerEventHandler for events of type {@link WebhookEvent}, which occur when a
 * webhook is received.
 */
@Component
public class WebhookEventHandler extends BaseTriggerEventHandler {
  private static final String TRIGGER_TYPE = "webhook";
  private static final List supportedTriggerTypes = Collections.singletonList(TRIGGER_TYPE);

  @Autowired
  public WebhookEventHandler(
      Registry registry,
      ObjectMapper objectMapper,
      FiatPermissionEvaluator fiatPermissionEvaluator) {
    super(registry, objectMapper, fiatPermissionEvaluator);
  }

  @Override
  public List supportedTriggerTypes() {
    return supportedTriggerTypes;
  }

  @Override
  public boolean handleEventType(String eventType) {
    return eventType != null && !eventType.equals("manual");
  }

  @Override
  public Class getEventType() {
    return WebhookEvent.class;
  }

  @Override
  public boolean isSuccessfulTriggerEvent(WebhookEvent webhookEvent) {
    return true;
  }

  @Override
  protected Function buildTrigger(WebhookEvent webhookEvent) {
    WebhookEvent.Content content = webhookEvent.getContent();
    Map payload = webhookEvent.getPayload();

    return trigger ->
        trigger
            .atParameters(content.getParameters())
            .atPayload(payload)
            .atEventId(webhookEvent.getEventId());
  }

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

  @Override
  protected Predicate matchTriggerFor(WebhookEvent webhookEvent) {
    final String type = webhookEvent.getDetails().getType();
    final String source = webhookEvent.getDetails().getSource();

    return trigger ->
        trigger.getType() != null
            && trigger.getType().equalsIgnoreCase(type)
            && trigger.getSource() != null
            && trigger.getSource().equals(source)
            && (
            // The Constraints in the Trigger could be null. That's OK.
            trigger.getPayloadConstraints() == null
                ||

                // If the Constraints are present, check that there are equivalents in the webhook
                // payload.
                (trigger.getPayloadConstraints() != null
                    && isJsonPathConstraintInPayload(
                        trigger.getPayloadConstraints(), webhookEvent.getPayload())));
  }

  @Override
  public Map getAdditionalTags(Pipeline pipeline) {
    Map tags = new HashMap<>();
    tags.put("type", pipeline.getTrigger().getType());
    return tags;
  }

  @Override
  protected List getArtifactsFromEvent(WebhookEvent webhookEvent, Trigger trigger) {
    return webhookEvent.getContent().getArtifacts();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy