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

40 lines
1.1 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
2020-06-16 12:32:47 +02:00
const json = await response.json()
2020-06-16 16:49:17 +02:00
const numbers = json.map((item) => item.stargazers_count)
2020-06-16 12:32:47 +02:00
const stars = arrSum(numbers)
const repositories = json.length
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. ' +
2020-06-16 12:32:47 +02:00
`Total: ${repositories} public projects with a total of ${stars} stargazers. ` +
`Elapsed: ${new Date() - start}ms`
)
2019-05-14 20:52:48 +02:00
2020-06-16 12:32:47 +02:00
return { stars, repositories }
2019-05-14 20:52:48 +02:00
}