forked from api-platform/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
603 lines (542 loc) · 17.7 KB
/
gatsby-node.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/* eslint-disable no-console */
const path = require('path');
const URL = require('url');
const fetch = require('node-fetch');
const { createFilePath } = require(`gatsby-source-filesystem`);
const jsyaml = require('js-yaml');
const { readFileSync } = require('fs');
const fs = require('fs');
const { current, versions } = require('./constants');
const versionHelper = require('./src/lib/versionHelper');
const staticEventsData = require('./src/data/events.json');
const repositories = require('./src/data/repositories.json');
if (fs.existsSync('.env.local')) {
// eslint-disable-next-line global-require
require('dotenv').config({
path: '.env.local',
});
}
const parseLinkHeader = (header) => {
if (0 === header.length) {
throw new Error('input must not be of zero length');
}
// Split parts by comma and parse each part into a named link
return header.split(/(?!\B"[^"]*),(?![^"]*"\B)/).reduce((links, part) => {
const section = part.split(/(?!\B"[^"]*);(?![^"]*"\B)/);
if (2 > section.length) {
throw new Error("section could not be split on ';'");
}
const url = section[0].replace(/<(.*)>/, '$1').trim();
const name = section[1].replace(/rel="(.*)"/, '$1').trim();
// eslint-disable-next-line no-param-reassign
links[name] = url;
return links;
}, {});
};
const navs = {};
versions.push(current);
versions.forEach((version) => {
const prefixedVersion = `${versionHelper.getPrefixedVersion(version)}/`;
navs[prefixedVersion] = jsyaml.safeLoad(readFileSync(`./src/pages/docs/${prefixedVersion}nav.yml`, 'utf8'));
});
const delay = (time) => new Promise((res) => setTimeout(() => res(), time));
// GITHUB
const fetchFromGithubApi = async (url) => {
const response = await fetch(url, {
headers: {
authorization: `token ${process.env.GITHUB_KEY}`,
},
});
if (401 === response.status) throw new Error('UNAUTHORIZED: check your github token');
// if rate limit excedeed : wait for reset time
if ('0' === response.headers.get('x-ratelimit-remaining')) {
const rateLimitResetTime = response.headers.get('x-ratelimit-reset') * 1000;
const timeToWait = rateLimitResetTime - new Date().getTime();
if (timeToWait > process.env.GATSBY_BUILD_TIMEOUT) {
throw new Error('rate limit reset time too long');
}
await delay(timeToWait);
return fetchFromGithubApi(url);
}
return response;
};
const sortByContributions = (a, b) => {
if (a.contributions < b.contributions) return 1;
if (a.contributions > b.contributions) return -1;
if (a.lines && a.lines < b.lines) return 1;
if (a.lines && a.lines > b.lines) return -1;
return 0;
};
const REPOSITORIES_TO_IGNORE = ['symfonycon-berlin-workshop-eod'];
const getRepositoryList = async (organizationName) => {
const repos = await fetchFromGithubApi(`https://api.github.com/orgs/${organizationName}/repos`);
const data = await repos.json();
return data.filter((repo) => !REPOSITORIES_TO_IGNORE.includes(repo.name));
};
const getStaticRepositoryList = async () => {
const repos = await Promise.all(
repositories.map((repoName) => fetchFromGithubApi(`https://api.github.com/repos/${repoName}`))
);
const data = await Promise.all(repos.map((repo) => repo.json()));
return data;
};
const getRepoContributorsStats = async (repository) => {
const response = await fetchFromGithubApi(`${repository.url}/stats/contributors`);
let stats = await response.json();
if (!Array.isArray(stats)) stats = [];
return stats.map((stat) => ({
id: stat.author.id,
additions: stat.weeks.reduce((acc, week) => acc + week.a, 0),
deletions: stat.weeks.reduce((acc, week) => acc + week.d, 0),
contributions: stat.total,
}));
};
const getListOfContributorsFromRepository = async (repository) => {
let pageToFetch = `${repository.url}/contributors?page=1&per_page=100`;
let contributors = [];
while (pageToFetch) {
// eslint-disable-next-line no-await-in-loop
const response = await fetchFromGithubApi(pageToFetch);
// eslint-disable-next-line no-await-in-loop
const newContributors = await response.json();
contributors = [...contributors, ...newContributors];
pageToFetch = response.headers.get('Link') && parseLinkHeader(response.headers.get('Link')).next;
}
return contributors.filter((c) => 'Bot' !== c.type);
};
const createContributor = (repository, contributor, stat) => {
return {
id: contributor.id,
url: contributor.url,
login: contributor.login,
avatar: contributor.avatar_url,
profile_url: contributor.html_url,
projects: [
{
name: repository.name,
fullName: repository.full_name,
link: repository.html_url,
contributions: contributor.contributions,
additions: stat ? stat.additions : 0,
deletions: stat ? stat.deletions : 0,
},
],
contributions: contributor.contributions,
lines: stat ? stat.additions + stat.deletions : 0,
};
};
const getAllContributorsFromOrganization = async (organizationName) => {
try {
const repos = await getRepositoryList(organizationName);
const staticRepos = await getStaticRepositoryList();
const allRepos = [...repos, ...staticRepos];
const allContributors = [];
await Promise.all(
allRepos.map(async (repo) => {
const contributors = await getListOfContributorsFromRepository(repo);
const stats = await getRepoContributorsStats(repo);
contributors.forEach((contributor) => {
const contributorStat = stats.find((stat) => stat.id === contributor.id);
const personFromList = allContributors.find((c) => c.login === contributor.login);
if (personFromList) {
personFromList.contributions += contributor.contributions;
if (contributorStat) {
personFromList.lines += contributorStat.additions + contributorStat.deletions;
}
personFromList.projects.push({
name: repo.name,
fullName: repo.full_name,
link: repo.html_url,
contributions: contributor.contributions,
additions: contributorStat ? contributorStat.additions : 0,
deletions: contributorStat ? contributorStat.deletions : 0,
});
personFromList.projects.sort(sortByContributions);
} else {
allContributors.push(createContributor(repo, contributor, contributorStat));
}
});
})
);
return allContributors.sort(sortByContributions).map((contributor, i) => ({ ...contributor, position: i + 1 }));
} catch (error) {
console.error(error);
return [];
}
};
// EVENTS
const fetchFromMeetupApi = async (url) => {
const response = await fetch(url);
// if rate limit excedeed : wait for reset time
if ('0' === response.headers.get('x-ratelimit-remaining')) {
const rateLimitResetTime = response.headers.get('x-ratelimit-reset') * 1000;
const timeToWait = rateLimitResetTime - new Date().getTime();
if (timeToWait > process.env.GATSBY_BUILD_TIMEOUT) {
throw new Error('rate limit reset time too long');
}
await delay(timeToWait);
return fetchFromMeetupApi(url);
}
return response;
};
const getAllMeetupEvents = async () => {
const events = await fetchFromMeetupApi(
'https://api.meetup.com/api-platform/events?desc=true&status=past,upcoming&fields=featured_photo'
);
const data = await events.json();
const staticEvents = await Promise.all(
staticEventsData.map(async (event) =>
fetchFromMeetupApi(`https://api.meetup.com/${event.group}/events/${event.id}?desc=true&fields=featured_photo`)
)
);
const staticEventsdata = await Promise.all(staticEvents.map((event) => event.json()));
return [...data, ...staticEventsdata];
};
const CONTRIBUTOR_NODE_TYPE = `Contributor`;
const EVENT_NODE_TYPE = `Event`;
const getOrganizationTeamMembers = async (organizationName, teamName) => {
const members = await fetchFromGithubApi(`https://api.github.com/orgs/${organizationName}/teams/${teamName}/members`);
const data = await members.json();
return data.map((member) => member.login);
};
const getOrganizationTeams = async (organizationName) => {
try {
const teams = await fetchFromGithubApi(`https://api.github.com/orgs/${organizationName}/teams`);
const data = await teams.json();
const fullTeams = await Promise.all(
data.map(async (team) => ({
...team,
members: await getOrganizationTeamMembers(organizationName, team.slug),
}))
);
return fullTeams;
} catch (error) {
console.error(
`UNAUTHORIZED: You have restricted rights to ${organizationName} teams. You can't retrieve core teams members`
);
return [];
}
};
exports.sourceNodes = async ({ actions, createContentDigest, createNodeId }) => {
const { createNode } = actions;
const teams = await getOrganizationTeams('api-platform');
const contributors = await getAllContributorsFromOrganization('api-platform');
const fullContributors = await Promise.all(
contributors.map(async (contributor) => {
const userResponse = await fetchFromGithubApi(contributor.url);
await delay(1000);
const user = await userResponse.json();
return {
...contributor,
name: user.name,
blog: user.blog,
location: user.location,
bio: user.bio,
company: user.company,
teams: [
'dummy-team',
...teams.filter((team) => team.members.includes(contributor.login)).map((team) => team.slug),
],
};
})
);
if (0 === fullContributors.length) {
// create dummy contributor to avoid graphql build error
fullContributors.push({
login: 'dummy-api-platform',
name: 'dummy',
company: 'dummy',
location: 'dummy',
blog: 'dummy',
bio: 'dummy',
projects: {
contributions: 0,
link: 'dummy',
name: 'dummy',
fullName: 'dummy',
additions: 0,
deletions: 0,
},
avatar: 'dummy',
contributions: 0,
position: 0,
lines: 0,
profile_url: 'dummy',
teams: ['dummy-team'],
});
}
fullContributors.forEach((item) => {
const nodeMetadata = {
id: createNodeId(`contributor-${item.id}`),
parent: null,
children: [],
internal: {
type: CONTRIBUTOR_NODE_TYPE,
content: JSON.stringify(item),
contentDigest: createContentDigest(item),
},
};
const node = { ...item, ...nodeMetadata };
createNode(node);
});
const events = await getAllMeetupEvents();
events.forEach((item) => {
const nodeMetadata = {
id: createNodeId(`event-${item.id}`),
parent: null,
children: [],
internal: {
type: EVENT_NODE_TYPE,
content: JSON.stringify(item),
contentDigest: createContentDigest(item),
},
};
const node = { ...item, ...nodeMetadata };
createNode(node);
});
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage, createRedirect } = actions;
// Redirect 301 old page
createRedirect({ fromPath: '/news/', toPath: '/resources/news/', isPermanent: true, redirectInBrowser: true });
createRedirect({ fromPath: '/support/', toPath: '/community/', isPermanent: true, redirectInBrowser: true });
createRedirect({
fromPath: '/docs/core/swagger/',
toPath: '/docs/core/openapi/',
isPermanent: true,
redirectInBrowser: true,
});
// Documentation pages
const docPageTemplate = path.resolve('src/templates/doc.js');
const docResult = await graphql(`
{
allMarkdownRemark(limit: 1000, filter: { frontmatter: { type: { eq: null } } }) {
edges {
node {
fileAbsolutePath
html
headings {
value
}
fields {
slug
redirect
}
}
}
}
}
`);
if (docResult.errors) {
throw docResult.errors;
}
const docPages = docResult.data.allMarkdownRemark.edges;
docPages.forEach((edge) => {
const { redirect } = edge.node.fields;
const slug = edge.node.fields.slug.replace(`${current}/`, '');
const slugArray = edge.node.fields.slug.split('/');
const prefixedVersion = slugArray[2];
const prefixedVersionSlug = `${prefixedVersion}/`.replace(`${current}/`, '');
const originalVersion = versionHelper.getOriginalVersion(slugArray[2]);
const section = slugArray[3];
const article = slugArray[4] ? slugArray[4] : 'index';
const previous = {};
const next = {};
if ('.github' === section || !edge.node.headings.length) return; // ignore .github folder and untitled files
const nav = navs[`${prefixedVersion}/`];
nav.chapters
.filter((chapter) => chapter.path === section)
.forEach((chapter) => {
chapter.items.forEach((item, indexItem) => {
if (item.id !== article) {
return;
}
if (chapter.items.length - 1 !== indexItem) {
next.slug = versionHelper.generateSlugNextChapter(
prefixedVersionSlug,
section,
chapter.items[indexItem + 1].id
);
next.title = chapter.items[indexItem + 1].title;
}
if (0 !== indexItem) {
previous.slug = versionHelper.generateSlugPreviousChapter(
prefixedVersionSlug,
section,
chapter.items[indexItem - 1].id
);
previous.title = chapter.items[indexItem - 1].title;
}
});
});
createPage({
component: docPageTemplate,
context: {
html: edge.node.html,
nav,
next,
prefixedVersion,
previous,
title: edge.node.headings[0].value,
urlEditDocumentation: versionHelper.generateSlugEditDocumentation(originalVersion, section, article),
version: prefixedVersionSlug,
},
path: slug,
});
const redirects = [slug.slice(0, -1)];
if (redirect) {
redirects.push(redirect, `${redirect}/`);
}
redirects.forEach((redirectPath) =>
createRedirect({
fromPath: redirectPath,
toPath: slug,
isPermanent: true,
redirectInBrowser: true,
})
);
});
// conferences pages
/* const conferenceTemplate = path.resolve('src/components/con/2021/templates/ConferenceTemplate.tsx');
const conferencesResult = await graphql(`
{
allMarkdownRemark(limit: 1000, filter: { frontmatter: { type: { eq: "conference" } } }) {
edges {
node {
html
frontmatter {
date
slot
title
type
speakers {
description
github
image
job
list
name
twitter
}
}
}
}
}
}
`);
const conferencePages = conferencesResult.data.allMarkdownRemark.edges;
conferencePages.forEach((edge) => {
createPage({
path: `/con/2021/${slugify(edge.node.frontmatter.title)}`,
component: conferenceTemplate,
context: {
html: edge.node.html,
...edge.node.frontmatter,
},
});
}); */
createRedirect({
fromPath: '/con/',
toPath: '/con/2021',
isPermanent: true,
redirectInBrowser: true,
});
// Contributors page
const contributors = await graphql(`
{
allContributor {
nodes {
login
name
company
location
blog
bio
projects {
contributions
link
name
fullName
additions
deletions
}
avatar
contributions
position
lines
teams
}
}
}
`);
contributors.data.allContributor.nodes.forEach((node) => {
if ('dummy-api-platform' !== node.login)
createPage({
path: `/community/contributors/${node.login}`,
component: path.resolve(`./src/templates/contributor.js`),
context: {
...node,
},
});
});
};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (`MarkdownRemark` === node.internal.type) {
const fileNode = getNode(node.parent);
const nodePath = fileNode.relativePath.replace('.md', '');
let html = node.internal.content;
const localUrls = [];
let matches;
const regex = /(\]\((?!http)(?!#)(.*?)\))/gi;
// eslint-disable-next-line no-cond-assign
while ((matches = regex.exec(html))) {
localUrls.push(matches[2]);
}
localUrls.map((url) => {
let newUrl = `/${URL.resolve(nodePath, url)}`;
newUrl = newUrl.replace(/(\/index)?\.md/, '/');
newUrl = newUrl.replace(`/${current}/`, '/');
html = html.replace(url, newUrl);
return true;
});
// eslint-disable-next-line no-param-reassign
node.internal.content = html;
const slug = createFilePath({ node, getNode, basePath: `pages` });
if ('index' === path.basename(nodePath)) {
createNodeField({
node,
name: 'redirect',
value: `/${nodePath}`,
});
}
createNodeField({
node,
name: `slug`,
value: slug,
});
}
};
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
alias: {
'@components': path.resolve(__dirname, 'src/components'),
'@images': path.resolve(__dirname, 'src/images'),
'@styles': path.resolve(__dirname, 'src/styles'),
},
},
});
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type MarkdownRemark implements Node {
frontmatter: Frontmatter
}
type Frontmatter {
type: String
}
`;
createTypes(typeDefs);
};