community-numbers/api/networks/github.js

71 lines
2.0 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'
2021-02-02 22:12:57 +01:00
Accept: 'application/vnd.github.preview',
Authorization: `token ${process.env.GITHUB_TOKEN}`
2020-06-16 12:32:47 +02:00
}
2019-05-14 20:52:48 +02:00
}
2021-02-02 22:12:57 +01:00
function getContributors(contributors) {
2021-02-02 22:35:49 +01:00
const filtered = contributors
// filter out duplicate contributions based on author.login
.filter(
(v, i, a) => a.findIndex((t) => t.author.login === v.author.login) === i
)
.map((item) => item)
2021-02-02 22:12:57 +01:00
2021-02-02 22:35:49 +01:00
return filtered.length
2021-02-02 22:12:57 +01:00
}
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 =
2021-02-02 22:12:57 +01:00
'https://api.github.com/orgs/oceanprotocol/repos?type=public&per_page=100'
2020-06-16 12:32:47 +02:00
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()
2021-02-02 22:12:57 +01:00
// filter out forks from public repos
const jsonReposCleaned = jsonRepos.filter((repo) => repo.fork === false)
2021-02-02 13:34:50 +01:00
const starsArray = []
const contribArray = []
2021-02-02 22:12:57 +01:00
for (let index = 0; index < jsonReposCleaned.length; index++) {
const item = jsonReposCleaned[index]
2021-02-02 13:34:50 +01:00
starsArray.push(item.stargazers_count)
const responseContrib = await fetch(
2021-02-02 22:12:57 +01:00
`https://api.github.com/repos/oceanprotocol/${item.name}/stats/contributors`,
2021-02-02 13:34:50 +01:00
options
)
const jsonContrib = await responseContrib.json()
2021-02-02 22:12:57 +01:00
jsonContrib && contribArray.push(jsonContrib[0])
}
2021-02-02 13:34:50 +01:00
const stars = arrSum(starsArray)
2021-02-02 22:12:57 +01:00
const repositories = jsonReposCleaned.length
const contributors = getContributors(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
}