Skip to content

Commit b5b570c

Browse files
authored
Fix Contributors List (ionic-team#952)
* very WIP * grabbing only the data we need * completing the contributor processing * ignore future changes to commits data * commenting out unecessary verbose task * removing unecssary data * adding the ignore back in
1 parent 8c8af41 commit b5b570c

File tree

9 files changed

+4125
-26
lines changed

9 files changed

+4125
-26
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ src/l10n/pages/**/*.json
88
src/components/menu/data/
99
src/components/page/data/
1010
src/components/search/data/
11+
scripts/data/github-commits.json
1112
.idea
1213
/.env

package-lock.json

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"express": "^4.16.4",
2929
"helmet": "^3.16.0",
3030
"ionicons": "^4.6.1",
31+
"simple-git": "^1.126.0",
3132
"throng": "^4.0.0"
3233
},
3334
"devDependencies": {

scripts/build-all/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import buildData from '../build-data';
55

66
const tasks = new Listr({ collapse: false });
77

8+
tasks.add({
9+
title: 'Data',
10+
task: () => buildData
11+
});
12+
813
tasks.add({
914
title: 'Pages',
1015
task: () => buildPages
@@ -15,11 +20,6 @@ tasks.add({
1520
task: () => buildMenus
1621
});
1722

18-
tasks.add({
19-
title: 'Data',
20-
task: () => buildData
21-
});
22-
2323
tasks.run().catch(err => {
2424
console.error(err);
2525
process.exit(1);
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import dotenv from 'dotenv';
2+
import { resolve } from 'path';
3+
import fetch from 'node-fetch';
4+
import url from 'url';
5+
import { outputJson } from 'fs-extra';
6+
7+
dotenv.config();
8+
9+
const OUTPUT_PATH = resolve(
10+
__dirname,
11+
'../data/github-commits.json'
12+
);
13+
14+
export default {
15+
title: 'Getting a list of past commits',
16+
task: async (_, task) => {
17+
const History = await getAllGHCommits(task);
18+
if (Object.keys(History).length > 0) {
19+
outputJson(OUTPUT_PATH, History, { spaces: 2 });
20+
}
21+
}
22+
};
23+
24+
async function getAllGHCommits(task, page = 1) {
25+
try {
26+
task.output = `Getting commits: ${page * 100 - 99} - ${page * 100}`;
27+
const request = await fetch(url.format({
28+
protocol: 'https',
29+
hostname: 'api.github.com',
30+
pathname: 'repos/ionic-team/ionic-docs/commits',
31+
query: {
32+
access_token: process.env.GITHUB_TOKEN,
33+
per_page: 100,
34+
page
35+
}
36+
}));
37+
38+
let commits = await request.json().then(list => list.reduce((obj, commit) => {
39+
obj[commit.sha] = {
40+
id: commit.author.login,
41+
avatar: commit.author.avatar_url,
42+
time: commit.commit.committer.date
43+
};
44+
return obj;
45+
}, {}));
46+
47+
// recursively get more commits if there are more to get, limit 2k
48+
if (Object.keys(commits).length === 100 && page < 20) {
49+
commits = { ...commits, ...await getAllGHCommits(task, page + 1) };
50+
}
51+
52+
return commits;
53+
} catch {
54+
return {};
55+
}
56+
}

scripts/build-data/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import Listr from 'listr';
22
import buildApiReference from './api-reference';
33
import buildReleaseNotes from './release-notes';
44
import buildSearchIndex from './search-index';
5+
import buildContributors from './file-contributors';
56

67
const tasks = new Listr([
78
buildApiReference,
9+
buildContributors,
810
buildReleaseNotes,
9-
buildSearchIndex
10-
]);
11+
buildSearchIndex,
12+
],
13+
// { renderer: 'verbose' }
14+
);
1115

1216
export default tasks;
1317

scripts/build-pages/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import CLI from './page-types/cli';
99
import Native from './page-types/native';
1010
import { convertHtmlToHypertextData } from './html-to-hypertext-data';
1111

12-
const tasks = new Listr();
12+
const tasks = new Listr(
13+
// { renderer: 'verbose' }
14+
);
1315
tasks.add(Static);
1416
tasks.add(API);
1517
tasks.add(CLI);
@@ -126,7 +128,9 @@ export function updatePageHtmlToHypertext(page: Page) {
126128
}
127129

128130
function writePage(page: Page): Promise<any> {
129-
if (listrStatus) listrStatus.output = `Writing Page: ${page.path}`;
131+
if (listrStatus && listrStatus._task && listrStatus._task.output !== 'Writing Pages') {
132+
listrStatus.output = 'Writing Pages';
133+
}
130134
return fs.outputJson(toFilePath(page.path), page, {
131135
spaces: 2
132136
});

scripts/build-pages/page-types/static.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import glob from 'fast-glob';
1111
import fetch from 'node-fetch';
1212
import frontMatter from 'front-matter';
1313
import markdownRenderer from '../markdown-renderer';
14+
import simplegit from 'simple-git/promise';
15+
16+
// ingored by git
17+
// generated in build-data/file-contrbutors.ts by build-data npm task
18+
import * as GITHUB_COMMITS from '../../data/github-commits.json';
1419

1520
export default {
1621
title: 'Build static pages',
@@ -54,24 +59,10 @@ const readMarkdown = (path: string): Promise<string> =>
5459
});
5560

5661
const getGitHubData = async (filePath: string) => {
57-
const [, path] = /^.+\/(src\/(l10n\/)?pages\/.+\.md)$/.exec(filePath);
58-
const since = new Date('2019-01-23').toISOString();
62+
const [, path] = /^.+\/(src\/pages\/.+\.md)$/.exec(filePath);
5963

6064
try {
61-
const request = await fetch(url.format({
62-
protocol: 'https',
63-
hostname: 'api.github.com',
64-
pathname: 'repos/ionic-team/ionic-docs/commits',
65-
query: {
66-
access_token: process.env.GITHUB_TOKEN,
67-
since,
68-
path
69-
}
70-
}));
71-
72-
const commits = await request.json();
73-
const contributors = Array.from(new Set(commits.map(commit => commit.author.login)));
74-
const lastUpdated = commits.length ? commits[0].commit.author.date : since;
65+
const { contributors, lastUpdated } = await getFileContributors(filePath);
7566
return {
7667
path,
7768
contributors,
@@ -81,7 +72,19 @@ const getGitHubData = async (filePath: string) => {
8172
return {
8273
path,
8374
contributors: [],
84-
lastUpdated: since
75+
lastUpdated: new Date('2019-01-23').toISOString()
8576
};
8677
}
8778
};
79+
80+
async function getFileContributors(filename) {
81+
return simplegit().log({ file: filename }).then(status => ({
82+
contributors: Array.from(new Set(status.all.map(commit =>
83+
// only add the user ID if we can find it based on the commit hash
84+
GITHUB_COMMITS[commit.hash] ? GITHUB_COMMITS[commit.hash].id : null
85+
// filter out null users
86+
).filter(user => !!user))),
87+
lastUpdated: status.latest.date
88+
})
89+
);
90+
}

0 commit comments

Comments
 (0)