Skip to content

Commit

Permalink
Handle 422 response
Browse files Browse the repository at this point in the history
Issue gh-32
  • Loading branch information
sjohnr committed Apr 17, 2024
1 parent b18ea1d commit 5a50fa5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
16 changes: 15 additions & 1 deletion api/github/src/main/java/com/github/api/GitHubApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions api/github/src/test/java/com/github/api/GitHubApiTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
@@ -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"}
3 changes: 2 additions & 1 deletion api/sagan/src/test/java/io/spring/api/SaganApiTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 5a50fa5

Please sign in to comment.