1
0
mirror of https://github.com/mc1arke/sonarqube-community-branch-plugin.git synced 2024-11-28 08:58:55 +02:00

#298: Prevent NullPointerException when branch with cancelled 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.
This commit is contained in:
jgrant216 2021-03-06 16:00:58 -05:00 committed by GitHub
parent 2abee0917a
commit 9d1dac3657
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View File

@ -70,8 +70,13 @@ public class CommunityProjectPullRequestsLoader implements ProjectPullRequestsLo
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);
}

View File

@ -138,6 +138,28 @@ public class CommunityProjectPullRequestsLoaderTest {
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);