Retrieve null licenses from licenses API

This commit is contained in:
cnagadya
2022-10-13 11:03:34 +00:00
parent 0cd2781117
commit ba9d7c1389
3 changed files with 1706 additions and 22 deletions
+1650 -18
View File
File diff suppressed because it is too large Load Diff
+1
View File
@@ -32,6 +32,7 @@
"ansi-styles": "^6.2.1", "ansi-styles": "^6.2.1",
"got": "^12.5.2", "got": "^12.5.2",
"nodemon": "^2.0.20", "nodemon": "^2.0.20",
"octokit": "^2.0.7",
"yaml": "^2.1.3", "yaml": "^2.1.3",
"zod": "^3.19.1" "zod": "^3.19.1"
}, },
+55 -4
View File
@@ -1,3 +1,5 @@
import * as core from '@actions/core'
import {Octokit} from 'octokit'
import {Change} from './schemas' import {Change} from './schemas'
/** /**
@@ -10,21 +12,27 @@ import {Change} from './schemas'
* we will ignore the deny list. * we will ignore the deny list.
* @param {Change[]} changes The list of changes to filter. * @param {Change[]} changes The list of changes to filter.
* @param { { allow?: string[], deny?: string[]}} licenses An object with `allow`/`deny` keys, each containing a list of licenses. * @param { { allow?: string[], deny?: string[]}} licenses An object with `allow`/`deny` keys, each containing a list of licenses.
* @returns {[Array<Change>, Array<Change]} A tuple where the first element is the list of denied changes and the second one is the list of changes with unknown licenses * @returns {Promise<[Array.<Change>, Array.<Change>]>} A promise to a 2 element tuple. The first element is the list of denied changes and the second one is the list of changes with unknown licenses
*/ */
export function getDeniedLicenseChanges( export async function getDeniedLicenseChanges(
changes: Change[], changes: Change[],
licenses: { licenses: {
allow?: string[] allow?: string[]
deny?: string[] deny?: string[]
} }
): [Change[], Change[]] { ): Promise<[Change[], Change[]]> {
const {allow, deny} = licenses const {allow, deny} = licenses
const disallowed: Change[] = [] const disallowed: Change[] = []
const unknown: Change[] = [] const unknown: Change[] = []
for (const change of changes) { const consolidatedChanges = changes.some(
({source_repository_url, license}) => !license && source_repository_url
)
? await setGHLicenses(changes)
: changes
for (const change of consolidatedChanges) {
if (change.change_type === 'removed') { if (change.change_type === 'removed') {
continue continue
} }
@@ -47,3 +55,46 @@ export function getDeniedLicenseChanges(
return [disallowed, unknown] return [disallowed, unknown]
} }
const fetchGHLicense = async (
owner: string,
repo: string
): Promise<string | null> => {
const octokit = new Octokit({
auth: core.getInput('repo-token', {required: true})
})
let response
try {
response = await octokit.request('GET /repos/{owner}/{repo}/license', {
owner,
repo
})
} catch (_) {
return null
}
return response?.data?.license?.spdx_id ?? null
}
const setGHLicenses = async (changes: Change[]): Promise<Change[]> => {
const updatedChanges = changes.map(async change => {
const {source_repository_url, license} = change
if (license || source_repository_url === null) {
return change
}
const repoNwo = source_repository_url.split('github.com/')[1]
const [owner, repo] = repoNwo.split('/')
const retrievedLicense = await fetchGHLicense(owner, repo)
return {
...change,
license: retrievedLicense
}
})
return await Promise.all(updatedChanges)
}