Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the cache web api call #1456

Merged
merged 3 commits into from
Jan 26, 2023

Conversation

costin-zaharia-sonarsource
Copy link
Member

Fixes #1454

@costin-zaharia-sonarsource costin-zaharia-sonarsource force-pushed the costin/update-cache-web-api branch 2 times, most recently from f2b6b34 to 1aaabce Compare January 24, 2023 08:34
@costin-zaharia-sonarsource costin-zaharia-sonarsource force-pushed the costin/update-cache-web-api branch 3 times, most recently from f163970 to ad740c7 Compare January 24, 2023 13:44
@@ -1169,7 +1182,7 @@ public async Task GetProperties_RequestUrl(string hostUrl, string versionUrl, st
private static Stream CreateCacheStream(IMessage message)
{
var stream = new MemoryStream();
message.WriteTo(stream);
message.WriteDelimitedTo(stream);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an important change that's well hidden.

The stream we receive from the server contains a sequence of objects (not a single one), and this sequence does not have a parent. In order to correctly emulate the serialization/deserialization and make it work properly, we have to use WriteDelimitedTo instead of WriteTo.

// - add it to your SonarQube
// - start SonarQube and analyze `IncrementalPRAnalysis` project with `/d:sonar.scanner.keepReport=true`
// - archive the content of `.sonarqube\out\.sonar\scanner-report\` (only the content, without parent folder) as `scanner-report.zip`
File reportZip = projectDir.resolve("scanner-report.zip").toFile();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous strategy was to upload a full analysis result that included a cache. When this was implemented on the scanner, the analyzers did not have the cache feature implemented so we missed a component that could populate the cache.

Now that we have the analyzers updated we can just run 2 analyses, the first one is full and it will populate the cache, and the second one is for a PR, and is downloading the cache.

@@ -67,7 +68,7 @@ public async Task Execute()
else
{
logger.LogInfo(Resources.MSG_Processing_PullRequest_Branch, baseBranch);
if (await server.DownloadCache(localSettings.ProjectKey, baseBranch) is { } cache)
if (await server.DownloadCache(localSettings.ProjectKey, baseBranch) is { Count: > 0 } cache)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we return an empty collection (empty cache) instead of null.

message AnalysisCacheMsg {
map<string, bytes> map = 1;

message SensorCacheEntry {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main contract change. Instead of one AnalysisCacheMsg instance, we receive a stream of SensorCacheEntry entries.

{
while (dataStream.Position < dataStream.Length)
{
cacheEntries.Add(SensorCacheEntry.Parser.ParseDelimitedFrom(dataStream));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is a bit tricky. Just by looking at the stream, we don't know how many objects there are and neither their type. We have to rely on the information we get from SonarQube team. If at any point they change the format without letting us know, the parser will try to match whatever it finds in the stream to the current data structure, leading to wrong data without raising an exception.

@costin-zaharia-sonarsource costin-zaharia-sonarsource marked this pull request as ready for review January 24, 2023 14:01
@costin-zaharia-sonarsource costin-zaharia-sonarsource force-pushed the costin/update-cache-web-api branch 6 times, most recently from 47e7e3a to 636be4f Compare January 25, 2023 09:41
Copy link
Contributor

@csaba-sagi-sonarsource csaba-sagi-sonarsource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@costin-zaharia-sonarsource costin-zaharia-sonarsource merged commit b22df55 into master Jan 26, 2023
@costin-zaharia-sonarsource costin-zaharia-sonarsource deleted the costin/update-cache-web-api branch January 26, 2023 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incremental analysis is broken due to the latest changes on SonarQube
2 participants