Skip to content

Commit

Permalink
Optimise setGHLicenses
Browse files Browse the repository at this point in the history
Co-authored-by: Henri Maurer <hmaurer@github.com>
Co-authored-by: Federico Builes <febuiles@github.com>
  • Loading branch information
3 people committed Oct 13, 2022
1 parent ba9d7c1 commit 2e3713a
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/licenses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,37 +64,53 @@ const fetchGHLicense = async (
auth: core.getInput('repo-token', {required: true})
})

let response
try {
response = await octokit.request('GET /repos/{owner}/{repo}/license', {
owner,
repo
})
const response = await octokit.request(
'GET /repos/{owner}/{repo}/license',
{
owner,
repo
}
)
return response.data.license?.spdx_id ?? null
} catch (_) {
return null
}
}

return response?.data?.license?.spdx_id ?? null
const parseGitHubURL = (url: string): {owner: string; repo: string} | null => {
try {
const parsed = new URL(url)
if (parsed.host !== 'github.com') {
return null
}
const components = parsed.pathname.split('/')
if (components.length < 3) {
return null
}
return {owner: components[1], repo: components[2]}
} catch (_) {
return 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) {
if (change.license !== null || change.source_repository_url === null) {
return change
}

const repoNwo = source_repository_url.split('github.com/')[1]
const [owner, repo] = repoNwo.split('/')
const githubUrl = parseGitHubURL(change.source_repository_url)

const retrievedLicense = await fetchGHLicense(owner, repo)
if (githubUrl === null) {
return change
}

return {
...change,
license: retrievedLicense
license: await fetchGHLicense(githubUrl.owner, githubUrl.repo)
}
})

return await Promise.all(updatedChanges)
return Promise.all(updatedChanges)
}

0 comments on commit 2e3713a

Please sign in to comment.