Skip to content

Commit

Permalink
Merge pull request #1024 from lumaxis/add-workflow-update-licensedb-m…
Browse files Browse the repository at this point in the history
…apping

Add Actions workflow to regularly update LicenseDB mapping
  • Loading branch information
lumaxis authored Feb 13, 2024
2 parents f6e969d + 8cf40c4 commit 6f930d7
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
File renamed without changes.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml → .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
name: Run tests

on:
push:
branches:
Expand Down
41 changes: 41 additions & 0 deletions .github/workflows/update-scancode-licensedb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Update mapping from ScanCode LicenseDB data

on:
schedule:
- cron: '43 5 * * 0'
workflow_dispatch: # This allows for manual triggering of the workflow

permissions:
contents: write
pull-requests: write

jobs:
update-database:
name: Update mapping
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v4.1.1

- name: Setup Node.js
uses: actions/setup-node@v4.0.1

- name: Fetch ScanCode LicenseDB
run: |
curl -o scancode-licensedb.json https://scancode-licensedb.aboutcode.org/index.json
- name: Transform JSON and Update scancodeMap.js
run: |
node ./scripts/transform-scancode-licensedb.js
- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
with:
add-paths: lib/scancodeMap.js
commit-message: Update ScanCode license mapping
branch: update-scancode-licensedb
title: Update license mapping with latest ScanCode LicenseDB data
body: |
This pull request updates the license mapping with the latest ScanCode LicenseDB data.
For more information, see https://github.com/nexB/scancode-licensedb
55 changes: 55 additions & 0 deletions scripts/transform-scancode-licensedb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Script to transform the scancode-licensedb data and write it to scancodeMap.js

/**
* @typedef {Object} LicenseEntry
* @property {string} license_key - Unique key for the license.
* @property {string} category - Category of the license.
* @property {string} spdx_license_key - Corresponding SPDX license key.
* @property {string[]} other_spdx_license_keys - Array of other SPDX license keys, if any.
* @property {boolean} is_exception - Indicates if the entry is an exception.
* @property {boolean} is_deprecated - Indicates if the license is deprecated.
* @property {string} json - Relative path to the JSON file for the license.
* @property {string} yaml - Relative path to the YAML file for the license.
* @property {string} html - Relative path to the HTML file for the license.
* @property {string} license - Relative path to the LICENSE file for the license.
*/

const fs = require('fs')

// Read the downloaded JSON file
const rawData = fs.readFileSync('scancode-licensedb.json')
const scancodeData = JSON.parse(rawData.toString())

// Transform the data
const transformedData = transformScancodeData(scancodeData)

// Write the transformed data to scancodeMap.js
fs.writeFileSync('lib/scancodeMap.js', generateScancodeMapContent(transformedData))

/**
* @param {LicenseEntry[]} data
* @returns {string[][]}
*/
function transformScancodeData(data) {
// Map the data to the required format
return data.map(entry => [entry.license_key, entry.spdx_license_key])
}

/**
* @param {string[][]} data
*/
function generateScancodeMapContent(data) {
// Convert the array pairs into string format
const content = data.map(([licenseKey, spdxLicenseKey]) => ` ['${licenseKey}', '${spdxLicenseKey}']`)
return `// Copyright (c) Microsoft Corporation and others. Licensed under the MIT license.
// SPDX-License-Identifier: MIT
// *** THIS FILE IS AUTO-GENERATED by service/.github/workflows/transform-scancode-licensedb.js ***
// *** DO NOT EDIT MANUALLY ***
//
// This is a mapping from scancode key to SPDX identifier based on https://scancode-licensedb.aboutcode.org/index.json
// See licenses in https://github.com/nexB/scancode-toolkit/blob/develop/src/licensedcode/data/licenses/
module.exports = new Map([
${content.join(',\n')}
]);`
}

0 comments on commit 6f930d7

Please sign in to comment.