chore: use public/free github api to avoid hitting rate limit (#605)

pull/606/head
Amio Jin 2023-06-10 18:29:51 +08:00 zatwierdzone przez GitHub
rodzic e35c8ff1ea
commit 169ae69aae
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 57 dodań i 21 usunięć

Wyświetl plik

@ -171,6 +171,11 @@ async function contributors ({ owner, repo }: PathArgs) {
}
}
async function meta ({ owner, repo }: PathArgs): Promise<any> {
const meta = await got(`https://api.github.com/repos/${owner}/${repo}`).json()
return meta
}
async function downloads ({ owner, repo, tag }: PathArgs) {
const releaseSelection = tag ? `tags/${tag}` : 'latest'
const release = await restGithub(`repos/${owner}/${repo}/releases/${releaseSelection}`)
@ -328,6 +333,45 @@ const makeRepoQuery = (topic, owner, repo, restArgs) => {
}
async function repoStats ({topic, owner, repo, ...restArgs}: PathArgs) {
switch (topic) {
case 'forks':
const { forks } = await meta({ owner, repo })
return {
subject: topic,
status: millify(forks),
color: 'blue'
}
case 'watchers':
const { watchers_count } = await meta({ owner, repo })
return {
subject: topic,
status: millify(watchers_count),
color: 'blue'
}
case 'open-issues':
const { open_issues_count } = await meta({ owner, repo })
return {
subject: topic,
status: millify(open_issues_count),
color: 'blue'
}
case 'stars':
const { stargazers_count } = await meta({ owner, repo })
return {
subject: topic,
status: millify(stargazers_count),
color: 'blue'
}
case 'license':
const { license } = await meta({ owner, repo })
return {
subject: topic,
status: license ? license.spdx_id : 'no license',
color: license ? 'blue' : 'grey'
}
}
// Use graphql when we cannot simply get info from public/free api
const result = await makeRepoQuery(topic, owner, repo, restArgs)
if (!result) {
@ -339,8 +383,6 @@ async function repoStats ({topic, owner, repo, ...restArgs}: PathArgs) {
}
switch (topic) {
case 'watchers':
case 'forks':
case 'issues':
case 'releases':
return {
@ -355,18 +397,12 @@ async function repoStats ({topic, owner, repo, ...restArgs}: PathArgs) {
status: millify(result.refs.totalCount),
color: 'blue'
}
case 'stars':
return {
subject: topic,
status: millify(result.stargazers.totalCount),
color: 'blue'
}
case 'open-issues':
return {
subject: 'open issues',
status: millify(result.issues.totalCount),
color: result.issues.totalCount === 0 ? 'green' : 'orange'
}
// case 'open-issues':
// return {
// subject: 'open issues',
// status: millify(result.issues.totalCount),
// color: result.issues.totalCount === 0 ? 'green' : 'orange'
// }
case 'closed-issues':
return {
subject: 'closed issues',
@ -417,13 +453,13 @@ async function repoStats ({topic, owner, repo, ...restArgs}: PathArgs) {
status: version(latestTag),
color: 'blue'
}
case 'license':
const li = result.licenseInfo
return {
subject: topic,
status: li ? li.spdxId : 'no license',
color: li ? 'blue' : 'grey'
}
// case 'license':
// const li = result.licenseInfo
// return {
// subject: topic,
// status: li ? li.spdxId : 'no license',
// color: li ? 'blue' : 'grey'
// }
case 'last-commit':
const commits = result.branch.target.history.nodes
const lastDate = commits.length && new Date(commits[0].committedDate)