-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathfetch-supporters.mjs
More file actions
255 lines (232 loc) · 7.62 KB
/
fetch-supporters.mjs
File metadata and controls
255 lines (232 loc) · 7.62 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
import lodash from 'lodash';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const { uniqBy } = lodash;
const asyncWriteFile = promisify(fs.writeFile);
const REQUIRED_KEYS = ['totalDonations', 'slug', 'name'];
const filename = '_supporters.json';
const absoluteFilename = path.resolve(
__dirname,
'..',
'components',
'Support',
filename
);
let graphqlEndpoint = 'https://api.opencollective.com/graphql/v2';
if (process.env.OPENCOLLECTIVE_API_KEY) {
// rate limit is 100 requests per minute with personal access token
// rate limit is 10 requests per minute without personal access token
console.log(
'Using personal access token to fetch supporters from OpenCollective'
);
// by default a personal access token of @chenxsan was used as I don't have access to the webpack one
// @doc https://graphql-docs-v2.opencollective.com/access#with-a-personal-token
graphqlEndpoint = `https://api.opencollective.com/graphql/v2?personalToken=${process.env.OPENCOLLECTIVE_API_KEY}`;
} else {
console.log(
'No personal access token found, using public API to fetch supporters from OpenCollective'
);
}
// https://github.com/opencollective/opencollective-api/blob/master/server/graphql/v2/query/TransactionsQuery.ts#L81
const graphqlPageSize = 1000;
const membersGraphqlQuery = `query account($limit: Int, $offset: Int) {
account(slug: "webpack") {
members(limit: $limit, offset: $offset) {
nodes {
account {
name
slug
website
imageUrl
}
totalDonations {
value
}
createdAt
}
}
}
}`;
// only query transactions in last year
const transactionsGraphqlQuery = `query transactions($dateFrom: DateTime, $limit: Int, $offset: Int) {
transactions(account: {
slug: "webpack"
}, dateFrom: $dateFrom, limit: $limit, offset: $offset, includeIncognitoTransactions: false) {
nodes {
amountInHostCurrency {
value
}
fromAccount {
name
slug
website
imageUrl
}
createdAt
}
}
}`;
const nodeToSupporter = (node) => ({
name: node.account.name,
slug: node.account.slug,
website: node.account.website,
avatar: node.account.imageUrl,
firstDonation: node.createdAt,
totalDonations: node.totalDonations.value * 100,
monthlyDonations: 0,
});
const getAllNodes = async (graphqlQuery, getNodes) => {
// Store original value
const originalTlsRejectUnauthorized =
process.env.NODE_TLS_REJECT_UNAUTHORIZED;
const isCI = process.env.CI === 'true' || (process.env.CI && process.env.VERCEL);
try {
// Only disable SSL verification in local development
if (!isCI) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
console.log('Running locally - SSL verification disabled');
}
const body = {
query: graphqlQuery,
variables: {
limit: graphqlPageSize,
offset: 0,
dateFrom: new Date(
new Date().setFullYear(new Date().getFullYear() - 1)
).toISOString(), // data from last year
},
};
let allNodes = [];
let limit = 10,
remaining = 10,
reset;
if (process.env.OPENCOLLECTIVE_API_KEY) {
limit = 100;
remaining = 100;
}
// Handling pagination if necessary
while (true) {
if (remaining === 0) {
console.log(`Rate limit exceeded. Sleeping until ${new Date(reset)}.`);
await new Promise((resolve) =>
setTimeout(resolve, reset - Date.now() + 100)
);
}
const fetchOptions = {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
},
};
const result = await fetch(graphqlEndpoint, fetchOptions).then(
async (response) => {
if (response.headers.get('content-type').includes('json')) {
const json = await response.json();
console.log('json', json);
if (json.error) {
// when rate limit exceeded, api won't return headers data like x-ratelimit-limit, etc.
remaining = 0;
reset = Date.now() + 1000 * 60; // 1 minute
} else {
limit = response.headers.get('x-ratelimit-limit') * 1;
remaining = response.headers.get('x-ratelimit-remaining') * 1;
reset = response.headers.get('x-ratelimit-reset') * 1000;
console.log(
`Rate limit: ${remaining}/${limit} remaining. Reset in ${new Date(
reset
)}`
);
}
return json;
} else {
// utilities/fetch-supporters: SyntaxError: Unexpected token < in JSON at position 0
console.log('something wrong when fetching supporters');
return {
error: {
message: await response.text(),
},
};
}
}
);
// when rate limit exceeded, api will return {error: {message: ''}}
// but we could hopefully avoid rate limit by sleeping in the beginning of the loop
// however, when there're multiple task running simultaneously, it's still possible to hit the rate limit
if (result.error) {
console.log('error', result.error);
// let the loop continue
} else {
const nodes = getNodes(result.data);
allNodes = [...allNodes, ...nodes];
body.variables.offset += graphqlPageSize;
if (nodes.length < graphqlPageSize) {
return allNodes;
} else {
// more nodes to fetch
}
}
}
} finally {
// Only restore if we modified it
if (!isCI) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = originalTlsRejectUnauthorized;
}
}
};
(async () => {
const members = await getAllNodes(
membersGraphqlQuery,
(data) => data.account.members.nodes
);
let supporters = members
.map(nodeToSupporter)
.sort((a, b) => b.totalDonations - a.totalDonations);
// Deduplicating supporters with multiple orders
supporters = uniqBy(supporters, 'slug');
const supportersBySlug = new Map();
for (const supporter of supporters) {
for (const key of REQUIRED_KEYS) {
if (!supporter || typeof supporter !== 'object') {
throw new Error(
`Supporters: ${JSON.stringify(supporter)} is not an object.`
);
}
if (!(key in supporter)) {
throw new Error(
`Supporters: ${JSON.stringify(supporter)} doesn't include ${key}.`
);
}
}
supportersBySlug.set(supporter.slug, supporter);
}
// Calculate monthly amount from transactions
const transactions = await getAllNodes(
transactionsGraphqlQuery,
(data) => data.transactions.nodes
);
for (const transaction of transactions) {
if (!transaction.amountInHostCurrency) continue;
const amount = transaction.amountInHostCurrency.value;
if (!amount || amount <= 0) continue;
const supporter = supportersBySlug.get(transaction.fromAccount.slug);
if (!supporter) continue;
supporter.monthlyDonations += (amount * 100) / 12;
}
for (const supporter of supporters) {
supporter.monthlyDonations = Math.round(supporter.monthlyDonations);
}
// Write the file
return asyncWriteFile(
absoluteFilename,
JSON.stringify(supporters, null, 2)
).then(() => console.log(`Fetched 1 file: ${filename}`));
})().catch((error) => {
console.error('utilities/fetch-supporters:', error);
process.exitCode = 1;
});