-
Notifications
You must be signed in to change notification settings - Fork 32
/
generate.ts
351 lines (329 loc) · 11.5 KB
/
generate.ts
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import { Octokit } from "@octokit/core";
import {
Issue,
IssueEdge,
Label,
LabelEdge,
Language,
Query,
Repository,
RepositoryTopic,
RepositoryTopicEdge,
SearchResultItemEdge,
validate
} from "@octokit/graphql-schema";
import { retry } from "@octokit/plugin-retry";
import { throttling } from "@octokit/plugin-throttling";
import { RequestOptions } from "@octokit/types";
import dayjs from "dayjs";
import fs from "fs";
import millify from "millify";
import slugify from "slugify";
import happycommits from "./happycommits.json";
import {
CountableTag as CountableTagModel,
Issue as IssueModel,
Repository as RepositoryModel,
Tag as TagModel
} from "./types";
/** Number of repositories to query per request (max 100, but set to a smaller number to prevent timeouts) */
const REPOS_PER_REQUEST = 25;
/** Maximum number of issues to retrieve per repository */
const MAX_ISSUES = 10;
const validTopicNames = ['sdg-1', 'sdg-2', 'sdg-3', 'sdg-4', 'sdg-5', 'sdg-6', 'sdg-7', 'sdg-8', 'sdg-9', 'sdg-10', 'sdg-11', 'sdg-12', 'sdg-13', 'sdg-14', 'sdg-15', 'sdg-16', 'sdg-17'];
// symbols to replace with slugify
slugify.extend({
"#": "sharp",
"+": "plus"
});
// Setup Octokit (GitHub API client)
const MyOctokit = Octokit.plugin(throttling, retry);
const octokit = new MyOctokit({
auth: process.env.GH_PERSONAL_ACCESS_TOKEN,
throttle: {
onRateLimit: (retryAfter: number, options: object, octokit: Octokit, retryCount: number) => {
const { method, url } = options as RequestOptions;
octokit.log.warn(`Request quota exhausted for request ${method} ${url}`);
if (retryCount < 1) {
// only retries once
octokit.log.info(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
onSecondaryRateLimit: (
retryAfter: number,
options: object,
octokit: Octokit,
retryCount: number
) => {
const { method, url } = options as RequestOptions;
octokit.log.warn(`SecondaryRateLimit detected for request ${method} ${url}`);
if (retryCount < 2) {
// retries twice
octokit.log.warn(`Retrying after ${retryAfter} seconds!`);
return true;
}
}
}
});
/**
* Retrieve a list of repositories by calling GitHub GraphQL API.
*
* Use {@link https://docs.github.com/en/graphql/overview/explorer GitHub's GraphQL API explorer} to
* build and test the search query.
*/
const getRepositories = async (
repositories: string[],
labels: string[]
): Promise<RepositoryModel[]> => {
// Filter results with search qualifiers
// See https://docs.github.com/en/search-github/searching-on-github/searching-for-repositories
const searchQuery = [
...repositories.map((repo) => `repo:${repo}`),
"archived:false",
"is:public",
`pushed:>=${dayjs().add(-1, "month").format("YYYY-MM-DD")}`
].join(" ");
const gqlQuery = `
query {
search(
query: "${searchQuery}"
type: REPOSITORY
first: 100
) {
repositoryCount
edges {
node {
... on Repository {
id
name
owner {
login
}
isArchived
isDisabled
isPrivate
primaryLanguage {
id
name
}
stargazerCount
# return first 10 open issues with one or more of the labels we want
issues(
states: OPEN
filterBy: {labels: [${labels.map((label) => `"${label}"`).join(",")}]}
orderBy: {field: CREATED_AT, direction: DESC}
first: ${MAX_ISSUES}
) {
totalCount
edges {
node {
id
title
number
url
comments {
totalCount
}
createdAt
}
}
}
pushedAt
licenseInfo {
name
}
description
url
repositoryTopics(first: 20) {
edges {
node {
topic {
name
id
}
}
}
}
}
}
}
}
}
`;
const gqlQueryErrors = validate(gqlQuery);
if (gqlQueryErrors.length > 0) {
// if query is invalid, gqlQueryErrors will contain errors
throw new Error(
`GraphQL query is invalid:\n\t${gqlQueryErrors.map((error) => error.message).join("\n\t")}`
);
}
const searchResults = await octokit.graphql<Pick<Query, "search">>({ query: gqlQuery });
// map response data to our Repository model
const repoData =
searchResults.search.edges
?.filter((edge) => edge !== undefined)
.map((edge) => (edge as SearchResultItemEdge).node as Repository)
// skip repos where language is null
.filter((repo) => !!(repo.primaryLanguage as Language))
.map(
(repo): RepositoryModel => ({
id: repo.id,
owner: repo.owner.login,
name: repo.name,
description: repo.description === undefined ? null : repo.description,
url: repo.url,
stars: repo.stargazerCount,
stars_display: millify(repo.stargazerCount),
license: repo.licenseInfo?.name,
last_modified: repo.pushedAt,
language: {
id: slugify((repo.primaryLanguage as Language).name, { lower: true }),
display: (repo.primaryLanguage as Language).name
},
topics: repo.repositoryTopics.edges
?.filter((edge) => edge !== undefined)
.map((edge) => (edge as RepositoryTopicEdge).node as RepositoryTopic)
.filter((topic) => validTopicNames.includes(topic.topic.name.toLowerCase()))
.map((topic) => ({
id: slugify(topic.topic.name, { lower: true }),
display: topic.topic.name
})),
issues:
repo.issues.edges
?.filter((edge) => edge !== undefined)
.map((edge) => (edge as IssueEdge).node as Issue)
.map(
(issue): IssueModel => ({
id: issue.id,
number: issue.number,
title: issue.title,
url: issue.url,
comments_count: issue.comments.totalCount,
created_at: issue.createdAt,
labels:
issue.labels?.edges
?.filter((edge) => edge !== undefined)
.map((edge) => (edge as LabelEdge).node as Label)
.map((label) => ({
id: slugify(label.name, { lower: true }),
display: label.name
})) ?? []
})
)
// sort issues by issue number
.sort((a, b) => a.number - b.number) ?? [],
has_new_issues:
repo.issues.edges
?.filter((edge) => edge !== undefined)
.map((edge) => (edge as IssueEdge).node as Issue)
.some(
// Repository has "new" issues if there are any issues created in the last week
(issue) => dayjs().diff(dayjs(issue.createdAt), "day") <= 7
) ?? false
})
) ?? [];
// unfortunately, there's no way to filter repositories by number of issues in the search query
// filter out repos with less than 3 open issues
return repoData;
};
[...new Set(happycommits.repositories)]
.slice(0, process.env.NODE_ENV === "development" ? 200 : happycommits.repositories.length)
.reduce((repoChunks: string[][], repo: string, index) => {
// Split repositories into smaller chunks, this helps prevent request timeouts
const chunkIndex = Math.floor(index / REPOS_PER_REQUEST);
if (!repoChunks[chunkIndex]) {
repoChunks[chunkIndex] = [];
}
repoChunks[chunkIndex].push(repo);
return repoChunks;
}, [])
.reduce<Promise<RepositoryModel[]>>(async (repoData, chunk, index, arr) => {
return repoData.then(async (repos) => {
console.log(
`Getting repositories - chunk ${index + 1} of ${arr.length} (size: ${chunk.length})`
);
const repositories = await getRepositories(chunk, happycommits.labels);
// wait 1s between requests
await new Promise((resolve) => setTimeout(resolve, 1000));
return [...repos, ...repositories];
});
}, Promise.resolve([]))
.then((repoData) => {
// Get a list of distinct languages with counts for use with filtering in the UI
const filterLanguages = Object.values(
repoData.reduce((arr: { [key: string]: CountableTagModel }, repo: RepositoryModel) => {
// group languages by id and count them
const { id, display } = repo.language;
if (arr[id] === undefined) arr[id] = { id, display, count: 1 };
else arr[id].count++;
return arr;
}, {} as { [key: string]: CountableTagModel })
)
// Ignore language with less than 3 repositories
.filter((language) => language.count >= 1)
// Sort alphabetically
.sort((a, b) => a.display.localeCompare(b.display));
// Get a list of distinct topics with counts for use with filtering in the UI
const filterTopics = Object.values(
repoData
.filter((repo) => repo.topics !== undefined)
.flatMap((repo) => repo.topics as TagModel[])
.reduce((arr: { [key: string]: CountableTagModel }, topic: TagModel) => {
// group topics by id and count them
const { id, display } = topic;
if (arr[id] === undefined) arr[id] = { id, display, count: 1 };
else arr[id].count++;
return arr;
}, {} as { [key: string]: CountableTagModel })
)
// Ignore topics with less than 3 repositories
.filter((topic) => topic.count >= 1)
// Sort by count desc
.sort((a, b) => b.count - a.count);
return {
// Sort the repositories randomly so that the list isn't always the same
repositories: repoData.sort(() => Math.random() - 0.5),
languages: filterLanguages,
topics: filterTopics
};
})
.then((data) => {
// Write generated data to file for use in the app
fs.writeFileSync("./generated.json", JSON.stringify(data));
console.log("Generated generated.json");
const topics = data.repositories
.filter((repo) => repo.topics !== undefined)
.flatMap((repo) => repo.topics as TagModel[])
.map((topic) => topic.display);
fs.writeFileSync("./topics.json", JSON.stringify(topics, null, 2));
console.log("Generated topics.json");
// Build sitemap
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://forgoodfirstissue.dev</loc>
</url>
${data.languages
.map(
(language: CountableTagModel) =>
`<url><loc>https://forgoodfirstissue.dev/language/${language.id}</loc></url>`
)
.join("")}
${data.topics
.map(
(topic: CountableTagModel) =>
`<url><loc>https://forgoodfirstissue.dev/topic/${topic.id}</loc></url>`
)
.join("")}
</urlset>
`;
fs.writeFileSync("./public/sitemap.xml", sitemap);
console.log("Generated sitemap.xml");
})
.finally(() => {
console.log("Data generation complete.");
})
.catch((error: any) => {
console.error(error);
});