This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
unified-list.js
120 lines (107 loc) · 3.31 KB
/
unified-list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'use strict';
const fileReader = require('./file-reader');
const messages = require('./messages');
/** @type { Object } */
let unifiedList;
const foundInList = (license, listItem) => {
return (license === listItem.fedora_abbrev ||
license === listItem.fedora_name ||
license === listItem.spdx_abbrev ||
license === listItem.spdx_name);
};
const checkName = (name) => {
if (name === 'UNKNOWN') {
return name;
}
if (unifiedList[name]) {
return unifiedList[name].url;
}
console.error(`No URL was found for [${name}]`);
return 'UNKNOWN';
};
// checks the licenses found against properties of approved list
// to verify if the license name is found.
const findApproved = (approvedList, licensesFound) => {
const approvedFound = new Set();
approvedList.forEach((a) => {
licensesFound.forEach((lf) => {
if (foundInList(lf.license, a)) {
approvedFound.add(lf);
}
});
});
return approvedFound;
};
// checks the licenses found against properties of not approved list
// to verify if the license name is found.
const findNotApproved = (notApprovedList, licensesFound) => {
const notApprovedFound = new Set();
notApprovedList.forEach((a) => {
licensesFound.forEach((lf) => {
if (foundInList(lf.license, a) ||
lf.license.indexOf('*') === lf.license.length - 1) {
notApprovedFound.add(lf);
}
});
});
return notApprovedFound;
};
const findUnknown = (unknownList, licensesFound) => {
const unknownFound = new Set();
unknownList.forEach((a) => {
licensesFound.forEach((lf) => {
if (foundInList(lf.license, a)) {
unknownFound.add(lf);
}
});
});
return unknownFound;
};
const getLicenses = (projectMetaData) => {
const licensesFound = projectMetaData.dependencies.dependency.map((dep) => {
const licenseNames = dep.licenses.license.map((license) => {
return license.name;
}).join();
return { name: dep.packageName, version: dep.version, license: licenseNames };
});
return licensesFound;
};
const load = (ul) => {
unifiedList = fileReader.readAsJson(ul);
};
const urlForName = (name) => {
if (name.indexOf(',') !== -1) {
return name.split(',').map((name) => {
return checkName(name.trim());
}).join(', ');
}
return checkName(name);
};
const check = (projectMetaData) => {
const licensesFound = getLicenses(projectMetaData);
// gets the approved, not approved and unknown licenses
// from the default unified list file.
const approvedList = [];
const notApprovedList = [];
const unknownList = [];
Object.keys(unifiedList).forEach(key => {
if (unifiedList[key].approved === 'yes') {
approvedList.push(unifiedList[key]);
} else if (unifiedList[key].approved === 'no') {
notApprovedList.push(unifiedList[key]);
} else {
unknownList.push(unifiedList[key]);
}
});
const approvedFound = findApproved(approvedList, licensesFound);
const notApprovedFound = findNotApproved(notApprovedList, licensesFound);
const unknown = findUnknown(unknownList, licensesFound);
messages.print(approvedFound, '========= APPROVED LICENSES ==========');
messages.print(notApprovedFound, '========= NOT APPROVED LICENSES ==========');
messages.print(unknown, '========= CLARIFICATION ADVISED ==========');
};
module.exports = {
load,
urlForName,
check
};