This repository was archived by the owner on Mar 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcountGithubFileAuthorStats.js
More file actions
109 lines (90 loc) · 2.79 KB
/
countGithubFileAuthorStats.js
File metadata and controls
109 lines (90 loc) · 2.79 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
const assert = require('assert');
const debug = require('debug')('lib/countGithubFileAuthorStats');
const config = require('../config');
const {waitLimit} = require('./rateLimit');
const path = require('path');
const fs = require('mz/fs');
const graphql = require('@octokit/graphql').defaults({
headers: {
authorization: `token ${config.secret.github.token}`
}
});
require('util').inspect.defaultOptions.depth = 10;
async function countGithubFileAuthorStats(langCode, fileRelPath, currentHead) {
let repoPath = path.join(config.repoRoot, `${langCode}.${config.repoSuffix}`);
let filePath = path.join(repoPath, fileRelPath);
let content = await fs.readFile(filePath, 'utf-8');
let contentLines = content.split('\n');
const {repositoryOwner, rateLimit} = await graphql(`query blame($owner: String!, $repo: String!, $currentHead: String!, $fileRelPath: String!) {
repositoryOwner(login:$owner) {
repository(name:$repo) {
object(expression:$currentHead) {
... on Commit {
blame(path:$fileRelPath) {
ranges {
startingLine,
endingLine,
commit {
author {
name,
avatarUrl,
email,
user {
id,
login,
url
}
}
}
}
}
}
}
}
}
rateLimit {
limit
cost
remaining
resetAt
}
}`, {
owner: config.org,
repo: `${langCode}.${config.repoSuffix}`,
currentHead,
fileRelPath
});
await waitLimit(rateLimit);
let ranges = repositoryOwner.repository.object.blame.ranges;
// console.log(ranges);
let statsByAuthor = Object.create(null);
for (let range of ranges) {
let linesCount = range.endingLine - range.startingLine + 1;
// if a line is blank -- remove it from the counter
for (let i = range.startingLine - 1; i < range.endingLine; i++) {
if (!contentLines[i]) {
linesCount--;
continue;
}
if (!contentLines[i].trim()) {
linesCount--;
}
}
if (!linesCount) continue;
let commit = range.commit;
let authorEmail = commit.author.email;
if (!statsByAuthor[authorEmail]) {
statsByAuthor[authorEmail] = {
author: commit.author,
linesCount
}
} else {
statsByAuthor[authorEmail].linesCount += linesCount;
}
}
debug("stats for file gathered", fileRelPath);
// debug("stats by author", statsByAuthor);
return statsByAuthor;
}
module.exports = countGithubFileAuthorStats;
//countGithubFileAuthorStats('xitu/javascript-tutorial-zh', '4-frames-and-windows/01-popup-windows/article.md', 'zh-hans');