Skip to content

Commit

Permalink
mc1arke#298: Prevent NullPointerException when branch with cancelled …
Browse files Browse the repository at this point in the history
…analysis is rescanned

When a background task for a pull request analysis is cancelled, a subsequent attempt to re-scan the pull request fails in the scanner since the `analysisDate` field is no longer present against the pull request. This change handles this missing date in a better manner, defaulting to having a `0` analysis date in the same way an error parsing the analysis date would.
  • Loading branch information
jgrant216 authored and Damian Galka committed Feb 16, 2022
1 parent ae3ded4 commit 0254395
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ private static JsonDeserializer<PullRequestInfo> createPullRequestInfoJsonDeseri
JsonObject jsonObject = jsonElement.getAsJsonObject();
long parsedDate = 0;
try {
parsedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.parse(jsonObject.get("analysisDate").getAsString()).getTime();
String analysisDate = Optional.ofNullable(jsonObject.get("analysisDate")).map(JsonElement::getAsString).orElse(null);
if(analysisDate == null) {
LOGGER.warn("Analysis Date not provided in Pull Requests API response. Will use '0' date");
} else {
parsedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.parse(analysisDate).getTime();
}
} catch (ParseException e) {
LOGGER.warn("Could not parse date from Pull Requests API response. Will use '0' date", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ public void testAllPullRequestsFromNonEmptyServerResponseWithoutBase() {
assertEquals("101", responseInfo.getKey());
}

@Test
public void testAllPullRequestsFromNonEmptyServerResponseWithoutAnalysisDateAndQualityGateStatusForCancelledPr() throws ParseException {
WsResponse mockResponse = mock(WsResponse.class);
when(scannerWsClient.call(any())).thenReturn(mockResponse);

StringReader stringReader = new StringReader(
"{\"pullRequests\":[{\"key\":\"101\",\"title\":\"dummybranch\",\"branch\":\"dummybranch\",\"base\":\"master\",\"status\":{\"bugs\":0,\"vulnerabilities\":0,\"codeSmells\":0}}]}");
when(mockResponse.contentReader()).thenReturn(stringReader);

CommunityProjectPullRequestsLoader testCase = new CommunityProjectPullRequestsLoader(scannerWsClient);
ProjectPullRequests response = testCase.load("key");
assertFalse(response.isEmpty());

PullRequestInfo responseInfo = response.get("dummybranch");
assertNotNull(responseInfo);
assertEquals(0,
responseInfo.getAnalysisDate());
assertEquals("master", responseInfo.getBase());
assertEquals("dummybranch", responseInfo.getBranch());
assertEquals("101", responseInfo.getKey());
}

@Test
public void testMessageExceptionOnIOException() {
WsResponse mockResponse = mock(WsResponse.class);
Expand Down

0 comments on commit 0254395

Please sign in to comment.