-
Notifications
You must be signed in to change notification settings - Fork 22
feat: add Generate SBOM action to IntelliJ plugin #258
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
127 changes: 127 additions & 0 deletions
127
src/main/java/org/jboss/tools/intellij/stackanalysis/GenerateSbomAction.java
This file contains hidden or 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,127 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2025 Red Hat, Inc. | ||
| * Distributed under license by Red Hat, Inc. All rights reserved. | ||
| * This program is made available under the terms of the | ||
| * Eclipse Public License v2.0 which accompanies this distribution, | ||
| * and is available at http://www.eclipse.org/legal/epl-v20.html | ||
| * | ||
| * Contributors: | ||
| * Red Hat, Inc. - initial API and implementation | ||
| ******************************************************************************/ | ||
| package org.jboss.tools.intellij.stackanalysis; | ||
|
|
||
| import com.intellij.openapi.actionSystem.ActionUpdateThread; | ||
| import com.intellij.openapi.actionSystem.AnAction; | ||
| import com.intellij.openapi.actionSystem.AnActionEvent; | ||
| import com.intellij.openapi.actionSystem.CommonDataKeys; | ||
| import com.intellij.openapi.actionSystem.PlatformDataKeys; | ||
| import com.intellij.openapi.application.ApplicationManager; | ||
| import com.intellij.openapi.diagnostic.Logger; | ||
| import com.intellij.openapi.fileChooser.FileChooserFactory; | ||
| import com.intellij.openapi.fileChooser.FileSaverDescriptor; | ||
| import com.intellij.openapi.fileChooser.FileSaverDialog; | ||
| import com.intellij.openapi.project.Project; | ||
| import com.intellij.openapi.ui.Messages; | ||
| import com.intellij.openapi.vfs.LocalFileSystem; | ||
| import com.intellij.openapi.vfs.VirtualFile; | ||
| import com.intellij.openapi.vfs.VirtualFileWrapper; | ||
| import com.intellij.psi.PsiFile; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| import com.intellij.notification.NotificationGroupManager; | ||
| import com.intellij.notification.NotificationType; | ||
| import org.jboss.tools.intellij.componentanalysis.CAAnnotator; | ||
| import org.jboss.tools.intellij.exhort.ApiService; | ||
|
|
||
| public class GenerateSbomAction extends AnAction { | ||
| private static final Logger logger = Logger.getInstance(GenerateSbomAction.class); | ||
|
|
||
| @Override | ||
| public @NotNull ActionUpdateThread getActionUpdateThread() { | ||
| return ActionUpdateThread.BGT; | ||
| } | ||
|
|
||
| @Override | ||
| public void actionPerformed(@NotNull AnActionEvent event) { | ||
| Project project = event.getProject(); | ||
| if (project == null) { | ||
| return; | ||
| } | ||
|
|
||
| VirtualFile manifestFile = event.getData(PlatformDataKeys.VIRTUAL_FILE); | ||
| if (manifestFile == null) { | ||
| return; | ||
| } | ||
|
|
||
| FileSaverDescriptor descriptor = new FileSaverDescriptor( | ||
| "Save SBOM", | ||
| "Choose location to save the CycloneDX SBOM", | ||
| "json" | ||
| ); | ||
| FileSaverDialog dialog = FileChooserFactory.getInstance().createSaveFileDialog(descriptor, project); | ||
|
|
||
| VirtualFile baseDir = LocalFileSystem.getInstance().findFileByPath( | ||
| manifestFile.getParent().getPath() | ||
| ); | ||
| VirtualFileWrapper fileWrapper = dialog.save(baseDir, "bom.json"); | ||
| if (fileWrapper == null) { | ||
| return; | ||
| } | ||
|
|
||
| Path savePath = fileWrapper.getFile().toPath(); | ||
|
|
||
| ApplicationManager.getApplication().executeOnPooledThread(() -> { | ||
| try { | ||
| ApiService apiService = ApplicationManager.getApplication().getService(ApiService.class); | ||
| String sbomJson = apiService.generateSbom( | ||
| CAAnnotator.getPackageManager(manifestFile.getName()), | ||
| manifestFile.getName(), | ||
| manifestFile.getPath() | ||
| ); | ||
|
|
||
| Files.writeString(savePath, sbomJson, StandardCharsets.UTF_8); | ||
|
|
||
| ApplicationManager.getApplication().invokeLater(() -> { | ||
| if (project.isDisposed()) { | ||
| return; | ||
| } | ||
| NotificationGroupManager.getInstance() | ||
| .getNotificationGroup("Red Hat Dependency Analytics") | ||
| .createNotification( | ||
| "SBOM saved to " + savePath, | ||
| NotificationType.INFORMATION | ||
| ) | ||
| .notify(project); | ||
| }); | ||
| } catch (Exception ex) { | ||
| logger.error(ex); | ||
| ApplicationManager.getApplication().invokeLater(() -> { | ||
| if (project.isDisposed()) { | ||
| return; | ||
| } | ||
| Messages.showErrorDialog( | ||
| project, | ||
| "SBOM generation failed: " + ex.getLocalizedMessage(), | ||
| "Error" | ||
| ); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void update(AnActionEvent event) { | ||
| PsiFile psiFile = event.getData(CommonDataKeys.PSI_FILE); | ||
| if (psiFile != null) { | ||
| event.getPresentation().setEnabledAndVisible( | ||
| SaUtils.SUPPORTED_MANIFEST_FILES.contains(psiFile.getName()) | ||
| ); | ||
| } else { | ||
| event.getPresentation().setEnabledAndVisible(false); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
48 changes: 48 additions & 0 deletions
48
src/test/java/org/jboss/tools/intellij/exhort/ApiServiceTest.java
This file contains hidden or 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,48 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2025 Red Hat, Inc. | ||
| * Distributed under license by Red Hat, Inc. All rights reserved. | ||
| * This program is made available under the terms of the | ||
| * Eclipse Public License v2.0 which accompanies this distribution, | ||
| * and is available at http://www.eclipse.org/legal/epl-v20.html | ||
| * | ||
| * Contributors: | ||
| * Red Hat, Inc. - initial API and implementation | ||
| ******************************************************************************/ | ||
|
|
||
| package org.jboss.tools.intellij.exhort; | ||
|
|
||
| import io.github.guacsec.trustifyda.Api; | ||
| import org.junit.Test; | ||
|
|
||
| import java.lang.reflect.Method; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
|
|
||
| /** | ||
| * Tests for ApiService that don't require IntelliJ platform initialization. | ||
| * The generateSbom method depends on TelemetryService and ApiSettingsState | ||
| * which need the platform, so we verify the API contract at the interface level. | ||
| */ | ||
| public class ApiServiceTest { | ||
|
|
||
| @Test | ||
| public void apiInterfaceHasGenerateSbomMethod() throws NoSuchMethodException { | ||
| Method method = Api.class.getMethod("generateSbom", String.class); | ||
| assertNotNull("Api interface should have generateSbom(String) method", method); | ||
| assertEquals("generateSbom should return String", String.class, method.getReturnType()); | ||
| } | ||
|
|
||
| @Test | ||
| public void apiServiceHasGenerateSbomMethod() throws NoSuchMethodException { | ||
| Method method = ApiService.class.getMethod("generateSbom", String.class, String.class, String.class); | ||
| assertNotNull("ApiService should have generateSbom(String, String, String) method", method); | ||
| assertEquals("generateSbom should return String", String.class, method.getReturnType()); | ||
| } | ||
|
|
||
| @Test | ||
| public void apiServiceAcceptsApiInConstructor() throws NoSuchMethodException { | ||
| // Verify the package-private constructor used for testing exists | ||
| ApiService.class.getDeclaredConstructor(Api.class); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.