Skip to content

Commit ae5242b

Browse files
author
Drew Haven
authored
Adds license details to attribution (#945)
* Adds license details to attribution * Fixes tests and formatting. * Adds changlog.
1 parent e564810 commit ae5242b

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

Changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44
- Vendor Dependencies: Considers `licence` and `license` equivalent when performing native license scan.
5+
- Adds copyright information to attribution reports in the JSON output.
56

67
## v3.3.0
78
- Telemetry: CLI collects telemetry by default. ([#936](https://github.com/fossas/fossa-cli/pull/936))

shell.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@
3232
shellHook = ''
3333
export LANG=C.UTF-8
3434
'';
35-
}
35+
}

src/App/Fossa/Report/Attribution.hs

+31-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ module App.Fossa.Report.Attribution (
66
Dependency (..),
77
License (..),
88
LicenseContents (..),
9+
LicenseCopyright (..),
10+
LicenseDetails (..),
911
LicenseName (..),
1012
Project (..),
1113
) where
1214

1315
import Data.Aeson
1416
import Data.Map.Strict (Map)
15-
import Data.Maybe (catMaybes)
17+
import Data.Maybe (catMaybes, isNothing)
1618
import Data.Text (Text)
1719

1820
newtype LicenseName = LicenseName {rawName :: Text}
@@ -21,11 +23,21 @@ newtype LicenseName = LicenseName {rawName :: Text}
2123
newtype LicenseContents = LicenseContents {rawContents :: Text}
2224
deriving (Eq, Ord, Show, FromJSON, ToJSON)
2325

26+
newtype LicenseCopyright = LicenseCopyright {rawCopyright :: Text}
27+
deriving (Eq, Ord, Show, FromJSON, ToJSON)
28+
29+
data LicenseDetails = LicenseDetails
30+
{ licenseText :: LicenseContents
31+
, licenseCopyrights :: [LicenseCopyright]
32+
}
33+
deriving (Eq, Ord, Show)
34+
2435
data Attribution = Attribution
2536
{ attribProject :: Project
2637
, attribDirectDeps :: [Dependency]
2738
, attribDeepDeps :: [Dependency]
2839
, attribLicenses :: Map LicenseName LicenseContents
40+
, attribLicenseDetails :: Maybe (Map LicenseName LicenseDetails)
2941
}
3042
deriving (Eq, Show, Ord)
3143

@@ -66,15 +78,19 @@ instance FromJSON Attribution where
6678
<*> obj .:? "directDependencies" .!= []
6779
<*> obj .:? "deepDependencies" .!= []
6880
<*> obj .: "licenses"
81+
<*> obj .:? "licenseDetails"
6982

7083
instance ToJSON Attribution where
7184
toJSON Attribution{..} =
72-
object
85+
object $
7386
[ "project" .= attribProject
7487
, "directDependencies" .= attribDirectDeps
7588
, "deepDependencies" .= attribDeepDeps
7689
, "licenses" .= attribLicenses
7790
]
91+
++ if isNothing attribLicenseDetails
92+
then []
93+
else ["licenseDetails" .= attribLicenseDetails]
7894

7995
instance FromJSON Dependency where
8096
parseJSON = withObject "Dependency" $ \obj ->
@@ -138,3 +154,16 @@ instance ToJSON Project where
138154
[ "name" .= projectName
139155
, "revision" .= projectRevision
140156
]
157+
158+
instance FromJSON LicenseDetails where
159+
parseJSON = withObject "LicenseDetails" $ \obj ->
160+
LicenseDetails
161+
<$> obj .: "text"
162+
<*> obj .: "copyrights"
163+
164+
instance ToJSON LicenseDetails where
165+
toJSON LicenseDetails{..} =
166+
object
167+
[ "text" .= licenseText
168+
, "copyrights" .= licenseCopyrights
169+
]

test/App/Fossa/Report/AttributionSpec.hs

+11-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ genAttribution =
4949
<*> Gen.list defaultRange genDependency
5050
<*> Gen.list defaultRange genDependency
5151
<*> genLicenseMap
52+
<*> Gen.maybe genLicenseDetailsMap
5253

5354
tuplify :: Monad m => m a -> m b -> m (a, b)
5455
tuplify = liftA2 (,)
@@ -59,14 +60,22 @@ genLicenseMap = do
5960
let genContents = LicenseContents <$> arbitraryText
6061
Gen.map defaultRange $ tuplify genName genContents
6162

63+
genLicenseDetailsMap :: Gen (Map LicenseName LicenseDetails)
64+
genLicenseDetailsMap =
65+
let genName = LicenseName <$> arbitraryText
66+
genContents = LicenseContents <$> arbitraryText
67+
genCopyrights = Gen.list defaultRange (LicenseCopyright <$> arbitraryText)
68+
genDetails = LicenseDetails <$> genContents <*> genCopyrights
69+
in Gen.map defaultRange $ tuplify genName genDetails
70+
6271
arbitraryText :: Gen Text
6372
arbitraryText = Gen.text (Range.linear 3 25) Gen.unicodeAll
6473

6574
spec :: Spec
6675
spec =
67-
describe "Attribution ToJSON/FromJSON instances" $
76+
describe "Attribution ToJSON/FromJSON" $
6877
modifyMaxSuccess (const 20) $
69-
it "are roundtrippable" $
78+
it "is round-trip-able" $
7079
hedgehog $
7180
do
7281
attr <- forAll genAttribution

0 commit comments

Comments
 (0)