diff --git a/fields.go b/fields.go index 2cc6473..ada4246 100644 --- a/fields.go +++ b/fields.go @@ -334,6 +334,8 @@ func validateFieldsV0(publiccode PublicCode, parser *Parser, network bool, baseU } } + vr = append(vr, deprecatedLicenseWarnings("legal.license", publiccodev0.Legal.License)...) + if len(vr) == 0 { return nil } @@ -464,6 +466,8 @@ func validateFieldsV1(publiccode PublicCode, parser *Parser, network bool, baseU } } + vr = append(vr, deprecatedLicenseWarnings("legal.license", publiccodev1.Legal.License)...) + if len(vr) == 0 { return nil } diff --git a/licenses.go b/licenses.go new file mode 100644 index 0000000..e0ad2e2 --- /dev/null +++ b/licenses.go @@ -0,0 +1,57 @@ +package publiccode + +import ( + "slices" + "strings" + + "github.com/github/go-spdx/v2/spdxexp" + "github.com/github/go-spdx/v2/spdxexp/spdxlicenses" +) + +// deprecatedLicenseWarnings warns once for every license identifier in the +// SPDX expression that the SPDX License List marks as deprecated. These +// identifiers are still valid, so the manifest is too, but some of them, +// like GPL-3.0, don't say whether later versions of the license apply. +func deprecatedLicenseWarnings(key string, expression string) ValidationResults { + // An invalid expression gives no licenses here: is_spdx_expression + // already reports it. + licenses, _ := spdxexp.ExtractLicenses(expression) + + var deprecated []string + + for _, license := range licenses { + // A license with an exception comes back as " WITH ". + id, _, _ := strings.Cut(license, " WITH ") + + if isDeprecated, canonical := spdxlicenses.IsDeprecatedLicense(id); isDeprecated && + !slices.Contains(deprecated, canonical) { + deprecated = append(deprecated, canonical) + } + } + + slices.Sort(deprecated) + + var vr ValidationResults + + for _, id := range deprecated { + only, orLater := id+"-only", id+"-or-later" + + if valid, _ := spdxexp.ValidateLicenses([]string{only, orLater}); valid { + vr = append(vr, newValidationWarningf( + key, + "'%s' is a DEPRECATED SPDX license identifier. Use '%s' or '%s' instead", + id, only, orLater, + )) + + continue + } + + vr = append(vr, newValidationWarningf( + key, + "'%s' is a DEPRECATED SPDX license identifier. Use its replacement from https://spdx.org/licenses instead", + id, + )) + } + + return vr +} diff --git a/licenses_test.go b/licenses_test.go new file mode 100644 index 0000000..afbec3f --- /dev/null +++ b/licenses_test.go @@ -0,0 +1,43 @@ +package publiccode + +import ( + "errors" + "slices" + "testing" +) + +func TestDeprecatedLicenseWarnings(t *testing.T) { + tests := []struct { + expression string + want []string + }{ + {"AGPL-3.0-or-later", nil}, + {"GPL-3.0+", nil}, + {"MIT OR Apache-2.0", nil}, + {"Invalid License", nil}, + {"", nil}, + {"gpl-3.0", []string{"'GPL-3.0' is a DEPRECATED SPDX license identifier. Use 'GPL-3.0-only' or 'GPL-3.0-or-later' instead"}}, + {"LGPL-2.1 OR LGPL-2.1", []string{"'LGPL-2.1' is a DEPRECATED SPDX license identifier. Use 'LGPL-2.1-only' or 'LGPL-2.1-or-later' instead"}}, + {"GPL-2.0 WITH Classpath-exception-2.0", []string{"'GPL-2.0' is a DEPRECATED SPDX license identifier. Use 'GPL-2.0-only' or 'GPL-2.0-or-later' instead"}}, + {"GPL-2.0-with-classpath-exception", []string{"'GPL-2.0-with-classpath-exception' is a DEPRECATED SPDX license identifier. Use its replacement from https://spdx.org/licenses instead"}}, + } + + for _, test := range tests { + t.Run(test.expression, func(t *testing.T) { + var got []string + + for _, result := range deprecatedLicenseWarnings("legal.license", test.expression) { + warning, ok := errors.AsType[ValidationWarning](result) + if !ok || warning.Key != "legal.license" { + t.Fatalf("got %#v, want a legal.license ValidationWarning", result) + } + + got = append(got, warning.Description) + } + + if !slices.Equal(got, test.want) { + t.Errorf("got %q, want %q", got, test.want) + } + }) + } +} diff --git a/testdata/v0/valid_with_warnings/no-network/license_deprecated.yml b/testdata/v0/valid_with_warnings/no-network/license_deprecated.yml new file mode 100644 index 0000000..57f4f1a --- /dev/null +++ b/testdata/v0/valid_with_warnings/no-network/license_deprecated.yml @@ -0,0 +1,50 @@ +publiccodeYmlVersion: "0" + +name: Medusa +url: "https://github.com/italia/developers.italia.it.git" + +platforms: + - web + +categories: + - cloud-management + +developmentStatus: development + +softwareType: "standalone/other" + +description: + en-GB: + localisedName: Medusa + shortDescription: > + A rather short description which + is probably useless + longDescription: > + Very long description of this software, also split + on multiple rows. You should note what the software + is and why one should need it. This is 158 characters. + Very long description of this software, also split + on multiple rows. You should note what the software + is and why one should need it. This is 316 characters. + Very long description of this software, also split + on multiple rows. You should note what the software + is and why one should need it. This is 474 characters. + Very long description of this software, also split + on multiple rows. You should note what the software + is and why one should need it. This is 632 characters. + features: + - Just one feature + +legal: + license: GPL-3.0 # Deprecated SPDX identifier: valid, but should produce a warning. + +maintenance: + type: "community" + + contacts: + - name: Francesco Rossi + +localisation: + localisationReady: true + availableLanguages: + - en diff --git a/testdata/v0/valid_with_warnings/no-network/license_deprecated_expression.yml b/testdata/v0/valid_with_warnings/no-network/license_deprecated_expression.yml new file mode 100644 index 0000000..99406e1 --- /dev/null +++ b/testdata/v0/valid_with_warnings/no-network/license_deprecated_expression.yml @@ -0,0 +1,50 @@ +publiccodeYmlVersion: "0" + +name: Medusa +url: "https://github.com/italia/developers.italia.it.git" + +platforms: + - web + +categories: + - cloud-management + +developmentStatus: development + +softwareType: "standalone/other" + +description: + en-GB: + localisedName: Medusa + shortDescription: > + A rather short description which + is probably useless + longDescription: > + Very long description of this software, also split + on multiple rows. You should note what the software + is and why one should need it. This is 158 characters. + Very long description of this software, also split + on multiple rows. You should note what the software + is and why one should need it. This is 316 characters. + Very long description of this software, also split + on multiple rows. You should note what the software + is and why one should need it. This is 474 characters. + Very long description of this software, also split + on multiple rows. You should note what the software + is and why one should need it. This is 632 characters. + features: + - Just one feature + +legal: + license: EUPL-1.2 OR GPL-2.0 WITH Classpath-exception-2.0 OR (AGPL-3.0 AND eCos-2.0) OR GPL-3.0+ # Warn for each deprecated identifier within the SPDX expression. + +maintenance: + type: "community" + + contacts: + - name: Francesco Rossi + +localisation: + localisationReady: true + availableLanguages: + - en diff --git a/testdata/v1/valid_with_warnings/license_deprecated.yml b/testdata/v1/valid_with_warnings/license_deprecated.yml new file mode 100644 index 0000000..ef33092 --- /dev/null +++ b/testdata/v1/valid_with_warnings/license_deprecated.yml @@ -0,0 +1,28 @@ +publiccodeYmlVersion: "1" +name: Firefax +url: https://example.com/firefax.git +softwareType: standalone/desktop +platforms: + - linux + - windows + - mac +developmentStatus: stable +description: + en: + shortDescription: Send and receive faxes from the desktop + longDescription: > + Firefax lets you send and receive faxes from a desktop computer + without a fax machine. You can search received faxes in an archive, + schedule outgoing faxes and track their delivery. +legal: + license: AGPL-3.0 OR MIT # AGPL-3.0 is deprecated even when combined with a current identifier. +maintenance: + type: community + contacts: + - name: Jane Doe +localisation: + localisationReady: true + availableLanguages: + - en + - it + - fr diff --git a/v0_test.go b/v0_test.go index a8679e4..6095d14 100644 --- a/v0_test.go +++ b/v0_test.go @@ -18,6 +18,14 @@ func TestValidWithWarningTestcasesV0_NoNetwork(t *testing.T) { ValidationWarning{File: "", Key: "legal.authorsFile", Description: "This key is DEPRECATED and will be removed in the future. It's safe to drop it", Line: 63, Column: 3}, ValidationWarning{File: "", Key: "IT", Description: "This key is DEPRECATED and will be removed in the future. Migrate its keys as their warnings say, then drop it", Line: 95, Column: 1}, }, + "license_deprecated.yml": ValidationResults{ + ValidationWarning{Key: "legal.license", Description: "'GPL-3.0' is a DEPRECATED SPDX license identifier. Use 'GPL-3.0-only' or 'GPL-3.0-or-later' instead", Line: 39, Column: 3}, + }, + "license_deprecated_expression.yml": ValidationResults{ + ValidationWarning{Key: "legal.license", Description: "'AGPL-3.0' is a DEPRECATED SPDX license identifier. Use 'AGPL-3.0-only' or 'AGPL-3.0-or-later' instead", Line: 39, Column: 3}, + ValidationWarning{Key: "legal.license", Description: "'GPL-2.0' is a DEPRECATED SPDX license identifier. Use 'GPL-2.0-only' or 'GPL-2.0-or-later' instead", Line: 39, Column: 3}, + ValidationWarning{Key: "legal.license", Description: "'eCos-2.0' is a DEPRECATED SPDX license identifier. Use its replacement from https://spdx.org/licenses instead", Line: 39, Column: 3}, + }, } dir := "testdata/v0/valid_with_warnings/no-network/"