Skip to content

Commit 323bbd4

Browse files
authored
feat: set rhda_token, rhda_source, MATCH_MANIFEST_VERSIONS and EXHORT_PYTHON_VIRTUAL_ENV for exhort api calls (#135)
* feat: set rhda_token and rhda_source for exhort api calls * feat: add golang and python package resolving options
1 parent c22e855 commit 323bbd4

File tree

4 files changed

+179
-75
lines changed

4 files changed

+179
-75
lines changed

src/main/java/org/jboss/tools/intellij/exhort/ApiService.java

Lines changed: 110 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111

1212
package org.jboss.tools.intellij.exhort;
1313

14+
import com.intellij.ide.plugins.PluginManagerCore;
15+
import com.intellij.openapi.application.ApplicationInfo;
1416
import com.intellij.openapi.components.Service;
1517
import com.intellij.openapi.diagnostic.Logger;
18+
import com.intellij.openapi.extensions.PluginDescriptor;
19+
import com.intellij.openapi.extensions.PluginId;
1620
import com.redhat.exhort.Api;
1721
import com.redhat.exhort.api.AnalysisReport;
1822
import com.redhat.exhort.impl.ExhortApi;
@@ -32,7 +36,7 @@ public final class ApiService {
3236
private static final Logger LOG = Logger.getInstance(ApiService.class);
3337

3438
enum TelemetryKeys {
35-
MANIFEST, ECOSYSTEM, PLATFORM;
39+
MANIFEST, ECOSYSTEM, PLATFORM, RHDA_TOKEN;
3640

3741
@Override
3842
public String toString() {
@@ -55,14 +59,14 @@ public Path getStackAnalysis(final String packageManager, final String manifestN
5559
telemetryMsg.property(TelemetryKeys.ECOSYSTEM.toString(), packageManager);
5660
telemetryMsg.property(TelemetryKeys.PLATFORM.toString(), System.getProperty("os.name"));
5761
telemetryMsg.property(TelemetryKeys.MANIFEST.toString(), manifestName);
62+
telemetryMsg.property(TelemetryKeys.RHDA_TOKEN.toString(), ApiSettingsState.getInstance().rhdaToken);
5863

5964
try {
60-
ApiSettingsState.getInstance().setApiOptions();
61-
LOG.info("Perform stack analysis");
65+
setRequestProperties(manifestName);
6266
var htmlContent = exhortApi.stackAnalysisHtml(manifestPath);
6367
var tmpFile = Files.createTempFile("exhort_", ".html");
6468
Files.write(tmpFile, htmlContent.get());
65-
LOG.info("Finish stack analysis");
69+
6670
telemetryMsg.send();
6771
return tmpFile;
6872

@@ -78,19 +82,18 @@ public AnalysisReport getComponentAnalysis(final String packageManager, final St
7882
telemetryMsg.property(TelemetryKeys.ECOSYSTEM.toString(), packageManager);
7983
telemetryMsg.property(TelemetryKeys.PLATFORM.toString(), System.getProperty("os.name"));
8084
telemetryMsg.property(TelemetryKeys.MANIFEST.toString(), manifestName);
85+
telemetryMsg.property(TelemetryKeys.RHDA_TOKEN.toString(), ApiSettingsState.getInstance().rhdaToken);
8186

8287
try {
83-
ApiSettingsState.getInstance().setApiOptions();
88+
setRequestProperties(manifestName);
8489
CompletableFuture<AnalysisReport> componentReport;
85-
LOG.info("Perform component analysis");
8690
if ("go.mod".equals(manifestName) || "requirements.txt".equals(manifestName)) {
8791
var manifestContent = Files.readAllBytes(Paths.get(manifestPath));
8892
componentReport = exhortApi.componentAnalysis(manifestName, manifestContent);
8993
} else {
9094
componentReport = exhortApi.componentAnalysis(manifestPath);
9195
}
9296
AnalysisReport report = componentReport.get();
93-
LOG.info("Finish component analysis");
9497
telemetryMsg.send();
9598
return report;
9699
} catch (IOException | InterruptedException | ExecutionException ex) {
@@ -108,4 +111,104 @@ public AnalysisReport getComponentAnalysis(final String packageManager, final St
108111
}
109112
return null;
110113
}
114+
115+
private void setRequestProperties(final String manifestName) {
116+
String ideName = ApplicationInfo.getInstance().getFullApplicationName();
117+
PluginDescriptor pluginDescriptor = PluginManagerCore.getPlugin(PluginId.getId("org.jboss.tools.intellij.analytics"));
118+
if (pluginDescriptor != null) {
119+
String pluginName = pluginDescriptor.getName() + " " + pluginDescriptor.getVersion();
120+
System.setProperty("RHDA_SOURCE", ideName + " / " + pluginName);
121+
} else {
122+
System.setProperty("RHDA_SOURCE", ideName);
123+
}
124+
125+
ApiSettingsState settings = ApiSettingsState.getInstance();
126+
System.setProperty("RHDA_TOKEN", settings.rhdaToken);
127+
128+
if (settings.mvnPath != null && !settings.mvnPath.isBlank()) {
129+
System.setProperty("EXHORT_MVN_PATH", settings.mvnPath);
130+
} else {
131+
System.clearProperty("EXHORT_MVN_PATH");
132+
}
133+
if (settings.javaPath != null && !settings.javaPath.isBlank()) {
134+
System.setProperty("JAVA_HOME", settings.javaPath);
135+
} else {
136+
System.clearProperty("JAVA_HOME");
137+
}
138+
if (settings.npmPath != null && !settings.npmPath.isBlank()) {
139+
System.setProperty("EXHORT_NPM_PATH", settings.npmPath);
140+
} else {
141+
System.clearProperty("EXHORT_NPM_PATH");
142+
}
143+
if (settings.nodePath != null && !settings.nodePath.isBlank()) {
144+
System.setProperty("NODE_HOME", settings.nodePath);
145+
} else {
146+
System.clearProperty("NODE_HOME");
147+
}
148+
if (settings.goPath != null && !settings.goPath.isBlank()) {
149+
System.setProperty("EXHORT_GO_PATH", settings.goPath);
150+
} else {
151+
System.clearProperty("EXHORT_GO_PATH");
152+
}
153+
if ("go.mod".equals(manifestName)) {
154+
if (settings.goMatchManifestVersions) {
155+
System.setProperty("MATCH_MANIFEST_VERSIONS", "true");
156+
} else {
157+
System.clearProperty("MATCH_MANIFEST_VERSIONS");
158+
}
159+
}
160+
if (settings.usePython2) {
161+
if (settings.pythonPath != null && !settings.pythonPath.isBlank()) {
162+
System.setProperty("EXHORT_PYTHON_PATH", settings.pythonPath);
163+
} else {
164+
System.clearProperty("EXHORT_PYTHON_PATH");
165+
}
166+
if (settings.pipPath != null && !settings.pipPath.isBlank()) {
167+
System.setProperty("EXHORT_PIP_PATH", settings.pipPath);
168+
} else {
169+
System.clearProperty("EXHORT_PIP_PATH");
170+
}
171+
System.clearProperty("EXHORT_PYTHON3_PATH");
172+
System.clearProperty("EXHORT_PIP3_PATH");
173+
} else {
174+
if (settings.pythonPath != null && !settings.pythonPath.isBlank()) {
175+
System.setProperty("EXHORT_PYTHON3_PATH", settings.pythonPath);
176+
} else {
177+
System.clearProperty("EXHORT_PYTHON3_PATH");
178+
}
179+
if (settings.pipPath != null && !settings.pipPath.isBlank()) {
180+
System.setProperty("EXHORT_PIP3_PATH", settings.pipPath);
181+
} else {
182+
System.clearProperty("EXHORT_PIP3_PATH");
183+
}
184+
System.clearProperty("EXHORT_PYTHON_PATH");
185+
System.clearProperty("EXHORT_PIP_PATH");
186+
}
187+
if (settings.usePythonVirtualEnv) {
188+
System.setProperty("EXHORT_PYTHON_VIRTUAL_ENV", "true");
189+
if (settings.pythonInstallBestEfforts) {
190+
System.setProperty("EXHORT_PYTHON_INSTALL_BEST_EFFORTS", "true");
191+
} else {
192+
System.clearProperty("EXHORT_PYTHON_INSTALL_BEST_EFFORTS");
193+
}
194+
} else {
195+
System.clearProperty("EXHORT_PYTHON_VIRTUAL_ENV");
196+
System.clearProperty("EXHORT_PYTHON_INSTALL_BEST_EFFORTS");
197+
}
198+
if ("requirements.txt".equals(manifestName)) {
199+
if (settings.pythonMatchManifestVersions) {
200+
System.setProperty("MATCH_MANIFEST_VERSIONS", "true");
201+
} else {
202+
System.clearProperty("MATCH_MANIFEST_VERSIONS");
203+
}
204+
}
205+
if (!"go.mod".equals(manifestName) && !"requirements.txt".equals(manifestName)) {
206+
System.clearProperty("MATCH_MANIFEST_VERSIONS");
207+
}
208+
if (settings.snykToken != null && !settings.snykToken.isBlank()) {
209+
System.setProperty("EXHORT_SNYK_TOKEN", settings.snykToken);
210+
} else {
211+
System.clearProperty("EXHORT_SNYK_TOKEN");
212+
}
213+
}
111214
}

src/main/java/org/jboss/tools/intellij/settings/ApiSettingsComponent.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,20 @@ public class ApiSettingsComponent {
3333
+ "<br>Specifies absolute path of the <i>directory</i> containing <b>node</b> executable.</html>";
3434
private final static String goPathLabel = "<html>Go > Executable: <b>Path</b>"
3535
+ "<br>Specifies absolute path of <b>go</b> executable.</html>";
36+
private final static String goMatchManifestVersionsLabel = "<html>Go > Match package version" +
37+
"<br>Specifies if comparing the resolved package versions with the versions defined in the manifest.</html>";
3638
private final static String pythonPathLabel = "<html>Python > Executable: <b>Path</b>"
3739
+ "<br>Specifies absolute path of <b>python</b> executable.</html>";
3840
private final static String pipPathLabel = "<html>Pip > Executable: <b>Path</b>"
3941
+ "<br>Specifies absolute path of <b>pip</b> executable.</html>";
40-
private final static String usePython2Label = "<html>Python > Executable: <b>Version</b></html>";
41-
private final static String usePythonVirtualEnvLabel = "<html>Python > Virtual Envrionment</html>";
42+
private final static String usePython2Label = "<html>Python > Executable: <b>Version</b>"
43+
+ "<br>Specifies if using python 2.x.</html>";
44+
private final static String usePythonVirtualEnvLabel = "<html>Python > Virtual Environment"
45+
+ "<br>Specifies if using virtual environment.</html>";
46+
private final static String pythonInstallBestEffortsLabel = "<html>Python > Virtual Environment: alternate package version"
47+
+ "<br>Specifies if allowing to use alternate package versions in virtual environment.</html>";
48+
private final static String pythonMatchManifestVersionsLabel = "<html>Python > Match package version"
49+
+ "<br>Specifies if comparing the resolved package versions with the versions defined in the manifest.</html>";
4250
private final static String snykTokenLabel = "<html>Red Hat Dependency Analytics: <b>Exhort Snyk Token</b>"
4351
+ "<br>Red Hat Dependency Analytics sever authentication token for Snyk.</html>";
4452

@@ -49,10 +57,13 @@ public class ApiSettingsComponent {
4957
private final TextFieldWithBrowseButton npmPathText;
5058
private final TextFieldWithBrowseButton nodePathText;
5159
private final TextFieldWithBrowseButton goPathText;
60+
private final JCheckBox goMatchManifestVersionsCheck;
5261
private final TextFieldWithBrowseButton pythonPathText;
5362
private final TextFieldWithBrowseButton pipPathText;
5463
private final JCheckBox usePython2Check;
5564
private final JCheckBox usePythonVirtualEnvCheck;
65+
private final JCheckBox pythonInstallBestEffortsCheck;
66+
private final JCheckBox pythonMatchManifestVersionsCheck;
5667
private final JBTextField snykTokenText;
5768

5869
public ApiSettingsComponent() {
@@ -101,6 +112,8 @@ public ApiSettingsComponent() {
101112
TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT
102113
);
103114

115+
goMatchManifestVersionsCheck = new JCheckBox("Strictly match package version");
116+
104117
pythonPathText = new TextFieldWithBrowseButton();
105118
pythonPathText.addBrowseFolderListener(
106119
null,
@@ -123,6 +136,10 @@ public ApiSettingsComponent() {
123136

124137
usePythonVirtualEnvCheck = new JCheckBox("Use python virtual environment");
125138

139+
pythonInstallBestEffortsCheck = new JCheckBox("Allow alternate package version");
140+
141+
pythonMatchManifestVersionsCheck = new JCheckBox("Strictly match package version");
142+
126143
snykTokenText = new JBTextField();
127144

128145
mainPanel = FormBuilder.createFormBuilder()
@@ -137,6 +154,8 @@ public ApiSettingsComponent() {
137154
.addSeparator(10)
138155
.addVerticalGap(10)
139156
.addLabeledComponent(new JBLabel(goPathLabel), goPathText, 1, true)
157+
.addVerticalGap(10)
158+
.addLabeledComponent(new JBLabel(goMatchManifestVersionsLabel), goMatchManifestVersionsCheck, 1, true)
140159
.addSeparator(10)
141160
.addVerticalGap(10)
142161
.addLabeledComponent(new JBLabel(pythonPathLabel), pythonPathText, 1, true)
@@ -146,6 +165,10 @@ public ApiSettingsComponent() {
146165
.addLabeledComponent(new JBLabel(usePython2Label), usePython2Check, 1, true)
147166
.addVerticalGap(10)
148167
.addLabeledComponent(new JBLabel(usePythonVirtualEnvLabel), usePythonVirtualEnvCheck, 1, true)
168+
.addVerticalGap(10)
169+
.addLabeledComponent(new JBLabel(pythonInstallBestEffortsLabel), pythonInstallBestEffortsCheck, 1, true)
170+
.addVerticalGap(10)
171+
.addLabeledComponent(new JBLabel(pythonMatchManifestVersionsLabel), pythonMatchManifestVersionsCheck, 1, true)
149172
.addSeparator(10)
150173
.addVerticalGap(10)
151174
.addLabeledComponent(new JBLabel(snykTokenLabel), snykTokenText, 1, true)
@@ -206,6 +229,14 @@ public void setGoPathText(@NotNull String text) {
206229
goPathText.setText(text);
207230
}
208231

232+
public boolean getGoMatchManifestVersionsCheck() {
233+
return goMatchManifestVersionsCheck.isSelected();
234+
}
235+
236+
public void setGoMatchManifestVersionsCheck(boolean selected) {
237+
goMatchManifestVersionsCheck.setSelected(selected);
238+
}
239+
209240
@NotNull
210241
public String getPythonPathText() {
211242
return pythonPathText.getText();
@@ -240,6 +271,22 @@ public void setUsePythonVirtualEnvCheck(boolean selected) {
240271
usePythonVirtualEnvCheck.setSelected(selected);
241272
}
242273

274+
public boolean getPythonInstallBestEffortsCheck() {
275+
return pythonInstallBestEffortsCheck.isSelected();
276+
}
277+
278+
public void setPythonInstallBestEffortsCheck(boolean selected) {
279+
pythonInstallBestEffortsCheck.setSelected(selected);
280+
}
281+
282+
public boolean getPythonMatchManifestVersionsCheck() {
283+
return pythonMatchManifestVersionsCheck.isSelected();
284+
}
285+
286+
public void setPythonMatchManifestVersionsCheck(boolean selected) {
287+
pythonMatchManifestVersionsCheck.setSelected(selected);
288+
}
289+
243290
@NotNull
244291
public String getSnykTokenText() {
245292
return snykTokenText.getText();

src/main/java/org/jboss/tools/intellij/settings/ApiSettingsConfigurable.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ public boolean isModified() {
4444
modified |= !settingsComponent.getNpmPathText().equals(settings.npmPath);
4545
modified |= !settingsComponent.getNodePathText().equals(settings.nodePath);
4646
modified |= !settingsComponent.getGoPathText().equals(settings.goPath);
47+
modified |= settingsComponent.getGoMatchManifestVersionsCheck() != settings.goMatchManifestVersions;
4748
modified |= !settingsComponent.getPythonPathText().equals(settings.pythonPath);
4849
modified |= !settingsComponent.getPipPathText().equals(settings.pipPath);
4950
modified |= settingsComponent.getUsePython2Check() != settings.usePython2;
5051
modified |= settingsComponent.getUsePythonVirtualEnvCheck() != settings.usePythonVirtualEnv;
52+
modified |= settingsComponent.getPythonInstallBestEffortsCheck() != settings.pythonInstallBestEfforts;
53+
modified |= settingsComponent.getPythonMatchManifestVersionsCheck() != settings.pythonMatchManifestVersions;
5154
modified |= !settingsComponent.getSnykTokenText().equals(settings.snykToken);
5255
return modified;
5356
}
@@ -60,10 +63,13 @@ public void apply() {
6063
settings.npmPath = settingsComponent.getNpmPathText();
6164
settings.nodePath = settingsComponent.getNodePathText();
6265
settings.goPath = settingsComponent.getGoPathText();
66+
settings.goMatchManifestVersions = settingsComponent.getGoMatchManifestVersionsCheck();
6367
settings.pythonPath = settingsComponent.getPythonPathText();
6468
settings.pipPath = settingsComponent.getPipPathText();
6569
settings.usePython2 = settingsComponent.getUsePython2Check();
6670
settings.usePythonVirtualEnv = settingsComponent.getUsePythonVirtualEnvCheck();
71+
settings.pythonInstallBestEfforts = settingsComponent.getPythonInstallBestEffortsCheck();
72+
settings.pythonMatchManifestVersions = settingsComponent.getPythonMatchManifestVersionsCheck();
6773
settings.snykToken = settingsComponent.getSnykTokenText();
6874
}
6975

@@ -75,10 +81,13 @@ public void reset() {
7581
settingsComponent.setNpmPathText(settings.npmPath != null ? settings.npmPath : "");
7682
settingsComponent.setNodePathText(settings.nodePath != null ? settings.nodePath : "");
7783
settingsComponent.setGoPathText(settings.goPath != null ? settings.goPath : "");
84+
settingsComponent.setGoMatchManifestVersionsCheck(settings.goMatchManifestVersions);
7885
settingsComponent.setPythonPathText(settings.pythonPath != null ? settings.pythonPath : "");
7986
settingsComponent.setPipPathText(settings.pipPath != null ? settings.pipPath : "");
8087
settingsComponent.setUsePython2Check(settings.usePython2);
8188
settingsComponent.setUsePythonVirtualEnvCheck(settings.usePythonVirtualEnv);
89+
settingsComponent.setPythonInstallBestEffortsCheck(settings.pythonInstallBestEfforts);
90+
settingsComponent.setPythonMatchManifestVersionsCheck(settings.pythonMatchManifestVersions);
8291
settingsComponent.setSnykTokenText(settings.snykToken != null ? settings.snykToken : "");
8392
}
8493

0 commit comments

Comments
 (0)