1
0
mirror of https://github.com/oceanprotocol/community-numbers.git synced 2024-06-17 18:03:16 +02:00
community-numbers/api/networks/github.js

55 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-06-16 11:12:27 +02:00
import fetch from 'node-fetch'
import { log, logError, arrSum } from '../utils'
2019-05-14 20:52:48 +02:00
// Request options for all fetch calls
const options = {
2020-06-16 12:32:47 +02:00
headers: {
// For getting topics, see note on https://developer.github.com/v3/search/
// Accept: 'application/vnd.github.mercy-preview+json'
Accept: 'application/vnd.github.preview'
}
2019-05-14 20:52:48 +02:00
}
//
// Fetch all public GitHub repos
//
2020-06-16 11:12:27 +02:00
export default async function fetchGitHubRepos() {
2020-06-16 12:32:47 +02:00
const url =
'https://api.github.com/orgs/oceanprotocol/repos?type=public&per_page=200'
const start = Date.now()
const response = await fetch(url, options)
2019-05-14 20:52:48 +02:00
2020-06-16 12:32:47 +02:00
if (response.status !== 200) {
logError(`Non-200 response code from GitHub: ${response.status}`)
return null
}
2019-05-14 20:52:48 +02:00
2021-02-02 13:34:50 +01:00
const jsonRepos = await response.json()
const starsArray = []
const contribArray = []
jsonRepos.forEach(async (item) => {
starsArray.push(item.stargazers_count)
const responseContrib = await fetch(
`https://api.github.com/orgs/oceanprotocol/${item.name}/stats/contributors`,
options
)
const jsonContrib = await responseContrib.json()
contribArray.push(jsonContrib.total)
})
const stars = arrSum(starsArray)
const repositories = jsonRepos.length
const contributors = arrSum(contribArray)
2019-05-14 20:52:48 +02:00
2020-06-16 12:32:47 +02:00
log(
2020-06-16 15:16:31 +02:00
'✓ GitHub. ' +
2021-02-02 13:34:50 +01:00
`Total: ${repositories} public projects with a total of ${stars} stargazers & ${contributors} contributors. ` +
2020-06-16 12:32:47 +02:00
`Elapsed: ${new Date() - start}ms`
)
2019-05-14 20:52:48 +02:00
2021-02-02 13:34:50 +01:00
return { stars, repositories, contributors }
2019-05-14 20:52:48 +02:00
}