-
Notifications
You must be signed in to change notification settings - Fork 59
feat(project): add standalone project sbom command
#1246
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
Soner (shyim)
merged 2 commits into
shopware:main
from
roboshyim:feat/project-sbom-command
Jul 24, 2026
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package project | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/shopware/shopware-cli/internal/shop" | ||
| "github.com/shopware/shopware-cli/internal/tui" | ||
| ) | ||
|
|
||
| var projectSbomCmd = &cobra.Command{ | ||
| Use: "sbom [path]", | ||
| Short: "Generate a CycloneDX SBOM from composer.lock", | ||
| Long: `Generate a Software Bill of Materials (SBOM) for a Shopware project. | ||
|
|
||
| Reads composer.lock (and optionally composer.json for the root component name | ||
| and version) and writes a CycloneDX 1.7 JSON document — the same artifact that | ||
| project ci produces, without running the rest of the CI build. | ||
|
|
||
| Examples: | ||
| # Write sbom.cdx.json into the current Shopware project | ||
| shopware-cli project sbom | ||
|
|
||
| # Explicit project path and output file | ||
| shopware-cli project sbom ./my-shop \ | ||
| --format cyclonedx-json \ | ||
| --output sbom.json | ||
|
|
||
| The command is non-interactive and exits non-zero when generation fails | ||
| (missing or unreadable composer.lock, unsupported format, write errors).`, | ||
| Args: cobra.MaximumNArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| root, err := resolveProjectSbomRoot(args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| format, err := cmd.Flags().GetString("format") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := shop.ValidateProjectSBOMFormat(format); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| output, err := cmd.Flags().GetString("output") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| includeDev, err := cmd.Flags().GetBool("include-dev-dependencies") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return shop.WriteProjectSBOM(cmd.Context(), root, shop.ProjectSBOMOptions{ | ||
| OutputPath: output, | ||
| SkipMissingLock: false, | ||
| IncludeDevDependencies: includeDev, | ||
| ToolVersion: tui.AppVersion, | ||
| }) | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| projectRootCmd.AddCommand(projectSbomCmd) | ||
| projectSbomCmd.Flags().String("format", shop.ProjectSBOMFormatCycloneDXJSON, "SBOM format (only cyclonedx-json is supported)") | ||
| projectSbomCmd.Flags().StringP("output", "o", "", fmt.Sprintf("Output file path (default: %s in the project root)", shop.DefaultProjectSBOMOutput)) | ||
| projectSbomCmd.Flags().Bool("include-dev-dependencies", false, "Include packages-dev from composer.lock (excluded by default, matching project ci)") | ||
| } | ||
|
|
||
| // resolveProjectSbomRoot picks the project directory: an explicit path argument, | ||
| // otherwise the closest Shopware project (composer.json/lock walk), falling back | ||
| // to the working directory when no Shopware markers are found further up. | ||
| func resolveProjectSbomRoot(args []string) (string, error) { | ||
| if len(args) == 1 { | ||
| return filepath.Abs(args[0]) | ||
| } | ||
|
|
||
| root, err := findClosestShopwareProject() | ||
| if err == nil { | ||
| return root, nil | ||
| } | ||
|
|
||
| // findClosestShopwareProject fails when no Shopware markers exist. Still | ||
| // allow generating an SBOM from a plain composer project in cwd so the | ||
| // command is useful outside full Shopware trees. | ||
| cwd, cwdErr := os.Getwd() | ||
| if cwdErr != nil { | ||
| return "", err | ||
| } | ||
| return cwd, nil | ||
| } |
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,106 @@ | ||
| package project | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/shopware/shopware-cli/internal/shop" | ||
| "github.com/shopware/shopware-cli/internal/tui" | ||
| ) | ||
|
|
||
| func TestGenerateProjectSBOMSkipsWhenLockMissing(t *testing.T) { | ||
| // CI wrapper must keep the historical skip-on-missing-lock behaviour. | ||
| root := t.TempDir() | ||
| assert.NoError(t, generateProjectSBOM(t.Context(), root)) | ||
|
|
||
| _, err := os.Stat(filepath.Join(root, shop.DefaultProjectSBOMOutput)) | ||
| assert.True(t, os.IsNotExist(err), "no SBOM should be written when composer.lock is absent") | ||
| } | ||
|
|
||
| func TestGenerateProjectSBOM(t *testing.T) { | ||
| root := t.TempDir() | ||
|
|
||
| require.NoError(t, os.WriteFile(filepath.Join(root, "composer.json"), []byte(`{ | ||
| "name": "acme/shop", | ||
| "version": "1.2.3" | ||
| }`), 0o644)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(root, "composer.lock"), []byte(`{ | ||
| "packages": [ | ||
| { | ||
| "name": "symfony/console", | ||
| "version": "v6.3.0", | ||
| "type": "library", | ||
| "license": ["MIT"], | ||
| "require": {"php": ">=8.1"} | ||
| } | ||
| ], | ||
| "packages-dev": [ | ||
| {"name": "phpunit/phpunit", "version": "10.0.0", "license": ["BSD-3-Clause"]} | ||
| ] | ||
| }`), 0o644)) | ||
|
|
||
| assert.NoError(t, generateProjectSBOM(t.Context(), root)) | ||
|
|
||
| data, err := os.ReadFile(filepath.Join(root, shop.DefaultProjectSBOMOutput)) | ||
| assert.NoError(t, err) | ||
|
|
||
| doc := map[string]interface{}{} | ||
| assert.NoError(t, json.Unmarshal(data, &doc)) | ||
| assert.Equal(t, "CycloneDX", doc["bomFormat"]) | ||
| assert.Equal(t, "1.7", doc["specVersion"]) | ||
| } | ||
|
|
||
| func TestProjectSbomCommandUnsupportedFormat(t *testing.T) { | ||
| root := t.TempDir() | ||
|
|
||
| require.NoError(t, projectSbomCmd.Flags().Set("format", "spdx-json")) | ||
| require.NoError(t, projectSbomCmd.Flags().Set("output", "")) | ||
| t.Cleanup(func() { | ||
| _ = projectSbomCmd.Flags().Set("format", shop.ProjectSBOMFormatCycloneDXJSON) | ||
| _ = projectSbomCmd.Flags().Set("output", "") | ||
| _ = projectSbomCmd.Flags().Set("include-dev-dependencies", "false") | ||
| }) | ||
|
|
||
| projectSbomCmd.SetContext(t.Context()) | ||
| err := projectSbomCmd.RunE(projectSbomCmd, []string{root}) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "unsupported SBOM format") | ||
| } | ||
|
|
||
| func TestProjectSbomCommandSuccess(t *testing.T) { | ||
| root := t.TempDir() | ||
| require.NoError(t, os.WriteFile(filepath.Join(root, "composer.json"), []byte(`{"name":"acme/shop","version":"1.2.3"}`), 0o644)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(root, "composer.lock"), []byte(`{ | ||
| "packages":[{"name":"symfony/console","version":"v6.3.0","type":"library","license":["MIT"]}], | ||
| "packages-dev":[] | ||
| }`), 0o644)) | ||
|
|
||
| out := filepath.Join(root, "from-cmd.json") | ||
| require.NoError(t, projectSbomCmd.Flags().Set("format", shop.ProjectSBOMFormatCycloneDXJSON)) | ||
| require.NoError(t, projectSbomCmd.Flags().Set("output", out)) | ||
| require.NoError(t, projectSbomCmd.Flags().Set("include-dev-dependencies", "false")) | ||
| t.Cleanup(func() { | ||
| _ = projectSbomCmd.Flags().Set("format", shop.ProjectSBOMFormatCycloneDXJSON) | ||
| _ = projectSbomCmd.Flags().Set("output", "") | ||
| _ = projectSbomCmd.Flags().Set("include-dev-dependencies", "false") | ||
| }) | ||
|
|
||
| // Ensure the tool version used by the command path is stable in tests. | ||
| prev := tui.AppVersion | ||
| tui.AppVersion = "test" | ||
| t.Cleanup(func() { tui.AppVersion = prev }) | ||
|
|
||
| projectSbomCmd.SetContext(t.Context()) | ||
| require.NoError(t, projectSbomCmd.RunE(projectSbomCmd, []string{root})) | ||
|
|
||
| data, err := os.ReadFile(out) | ||
| require.NoError(t, err) | ||
| doc := map[string]interface{}{} | ||
| require.NoError(t, json.Unmarshal(data, &doc)) | ||
| assert.Equal(t, "1.7", doc["specVersion"]) | ||
| } |
Oops, something went wrong.
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.