This repository was archived by the owner on Oct 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 552
/
Copy pathfetch-archive.js
214 lines (189 loc) · 6.38 KB
/
fetch-archive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const path = require('path')
const fs = require('fs-extra')
const axios = require('axios')
const { compact, map, find, fromPairs, mapValues, partial, reverse, sortBy } = require('lodash')
const fetch = require('node-fetch')
const dateFns = require('date-fns')
const { Gitlab } = require('gitlab')
const twitterFollowersCount = require('twitter-followers-count')
const throttleConcurrency = require('./throttle-concurrency')
const config = {}
let github, gitlab, getTwitterFollowers
function githubInit(token) {
const throttledAxios = throttleConcurrency(axios, 20, 30000)
const githubRequest = async (method, endpoint, params = {}, data) => {
const url = `https://api.github.com/${endpoint}`
const headers = { Authorization: `token ${token}` }
try {
return throttledAxios({
url,
method,
params,
headers,
...(data ? { data } : {}),
})
} catch (err) {
console.error(err)
}
}
return ['get', 'post', 'patch'].reduce((obj, method) => {
return { ...obj, [method]: partial(githubRequest, method) }
}, {})
}
function init(tokens) {
github = githubInit(tokens.githubToken)
gitlab = new Gitlab({ personaltoken: tokens.gitlabToken })
getTwitterFollowers = twitterFollowersCount({
consumer_key: tokens.twitterConsumerKey,
consumer_secret: tokens.twitterConsumerSecret,
access_token_key: tokens.twitterAccessTokenKey,
access_token_secret: tokens.twitterAccessTokenSecret,
})
}
async function getProjectGitHubData(repo) {
const { data } = await github.get(`repos/${repo}`)
const { stargazers_count, forks_count, open_issues_count } = data
return { s: stargazers_count, fk: forks_count, i: open_issues_count }
}
async function getProjectGitLabData(repo) {
const { id, star_count, forks_count } = await gitlab.Projects.show(repo)
const open_issues_count = (await gitlab.Issues.all({ projectId: id, state: 'opened' })).length
return { s: star_count, fk: forks_count, i: open_issues_count }
}
async function getAllProjectRepoData(repos) {
const getRepoData = {
github: getProjectGitHubData,
gitlab: getProjectGitLabData,
}
let count = 0
const reportRepoReceived = repo => {
count++
console.log(`received ${count}/${repos.length} ${repo}`)
}
const data = repos.map(async ({ repo, repohost = 'github' }, idx, arr) => {
const repoData = await getRepoData[repohost](repo)
reportRepoReceived(repo)
return [repo, repoData]
}, [])
return fromPairs(await Promise.all(data))
}
async function getAllProjectData(projects) {
const twitterScreenNames = compact(map(projects, 'twitter'))
const twitterFollowers =
twitterScreenNames.length && (await getTwitterFollowers(twitterScreenNames))
const repos = compact(map(projects, ({ repo, repohost }) => ({ repo, repohost })))
const reposData = await getAllProjectRepoData(repos)
return projects.map(({ id, repo, twitter }) => {
const twitterData = twitter ? { f: twitterFollowers[twitter] } : {}
const repoData = repo ? reposData[repo] : {}
return { id, ...twitterData, ...repoData }
})
}
async function getLocalArchive() {
try {
return await fs.readJson(path.join(process.cwd(), config.localArchivePath))
} catch (e) {
console.log('Local archive not found, fetching new data.')
}
}
function updateLocalArchive(data) {
return fs.outputJson(path.join(process.cwd(), config.localArchivePath), data)
}
function getArchiveJson(archive) {
if (archive.truncated) {
return fetch(archive.raw_url).then(resp => resp.json())
}
return JSON.parse(archive.content)
}
async function getArchiveId() {
const gists = await github.get('gists')
const gistArchive = find(gists.data, {
description: config.gistArchiveDescription,
})
if (gistArchive) {
return gistArchive.id
}
console.log('No gist archive found. Creating new gist.')
await createGist()
return getArchiveId()
}
async function getArchive() {
const gistArchiveId = await getArchiveId()
const gistArchiveContent = await github.get(`gists/${gistArchiveId}`)
const archive = gistArchiveContent.data.files[config.archiveFilename]
return getArchiveJson(archive)
}
function createGist(content) {
return github.post(
'gists',
{},
{
files: { [config.archiveFilename]: { content: '[]' } },
public: true,
description: config.gistArchiveDescription,
}
)
}
async function editGist(content) {
const gistArchiveId = await getArchiveId()
const files = { [config.archiveFilename]: { content } }
return github.patch(`gists/${gistArchiveId}`, {}, { files })
}
async function updateArchive(data, archive) {
const updatedArchive = [[Date.now(), data], ...(archive || [])]
// truncate to 60 days of data
const truncatedArchive =
updatedArchive.length <= 60
? updatedArchive
: reverse(sortBy(updatedArchive, ([timestamp]) => timestamp)).slice(0, 60)
const content = JSON.stringify(truncatedArchive)
await editGist(content)
return truncatedArchive
}
/**
* Check if archive is expired. Currently set to 1410 minutes, which is 30
* minutes short of 24 hours, just in case a daily refresh webhook gets called
* a little early.
*/
function archiveExpired(archive) {
if (!archive.length) {
return true
}
const timestamps = archive.map(([timestamp]) => timestamp)
const newestTimestamp = dateFns.max(timestamps).getTime()
return dateFns.differenceInMinutes(Date.now(), newestTimestamp) > 1410
}
function expand(data) {
return data.map(([timestamp, datum]) => {
const expanded = datum.map(({ id, s, fk, i, f }) => ({
id,
stars: s,
forks: fk,
issues: i,
followers: f,
}))
return [timestamp, expanded]
})
}
module.exports = async function run(
projects,
{ archiveFilename, localArchivePath, gistArchiveDescription, ...tokens }
) {
config.archiveFilename = archiveFilename
config.localArchivePath = localArchivePath
config.gistArchiveDescription = gistArchiveDescription
const localArchive = await getLocalArchive()
if (localArchive && !archiveExpired(localArchive)) {
return expand(localArchive)
}
init(tokens)
const archive = await getArchive()
if (archive && !archiveExpired(archive)) {
await updateLocalArchive(archive)
return expand(archive)
}
const projectData = await getAllProjectData(projects)
const updatedArchive = await updateArchive(projectData, archive)
await updateLocalArchive(updatedArchive)
return expand(updatedArchive)
}