-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for LATEST_RELEASE (#182)
- Loading branch information
1 parent
617459b
commit 52f2245
Showing
7 changed files
with
229 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,3 @@ | |
package com.sonar.orchestrator.build.dotnet.scanner; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; | ||
|
25 changes: 25 additions & 0 deletions
25
sonar-orchestrator/src/main/java/com/sonar/orchestrator/locator/GitHub.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Orchestrator | ||
* Copyright (C) 2011-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
package com.sonar.orchestrator.locator; | ||
|
||
public interface GitHub { | ||
String getLatestScannerReleaseVersion(); | ||
} |
95 changes: 95 additions & 0 deletions
95
sonar-orchestrator/src/main/java/com/sonar/orchestrator/locator/GitHubImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Orchestrator | ||
* Copyright (C) 2011-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
package com.sonar.orchestrator.locator; | ||
|
||
import com.eclipsesource.json.Json; | ||
import com.eclipsesource.json.JsonValue; | ||
import com.sonar.orchestrator.http.HttpCall; | ||
import com.sonar.orchestrator.http.HttpClientFactory; | ||
import okhttp3.HttpUrl; | ||
import org.apache.commons.io.FileUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
|
||
public class GitHubImpl implements GitHub { | ||
private static final Logger LOG = LoggerFactory.getLogger(GitHubImpl.class); | ||
private static final Object cacheLock = new Object(); | ||
private static volatile String cachedVersion; | ||
private final String baseUrl; | ||
|
||
public GitHubImpl() { | ||
this("https://api.github.com"); | ||
} | ||
|
||
GitHubImpl(String baseUrl) { | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
@Override | ||
public synchronized String getLatestScannerReleaseVersion() { | ||
// GitHub API has a rate limit of 60 requests per hour for unauthenticated users. | ||
// To avoid reaching that limit when running integration tests, the returned value needs to be cached. | ||
synchronized (cacheLock) { | ||
if (cachedVersion != null) { | ||
return cachedVersion; | ||
} | ||
} | ||
LOG.info("Retrieving the latest scanner release."); | ||
HttpUrl url = HttpUrl | ||
.parse(baseUrl) | ||
.newBuilder() | ||
.addPathSegments("repos/SonarSource/sonar-scanner-msbuild/releases/latest") | ||
.build(); | ||
|
||
HttpCall call = HttpClientFactory | ||
.create() | ||
.newCall(url) | ||
// GitHub recommendation: https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#get-the-latest-release | ||
.setHeader("Accept","application/vnd.github+json") | ||
.setHeader("X-GitHub-Api-Version", "2022-11-28"); | ||
|
||
try { | ||
File jsonFile = new File("scanner-latest-release.json"); | ||
|
||
LOG.info("Downloading {}", url); | ||
call.downloadToFile(jsonFile); | ||
LOG.info("SonarScanner for .Net latest release details downloaded to {}", jsonFile); | ||
|
||
String jsonContent = FileUtils.readFileToString(jsonFile, StandardCharsets.UTF_8); | ||
JsonValue response = Json.parse(jsonContent); | ||
Files.delete(jsonFile.toPath()); | ||
cachedVersion = response.asObject().getString("tag_name", null); | ||
return cachedVersion; | ||
} catch (Exception exception) { | ||
throw new IllegalStateException("Fail to download the latest release details for SonarScanner for .Net", exception); | ||
} | ||
} | ||
|
||
public static synchronized void resetCache() { | ||
synchronized (cacheLock) { | ||
cachedVersion = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
sonar-orchestrator/src/test/java/com/sonar/orchestrator/locator/GitHubImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Orchestrator | ||
* Copyright (C) 2011-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package com.sonar.orchestrator.locator; | ||
|
||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import org.junit.After; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class GitHubImplTest { | ||
@Rule | ||
public MockWebServer server = new MockWebServer(); | ||
|
||
@Test | ||
public void getLatestScannerReleaseVersion_returns_valid_version() { | ||
prepareResponse("1.2.3.4"); | ||
|
||
GitHubImpl sut = new GitHubImpl(server.url("/").toString()); | ||
String latestVersion = sut.getLatestScannerReleaseVersion(); | ||
assertThat(latestVersion).isEqualTo("1.2.3.4"); | ||
} | ||
|
||
@Test | ||
public void getLatestScannerReleaseVersion_two_calls_make_a_single_request() { | ||
prepareResponse("version"); | ||
|
||
GitHubImpl sut = new GitHubImpl(server.url("/").toString()); | ||
String first = sut.getLatestScannerReleaseVersion(); | ||
assertThat(first).isEqualTo("version"); | ||
String second = sut.getLatestScannerReleaseVersion(); | ||
assertThat(second).isEqualTo("version"); | ||
assertThat(server.getRequestCount()).isEqualTo(1); | ||
} | ||
|
||
@After | ||
public void resetCache() { | ||
GitHubImpl.resetCache(); | ||
} | ||
|
||
private void prepareResponse(String version) { | ||
server.enqueue(new MockResponse().setBody("{ \"tag_name\": \"" + version + "\" }")); | ||
} | ||
} |