Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(retrofit2): upgrade code to use retrofit2 #1313

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fiatVersion=1.53.0
fiatVersion=1.54.0
korkVersion=7.251.0
org.gradle.parallel=true
spinnakerGradleVersion=8.32.1
Expand Down
6 changes: 4 additions & 2 deletions igor-core/igor-core.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ dependencies {
implementation "io.spinnaker.fiat:fiat-core:$fiatVersion"
implementation "javax.validation:validation-api"

// TODO(rz): Get rid of this dependency!
implementation "com.squareup.retrofit:retrofit"
implementation "com.squareup.retrofit2:retrofit"
implementation "com.squareup.retrofit2:converter-jackson"

testImplementation "com.netflix.spectator:spectator-reg-micrometer"
testImplementation "io.spinnaker.kork:kork-retrofit"
testImplementation "com.squareup.okhttp3:mockwebserver"
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
package com.netflix.spinnaker.igor.history;

import com.netflix.spinnaker.igor.history.model.Event;
import retrofit.http.Body;
import retrofit.http.POST;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

/** Posts new build executions to echo */
public interface EchoService {
@POST("/")
public abstract String postEvent(@Body Event event);
@POST(".")
Call<String> postEvent(@Body Event event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
package com.netflix.spinnaker.igor.keel;

import java.util.Map;
import retrofit.http.Body;
import retrofit.http.POST;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

public interface KeelService {
/**
* Events should be sent with this format (inherited from echo events): [ payload: [artifacts:
* List<Artifact>, details: Map], eventName: String ]
*/
@POST("/artifacts/events")
Void sendArtifactEvent(@Body Map event);
@POST("artifacts/events")
Call<Void> sendArtifactEvent(@Body Map event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2025 OpsMx, 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.igor.util;

import okhttp3.HttpUrl;

public class RetrofitUtils {

/**
* Converts a given URL to a valid base URL for use in a {@link retrofit2.Retrofit} instance. If
* the URL is invalid, an {@link IllegalArgumentException} is thrown. If the URL does not end with
* a slash, a slash is appended to the end of the URL.
*
* @param suppliedBaseUrl the URL to convert
* @return a valid base URL for use in a Retrofit instance
*/
public static String getBaseUrl(String suppliedBaseUrl) {
HttpUrl parsedUrl = HttpUrl.parse(suppliedBaseUrl);
if (parsedUrl == null) {
throw new IllegalArgumentException("Invalid URL: " + suppliedBaseUrl);
}
String baseUrl = parsedUrl.newBuilder().build().toString();
if (!baseUrl.endsWith("/")) {
baseUrl += "/";
}
return baseUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright 2025 The Home Depot, 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.igor.util;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.netflix.spinnaker.kork.retrofit.Retrofit2SyncCall;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

public class RetrofitUtilsTest {
MockWebServer server;
RetrofitService service;
String baseUrl = "http://localhost/v1";

@BeforeEach
void setup() throws IOException {
service =
new Retrofit.Builder()
.baseUrl(RetrofitUtils.getBaseUrl(baseUrl))
.client(new OkHttpClient())
.build()
.create(RetrofitService.class);
server = new MockWebServer();
server.start(0);
}

@AfterEach
void teardown() throws IOException {
server.close();
}

@Test
void urlWithoutTrailingSlashFailsWithoutUtilsFunc() {
assertThrows(
IllegalArgumentException.class,
() -> {
new Retrofit.Builder().baseUrl(baseUrl).build().create(RetrofitService.class);
});
}

@Test
void addsTrailingSlashIfMissing() {
assertDoesNotThrow(
() -> {
new Retrofit.Builder()
.baseUrl(RetrofitUtils.getBaseUrl(baseUrl))
.build()
.create(RetrofitService.class);
});
}

@Test
void testGetRoot() {
server.enqueue(new MockResponse().setResponseCode(200));
Response resp = Retrofit2SyncCall.executeCall(service.getRoot());

// @GET("/") uses server root
assertThat(resp.raw().request().url().toString()).isEqualTo(baseUrl.replace("v1", ""));
}

@Test
void testGetBaseUrl() {
server.enqueue(new MockResponse().setResponseCode(200));
Response resp = Retrofit2SyncCall.executeCall(service.getBaseUrl());

// @GET(".") uses base url ending in slash
assertThat(resp.raw().request().url().toString()).isEqualTo(baseUrl + "/");
}

@Test
void testGetResource() {
server.enqueue(new MockResponse().setResponseCode(200));
Response resp = Retrofit2SyncCall.executeCall(service.getUsers());

// @GET("users") uses relative path /users of base url
assertThat(resp.raw().request().url().toString()).isEqualTo(baseUrl + "/users");
}

@Test
void testAbsolutePath() {
server.enqueue(new MockResponse().setResponseCode(200));
Response resp = Retrofit2SyncCall.executeCall(service.getV2());

// @GET("/v2") uses absolute path
assertThat(resp.raw().request().url().toString()).isEqualTo(baseUrl.replace("v1", "v2"));
}

private interface RetrofitService {
@GET("users")
Call<Void> getUsers();

@GET("/")
Call<Void> getRoot();

@GET(".")
Call<Void> getBaseUrl();

@GET("/v2")
Call<Void> getV2();
}
}
4 changes: 1 addition & 3 deletions igor-monitor-artifactory/igor-monitor-artifactory.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {

implementation "io.spinnaker.kork:kork-artifacts"
implementation "io.spinnaker.kork:kork-core"
implementation "io.spinnaker.kork:kork-retrofit"
implementation "io.spinnaker.kork:kork-security"
implementation "org.springframework.boot:spring-boot-starter-actuator"
implementation "org.springframework.boot:spring-boot-starter-web"
Expand All @@ -34,8 +35,5 @@ dependencies {
// TODO(rz): Get rid of this dependency!
implementation "net.logstash.logback:logstash-logback-encoder"

// TODO(rz): Get rid of this dependency!
implementation "com.squareup.retrofit:retrofit"

testImplementation "com.squareup.okhttp3:mockwebserver"
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
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.security.AuthenticatedRequest;
import java.io.IOException;
import java.time.Instant;
Expand Down Expand Up @@ -212,9 +213,11 @@ private void postEvent(Artifact artifact, String name) {
if (artifact != null) {
AuthenticatedRequest.allowAnonymous(
() ->
echoService
.get()
.postEvent(new ArtifactoryEvent(new ArtifactoryEvent.Content(name, artifact))));
Retrofit2SyncCall.execute(
echoService
.get()
.postEvent(
new ArtifactoryEvent(new ArtifactoryEvent.Content(name, artifact)))));
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion igor-monitor-plugins/igor-monitor-plugins.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ dependencies {
implementation "io.spinnaker.kork:kork-artifacts"
implementation "io.spinnaker.kork:kork-core"
implementation "io.spinnaker.kork:kork-jedis"
implementation "io.spinnaker.kork:kork-retrofit"
implementation "io.spinnaker.kork:kork-security"

implementation "org.springframework.boot:spring-boot-starter-actuator"

implementation "com.squareup.retrofit:retrofit"
implementation "com.squareup.retrofit2:retrofit"

testImplementation "com.squareup.retrofit2:retrofit-mock"
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.netflix.spinnaker.igor.polling.PollingDelta;
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.security.AuthenticatedRequest;
import java.time.Instant;
import java.util.Comparator;
Expand Down Expand Up @@ -112,7 +113,7 @@ private void postEvent(PluginRelease release) {
registry.counter(missedNotificationId.withTag("monitor", getName())).increment();
} else if (release != null) {
AuthenticatedRequest.allowAnonymous(
() -> echoService.get().postEvent(new PluginEvent(release)));
() -> Retrofit2SyncCall.execute(echoService.get().postEvent(new PluginEvent(release))));
log.debug("{} event posted", release);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package com.netflix.spinnaker.igor.plugins.front50;

import java.util.List;
import retrofit.http.GET;
import retrofit2.Call;
import retrofit2.http.GET;

public interface Front50Service {

@GET("/pluginInfo")
List<PluginInfo> listPluginInfo();
@GET("pluginInfo")
Call<List<PluginInfo>> listPluginInfo();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.netflix.spinnaker.igor.plugins.front50;

import com.netflix.spinnaker.igor.plugins.model.PluginRelease;
import com.netflix.spinnaker.kork.retrofit.Retrofit2SyncCall;
import com.netflix.spinnaker.security.AuthenticatedRequest;
import java.time.Instant;
import java.time.format.DateTimeParseException;
Expand Down Expand Up @@ -66,7 +67,7 @@ private Optional<Instant> parseReleaseTimestamp(PluginRelease release) {
private List<PluginRelease> getPluginReleases() {
return AuthenticatedRequest.allowAnonymous(
() ->
front50Service.listPluginInfo().stream()
Retrofit2SyncCall.execute(front50Service.listPluginInfo()).stream()
.flatMap(
info ->
info.releases.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.netflix.spinnaker.igor.plugins.front50

import retrofit2.mock.Calls
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Subject
Expand Down Expand Up @@ -57,7 +58,7 @@ class PluginReleaseServiceSpec extends Specification {

then:
result*.version == expectedVersions
1 * front50Service.listPluginInfo() >> [plugin1, plugin2]
1 * front50Service.listPluginInfo() >> Calls.response([plugin1, plugin2])

where:
timestamp || expectedVersions
Expand Down
6 changes: 3 additions & 3 deletions igor-monitor-travis/igor-monitor-travis.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ dependencies {
implementation "io.spinnaker.kork:kork-web"
implementation "io.spinnaker.kork:kork-retrofit"

implementation "com.squareup.retrofit2:retrofit"
implementation "com.squareup.retrofit2:converter-jackson"
implementation "com.fasterxml.jackson.core:jackson-databind"
implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-xml"
implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml"
implementation "com.jakewharton.retrofit:retrofit1-okhttp3-client"
implementation "com.squareup.retrofit:converter-jackson"
implementation "com.squareup.retrofit:retrofit"
implementation "io.reactivex:rxjava" // TODO(jervi): get rid of this dependency
implementation "javax.validation:validation-api"
implementation "javax.xml.bind:jaxb-api"

testImplementation "com.squareup.okhttp3:mockwebserver"
testImplementation "com.squareup.retrofit2:retrofit-mock"
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.netflix.spinnaker.igor.travis.service.TravisService;
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.security.AuthenticatedRequest;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -271,7 +272,8 @@ private void sendEventForBuild(
GenericBuildEvent event = new GenericBuildEvent();
event.setContent(content);

AuthenticatedRequest.allowAnonymous(() -> echoService.get().postEvent(event));
AuthenticatedRequest.allowAnonymous(
() -> Retrofit2SyncCall.execute(echoService.get().postEvent(event)));
} else {
log.warn("Cannot send build event notification: Echo is not configured");
log.info(
Expand Down
Loading
Loading