Skip to content

Commit

Permalink
SLCORE-613 pass PR or branch param to the sources request
Browse files Browse the repository at this point in the history
  • Loading branch information
sophio-japharidze-sonarsource committed Nov 6, 2023
1 parent 4bd7040 commit ef22f06
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import org.sonarsource.sonarlint.core.serverapi.rules.RulesApi;
import org.sonarsource.sonarlint.core.telemetry.TelemetryServiceImpl;

import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

Expand Down Expand Up @@ -127,19 +126,19 @@ private void showIssueForConnection(String connectionId, String projectKey, Stri
private void showIssueForScope(String connectionId, String configScopeId, String issueKey, String projectKey, String branch, String pullRequest) {
tryFetchIssue(connectionId, issueKey, projectKey, branch, pullRequest)
.ifPresentOrElse(
issueDetails -> client.showIssue(getShowIssueParams(issueDetails, connectionId, configScopeId)),
issueDetails -> client.showIssue(getShowIssueParams(issueDetails, connectionId, configScopeId, branch, pullRequest)),
() -> client.showMessage(new ShowMessageParams(MessageType.ERROR, "Could not show the issue. See logs for more details")));
}

@VisibleForTesting
ShowIssueParams getShowIssueParams(IssueApi.ServerIssueDetails issueDetails, String connectionId, String configScopeId) {
ShowIssueParams getShowIssueParams(IssueApi.ServerIssueDetails issueDetails, String connectionId, String configScopeId, String branch, String pullRequest) {
var flowLocations = issueDetails.flowList.stream().map(flow -> {
var locations = flow.getLocationsList().stream().map(location -> {
var locationComponent =
issueDetails.componentsList.stream().filter(component -> component.getKey().equals(location.getComponent())).findFirst();
var filePath = locationComponent.map(Issues.Component::getPath).orElse("");
var locationTextRange = location.getTextRange();
var codeSnippet = tryFetchCodeSnippet(connectionId, locationComponent.map(Issues.Component::getKey).orElse(""), locationTextRange);
var codeSnippet = tryFetchCodeSnippet(connectionId, locationComponent.map(Issues.Component::getKey).orElse(""), locationTextRange, branch, pullRequest);
var locationTextRangeDto = new TextRangeDto(locationTextRange.getStartLine(), locationTextRange.getStartOffset(),
locationTextRange.getEndLine(), locationTextRange.getEndOffset());
return new LocationDto(locationTextRangeDto, location.getMsg(), filePath, codeSnippet.orElse(""));
Expand Down Expand Up @@ -170,13 +169,13 @@ private Optional<IssueApi.ServerIssueDetails> tryFetchIssue(String connectionId,
return serverApi.get().issue().fetchServerIssue(issueKey, projectKey, branch, pullRequest);
}

private Optional<String> tryFetchCodeSnippet(String connectionId, String fileKey, Common.TextRange textRange) {
private Optional<String> tryFetchCodeSnippet(String connectionId, String fileKey, Common.TextRange textRange, String branch, String pullRequest) {
var serverApi = serverApiProvider.getServerApi(connectionId);
if (serverApi.isEmpty() || fileKey.isEmpty()) {
// should not happen since we found the connection just before, improve the design ?
return Optional.empty();
}
return serverApi.get().issue().getCodeSnippet(fileKey, textRange);
return serverApi.get().issue().getCodeSnippet(fileKey, textRange, branch, pullRequest);
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void should_transform_ServerIssueDetail_to_ShowIssueParams() {

when(serverApiProvider.getServerApi(any())).thenReturn(Optional.of(serverApi));
when(serverApi.issue()).thenReturn(issueApi);
when(issueApi.getCodeSnippet(eq(locationComponentKey_1), any())).thenReturn(Optional.of(locationCodeSnippet_1));
when(issueApi.getCodeSnippet(eq(locationComponentKey_1), any(), any(), any())).thenReturn(Optional.of(locationCodeSnippet_1));

var showIssueRequestHandler = new ShowIssueRequestHandler(sonarLintClient, repository, configurationService,
bindingSuggestionProvider, serverApiProvider, telemetryService);
Expand All @@ -100,7 +100,7 @@ void should_transform_ServerIssueDetail_to_ShowIssueParams() {
);
var serverIssueDetails = new IssueApi.ServerIssueDetails(issue, issuePath, components, codeSnippet);

var showIssueParams = showIssueRequestHandler.getShowIssueParams(serverIssueDetails, connectionId, configScopeId);
var showIssueParams = showIssueRequestHandler.getShowIssueParams(serverIssueDetails, connectionId, configScopeId, "branch", "");
assertThat(showIssueParams.getConfigScopeId()).isEqualTo(configScopeId);
assertThat(showIssueParams.getIssueKey()).isEqualTo(issueKey);
assertThat(showIssueParams.getCreationDate()).isEqualTo(issueCreationDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,12 @@ private void registerIssueAnticipateTransitionResponses() {
}

private void registerSourceApiResponses() {
sourceFileByComponentKey
.forEach((componentKey, sourceFile) -> mockServer.stubFor(get("/api/sources/raw?key=" + urlEncode(componentKey) + "&branch=branchName").willReturn(aResponse().withBody(sourceFile.code))));

sourceFileByComponentKey
.forEach((componentKey, sourceFile) -> mockServer.stubFor(get("/api/sources/raw?key=" + urlEncode(componentKey) + "&pullRequest=pullRequest").willReturn(aResponse().withBody(sourceFile.code))));

sourceFileByComponentKey
.forEach((componentKey, sourceFile) -> mockServer.stubFor(get("/api/sources/raw?key=" + urlEncode(componentKey)).willReturn(aResponse().withBody(sourceFile.code))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void it_should_open_an_issue_in_ide() throws Exception {
.withEmbeddedServer()
.build(fakeClient);

var statusCode = executeOpenIssueRequest(ISSUE_KEY, PROJECT_KEY, BRANCH_NAME, "");
var statusCode = executeOpenIssueRequest(ISSUE_KEY, PROJECT_KEY, BRANCH_NAME);

assertThat(statusCode).isEqualTo(200);
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> assertThat(fakeClient.getIssueParamsToShowByIssueKey()).containsOnlyKeys(issueKey));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public Optional<ServerIssueDetails> fetchServerIssue(String issueKey, String pro
}

var fileKey = issue.getComponent();
var codeSnippet = getCodeSnippet(fileKey, issue.getTextRange());
var codeSnippet = getCodeSnippet(fileKey, issue.getTextRange(), branch, pullRequest);

return Optional.of(new ServerIssueDetails(issue, optionalComponentWithPath.get().getPath(), response.getComponentsList(), codeSnippet.orElse("")));
} catch (Exception e) {
Expand All @@ -296,8 +296,8 @@ public Optional<ServerIssueDetails> fetchServerIssue(String issueKey, String pro
}
}

public Optional<String> getCodeSnippet(String fileKey, Common.TextRange textRange) {
var source = new SourceApi(serverApiHelper).getRawSourceCode(fileKey);
public Optional<String> getCodeSnippet(String fileKey, Common.TextRange textRange, String branch, @Nullable String pullRequest) {
var source = new SourceApi(serverApiHelper).getRawSourceCodeForBranchAndPullRequest(fileKey, branch, pullRequest);
if (source.isPresent()) {
try {
var codeSnippet = ServerApiUtils.extractCodeSnippet(source.get(), textRange);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
package org.sonarsource.sonarlint.core.serverapi.source;

import java.util.Optional;
import javax.annotation.Nullable;
import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger;
import org.sonarsource.sonarlint.core.serverapi.ServerApiHelper;
import org.sonarsource.sonarlint.core.serverapi.UrlUtils;

import static org.sonarsource.sonarlint.core.serverapi.UrlUtils.urlEncode;

public class SourceApi {

private static final SonarLintLogger LOG = SonarLintLogger.get();
Expand All @@ -41,7 +44,22 @@ public SourceApi(ServerApiHelper serverApiHelper) {
* @param key project key, or file key.
*/
public Optional<String> getRawSourceCode(String fileKey) {
try (var r = serverApiHelper.get("/api/sources/raw?key=" + UrlUtils.urlEncode(fileKey))) {
try (var r = serverApiHelper.get("/api/sources/raw?key=" + urlEncode(fileKey))) {
return Optional.of(r.bodyAsString());
} catch (Exception e) {
LOG.debug("Unable to fetch source code of '" + fileKey + "'", e);
return Optional.empty();
}
}

public Optional<String> getRawSourceCodeForBranchAndPullRequest(String fileKey, String branch, @Nullable String pullRequest) {
var url = "/api/sources/raw?key=" + urlEncode(fileKey);
if (pullRequest != null && !pullRequest.isEmpty()) {
url = url.concat("&pullRequest=").concat(urlEncode(pullRequest));
} else if (!branch.isEmpty()) {
url = url.concat("&branch=").concat(urlEncode(branch));
}
try (var r = serverApiHelper.get(url)) {
return Optional.of(r.bodyAsString());
} catch (Exception e) {
LOG.debug("Unable to fetch source code of '" + fileKey + "'", e);
Expand Down

0 comments on commit ef22f06

Please sign in to comment.