Skip to content

Commit

Permalink
#47: Fix NPE when branch is orphaned
Browse files Browse the repository at this point in the history
Where a Pull Request no longer has a target branch - such as happens when the branch is deleted from SonarQube after the Pull Request is analysed - the `base` field in the branch information will be `null`. To prevent a `NullPointerException` being thrown in this scenario, an explicit check is being added for this field being `null` before attempting to extract the text value from it.
  • Loading branch information
bjrke authored and mc1arke committed Nov 19, 2019
1 parent e1e16dd commit 819638c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.log.Logger;
Expand All @@ -40,6 +41,7 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* Loads the Pull Requests currently known by SonarQube from the server component for client applications.
Expand Down Expand Up @@ -72,8 +74,9 @@ private static JsonDeserializer<PullRequestInfo> createPullRequestInfoJsonDeseri
} catch (ParseException e) {
LOGGER.warn("Could not parse date from Pull Requests API response. Will use '0' date", e);
}
final String base = Optional.ofNullable(jsonObject.get("base")).map(JsonElement::getAsString).orElse(null);
return new PullRequestInfo(jsonObject.get("key").getAsString(), jsonObject.get("branch").getAsString(),
jsonObject.get("base").getAsString(), parsedDate);
base, parsedDate);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -116,6 +117,27 @@ public void testAllPullRequestsFromNonEmptyServerResponseWithInvalidDate() {
assertEquals("101", responseInfo.getKey());
}

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

StringReader stringReader = new StringReader(
"{\"pullRequests\":[{\"key\":\"101\",\"title\":\"dummybranch\",\"branch\":\"dummybranch\",\"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());

PullRequestInfo responseInfo = response.get("dummybranch");
assertNotNull(responseInfo);
assertEquals(0, responseInfo.getAnalysisDate());
assertNull(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 819638c

Please sign in to comment.