Skip to content

Commit

Permalink
strip off .json or .xml extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
spatten committed May 29, 2024
1 parent a257939 commit c753995
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/references/subcommands/sbom.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ FOSSA supports SPDX (JSON only) and CycloneDX (XML and JSON) SBOM files. For mor

## `fossa sbom analyze <path to sbom file>`

`fossa sbom analyze <path to sbom file>` 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 <path to sbom file>` 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).

Expand Down
5 changes: 3 additions & 2 deletions src/App/Fossa/Config/SBOM/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 58 in src/App/Fossa/Config/SBOM/Common.hs

View workflow job for this annotation

GitHub Actions / Windows-build

• Couldn't match expected type: Path.Windows.SomeBase

Check failure on line 58 in src/App/Fossa/Config/SBOM/Common.hs

View workflow job for this annotation

GitHub Actions / Windows-build

• Couldn't match expected type: Path.Internal.Windows.Path

Check failure on line 58 in src/App/Fossa/Config/SBOM/Common.hs

View workflow job for this annotation

GitHub Actions / Windows-build

• Couldn't match expected type: Path.Windows.SomeBase

Check failure on line 58 in src/App/Fossa/Config/SBOM/Common.hs

View workflow job for this annotation

GitHub Actions / Windows-build

• Couldn't match expected type: Path.Internal.Windows.Path
Rel f -> inferProjectDefaultFromFile f extensions

Check failure on line 59 in src/App/Fossa/Config/SBOM/Common.hs

View workflow job for this annotation

GitHub Actions / Windows-build

• Couldn't match expected type: Path.Windows.SomeBase

Check failure on line 59 in src/App/Fossa/Config/SBOM/Common.hs

View workflow job for this annotation

GitHub Actions / Windows-build

• Couldn't match expected type: Path.Internal.Windows.Path

Check failure on line 59 in src/App/Fossa/Config/SBOM/Common.hs

View workflow job for this annotation

GitHub Actions / Windows-build

• Couldn't match expected type: Path.Windows.SomeBase

Check failure on line 59 in src/App/Fossa/Config/SBOM/Common.hs

View workflow job for this annotation

GitHub Actions / Windows-build

• Couldn't match expected type: Path.Internal.Windows.Path

inferredVersion <- case cacheStrategy of
ReadOnly -> do
Expand Down
17 changes: 13 additions & 4 deletions src/App/Fossa/ProjectInference.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit c753995

Please sign in to comment.