Skip to content

Commit

Permalink
refactor(retrofit2): remove remaining references to retrofit1
Browse files Browse the repository at this point in the history
  • Loading branch information
kirangodishala committed Jan 2, 2025
1 parent 9550a81 commit 68f095f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.netflix.spinnaker.echo.model.trigger.ManualEvent.Content;
import com.netflix.spinnaker.echo.pipelinetriggers.PipelineCache;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerHttpException;
import java.util.*;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
Expand All @@ -39,7 +40,6 @@
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import retrofit.RetrofitError;

/**
* Implementation of TriggerEventHandler for events of type {@link ManualEvent}, which occur when a
Expand Down Expand Up @@ -276,9 +276,8 @@ protected List<Artifact> resolveArtifacts(List<Map<String, Object>> manualTrigge
.getArtifactByVersion(
artifact.getLocation(), artifact.getName(), artifact.getVersion());
resolvedArtifacts.add(resolvedArtifact);
} catch (RetrofitError e) {
if (e.getResponse() != null
&& e.getResponse().getStatus() == HttpStatus.NOT_FOUND.value()) {
} catch (SpinnakerHttpException e) {
if (e.getResponseCode() == HttpStatus.NOT_FOUND.value()) {
log.error(
"Artifact "
+ artifact.getName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import com.netflix.spinnaker.kork.discovery.DiscoveryStatusListener;
import com.netflix.spinnaker.kork.dynamicconfig.DynamicConfigService;
import com.netflix.spinnaker.kork.retrofit.Retrofit2SyncCall;
import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerHttpException;
import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerServerException;
import com.netflix.spinnaker.security.AuthenticatedRequest;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -46,12 +48,7 @@
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import retrofit.RetrofitError;
import retrofit.RetrofitError.Kind;
import retrofit.client.Response;
import retrofit.mime.TypedByteArray;

/** Triggers a {@link Pipeline} by invoking _Orca_. */
@Component
Expand Down Expand Up @@ -178,14 +175,11 @@ public void startPipeline(Pipeline pipeline, TriggerSource triggerSource) {
AuthenticatedRequest.allowAnonymous(
() -> Retrofit2SyncCall.execute(orca.plan(pipelineToPlan, true)));
pipeline = objectMapper.convertValue(resolvedPipelineMap, Pipeline.class);
} catch (RetrofitError e) {
String orcaResponse = "N/A";

if (e.getResponse() != null && e.getResponse().getBody() != null) {
orcaResponse = new String(((TypedByteArray) e.getResponse().getBody()).getBytes());
}

log.error("Failed planning {}: \n{}", pipeline, orcaResponse);
} catch (SpinnakerServerException e) {
log.error(
"Failed planning {}: \n{}",
pipeline,
e.getMessage() == null ? "N/A" : e.getMessage());

// Continue anyway, so that the execution will appear in Deck
pipeline = pipeline.withPlan(false);
Expand Down Expand Up @@ -262,22 +256,16 @@ private Void triggerPipelineImpl(Pipeline pipeline, TriggerSource triggerSource)
"triggerType",
getTriggerType(pipeline))
.increment();
} catch (RetrofitError e) {
} catch (SpinnakerHttpException e) {
String orcaResponse = "N/A";
int status = 0;

if (e.getResponse() != null) {
status = e.getResponse().getStatus();

if (e.getResponse().getBody() != null) {
orcaResponse = new String(((TypedByteArray) e.getResponse().getBody()).getBytes());
}
if (e.getResponseBody() != null) {
orcaResponse = e.getResponseBody().toString();
}

log.error(
"Failed to trigger {} HTTP: {}\norca error: {}\npayload: {}",
pipeline,
status,
e.getResponseCode(),
orcaResponse,
pipelineAsString(pipeline));

Expand All @@ -299,8 +287,8 @@ private TriggerResponse triggerWithRetries(Pipeline pipeline) {
try {
attempts++;
return Retrofit2SyncCall.execute(orca.trigger(pipeline));
} catch (RetrofitError e) {
if ((attempts >= retryCount) || !isRetryableError(e)) {
} catch (SpinnakerServerException e) {
if ((attempts >= retryCount) || (e.getRetryable() != null && !e.getRetryable())) {
throw e;
} else {
log.warn(
Expand Down Expand Up @@ -412,22 +400,4 @@ private boolean isEnabled(TriggerSource triggerSource) {

return triggerEnabled && dynamicConfigService.isEnabled("orca", true);
}

private static boolean isRetryableError(Throwable error) {
if (!(error instanceof RetrofitError)) {
return false;
}
RetrofitError retrofitError = (RetrofitError) error;

if (retrofitError.getKind() == Kind.NETWORK) {
return true;
}

if (retrofitError.getKind() == Kind.HTTP) {
Response response = retrofitError.getResponse();
return (response != null && response.getStatus() != HttpStatus.BAD_REQUEST.value());
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package com.netflix.spinnaker.echo.pipelinetriggers.eventhandlers

import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spinnaker.echo.artifacts.ArtifactInfoService
import com.netflix.spinnaker.echo.build.BuildInfoService
import com.netflix.spinnaker.echo.jackson.EchoObjectMapper
Expand All @@ -27,8 +26,11 @@ import com.netflix.spinnaker.echo.model.trigger.ManualEvent
import com.netflix.spinnaker.echo.pipelinetriggers.PipelineCache
import com.netflix.spinnaker.echo.test.RetrofitStubs
import com.netflix.spinnaker.kork.artifacts.model.Artifact
import retrofit.RetrofitError
import retrofit.client.Response
import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerHttpException
import okhttp3.MediaType
import okhttp3.ResponseBody
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory
import spock.lang.Specification
import spock.lang.Subject

Expand Down Expand Up @@ -109,9 +111,7 @@ class ManualEventHandlerSpec extends Specification implements RetrofitStubs {
List<Map<String, Object>> triggerArtifacts = [triggerArtifact]

when:
artifactInfoService.getArtifactByVersion("artifactory", "my-package", "v2.2.2") >> {
throw RetrofitError.httpError("http://foo", new Response("http://foo", 404, "not found", [], null), null, null)
}
artifactInfoService.getArtifactByVersion("artifactory", "my-package", "v2.2.2") >> { throw makeSpinnakerHttpException() }
List<Artifact> resolvedArtifacts = eventHandler.resolveArtifacts(triggerArtifacts)
Map<String, Object> firstArtifact = objectMapper.convertValue(resolvedArtifacts.first(), Map.class)
firstArtifact = firstArtifact.findAll { key, value -> value && key != "customKind"}
Expand All @@ -133,9 +133,7 @@ class ManualEventHandlerSpec extends Specification implements RetrofitStubs {
Trigger manualTrigger = Trigger.builder().type("manual").artifacts([triggerArtifact]).build()

when:
artifactInfoService.getArtifactByVersion("artifactory", "my-package", "v2.2.2") >> {
throw RetrofitError.httpError("http://foo", new Response("http://foo", 404, "not found", [], null), null, null)
}
artifactInfoService.getArtifactByVersion("artifactory", "my-package", "v2.2.2") >> { throw makeSpinnakerHttpException() }
def resolvedPipeline = eventHandler.buildTrigger(inputPipeline, manualTrigger)

then:
Expand All @@ -153,9 +151,7 @@ class ManualEventHandlerSpec extends Specification implements RetrofitStubs {
Trigger manualTrigger = Trigger.builder().type("manual").artifacts([triggerArtifact]).build()

when:
artifactInfoService.getArtifactByVersion("artifactory", "my-package", "v2.2.2") >> {
throw RetrofitError.httpError("http://foo", new Response("http://foo", 404, "not found", [], null), null, null)
}
artifactInfoService.getArtifactByVersion("artifactory", "my-package", "v2.2.2") >> { throw makeSpinnakerHttpException() }
def resolvedPipeline = eventHandler.buildTrigger(inputPipeline, manualTrigger)

then:
Expand Down Expand Up @@ -403,4 +399,21 @@ class ManualEventHandlerSpec extends Specification implements RetrofitStubs {
pipelines.first().name == pipelineName
pipelines.first().errorMessage == arbitraryException.toString()
}

SpinnakerHttpException makeSpinnakerHttpException(){
String url = "https://some-url";
retrofit2.Response retrofit2Response =
retrofit2.Response.error(
404,
ResponseBody.create(
"{ \"message\": \"arbitrary message\" }", MediaType.parse("application/json")));

Retrofit retrofit =
new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(JacksonConverterFactory.create())
.build();

new SpinnakerHttpException(retrofit2Response, retrofit);
}
}
3 changes: 0 additions & 3 deletions echo-scheduler/echo-scheduler.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ dependencies {
implementation project(':echo-model')
implementation project(':echo-pipelinetriggers')

implementation "com.squareup.retrofit:retrofit"
implementation "com.squareup.retrofit:converter-jackson"

implementation "com.netflix.spectator:spectator-api"
implementation "io.spinnaker.kork:kork-artifacts"
implementation "io.spinnaker.kork:kork-retrofit"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import java.util.concurrent.atomic.AtomicInteger

import static com.netflix.spinnaker.echo.model.trigger.BuildEvent.Result.BUILDING
import static java.net.HttpURLConnection.HTTP_UNAVAILABLE
import static retrofit.RetrofitError.httpError

trait RetrofitStubs {

Expand Down

0 comments on commit 68f095f

Please sign in to comment.