Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zglicz committed Dec 17, 2024
1 parent 5f4a453 commit 9a36b30
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.sonar.api.Startable;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.scanner.ScannerSide;
import org.sonar.plugins.javascript.bridge.protobuf.Node;
import org.sonar.plugins.javascript.nodejs.NodeCommand;
import org.sonarsource.api.sonarlint.SonarLintSide;

@ScannerSide
Expand Down Expand Up @@ -69,9 +69,7 @@ void initLinter(

TsConfigFile createTsConfigFile(String content) throws IOException;

TelemetryNodeResponse getTelemetry();

NodeCommand command();
TelemetryData getTelemetry();

record JsAnalysisRequest(
String filePath,
Expand Down Expand Up @@ -281,7 +279,9 @@ public String toString() {

record TsProgramRequest(String tsConfig) {}

record TelemetryNodeResponse(List<Dependency> dependencies) {}
record TelemetryEslintBridgeResponse(List<Dependency> dependencies) {}

record TelemetryData(List<Dependency> dependencies, Map<String, String> runtime) {}

record Dependency(String name, String version) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -534,20 +534,29 @@ public TsConfigFile createTsConfigFile(String content) {
}

@Override
public TelemetryNodeResponse getTelemetry() {
public TelemetryData getTelemetry() {
return new TelemetryData(
getTelemetryEslintBridgeResponse().dependencies(),
Map.of(
"version",
nodeCommand.getActualNodeVersion().toString(),
"major-version",
Integer.toString(nodeCommand.getActualNodeVersion().major()),
"node-executable-origin",
nodeCommand.getNodeExecutableOrigin()
)
);
}

private TelemetryEslintBridgeResponse getTelemetryEslintBridgeResponse() {
try {
var result = http.get(url("get-telemetry"));
return GSON.fromJson(result, TelemetryNodeResponse.class);
return GSON.fromJson(result, TelemetryEslintBridgeResponse.class);
} catch (IOException e) {
return new TelemetryNodeResponse(List.of());
return new TelemetryEslintBridgeResponse(List.of());
}
}

@Override
public NodeCommand command() {
return nodeCommand;
}

private static <T> List<T> emptyListIfNull(@Nullable List<T> list) {
return list == null ? emptyList() : list;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import org.sonar.plugins.javascript.bridge.BridgeServer.CssAnalysisRequest;
import org.sonar.plugins.javascript.bridge.BridgeServer.Dependency;
import org.sonar.plugins.javascript.bridge.BridgeServer.JsAnalysisRequest;
import org.sonar.plugins.javascript.bridge.BridgeServer.TelemetryNodeResponse;
import org.sonar.plugins.javascript.bridge.BridgeServer.TsProgram;
import org.sonar.plugins.javascript.bridge.BridgeServer.TsProgramRequest;
import org.sonar.plugins.javascript.bridge.protobuf.Node;
Expand Down Expand Up @@ -759,9 +758,10 @@ void should_return_telemetry() throws Exception {
bridgeServer = createBridgeServer(START_SERVER_SCRIPT);
bridgeServer.startServer(serverConfig, emptyList());
var telemetry = bridgeServer.getTelemetry();
assertThat(telemetry).isEqualTo(
new TelemetryNodeResponse(List.of(new Dependency("pkg1", "1.0.0")))
);
assertThat(telemetry.dependencies()).isEqualTo(List.of(new Dependency("pkg1", "1.0.0")));
assertThat(telemetry.runtime()).containsEntry("node-executable-origin", "host");
assertThat(telemetry.runtime()).containsKey("version");
assertThat(telemetry.runtime()).containsKey("major-version");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.sonar.plugins.javascript.analysis;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -50,32 +51,21 @@ void reportTelemetry() {
// https://github.com/SonarSource/sonar-plugin-api/releases/tag/10.9.0.2362
return;
}
var dependenciesResponse = server.getTelemetry();
var keyMapToSave = new HashMap<String, String>();
if (dependenciesResponse != null) {
keyMapToSave.putAll(
dependenciesResponse
.dependencies()
.stream()
.collect(
Collectors.toMap(
dependency -> DEPENDENCY_PREFIX + dependency.name(),
Dependency::version
)
)
);
}
keyMapToSave.put(
RUNTIME_PREFIX + "major-version",
Integer.toString(server.command().getActualNodeVersion().major())
);
keyMapToSave.put(
RUNTIME_PREFIX + "version",
server.command().getActualNodeVersion().toString()
var telemetry = server.getTelemetry();
var keyMapToSave = new HashMap<String, String>(
telemetry
.dependencies()
.stream()
.collect(
Collectors.toMap(dependency -> DEPENDENCY_PREFIX + dependency.name(), Dependency::version)
)
);
keyMapToSave.put(
RUNTIME_PREFIX + "node-executable-origin",
server.command().getNodeExecutableOrigin()
keyMapToSave.putAll(
telemetry
.runtime()
.entrySet()
.stream()
.collect(Collectors.toMap(key -> RUNTIME_PREFIX + key.getKey(), value -> value.getValue()))
);
keyMapToSave.forEach(ctx::addTelemetryProperty);
LOG.debug("Telemetry saved: {}", keyMapToSave);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
import org.sonar.plugins.javascript.bridge.BridgeServer.AnalysisResponse;
import org.sonar.plugins.javascript.bridge.BridgeServer.Dependency;
import org.sonar.plugins.javascript.bridge.BridgeServer.JsAnalysisRequest;
import org.sonar.plugins.javascript.bridge.BridgeServer.TelemetryNodeResponse;
import org.sonar.plugins.javascript.bridge.BridgeServer.TelemetryEslintBridgeResponse;
import org.sonar.plugins.javascript.bridge.BridgeServer.TsProgram;
import org.sonar.plugins.javascript.bridge.EslintRule;
import org.sonar.plugins.javascript.bridge.PluginInfo;
Expand Down Expand Up @@ -152,21 +152,6 @@ public void setUp() throws Exception {
);
tsProgram = new TsProgram("", new ArrayList<>(), List.of());
when(bridgeServerMock.createProgram(any())).thenReturn(tsProgram);
Consumer<String> noop = s -> {};
when(bridgeServerMock.command()).thenReturn(
new NodeCommand(
mock(ProcessWrapper.class),
"node",
Version.create(22, 9, 0),
Collections.emptyList(),
null,
Collections.emptyList(),
noop,
noop,
Map.of(),
"embedded"
)
);
context = SensorContextTester.create(baseDir);
context.fileSystem().setWorkDir(workDir);
context.setRuntime(
Expand Down Expand Up @@ -778,7 +763,10 @@ void log_debug_analyzed_filename() throws Exception {
void should_add_telemetry_for_scanner_analysis() throws Exception {
when(bridgeServerMock.analyzeJavaScript(any())).thenReturn(new AnalysisResponse());
when(bridgeServerMock.getTelemetry()).thenReturn(
new TelemetryNodeResponse(List.of(new Dependency("pkg1", "1.1.0")))
new BridgeServer.TelemetryData(
List.of(new Dependency("pkg1", "1.1.0")),
Map.of("version", "22.9", "major-version", "22", "node-executable-origin", "embedded")
)
);
var sensor = createSensor();
context.setRuntime(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,6 @@ public void setUp() throws Exception {
when(bridgeServerMock.createTsConfigFile(any())).thenReturn(
new TsConfigFile(tempFolder.newFile().getAbsolutePath(), emptyList(), emptyList())
);
Consumer<String> noop = s -> {};
when(bridgeServerMock.command()).thenReturn(
new NodeCommand(
mock(ProcessWrapper.class),
"node",
Version.create(22, 9, 0),
Collections.emptyList(),
null,
Collections.emptyList(),
noop,
noop,
Map.of(),
"embedded"
)
);

context = createSensorContext(baseDir);
context.setPreviousCache(mock(ReadCache.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.sonar.api.utils.Version;
import org.sonar.plugins.javascript.bridge.BridgeServer;
import org.sonar.plugins.javascript.bridge.BridgeServer.Dependency;
import org.sonar.plugins.javascript.bridge.BridgeServer.TelemetryNodeResponse;
import org.sonar.plugins.javascript.bridge.BridgeServer.TelemetryEslintBridgeResponse;
import org.sonar.plugins.javascript.nodejs.NodeCommand;
import org.sonar.plugins.javascript.nodejs.ProcessWrapper;

Expand All @@ -50,26 +50,12 @@ void setUp() {
when(ctx.runtime()).thenReturn(sonarRuntime);

BridgeServer server = mock(BridgeServer.class);
TelemetryNodeResponse telemetryNodeResponse = new TelemetryNodeResponse(
List.of(new Dependency("pkg1", "1.0.0"))
);
when(server.getTelemetry()).thenReturn(telemetryNodeResponse);
Consumer<String> noop = s -> {};
when(server.command()).thenReturn(
new NodeCommand(
mock(ProcessWrapper.class),
"node",
Version.create(22, 9, 0),
Collections.emptyList(),
null,
Collections.emptyList(),
noop,
noop,
Map.of(),
"embedded"
when(server.getTelemetry()).thenReturn(
new BridgeServer.TelemetryData(
List.of(new Dependency("pkg1", "1.0.0")),
Map.of("version", "22.9", "major-version", "22", "node-executable-origin", "embedded")
)
);

pluginTelemetry = new PluginTelemetry(ctx, server);
}

Expand Down

0 comments on commit 9a36b30

Please sign in to comment.