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

48 lines
1.2 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 = {
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'
}
}
//
// Fetch all public GitHub repos
//
2020-06-16 11:12:27 +02:00
export default async function fetchGitHubRepos() {
2019-05-14 20:52:48 +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)
if (response.status !== 200) {
logError(`Non-200 response code from GitHub: ${response.status}`)
return null
}
const json = await response.json()
2019-08-19 13:01:25 +02:00
const numbers = []
2019-05-14 20:52:48 +02:00
json.map(item => {
if (item.stargazers_count) {
return numbers.push(item.stargazers_count)
}
return null
})
const stars = arrSum(numbers)
const repositories = json.length
log(
'Re-fetched GitHub. ' +
2019-05-14 20:52:48 +02:00
`Total: ${repositories} public projects with a total of ${stars} stargazers. ` +
`Elapsed: ${new Date() - start}ms`
)
return { stars, repositories }
}