Skip to content

Commit

Permalink
#6: Fix error when loading project's existing Pull Requests
Browse files Browse the repository at this point in the history
When running a scan, the scanner engine loads existing Pull Request details, but fails to deserialise the response due to a mismatch between the returned data structure and the target model.

Changing the deserialisation method to parse the impacted date field to a Date object using a Date Formatter, and then convert the parsed date to the target model's `long` type resolves this issue.
  • Loading branch information
mc1arke authored Apr 8, 2019
1 parent c06c929 commit 2feecd2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
package com.github.mc1arke.sonarqube.plugin.scanner;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonObject;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.scanner.bootstrap.ScannerWsClient;
import org.sonar.scanner.protocol.GsonHelper;
import org.sonar.scanner.scan.branch.ProjectPullRequests;
import org.sonar.scanner.scan.branch.ProjectPullRequestsLoader;
import org.sonar.scanner.scan.branch.PullRequestInfo;
Expand All @@ -32,6 +36,8 @@

import java.io.IOException;
import java.io.Reader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -42,6 +48,7 @@
*/
public class CommunityProjectPullRequestsLoader implements ProjectPullRequestsLoader {

private static final Logger LOGGER = Loggers.get(CommunityProjectPullRequestsLoader.class);
private static final String PROJECT_PULL_REQUESTS_URL = "/api/project_pull_requests/list?project=";

private final ScannerWsClient scannerWsClient;
Expand All @@ -50,7 +57,24 @@ public class CommunityProjectPullRequestsLoader implements ProjectPullRequestsLo
public CommunityProjectPullRequestsLoader(ScannerWsClient scannerWsClient) {
super();
this.scannerWsClient = scannerWsClient;
this.gson = GsonHelper.create();
this.gson =
new GsonBuilder().registerTypeAdapter(PullRequestInfo.class, createPullRequestInfoJsonDeserialiser())
.create();
}

private static JsonDeserializer<PullRequestInfo> createPullRequestInfoJsonDeserialiser() {
return (jsonElement, type, jsonDeserializationContext) -> {
JsonObject jsonObject = jsonElement.getAsJsonObject();
long parsedDate = 0;
try {
parsedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.parse(jsonObject.get("analysisDate").getAsString()).getTime();
} catch (ParseException e) {
LOGGER.warn("Could not parse date from Pull Requests API response. Will use '0' date", e);
}
return new PullRequestInfo(jsonObject.get("key").getAsString(), jsonObject.get("branch").getAsString(),
jsonObject.get("base").getAsString(), parsedDate);
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand All @@ -58,7 +59,7 @@ public ExpectedException expectedException() {
}

@Test
public void testEmptyBranchesOnEmptyServerResponse() {
public void testEmptyPullRequestsOnEmptyServerResponse() {
WsResponse mockResponse = mock(WsResponse.class);
when(scannerWsClient.call(any())).thenReturn(mockResponse);

Expand All @@ -73,30 +74,46 @@ public void testEmptyBranchesOnEmptyServerResponse() {
}

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

List<PullRequestInfo> infos = new ArrayList<>();
for (int i = 0; i < 10; i++) {
infos.add(new PullRequestInfo("key" + i, "branch" + i, "base" + i, i));
}
StringReader stringReader = new StringReader(
"{\"pullRequests\":[{\"key\":\"101\",\"title\":\"dummybranch\",\"branch\":\"dummybranch\",\"base\":\"master\",\"status\":{\"qualityGateStatus\":\"OK\",\"bugs\":0,\"vulnerabilities\":0,\"codeSmells\":0},\"analysisDate\":\"2019-04-04T19:44:27+0100\"}]}");
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(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse("2019-04-04T19:44:27+0100").getTime(),
responseInfo.getAnalysisDate());
assertEquals("master", responseInfo.getBase());
assertEquals("dummybranch", responseInfo.getBranch());
assertEquals("101", responseInfo.getKey());
}

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

StringReader stringReader = new StringReader(
GsonHelper.create().toJson(new CommunityProjectPullRequestsLoader.PullRequestsResponse(infos)));
"{\"pullRequests\":[{\"key\":\"101\",\"title\":\"dummybranch\",\"branch\":\"dummybranch\",\"base\":\"master\",\"status\":{\"qualityGateStatus\":\"OK\",\"bugs\":0,\"vulnerabilities\":0,\"codeSmells\":0},\"analysisDate\":\"\"}]}");
when(mockResponse.contentReader()).thenReturn(stringReader);

CommunityProjectPullRequestsLoader testCase = new CommunityProjectPullRequestsLoader(scannerWsClient);
ProjectPullRequests response = testCase.load("key");
assertFalse(response.isEmpty());
for (PullRequestInfo info : infos) {
PullRequestInfo responseInfo = response.get(info.getBranch());
assertNotNull(responseInfo);
assertEquals(info.getAnalysisDate(), responseInfo.getAnalysisDate());
assertEquals(info.getBase(), responseInfo.getBase());
assertEquals(info.getBranch(), responseInfo.getBranch());
assertEquals(info.getKey(), responseInfo.getKey());
}

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

@Test
Expand Down

0 comments on commit 2feecd2

Please sign in to comment.