Skip to content

Commit

Permalink
Merge pull request #1234 from yashkohli88/yk/sourcearchive-declared-l…
Browse files Browse the repository at this point in the history
…icense

Extract license information from pom.xml for sourcearchive
  • Loading branch information
qtomlinson authored Jan 15, 2025
2 parents 9ca862a + c1895f8 commit 1d8ff60
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 12 deletions.
23 changes: 15 additions & 8 deletions providers/summary/clearlydefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,7 @@ class ClearlyDescribedSummarizer {
return urls
}

addMavenData(result, data, coordinates) {
const urls = this.getMavenUrls(coordinates)

setIfValue(result, 'described.releaseDate', extractDate(data.releaseDate))
setIfValue(result, 'described.urls.registry', urls.registry)
setIfValue(result, 'described.urls.version', `${get(result, 'described.urls.registry')}/${coordinates.revision}`)
setIfValue(result, 'described.urls.download', urls.download)
getDeclaredLicenseMaven(data) {
const projectSummaryLicenses =
get(data, 'manifest.summary.licenses') || get(data, 'manifest.summary.project.licenses') // the project layer was removed in 1.2.0
if (!projectSummaryLicenses) return
Expand All @@ -192,7 +186,18 @@ class ClearlyDescribedSummarizer {
const licenseNames = uniq(flatten(licenseSummaries.map(license => license.name)))
let licenses = licenseUrls.map(extractLicenseFromLicenseUrl).filter(x => x)
if (!licenses.length) licenses = licenseNames.map(x => SPDX.lookupByName(x) || x).filter(x => x)
if (licenses.length) setIfValue(result, 'licensed.declared', SPDX.normalize(licenses.join(' OR ')))
return licenses
}

addMavenData(result, data, coordinates) {
const urls = this.getMavenUrls(coordinates)

setIfValue(result, 'described.releaseDate', extractDate(data.releaseDate))
setIfValue(result, 'described.urls.registry', urls.registry)
setIfValue(result, 'described.urls.version', `${get(result, 'described.urls.registry')}/${coordinates.revision}`)
setIfValue(result, 'described.urls.download', urls.download)
const licenses = this.getDeclaredLicenseMaven(data)
if (licenses?.length) setIfValue(result, 'licensed.declared', SPDX.normalize(licenses.join(' OR ')))
}

addCondaData(result, data, coordinates) {
Expand Down Expand Up @@ -239,6 +244,8 @@ class ClearlyDescribedSummarizer {
'described.urls.download',
`https://repo1.maven.org/maven2/${namespaceAsFolders}/${coordinates.name}/${coordinates.revision}/${coordinates.name}-${coordinates.revision}.jar`
)
const licenses = this.getDeclaredLicenseMaven(data)
if (licenses?.length) setIfValue(result, 'licensed.declared', SPDX.normalize(licenses.join(' OR ')))
}

addNuGetData(result, data, coordinates) {
Expand Down
79 changes: 75 additions & 4 deletions test/summary/clearlydefinedTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,37 @@ describe('ClearlyDescribedSummarizer addNuGetData', () => {
})
})

describe('ClearlyDescribedSummarizer getDeclaredLicenseMaven', () => {
const data = new Map([
[[{ name: ['Apache-2.0'], url: ['https://opensource.org/licenses/Apache-2.0'] }], ['Apache-2.0']],
[[{ name: ['The MIT License (MIT)'], url: ['http://opensource.org/licenses/MIT'] }], ['MIT']],
[
[
{ name: ['LGPL 2.1'], url: ['https://www.gnu.org/licenses/lgpl-2.1.html'] },
{ name: ['Apache License 2.0'], url: ['https://www.apache.org/licenses/LICENSE-2.0'] }
],
['LGPL-2.1', 'Apache-2.0']
],
[
[{ name: ['CDDL + GPLv2 with classpath exception'], url: ['https://oss.oracle.com/licenses/CDDL+GPL-1.1'] }],
['CDDL + GPLv2 with classpath exception']
],
[[{ name: ['NOASSERTION'], url: ['https://something.com'] }], ['NOASSERTION']],
[[{ name: [''], url: [''] }], []]
])

it('should return licenses extracted from manifest.summary', () => {
data.forEach((expected, license) => {
const licenses = summarizer.getDeclaredLicenseMaven({ manifest: { summary: { licenses: [{ license }] } } })
const licenseProject = summarizer.getDeclaredLicenseMaven({
manifest: { summary: { project: { licenses: [{ license }] } } }
})
assert.deepEqual(licenses, expected)
assert.deepEqual(licenseProject, expected)
})
})
})

describe('ClearlyDescribedSummarizer addMavenData', () => {
const expectedUrls = {
download: 'https://repo1.maven.org/maven2/io/clearlydefined/test/1.0/test-1.0.jar',
Expand Down Expand Up @@ -614,6 +645,7 @@ describe('ClearlyDescribedSummarizer addMavenData', () => {
version: 'https://maven.google.com/web/index.html#io.clearlydefined:test:1.0/1.0'
}
const expectedResult = { described: { urls: expectedUrls } }

it('should set declared license from manifest licenseUrl', () => {
const data = {
'https://opensource.org/licenses/MIT': 'MIT',
Expand Down Expand Up @@ -670,14 +702,17 @@ describe('ClearlyDescribedSummarizer addMavenData', () => {
})

describe('ClearlyDescribedSummarizer addSourceArchiveData', () => {
const sourceArchiveTestCoordinates = EntityCoordinates.fromString(
'sourcearchive/mavencentral/io.clearlydefined/test/1.0'
)
const expectedUrls = {
download: 'https://repo1.maven.org/maven2/io/clearlydefined/test/1.0/test-1.0.jar',
registry: 'https://repo1.maven.org/maven2/io/clearlydefined/test',
version: 'https://repo1.maven.org/maven2/io/clearlydefined/test/1.0'
}

const expectedResult = { described: { urls: expectedUrls } }
it('should set the correct urls', () => {

it('should set the correct urls and license', () => {
const data = {
'https://opensource.org/licenses/MIT': 'MIT'
}
Expand All @@ -687,15 +722,51 @@ describe('ClearlyDescribedSummarizer addSourceArchiveData', () => {
summarizer.addSourceArchiveData(
result,
{ manifest: { summary: { project: { licenses: [{ license: { url } }] } } } },
testCoordinates
sourceArchiveTestCoordinates
)
if (data[url])
assert.deepEqual(result, {
...expectedResult
...expectedResult,
licensed: { declared: data[url] }
})
else assert.deepEqual(result, expectedResult)
}
})

it('should set the correct urls and no license when data section is empty', () => {
let result = {}
summarizer.addSourceArchiveData(result, {}, testCoordinates)
assert.deepEqual(result, expectedResult)
})

it('should set declared license from manifest license name and URL', () => {
const data = new Map([
[[{ name: ['Apache-2.0'], url: ['https://opensource.org/licenses/Apache-2.0'] }], 'Apache-2.0'],
[[{ name: ['The MIT License (MIT)'], url: ['http://opensource.org/licenses/MIT'] }], 'MIT'],
[
[
{ name: ['LGPL 2.1'], url: ['https://www.gnu.org/licenses/lgpl-2.1.html'] },
{ name: ['Apache License 2.0'], url: ['https://www.apache.org/licenses/LICENSE-2.0'] }
],
'LGPL-2.1 OR Apache-2.0'
],
[
[{ name: ['CDDL + GPLv2 with classpath exception'], url: ['https://oss.oracle.com/licenses/CDDL+GPL-1.1'] }],
'NOASSERTION'
]
])

data.forEach((expected, license) => {
let result = {}
summarizer.addSourceArchiveData(
result,
{ manifest: { summary: { licenses: [{ license }] } } },
sourceArchiveTestCoordinates
)
if (expected) assert.deepEqual(result, { described: { urls: expectedUrls }, licensed: { declared: expected } })
else assert.deepEqual(result, expectedResult)
})
})
})

describe('ClearlyDescribedSummarizer addDebData', () => {
Expand Down

0 comments on commit 1d8ff60

Please sign in to comment.