diff --git a/docs/references/subcommands/sbom.md b/docs/references/subcommands/sbom.md index e79a4062d7..87b314042e 100644 --- a/docs/references/subcommands/sbom.md +++ b/docs/references/subcommands/sbom.md @@ -8,13 +8,13 @@ FOSSA supports SPDX (JSON only) and CycloneDX (XML and JSON) SBOM files. For mor ## `fossa sbom analyze ` -`fossa sbom analyze ` will upload the SBOM file to FOSSA and Analyze it. The result will be a project with a name of the SBOM file and a revision of the current timestamp. For example, if you run this command: +`fossa sbom analyze ` will upload the SBOM file to FOSSA and Analyze it. The name of the project will be the name of the SBOM file with extensions of ".xml" or ".json" removed. The revision will be derived from the current time. For example, if you run this command: ``` fossa sbom analyze /path/to/sampleCycloneDX.json ``` -Then the project will be named "sampleCycloneDX.json" and the revision will be a timestamp based on the current time. +Then the project will be named "sampleCycloneDX" and the revision will be a timestamp based on the current time. You can override the project name and revision using the `--project` and `--revision` flags, described below in [Common FOSSA Project Flags](#common-fossa-project-flags). diff --git a/src/App/Fossa/Config/SBOM/Common.hs b/src/App/Fossa/Config/SBOM/Common.hs index 75e5784177..fa6e625bad 100644 --- a/src/App/Fossa/Config/SBOM/Common.hs +++ b/src/App/Fossa/Config/SBOM/Common.hs @@ -53,9 +53,10 @@ getProjectRevision :: getProjectRevision sbomPath override cacheStrategy = do let path = unSBOMFile $ sbomPath parsedPath <- context "Parsing `sbom` path" $ fromEitherShow $ parseSomeFile (toString path) + let extensions = [".json", ".xml"] inferred <- case parsedPath of - Abs f -> inferProjectDefaultFromFile f - Rel f -> inferProjectDefaultFromFile f + Abs f -> inferProjectDefaultFromFile f extensions + Rel f -> inferProjectDefaultFromFile f extensions inferredVersion <- case cacheStrategy of ReadOnly -> do diff --git a/src/App/Fossa/ProjectInference.hs b/src/App/Fossa/ProjectInference.hs index 3df05922c5..9df0ee4fdb 100644 --- a/src/App/Fossa/ProjectInference.hs +++ b/src/App/Fossa/ProjectInference.hs @@ -23,9 +23,11 @@ import Control.Carrier.Diagnostics hiding (fromMaybe) import Control.Effect.Lift (Lift, sendIO) import Control.Monad (unless) import Data.ByteString.Lazy qualified as BL +import Data.Char (toLower) import Data.Foldable (find) import Data.HashMap.Strict qualified as HM -import Data.Maybe (fromMaybe, mapMaybe) +import Data.List (elemIndex) +import Data.Maybe (fromMaybe, isJust, mapMaybe) import Data.String.Conversion (decodeUtf8, toString, toText) import Data.Text (Text) import Data.Text qualified as Text @@ -72,13 +74,20 @@ inferProjectDefault dir = context "Inferring project from directory name / times pure (InferredProject (toText name) (stamp <> "Z") Nothing) -- | Infer a default project name from a filename, and a default revision from the current time. -inferProjectDefaultFromFile :: (Has (Lift IO) sig m, Has Diagnostics sig m) => Path b File -> m InferredProject -inferProjectDefaultFromFile file = context "Inferring project from filename / timestamp" . sendIO $ do +-- If any extensions are passed in, then those extensions will be removed when creating the project name +inferProjectDefaultFromFile :: (Has (Lift IO) sig m, Has Diagnostics sig m) => Path b File -> [String] -> m InferredProject +inferProjectDefaultFromFile file extensions = context "Inferring project from filename / timestamp" . sendIO $ do let name = FP.dropTrailingPathSeparator (fromRelFile (filename file)) + let ext = map toLower $ FP.takeExtension (name) + let nameWithoutExt = + if (ext /= "") && isJust (elemIndex ext extensions) + then FP.dropExtension name + else name + time <- iso8601Show <$> getCurrentTime let stamp = Text.takeWhile (/= '.') $ toText time -- trim milliseconds off, format is yyyy-mm-ddThh:mm:ss[.sss] - pure (InferredProject (toText name) (stamp <> "Z") Nothing) + pure (InferredProject (toText nameWithoutExt) (stamp <> "Z") Nothing) svnCommand :: Command svnCommand =