Skip to content

Commit

Permalink
read and write to the revision cache properly
Browse files Browse the repository at this point in the history
  • Loading branch information
spatten committed May 28, 2024
1 parent c6c856c commit c3823f5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/App/Fossa/Config/SBOM/Analyze.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module App.Fossa.Config.SBOM.Analyze (
) where

import App.Fossa.Config.Common (
CacheAction (..),
CommonOpts (..),
collectApiOpts,
collectRevisionOverride,
Expand Down Expand Up @@ -55,7 +56,6 @@ instance ToJSON SBOMScanDestination where
data SBOMAnalyzeConfig = SBOMAnalyzeConfig
{ sbomBaseDir :: BaseDir
, sbomScanDestination :: SBOMScanDestination
, revisionOverride :: OverrideProject
, sbomPath :: SBOMFile
, severity :: Severity
, sbomRebuild :: DependencyRebuild
Expand Down Expand Up @@ -118,11 +118,10 @@ mergeOpts cfgfile envvars cliOpts@SBOMAnalyzeOptions{..} = do
(Nothing)

forceRescans = if fromFlag ForceRescan forceRescan then DependencyRebuildInvalidateCache else DependencyRebuildReuseCache
revision <- getProjectRevision fileLoc revOverride
revision <- getProjectRevision fileLoc revOverride WriteOnly
SBOMAnalyzeConfig
(BaseDir baseDir)
<$> scanDest
<*> pure revOverride
<*> pure fileLoc
<*> pure severity
<*> pure forceRescans
Expand Down
20 changes: 16 additions & 4 deletions src/App/Fossa/Config/SBOM/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ module App.Fossa.Config.SBOM.Common (
getProjectRevision,
) where

import App.Fossa.ProjectInference (InferredProject (..), inferProjectDefaultFromFile)
import App.Fossa.Config.Common (CacheAction (..))
import App.Fossa.ProjectInference (InferredProject (..), inferProjectDefaultFromFile, readCachedRevision, saveRevision)
import App.Types (OverrideProject (..), ProjectRevision (..))
import Control.Algebra (Has)
import Control.Carrier.Diagnostics (fromEitherShow)
import Control.Carrier.Diagnostics (fromEitherShow, (<||>))
import Control.Effect.Diagnostics (context)
import Control.Effect.Diagnostics qualified as Diag
import Control.Effect.Lift (Lift)
import Control.Monad (when)
import Data.Aeson (ToJSON (toEncoding), defaultOptions, genericToEncoding)
import Data.Maybe (fromMaybe)
import Data.String.Conversion (toString)
import Data.Text (Text)
import Effect.ReadFS (ReadFS)
import GHC.Generics (Generic)
import Options.Applicative (Parser, argument, metavar, str)
import Path (parseSomeFile)
Expand All @@ -35,17 +38,26 @@ sbomFileArg = SBOMFile <$> argument str (applyFossaStyle <> metavar "SBOM" <> st
getProjectRevision ::
( Has Diag.Diagnostics sig m
, Has (Lift IO) sig m
, Has ReadFS sig m
) =>
SBOMFile ->
OverrideProject ->
CacheAction ->
m ProjectRevision
getProjectRevision sbomPath override = do
getProjectRevision sbomPath override cacheStrategy = do
let path = unSBOMFile $ sbomPath
parsedPath <- context "Parsing `sbom` path" $ fromEitherShow $ parseSomeFile (toString path)
inferred <- case parsedPath of
Abs f -> inferProjectDefaultFromFile f

Check failure on line 51 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 51 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 51 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 51 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

Check failure on line 52 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 52 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 52 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 52 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
readCachedRevision <||> pure (inferredRevision inferred)
WriteOnly -> do
pure $ inferredRevision inferred
let name = fromMaybe (inferredName inferred) $ overrideName override
let version = fromMaybe (inferredRevision inferred) $ overrideRevision override
let version = fromMaybe inferredVersion $ overrideRevision override
let revision = ProjectRevision name version Nothing
when (cacheStrategy == WriteOnly) $ saveRevision revision
pure $ ProjectRevision name version Nothing
3 changes: 2 additions & 1 deletion src/App/Fossa/Config/SBOM/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module App.Fossa.Config.SBOM.Test (
) where

import App.Fossa.Config.Common (
CacheAction (..),
CommonOpts (..),
collectApiOpts,
collectRevisionOverride,
Expand Down Expand Up @@ -157,7 +158,7 @@ mergeOpts maybeConfig envvars SBOMTestCliOpts{..} = do
(optProjectRevision testCommons)
(Nothing)

revision <- App.Fossa.Config.SBOM.Common.getProjectRevision sbomFile revOverride
revision <- App.Fossa.Config.SBOM.Common.getProjectRevision sbomFile revOverride ReadOnly
testOutputFormat <- validateOutputFormat testOutputFmt

TestConfig
Expand Down

0 comments on commit c3823f5

Please sign in to comment.