Skip to content

Commit

Permalink
fix(jenkins): Fix build event details serialization (#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
robzienert authored Feb 13, 2020
1 parent d08f63e commit 5e7fbe2
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
/** Posts new build executions to echo */
public interface EchoService {
@POST("/")
public abstract String postEvent(@Body Event event);
String postEvent(@Body Event event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@
*/
package com.netflix.spinnaker.igor.history.model;

import java.util.HashMap;
import com.google.common.collect.ImmutableMap;
import java.util.Map;

/** TODO(rz): Documnt. */
public interface BuildEvent<T extends BuildContent> extends Event {
public abstract class BuildEvent<T extends BuildContent> implements Event {

T getContent();
private static final Map<String, String> BUILD_DETAILS =
ImmutableMap.<String, String>builder().put("type", "build").put("source", "igor").build();

default Map<?, ?> getDetails() {
Map<String, String> d = new HashMap<>();
d.put("type", "build");
d.put("source", "igor");
return d;
public abstract T getContent();

public final Map<String, String> getDetails() {
return BUILD_DETAILS;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

@Data
@AllArgsConstructor
public class JenkinsBuildEvent implements BuildEvent<JenkinsBuildContent> {
public class JenkinsBuildEvent extends BuildEvent<JenkinsBuildContent> {
private JenkinsBuildContent content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static net.logstash.logback.argument.StructuredArguments.kv;

import com.google.common.base.Charsets;
import com.google.common.io.ByteSource;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.igor.IgorConfigurationProperties;
Expand All @@ -16,6 +18,7 @@
import com.netflix.spinnaker.igor.service.BuildServiceProvider;
import com.netflix.spinnaker.igor.service.BuildServices;
import com.netflix.spinnaker.security.AuthenticatedRequest;
import java.io.*;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
Expand All @@ -28,6 +31,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import retrofit.RetrofitError;
import retrofit.client.Response;

/** Monitors new jenkins builds */
@Slf4j
Expand Down Expand Up @@ -279,16 +283,52 @@ private void postEvent(final Project project, final String master) {
return;
}

JenkinsBuildEvent event = new JenkinsBuildEvent(new JenkinsBuildContent(project, master));
AuthenticatedRequest.allowAnonymous(
() -> {
echoService.ifPresent(
echo ->
echo.postEvent(new JenkinsBuildEvent(new JenkinsBuildContent(project, master))));
echo -> {
try {
echo.postEvent(event);
} catch (RetrofitError e) {
String rawResponse = "failed to retrieve response body";
try {
rawResponse = getResponseBody(e);
} catch (IOException ioe) {
log.error(
"Failed to read response while handling error communicating with Echo",
ioe);
}
log.error(
"Failed to post jenkins build event to Echo ({}:{}): {}",
master,
project.getName(),
rawResponse,
e);
throw e;
}
});
// TODO(rz): Add allowAnonymous(Runnable)
return null;
});
}

private String getResponseBody(RetrofitError e) throws IOException {
Response re = e.getResponse();
if (re != null && re.getBody() != null) {
ByteSource byteSource =
new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return e.getResponse().getBody().in();
}
};
return byteSource.asCharSource(Charsets.UTF_8).read();
} else {
return "<empty response body>";
}
}

/** TODO(jc): First pass, not sure what this cursor actually is. Document. */
private boolean cursorIsUnset(Long cursor) {
return (cursor == null || cursor == 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import lombok.Data;

@Data
public class GitlabCiBuildEvent implements BuildEvent<GitlabCiBuildContent> {
public class GitlabCiBuildEvent extends BuildEvent<GitlabCiBuildContent> {
private GitlabCiBuildContent content;

public GitlabCiBuildEvent(GitlabCiBuildContent content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@

@Data
@AllArgsConstructor
public class WerckerBuildEvent implements BuildEvent<WerckerBuildContent> {
public class WerckerBuildEvent extends BuildEvent<WerckerBuildContent> {
private WerckerBuildContent content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import com.netflix.spinnaker.igor.build.model.GenericProject
import com.netflix.spinnaker.igor.build.model.Result
import com.netflix.spinnaker.igor.config.WerckerProperties
import com.netflix.spinnaker.igor.history.EchoService
import com.netflix.spinnaker.igor.history.model.EmptyBuildContent
import com.netflix.spinnaker.igor.history.model.GenericBuildEvent
import com.netflix.spinnaker.igor.service.BuildServiceProvider
import com.netflix.spinnaker.igor.polling.CommonPollingMonitor
import com.netflix.spinnaker.igor.polling.DeltaItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import lombok.Data;

@Data
public class ConcourseBuildEvent implements BuildEvent<ConcourseBuildContent> {
public class ConcourseBuildEvent extends BuildEvent<ConcourseBuildContent> {
private ConcourseBuildContent content;

public ConcourseBuildEvent(ConcourseBuildContent content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@

@Data
@AllArgsConstructor
public class TravisBuildEvent implements BuildEvent<TravisBuildContent> {
public class TravisBuildEvent extends BuildEvent<TravisBuildContent> {
private TravisBuildContent content;
}

0 comments on commit 5e7fbe2

Please sign in to comment.