|
| 1 | +import Ajv from 'ajv/dist/jtd.js' |
| 2 | + |
| 3 | +const ABOUTCODE_LICENSE_DB = |
| 4 | + 'https://scancode-licensedb.aboutcode.org/index.json' |
| 5 | +const SPDX_LICENSE_DB = |
| 6 | + 'https://raw.githubusercontent.com/composer/spdx-licenses/refs/heads/main/res/spdx-licenses.json' |
| 7 | + |
| 8 | +const ajv = new Ajv() |
| 9 | + |
| 10 | +const inputSchema = /** @type {const} */ ({ |
| 11 | + additionalProperties: true, |
| 12 | + properties: { |
| 13 | + document: { |
| 14 | + additionalProperties: true, |
| 15 | + properties: { |
| 16 | + license_expression: { |
| 17 | + type: 'string', |
| 18 | + }, |
| 19 | + }, |
| 20 | + }, |
| 21 | + }, |
| 22 | +}) |
| 23 | + |
| 24 | +const validateInput = ajv.compile(inputSchema) |
| 25 | + |
| 26 | +/** |
| 27 | + * Read JSON from given URL |
| 28 | + * @param {string | URL | Request} dataUrl |
| 29 | + * @returns |
| 30 | + */ |
| 31 | +async function readJson(dataUrl) { |
| 32 | + /** @type {any} */ |
| 33 | + const headers = { Accept: 'application/vnd.github.v3+json' } |
| 34 | + const response = await fetch(dataUrl, { headers }) |
| 35 | + if (!response.ok) { |
| 36 | + throw new Error(`Response status: ${response.status}`) |
| 37 | + } |
| 38 | + |
| 39 | + return await response.json() |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Read considered licenses from SPDX and About Code |
| 44 | + * @returns {Promise<Set<string>>} |
| 45 | + */ |
| 46 | +async function readConsideredLicenses() { |
| 47 | + /** @type {Array<{ license_key: string; spdx_license_key: string }>} */ |
| 48 | + const aboutcodeLicenses = await readJson(ABOUTCODE_LICENSE_DB) |
| 49 | + /** @type {Record<string, []>} */ |
| 50 | + const spdxLicenses = await readJson(SPDX_LICENSE_DB) |
| 51 | + |
| 52 | + const consideredLicenses = new Set( |
| 53 | + aboutcodeLicenses.map((aboutCode) => aboutCode.license_key) |
| 54 | + ) |
| 55 | + Object.keys(spdxLicenses).forEach((item) => consideredLicenses.add(item)) |
| 56 | + return consideredLicenses |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * It MUST be tested that the all license identifiers and exceptions are listed either |
| 61 | + * in the official SPDX license identifier list or AboutCode's "ScanCode LicenseDB". |
| 62 | + * @param {unknown} doc |
| 63 | + * @returns |
| 64 | + */ |
| 65 | +export async function informativeTest_6_3_17(doc) { |
| 66 | + const ctx = { |
| 67 | + infos: /** @type {Array<{ message: string; instancePath: string }>} */ ([]), |
| 68 | + } |
| 69 | + |
| 70 | + if (!validateInput(doc)) { |
| 71 | + return ctx |
| 72 | + } |
| 73 | + |
| 74 | + const consideredLicenses = await readConsideredLicenses() |
| 75 | + |
| 76 | + const licenseToCheck = doc.document.license_expression |
| 77 | + if (!consideredLicenses.has(licenseToCheck)) { |
| 78 | + ctx.infos.push({ |
| 79 | + instancePath: '/document/license_expression', |
| 80 | + message: `Invalid license: '${licenseToCheck}'`, |
| 81 | + }) |
| 82 | + } |
| 83 | + |
| 84 | + return ctx |
| 85 | +} |
0 commit comments