Skip to content

Commit

Permalink
feat: Add license parsing for java (#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
dja-fr authored Dec 20, 2022
1 parent 4ffbeee commit 82f32c7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions syft/pkg/cataloger/java/archive_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (j *archiveParser) discoverMainPackage() (*pkg.Package, error) {
return &pkg.Package{
Name: selectName(manifest, j.fileInfo),
Version: selectVersion(manifest, j.fileInfo),
Licenses: selectLicense(manifest),
Language: pkg.Java,
Locations: source.NewLocationSet(j.location),
Type: j.fileInfo.pkgType(),
Expand Down
3 changes: 3 additions & 0 deletions syft/pkg/cataloger/java/archive_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func TestParseJar(t *testing.T) {
Name: "example-jenkins-plugin",
Version: "1.0-SNAPSHOT",
PURL: "pkg:maven/io.jenkins.plugins/[email protected]",
Licenses: []string{"MIT License"},
Language: pkg.Java,
Type: pkg.JenkinsPluginPkg,
MetadataType: pkg.JavaMetadataType,
Expand Down Expand Up @@ -149,6 +150,7 @@ func TestParseJar(t *testing.T) {
Name: "example-java-app-gradle",
Version: "0.1.0",
PURL: "pkg:maven/example-java-app-gradle/[email protected]",
Licenses: []string{},
Language: pkg.Java,
Type: pkg.JavaPkg,
MetadataType: pkg.JavaMetadataType,
Expand All @@ -174,6 +176,7 @@ func TestParseJar(t *testing.T) {
Name: "example-java-app-maven",
Version: "0.1.0",
PURL: "pkg:maven/org.anchore/[email protected]",
Licenses: []string{},
Language: pkg.Java,
Type: pkg.JavaPkg,
MetadataType: pkg.JavaMetadataType,
Expand Down
20 changes: 20 additions & 0 deletions syft/pkg/cataloger/java/parse_java_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ func selectVersion(manifest *pkg.JavaManifest, filenameObj archiveFilename) stri
return ""
}

func selectLicense(manifest *pkg.JavaManifest) []string {
result := []string{}
if manifest == nil {
return result
}

fieldNames := []string{
"Bundle-License",
"Plugin-License-Name",
}

for _, fieldName := range fieldNames {
if v := fieldValueFromManifest(*manifest, fieldName); v != "" {
result = append(result, v)
}
}

return result
}

func fieldValueFromManifest(manifest pkg.JavaManifest, fieldName string) string {
if value := manifest.Main[fieldName]; value != "" {
return value
Expand Down

0 comments on commit 82f32c7

Please sign in to comment.