-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (129 loc) · 3.74 KB
/
index.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
/** @param {string} repo
* @returns {string}
*/
function parseRepo ( repo ) {
const [ owner, name ] = repo.split( "/" );
return `owner: "${ owner }", name: "${ name }"`;
}
;
const span = new Date( Date.now() - 2 * 365 * 864e5 ).toISOString();
/** @returns {number} */
const rand = () => Math.abs( Math.random() * 10e2 | 0 );
/** @returns {string} */
const query = ( repo, time = span ) => /*GraphQL*/ `
r${ rand() }: repository(${ parseRepo( repo ) }) {
defaultBranchRef {
target {
... on Commit {
history(since: "${ time }") {
edges{
node {
author { user { name, login } }
additions
deletions
}
}
}
}
}
}
}`;
/** @returns {{ contributors: { name: string; login: string; diff: string; }[]; total_diffs: number; total_devs: number; }} */
function parseKey ( node ) {
if ( !node.defaultBranchRef ) return {
contributors: [], total_devs: 0
};
const target = node.defaultBranchRef.target;
const edges = target.history.edges;
let total_diffs = 0;
let contributors = [];
const len = edges.length;
for ( let i = 0;i < len;i++ ) {
const node = edges[ i ].node;
const { author, additions, deletions } = node;
if ( !author || !author.user )
continue;
let login = author.user?.login
let name = author.user?.name || login;
const index = contributors.findIndex( ( c ) => c.login === login );
const diff = additions + deletions;
total_diffs += diff;
if ( index === -1 ) {
contributors.push( {
name: name || login,
login, diff
} );
}
else {
contributors[ index ].diff += diff;
}
}
;
contributors.sort( ( a, b ) => b.diff - a.diff );
const names = [];
const logins = [];
const diffs = [];
let sum = 0;
for ( let i = 0;i < contributors.length;i++ ) {
const { name, login, diff } = contributors[ i ];
names.push( name );
logins.push( login );
diffs.push( diff );
sum += diff;
if ( sum > total_diffs * 0.68 ) break;
}
;
const contributors68 = new Array( names.length )
.fill( 0 )
.map( ( _, i ) => ( {
name: names[ i ],
login: logins[ i ],
diff: Math.round( diffs[ i ] * 100 / total_diffs )
} ) );
return {
contributors: contributors68,
total_devs: contributors.length,
};
}
;
/** @param {string} repos
* @param {string} APIkey
* @returns {Promise<string>}
*/
async function getRepos ( repos, APIkey ) {
const res = await fetch( "https://api.github.com/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `bearer ${ APIkey || GITHUB_PAT }`,
},
body: JSON.stringify( {
query: `{
${ repos.map( ( repo ) => query( repo ) ).join( ",\n" ) }
}`,
} )
} ).then( ( res ) => res.json() );
const keys = Object.keys( res.data );
const parsed = keys.map( ( key ) => parseKey( res.data[ key ] ) );
return JSON.stringify(
keys.map( ( key, i ) => {
const { contributors, total_devs } = parsed[ i ];
return {
repo: repos[ i ],
contributors,
total_devs,
};
} )
);
};
// worker code
self.onmessage = async ( e ) => {
const { repos, APIkey } = e.data;
const save = await getRepos( repos, APIkey );
self.postMessage( save );
};
/** @typedef {Object} Contributors
* @property {string} login
* @property {string} name
* @property {number} diff
*/