-
Notifications
You must be signed in to change notification settings - Fork 153
/
api.js
93 lines (80 loc) · 2.6 KB
/
api.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
// utility functions //
window.$ = (id) => document.getElementById(id);
window.enquery = (key, val) => `${key}=${encodeURIComponent(val)}`;
const REPORT_DIR = '.';
// data retrieval functions //
function loadJSON(fname) {
if (window.REPORT_ZIP) {
const zipObject = window.REPORT_ZIP.file(fname.slice(2));
if (zipObject) {
return zipObject.async('text').then((txt) => JSON.parse(txt));
}
return Promise.reject(new Error('Zip file is invalid.'));
}
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', fname);
xhr.onload = function xhrOnload() {
if (xhr.status === 200) {
resolve(JSON.parse(xhr.responseText));
} else {
reject(new Error('Unable to get file.'));
}
};
xhr.send(null);
});
}
window.api = {
loadSummary() {
window.REPOS = {};
return loadJSON(`${REPORT_DIR}/summary.json`)
.then((data) => {
window.app.creationDate = data.reportGeneratedTime;
const names = [];
data.repos.forEach((repo) => {
const repoName = `${repo.displayName}`;
window.REPOS[repoName] = repo;
names.push(repoName);
});
return names;
});
},
loadCommits(repoName) {
return loadJSON(`${REPORT_DIR}/${repoName}/commits.json`).then((commits) => {
const res = [];
const repo = window.REPOS[repoName];
Object.keys(commits.authorDisplayNameMap).forEach((author) => {
if (author) {
const obj = {
name: author,
repoId: repoName,
variance: commits.authorContributionVariance[author],
displayName: commits.authorDisplayNameMap[author],
dailyCommits: commits.authorDailyContributionsMap[author],
totalCommits: commits.authorFinalContributionMap[author],
fileFormatContribution: commits.authorFileFormatContributionMap[author],
};
const searchParams = [
repo.displayName,
obj.displayName, author,
];
obj.searchPath = searchParams.join('_').toLowerCase();
obj.repoPath = `${repo.displayName}`;
obj.repoName = `${repo.displayName}`;
obj.location = `${repo.location.location}`;
res.push(obj);
}
});
repo.commits = commits;
repo.users = res;
return res;
});
},
loadAuthorship(repoName) {
return loadJSON(`${REPORT_DIR}/${repoName}/authorship.json`)
.then((files) => {
window.REPOS[repoName].files = files;
return files;
});
},
};