Skip to content

Commit

Permalink
URL encode issues/search parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
sophio-japharidze-sonarsource committed Nov 6, 2023
1 parent c11169c commit 4bd7040
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@
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;

@Named
@Singleton
Expand Down Expand Up @@ -208,7 +210,14 @@ public ShowIssueQuery(String serverUrl, String projectKey, String issueKey, Stri
}

public boolean isValid() {
return isNotBlank(serverUrl) && isNotBlank(projectKey) && isNotBlank(issueKey) && isNotBlank(branch);
return isNotBlank(serverUrl) && isNotBlank(projectKey) && isNotBlank(issueKey) && isNotBlank(branch) && isPullRequestParamValid();
}

public boolean isPullRequestParamValid() {
if (pullRequest != null) {
return isNotEmpty(pullRequest);
}
return true;
}

public String getServerUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void should_validate_issue_query() {
assertThat(new ShowIssueRequestHandler.ShowIssueQuery("serverUrl", "", "issue", "branch", "pullRequest").isValid()).isFalse();
assertThat(new ShowIssueRequestHandler.ShowIssueQuery("serverUrl", "project", "", "branch", "pullRequest").isValid()).isFalse();
assertThat(new ShowIssueRequestHandler.ShowIssueQuery("serverUrl", "project", "issue", "", "").isValid()).isFalse();
assertThat(new ShowIssueRequestHandler.ShowIssueQuery("serverUrl", "project", "issue", "branch", "").isValid()).isTrue();
assertThat(new ShowIssueRequestHandler.ShowIssueQuery("serverUrl", "project", "issue", "branch", null).isValid()).isTrue();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ public CompletableFuture<Issue> searchByKey(String issueKey) {
public Optional<ServerIssueDetails> fetchServerIssue(String issueKey, String projectKey, String branch, @Nullable String pullRequest) {
String searchUrl = "/api/issues/search.protobuf?issues=" + urlEncode(issueKey) + "&componentKeys=" + projectKey + "&ps=1&p=1";
if (pullRequest != null && !pullRequest.isEmpty()) {
searchUrl = searchUrl.concat("&pullRequest=").concat(pullRequest);
searchUrl = searchUrl.concat("&pullRequest=").concat(urlEncode(pullRequest));
} else if (!branch.isEmpty()) {
// If we do have a pullRequest, no need to pass branch too
searchUrl = searchUrl.concat("&branch=").concat(branch);
searchUrl = searchUrl.concat("&branch=").concat(urlEncode(branch));
}

try (var wsResponse = serverApiHelper.get(searchUrl); var is = wsResponse.bodyAsStream()) {
Expand Down

0 comments on commit 4bd7040

Please sign in to comment.