Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
trustworthy-module-registry/src/metrics/LicenseCalculator.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
49 lines (41 sloc)
1.4 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { MetricCalculator } from './MetricCalculator'; | |
export class LicenseCalculator extends MetricCalculator { | |
private explicitlyCompatibleLicenses: string[] = ['MIT', 'GPLv2', 'GPLv3', 'LGPLv2.1', 'LGPLv3']; | |
private licensesNeedingConsideration: string[] = ['Apache-2.0', 'BSD-2-Clause', 'BSD-3-Clause']; | |
private license: string; | |
constructor(data: any) { | |
super(data); | |
if (data?.data?.repository?.licenseInfo?.spdxId) { | |
this.license = data.data.repository.licenseInfo.spdxId; | |
} else if (data?.spdxId) { | |
this.license = data.spdxId; | |
} else if (typeof data === 'string') { | |
// Handle case where data is the license spdxId as a string | |
this.license = data; | |
} else { | |
this.license = ''; | |
} | |
} | |
/** | |
* Calculates the license metric. | |
* @returns a score between 0 and 1. | |
*/ | |
calculate(): number { | |
if (!this.validateData()) { | |
return 0; | |
} | |
if (this.explicitlyCompatibleLicenses.includes(this.license)) { | |
return 1; | |
} | |
if (this.licensesNeedingConsideration.includes(this.license)) { | |
return 0.5; | |
} | |
return 0; | |
} | |
/** | |
* Validates the input data. | |
*/ | |
override validateData(): boolean { | |
return super.validateData() && typeof this.license === 'string' && this.license !== ''; | |
} | |
} |