From 5a50fa5f9e7cddbf4f6ae96c0295783ac6e8003c Mon Sep 17 00:00:00 2001 From: Steve Riesenberg <5248162+sjohnr@users.noreply.github.com> Date: Wed, 17 Apr 2024 15:51:48 -0500 Subject: [PATCH] Handle 422 response Issue gh-32 --- .../main/java/com/github/api/GitHubApi.java | 16 ++++++++++++++- .../java/com/github/api/GitHubApiTests.java | 20 +++++++++++++++++++ .../CreateMilestoneErrorResponse.json | 1 + .../java/io/spring/api/SaganApiTests.java | 3 ++- 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 api/github/src/test/resources/CreateMilestoneErrorResponse.json diff --git a/api/github/src/main/java/com/github/api/GitHubApi.java b/api/github/src/main/java/com/github/api/GitHubApi.java index e0cc03b..082752b 100644 --- a/api/github/src/main/java/com/github/api/GitHubApi.java +++ b/api/github/src/main/java/com/github/api/GitHubApi.java @@ -25,6 +25,7 @@ import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.logging.Logger; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonProcessingException; @@ -37,6 +38,8 @@ */ public class GitHubApi { + private static final Logger LOGGER = Logger.getLogger(GitHubApi.class.getName()); + private final HttpClient httpClient; private final ObjectMapper objectMapper; @@ -110,7 +113,18 @@ public void createMilestone(Repository repository, Milestone milestone) { .POST(bodyValue(milestone)) .build(); // @formatter:on - performRequest(httpRequest, Void.class); + try { + performRequest(httpRequest, Void.class); + } + catch (HttpClientException ex) { + if (ex.getStatusCode() == 422) { + LOGGER.warning("Unable to create milestone %s: response=%s".formatted(milestone.title(), + ex.getResponseBody())); + } + else { + throw ex; + } + } } /** diff --git a/api/github/src/test/java/com/github/api/GitHubApiTests.java b/api/github/src/test/java/com/github/api/GitHubApiTests.java index d879182..aaefbb2 100644 --- a/api/github/src/test/java/com/github/api/GitHubApiTests.java +++ b/api/github/src/test/java/com/github/api/GitHubApiTests.java @@ -132,6 +132,26 @@ public void createMilestoneWhenValidParametersThenSuccess() throws Exception { json.assertThat("$.due_on", is("2022-05-04T12:00:00Z")); } + @Test + public void createMilestoneWhenAlreadyExistsThenSuccessWithWarning() throws Exception { + this.server.enqueue(json("CreateMilestoneErrorResponse.json").setResponseCode(422)); + + var dueOn = Instant.parse("2022-05-04T12:00:00Z"); + var milestone = new Milestone("1.0.0", null, dueOn); + this.githubApi.createMilestone(this.repository, milestone); + + var recordedRequest = this.server.takeRequest(); + assertThat(recordedRequest.getMethod()).isEqualTo("POST"); + assertThat(recordedRequest.getPath()).isEqualTo("/repos/spring-projects/spring-security/milestones"); + assertThat(recordedRequest.getHeader("Accept")).isEqualTo("application/json"); + assertThat(recordedRequest.getHeader("Content-Type")).isEqualTo("application/json"); + assertThat(recordedRequest.getHeader("Authorization")).isEqualTo("Bearer %s".formatted(AUTH_TOKEN)); + + var json = JsonAssert.with(recordedRequest.getBody().readString(Charset.defaultCharset())); + json.assertThat("$.title", is("1.0.0")); + json.assertThat("$.due_on", is("2022-05-04T12:00:00Z")); + } + @Test public void getMilestonesWhenExistsThenSuccess() throws Exception { this.server.enqueue(json("MilestonesResponse.json")); diff --git a/api/github/src/test/resources/CreateMilestoneErrorResponse.json b/api/github/src/test/resources/CreateMilestoneErrorResponse.json new file mode 100644 index 0000000..524c00e --- /dev/null +++ b/api/github/src/test/resources/CreateMilestoneErrorResponse.json @@ -0,0 +1 @@ +{"message":"Validation Failed","errors":[{"resource":"Milestone","code":"already_exists","field":"title"}],"documentation_url":"https://docs.github.com/rest/issues/milestones#create-a-milestone"} \ No newline at end of file diff --git a/api/sagan/src/test/java/io/spring/api/SaganApiTests.java b/api/sagan/src/test/java/io/spring/api/SaganApiTests.java index 6a2fbcb..5f9a247 100644 --- a/api/sagan/src/test/java/io/spring/api/SaganApiTests.java +++ b/api/sagan/src/test/java/io/spring/api/SaganApiTests.java @@ -159,7 +159,8 @@ public void createReleaseWhenValidParametersThenSuccess() throws Exception { var json = JsonAssert.with(recordedRequest.getBody().readString(Charset.defaultCharset())); json.assertThat("$.version", is("6.1.0")); - json.assertThat("$.referenceDocUrl", is("https://docs.spring.io/spring-security/reference/{version}/index.html")); + json.assertThat("$.referenceDocUrl", + is("https://docs.spring.io/spring-security/reference/{version}/index.html")); json.assertThat("$.apiDocUrl", is("https://docs.spring.io/spring-security/site/docs/{version}/api/")); json.assertThat("$.status", is("GENERAL_AVAILABILITY")); json.assertThat("$.current", is(true));