com.netflix.spinnaker.kork.artifacts.model.Artifact Java Examples

The following examples show how to use com.netflix.spinnaker.kork.artifacts.model.Artifact. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GoogleCloudBuildArtifactExtractor.java    From echo with Apache License 2.0 6 votes vote down vote up
@Override
public List<Artifact> getArtifacts(String messagePayload) {
  TypedInput build =
      new TypedByteArray("application/json", messagePayload.getBytes(StandardCharsets.UTF_8));
  try {
    return retrySupport.retry(
        () ->
            AuthenticatedRequest.allowAnonymous(
                () -> igorService.extractGoogleCloudBuildArtifacts(account, build)),
        5,
        2000,
        false);
  } catch (Exception e) {
    log.error("Failed to fetch artifacts for build: {}", e);
    return Collections.emptyList();
  }
}
 
Example #2
Source File: NexusArtifactExtractor.java    From echo with Apache License 2.0 6 votes vote down vote up
@Override
public List<Artifact> getArtifacts(String source, Map payloadMap) {
  NexusPayload payload = mapper.convertValue(payloadMap, NexusPayload.class);
  if (payload.getComponent() == null) {
    return Collections.emptyList();
  } else {
    Component component = payload.getComponent();
    return Collections.singletonList(
        Artifact.builder()
            .type("maven/file")
            .name(component.getGroup() + ":" + component.getName())
            .reference(
                component.getGroup() + ":" + component.getName() + ":" + component.getVersion())
            .version(component.getVersion())
            .provenance(payload.getRepositoryName())
            .build());
  }
}
 
Example #3
Source File: QuayArtifactExtractor.java    From echo with Apache License 2.0 6 votes vote down vote up
@Override
public List<Artifact> getArtifacts(String source, Map payload) {
  PushEvent pushEvent = objectMapper.convertValue(payload, PushEvent.class);
  String repository = pushEvent.getRepository();
  List<String> updatedTags = pushEvent.getUpdatedTags();
  if (repository == null || updatedTags.size() == 0) {
    log.warn("Malformed push event from quay: {}", payload);
    return Collections.emptyList();
  }

  return updatedTags.stream()
      .map(
          a ->
              Artifact.builder()
                  .type("docker/image")
                  .reference(String.format("%s:%s", pushEvent.getDockerUrl(), a))
                  .name(pushEvent.getDockerUrl())
                  .version(a)
                  .provenance(pushEvent.getHomepage())
                  .build())
      .collect(Collectors.toList());
}
 
Example #4
Source File: ArtifactExtractor.java    From echo with Apache License 2.0 5 votes vote down vote up
public List<Artifact> extractArtifacts(String type, String source, Map payload) {
  return artifactExtractors.stream()
      .filter(p -> p.handles(type, source))
      .map(e -> e.getArtifacts(source, payload))
      .flatMap(Collection::stream)
      .collect(Collectors.toList());
}
 
Example #5
Source File: JinjaArtifactExtractor.java    From kork with Apache License 2.0 5 votes vote down vote up
private List<Artifact> readArtifactList(String hydratedTemplate) {
  try {
    return objectMapper.readValue(hydratedTemplate, artifactListReference);
  } catch (IOException ioe) {
    // Failure to parse artifacts from the message indicates either
    // the message payload does not match the provided template or
    // there is no template and no artifacts are expected
    log.warn("Unable to parse artifact from {}", hydratedTemplate, ioe);
  }
  return Collections.emptyList();
}
 
Example #6
Source File: BuildEventHandler.java    From echo with Apache License 2.0 5 votes vote down vote up
protected List<Artifact> getArtifactsFromEvent(BuildEvent event, Trigger trigger) {
  if (buildInfoService.isPresent()) {
    try {
      return AuthenticatedRequest.propagate(
              () -> buildInfoService.get().getArtifactsFromBuildEvent(event, trigger),
              getKorkUser(trigger))
          .call();
    } catch (Exception e) {
      log.warn("Unable to get artifacts from event {}, trigger {}", event, trigger, e);
    }
  }
  return Collections.emptyList();
}
 
Example #7
Source File: ArtifactoryEventHandler.java    From echo with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Artifact> getArtifactsFromEvent(
    ArtifactoryEvent artifactoryEvent, Trigger trigger) {
  return artifactoryEvent.getContent() != null
          && artifactoryEvent.getContent().getArtifact() != null
      ? Collections.singletonList(artifactoryEvent.getContent().getArtifact())
      : new ArrayList<>();
}
 
Example #8
Source File: DockerEventHandler.java    From echo with Apache License 2.0 5 votes vote down vote up
protected List<Artifact> getArtifactsFromEvent(DockerEvent dockerEvent, Trigger trigger) {
  DockerEvent.Content content = dockerEvent.getContent();

  String name = content.getRegistry() + "/" + content.getRepository();
  String reference = name + ":" + content.getTag();
  return Collections.singletonList(
      Artifact.builder()
          .type("docker/image")
          .name(name)
          .version(content.getTag())
          .reference(reference)
          .build());
}
 
Example #9
Source File: ManualEventHandler.java    From echo with Apache License 2.0 5 votes vote down vote up
/**
 * If possible, replace trigger artifact with full artifact from the artifactInfoService. If there
 * are no artifactInfo providers, or if the artifact is not found, the artifact is returned as is.
 */
protected List<Artifact> resolveArtifacts(List<Map<String, Object>> manualTriggerArtifacts) {
  List<Artifact> resolvedArtifacts = new ArrayList<>();
  for (Map a : manualTriggerArtifacts) {
    Artifact artifact = objectMapper.convertValue(a, Artifact.class);

    if (Strings.isNullOrEmpty(artifact.getName())
        || Strings.isNullOrEmpty(artifact.getVersion())
        || Strings.isNullOrEmpty(artifact.getLocation())) {
      resolvedArtifacts.add(artifact);
    } else {
      try {
        Artifact resolvedArtifact =
            artifactInfoService
                .get()
                .getArtifactByVersion(
                    artifact.getLocation(), artifact.getName(), artifact.getVersion());
        resolvedArtifacts.add(resolvedArtifact);
      } catch (RetrofitError e) {
        if (e.getResponse() != null
            && e.getResponse().getStatus() == HttpStatus.NOT_FOUND.value()) {
          log.error(
              "Artifact "
                  + artifact.getName()
                  + " "
                  + artifact.getVersion()
                  + " not found in image provider "
                  + artifact.getLocation());
          resolvedArtifacts.add(artifact);
        } else throw e;
      }
    }
  }
  return resolvedArtifacts;
}
 
Example #10
Source File: ArtifactMatcher.java    From echo with Apache License 2.0 5 votes vote down vote up
public static boolean anyArtifactsMatchExpected(
    List<Artifact> messageArtifacts,
    Trigger trigger,
    List<ExpectedArtifact> pipelineExpectedArtifacts) {
  messageArtifacts = messageArtifacts == null ? new ArrayList<>() : messageArtifacts;
  List<String> expectedArtifactIds = trigger.getExpectedArtifactIds();

  if (expectedArtifactIds == null || expectedArtifactIds.isEmpty()) {
    return true;
  }

  List<ExpectedArtifact> expectedArtifacts =
      pipelineExpectedArtifacts == null
          ? new ArrayList<>()
          : pipelineExpectedArtifacts.stream()
              .filter(e -> expectedArtifactIds.contains(e.getId()))
              .collect(Collectors.toList());

  if (messageArtifacts.size() > expectedArtifactIds.size()) {
    log.warn(
        "Parsed message artifacts (size {}) greater than expected artifacts (size {}), continuing trigger anyway",
        messageArtifacts.size(),
        expectedArtifactIds.size());
  }

  Predicate<Artifact> expectedArtifactMatch =
      a -> expectedArtifacts.stream().anyMatch(e -> e.matches(a));

  boolean result = messageArtifacts.stream().anyMatch(expectedArtifactMatch);
  if (!result) {
    log.info("Skipping trigger {} as artifact constraints were not satisfied", trigger);
  }
  return result;
}
 
Example #11
Source File: BuildInfoService.java    From echo with Apache License 2.0 5 votes vote down vote up
public List<Artifact> getArtifactsFromBuildEvent(BuildEvent event, Trigger trigger) {
  List<Artifact> buildArtifacts =
      Optional.ofNullable(event.getContent())
          .map(BuildEvent.Content::getProject)
          .map(BuildEvent.Project::getLastBuild)
          .map(BuildEvent.Build::getArtifacts)
          .orElse(Collections.emptyList());

  List<Artifact> result = new ArrayList<>();
  result.addAll(buildArtifacts);
  result.addAll(getArtifactsFromPropertyFile(event, trigger.getPropertyFile()));
  result.addAll(getArtifactsFromBuildInfo(trigger));
  return result;
}
 
Example #12
Source File: BuildInfoService.java    From echo with Apache License 2.0 5 votes vote down vote up
private List<Artifact> getArtifactsFromBuildInfo(Trigger trigger) {
  Map<String, Object> buildInfo = trigger.getBuildInfo();
  if (buildInfo != null) {
    Object artifacts = buildInfo.get("artifacts");
    if (artifacts != null) {
      return objectMapper.convertValue(artifacts, new TypeReference<List<Artifact>>() {});
    }
  }
  return Collections.emptyList();
}
 
Example #13
Source File: BuildInfoService.java    From echo with Apache License 2.0 5 votes vote down vote up
private List<Artifact> getArtifactsFromPropertyFile(BuildEvent event, String propertyFile) {
  String master = event.getContent().getMaster();
  String job = event.getContent().getProject().getName();
  int buildNumber = event.getBuildNumber();
  if (StringUtils.isNoneEmpty(master, job, propertyFile)) {
    return retry(() -> igorService.getArtifacts(buildNumber, propertyFile, master, job));
  }
  return Collections.emptyList();
}
 
Example #14
Source File: MessageArtifactTranslator.java    From echo with Apache License 2.0 5 votes vote down vote up
public List<Artifact> parseArtifacts(String messagePayload) {
  List<Artifact> artifacts = artifactExtractor.getArtifacts(messagePayload);
  if (!artifacts.isEmpty()) {
    applicationEventPublisher.publishEvent(new ArtifactEvent(null, artifacts));
  }
  return artifacts;
}
 
Example #15
Source File: GitlabV4ArtifactExtractor.java    From echo with Apache License 2.0 5 votes vote down vote up
@Override
public List<Artifact> getArtifacts(String source, Map payload) {
  PushEvent pushEvent = objectMapper.convertValue(payload, PushEvent.class);
  String sha = pushEvent.after;
  Project project = pushEvent.project;
  // since gitlab doesn't provide us with explicit API urls we have to assume the baseUrl from
  // other
  // urls that are provided
  String gitlabBaseUrl = extractBaseUrlFromHomepage(project.homepage, project.pathWithNamespace);
  String apiBaseUrl =
      String.format(
          "%s/api/v4/projects/%s/repository/files",
          gitlabBaseUrl, URLEncoder.encode(project.pathWithNamespace));

  Set<String> affectedFiles =
      pushEvent.commits.stream()
          .map(
              c -> {
                List<String> fs = new ArrayList<>();
                fs.addAll(c.added);
                fs.addAll(c.modified);
                return fs;
              })
          .flatMap(Collection::stream)
          .collect(Collectors.toSet());

  return affectedFiles.stream()
      .map(
          f ->
              Artifact.builder()
                  .name(f)
                  .version(sha)
                  .type("gitlab/file")
                  .reference(String.format("%s/%s/raw", apiBaseUrl, URLEncoder.encode(f)))
                  .build())
      .collect(Collectors.toList());
}
 
Example #16
Source File: PubsubEventCreator.java    From echo with Apache License 2.0 5 votes vote down vote up
private List<Artifact> parseArtifacts(String messagePayload) {
  if (!messageArtifactTranslator.isPresent()) {
    return Collections.emptyList();
  }
  List<Artifact> artifacts = messageArtifactTranslator.get().parseArtifacts(messagePayload);
  // Artifact must have at least a reference defined.
  if (CollectionUtils.isEmpty(artifacts)
      || StringUtils.isEmpty(artifacts.get(0).getReference())) {
    return Collections.emptyList();
  }
  return artifacts;
}
 
Example #17
Source File: NexusArtifactExtractorTest.java    From echo with Apache License 2.0 5 votes vote down vote up
@Test
void extractNexusArtifact() throws IOException {
  ObjectMapper mapper = EchoObjectMapper.getInstance();

  String payloadStr =
      "{"
          + "\"action\": \"UPDATED\","
          + "\"component\": {"
          + "   \"format\": \"maven2\","
          + "   \"group\": \"io.pivotal.spinnaker\","
          + "   \"id\": \"76d7d7e4186a390fd96db295b80986ab\","
          + "   \"name\": \"multifoundationmetrics\","
          + "   \"version\": \"0.3.3\""
          + "},"
          + "\"initiator\": \"admin/172.17.0.1\","
          + "\"nodeId\": \"25F88840-1F7BAEDC-C7C8DFE0-DF41CE6C-697E9B6C\","
          + "\"repositoryName\": \"maven-releases\","
          + "\"timestamp\": \"2019-05-10T16:08:38.565+0000\""
          + "}";

  Map payload = mapper.readValue(payloadStr, Map.class);

  NexusArtifactExtractor extractor = new NexusArtifactExtractor();

  assertThat(extractor.getArtifacts("nexus", payload))
      .containsExactly(
          Artifact.builder()
              .type("maven/file")
              .name("io.pivotal.spinnaker:multifoundationmetrics")
              .reference("io.pivotal.spinnaker:multifoundationmetrics:0.3.3")
              .version("0.3.3")
              .provenance("maven-releases")
              .build());
}
 
Example #18
Source File: DockerHubArtifactExtractor.java    From echo with Apache License 2.0 5 votes vote down vote up
@Override
public List<Artifact> getArtifacts(String source, Map payload) {
  WebhookEvent webhookEvent = objectMapper.convertValue(payload, WebhookEvent.class);
  Repository repository = webhookEvent.getRepository();
  PushData pushData = webhookEvent.getPushData();
  if (repository == null || pushData == null) {
    log.warn("Malformed push data from dockerhub: {}", payload);
    return Collections.emptyList();
  }

  String name = String.format("index.docker.io/%s", repository.getRepoName());
  String version = pushData.getTag();
  Map<String, Object> metadata =
      new ImmutableMap.Builder<String, Object>()
          .put("pusher", pushData.getPusher() != null ? pushData.getPusher() : "")
          .build();

  return Collections.singletonList(
      Artifact.builder()
          .type("docker/image")
          .reference(String.format("%s:%s", name, version))
          .name(name)
          .version(version)
          .provenance(webhookEvent.getCallbackUrl())
          .metadata(metadata)
          .build());
}
 
Example #19
Source File: GitHubArtifactExtractor.java    From echo with Apache License 2.0 5 votes vote down vote up
@Override
public List<Artifact> getArtifacts(String source, Map payload) {
  PushEvent pushEvent = objectMapper.convertValue(payload, PushEvent.class);
  String sha = pushEvent.after;
  Set<String> affectedFiles =
      pushEvent.commits.stream()
          .map(
              c -> {
                List<String> fs = new ArrayList<>();
                fs.addAll(c.added);
                fs.addAll(c.modified);
                return fs;
              })
          .flatMap(Collection::stream)
          .collect(Collectors.toSet());

  return affectedFiles.stream()
      .map(
          f ->
              Artifact.builder()
                  .name(f)
                  .version(sha)
                  .type("github/file")
                  .reference(pushEvent.repository.contentsUrl.replace("{+path}", f))
                  .build())
      .collect(Collectors.toList());
}
 
Example #20
Source File: ManualEventHandler.java    From echo with Apache License 2.0 4 votes vote down vote up
private List<Map<String, Object>> convertToListOfMaps(List<Artifact> artifacts) {
  return objectMapper.convertValue(artifacts, new TypeReference<List<Map<String, Object>>>() {});
}
 
Example #21
Source File: JinjaArtifactExtractor.java    From kork with Apache License 2.0 4 votes vote down vote up
public List<Artifact> getArtifacts(String messagePayload) {
  if (StringUtils.isEmpty(messagePayload)) {
    return Collections.emptyList();
  }
  return readArtifactList(jinjaTransform(messagePayload));
}
 
Example #22
Source File: DockerRegistryArtifactExtractor.java    From echo with Apache License 2.0 4 votes vote down vote up
@Override
public List<Artifact> getArtifacts(String source, Map payload) {
  Notification notification = objectMapper.convertValue(payload, Notification.class);
  List<Event> pushEvents =
      notification.getEvents().stream()
          .filter(
              e ->
                  e.action.equals("push")
                      && e.getTarget() != null
                      && (e.getTarget().getMediaType().equals(MEDIA_TYPE_V1_MANIFEST)
                          || e.getTarget().getMediaType().equals(MEDIA_TYPE_V2_MANIFEST))
                      && e.getTarget().getRepository() != null
                      && (e.getTarget().getTag() != null || e.getTarget().getDigest() != null))
          .collect(Collectors.toList());

  if (pushEvents.isEmpty()) {
    return Collections.EMPTY_LIST;
  }

  return pushEvents.stream()
      .map(
          e -> {
            String tag;
            String tagSeparator = ":";
            if (e.getTarget().getTag() != null) {
              tag = e.getTarget().getTag();
            } else if (e.getTarget().getDigest() != null) {
              tag = e.getTarget().getDigest();
              tagSeparator = "@";
            } else {
              return null;
            }
            String host =
                (e.getRequest() != null && e.getRequest().getHost() != null)
                    ? (e.getRequest().getHost() + "/")
                    : "";
            return Artifact.builder()
                .name(host + e.getTarget().getRepository())
                .version(tag)
                .reference(host + e.getTarget().getRepository() + tagSeparator + tag)
                .type("docker/image")
                .build();
          })
      .filter(Objects::nonNull)
      .collect(Collectors.toList());
}
 
Example #23
Source File: BaseTriggerEventHandler.java    From echo with Apache License 2.0 4 votes vote down vote up
private List<Artifact> getArtifacts(T event, Trigger trigger) {
  List<Artifact> results = new ArrayList<>();
  Optional.ofNullable(getArtifactsFromEvent(event, trigger)).ifPresent(results::addAll);
  return results;
}
 
Example #24
Source File: PubsubEventHandler.java    From echo with Apache License 2.0 4 votes vote down vote up
@Override
protected List<Artifact> getArtifactsFromEvent(PubsubEvent pubsubEvent, Trigger trigger) {
  return pubsubEvent.getContent().getMessageDescription().getArtifacts();
}
 
Example #25
Source File: WebhookEventHandler.java    From echo with Apache License 2.0 4 votes vote down vote up
@Override
protected List<Artifact> getArtifactsFromEvent(WebhookEvent webhookEvent, Trigger trigger) {
  return webhookEvent.getContent().getArtifacts();
}
 
Example #26
Source File: IgorService.java    From echo with Apache License 2.0 4 votes vote down vote up
@GET("/builds/artifacts/{buildNumber}/{master}/{job}")
List<Artifact> getArtifacts(
    @Path("buildNumber") Integer buildNumber,
    @Query("propertyFile") String propertyFile,
    @Path("master") String master,
    @Path(value = "job", encode = false) String job);
 
Example #27
Source File: GitEventHandler.java    From echo with Apache License 2.0 4 votes vote down vote up
@Override
protected List<Artifact> getArtifactsFromEvent(GitEvent gitEvent, Trigger trigger) {
  return gitEvent.getContent() != null && gitEvent.getContent().getArtifacts() != null
      ? gitEvent.getContent().getArtifacts()
      : new ArrayList<>();
}
 
Example #28
Source File: PluginEventHandler.java    From echo with Apache License 2.0 4 votes vote down vote up
@Override
protected List<Artifact> getArtifactsFromEvent(PluginEvent event, Trigger trigger) {
  // TODO(rz): Add artifact support someday
  return new ArrayList<>();
}
 
Example #29
Source File: NexusEventHandler.java    From echo with Apache License 2.0 4 votes vote down vote up
@Override
protected List<Artifact> getArtifactsFromEvent(NexusEvent nexusEvent, Trigger trigger) {
  return nexusEvent.getContent() != null && nexusEvent.getContent().getArtifact() != null
      ? Collections.singletonList(nexusEvent.getContent().getArtifact())
      : new ArrayList<>();
}
 
Example #30
Source File: ArtifactInfoService.java    From echo with Apache License 2.0 4 votes vote down vote up
public Artifact getArtifactByVersion(String provider, String packageName, String version) {
  return igorService.getArtifactByVersion(provider, packageName, version);
}