-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrank.js
More file actions
266 lines (230 loc) · 6.67 KB
/
rank.js
File metadata and controls
266 lines (230 loc) · 6.67 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
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
const { Octokit } = require('@octokit/rest')
/**
* GitHub Readme Stats 评分算法实现
* 参考:https://github.com/anuraghazra/github-readme-stats
*/
// 权重配置
const WEIGHTS = {
COMMITS: 2,
PRS: 3,
ISSUES: 1,
REVIEWS: 1,
STARS: 4,
FOLLOWERS: 1
}
const TOTAL_WEIGHT = Object.values(WEIGHTS).reduce((a, b) => a + b, 0) // 12
// MEDIAN 参考值(来自 github-readme-stats)
const MEDIANS = {
COMMITS: 250, // 普通模式
PRS: 50,
ISSUES: 25,
REVIEWS: 2,
STARS: 50,
FOLLOWERS: 10
}
// 等级阈值(百分位)
const RANK_THRESHOLDS = {
S: 1, // 前 1%
'A+': 12.5, // 1% - 12.5%
A: 25, // 12.5% - 25%
'A-': 37.5, // 25% - 37.5%
'B+': 50, // 37.5% - 50%
B: 62.5, // 50% - 62.5%
'B-': 75, // 62.5% - 75%
'C+': 87.5, // 75% - 87.5%
C: 100 // 87.5% - 100%
}
/**
* 指数累积分布函数
* 用于 commits, PRs, issues, reviews
*/
function exponentialCDF (x) {
return 1 - Math.pow(2, -x)
}
/**
* 对数正态累积分布函数(近似)
* 用于 stars, followers
*/
function logNormalCDF (x) {
return x / (1 + x)
}
/**
* 计算开发者等级
* @param {Object} stats - 开发者统计数据
* @returns {Object} - { percentile, level, score }
*/
function calculateRank (stats) {
const {
commits = 0,
prs = 0,
issues = 0,
reviews = 0,
stars = 0,
followers = 0
} = stats
// 计算加权评分
const score =
WEIGHTS.COMMITS * exponentialCDF(commits / MEDIANS.COMMITS) +
WEIGHTS.PRS * exponentialCDF(prs / MEDIANS.PRS) +
WEIGHTS.ISSUES * exponentialCDF(issues / MEDIANS.ISSUES) +
WEIGHTS.REVIEWS * exponentialCDF(reviews / MEDIANS.REVIEWS) +
WEIGHTS.STARS * logNormalCDF(stars / MEDIANS.STARS) +
WEIGHTS.FOLLOWERS * logNormalCDF(followers / MEDIANS.FOLLOWERS)
// 计算百分位(rank 越小越好)
const rank = 1 - score / TOTAL_WEIGHT
const percentile = rank * 100
// 确定等级
let level = 'C'
for (const [rankLevel, threshold] of Object.entries(RANK_THRESHOLDS)) {
if (percentile <= threshold) {
level = rankLevel
break
}
}
return {
percentile: parseFloat(percentile.toFixed(2)),
level,
score: parseFloat(score.toFixed(2))
}
}
/**
* 从 GitHub API 获取用户统计数据
* @param {string} username - GitHub 用户名
* @param {string} token - GitHub Token
* @returns {Promise<Object>} - 统计数据
*/
async function fetchUserStats (username, token) {
const octokit = new Octokit({ auth: token })
try {
// 获取用户基本信息
const { data: user } = await octokit.users.getByUsername({ username })
// GraphQL 查询获取详细统计
const query = `
query($login: String!) {
user(login: $login) {
contributionsCollection {
totalCommitContributions
totalPullRequestReviewContributions
}
pullRequests {
totalCount
}
issues {
totalCount
}
repositories(first: 100, ownerAffiliations: OWNER, orderBy: {field: STARGAZERS, direction: DESC}) {
totalCount
nodes {
stargazers {
totalCount
}
}
}
followers {
totalCount
}
}
}
`
const { data } = await octokit.graphql(query, { login: username })
// Debug: 输出完整的GraphQL响应
console.log('GraphQL Response:', JSON.stringify(data, null, 2))
if (!data || !data.user) {
console.error('GraphQL data.user is null or undefined')
console.error('Full response:', JSON.stringify(data, null, 2))
throw new Error(`User not found or insufficient permissions to access user data`)
}
const userData = data.user
// 计算总星标数
const totalStars = userData.repositories.nodes.reduce(
(sum, repo) => sum + repo.stargazers.totalCount,
0
)
return {
username,
name: user.name || username,
commits: userData.contributionsCollection.totalCommitContributions,
prs: userData.pullRequests.totalCount,
issues: userData.issues.totalCount,
reviews: userData.contributionsCollection.totalPullRequestReviewContributions,
stars: totalStars,
followers: userData.followers.totalCount,
createdAt: user.created_at,
publicRepos: user.public_repos
}
} catch (error) {
throw new Error(`Failed to fetch stats for ${username}: ${error.message}`)
}
}
/**
* 评估用户是否符合条件
* @param {Object} stats - 用户统计数据
* @returns {Object} - { approved, action, reason, rank }
*/
function evaluateUser (stats) {
const rank = calculateRank(stats)
// Top 25% - 自动批准 (S, A+, A)
if (rank.percentile <= 25) {
return {
approved: true,
action: 'auto_approve',
reason: `Top ${rank.percentile.toFixed(1)}% developer (Rank: ${rank.level})`,
rank
}
}
// 低于 75% - 自动拒绝 (C+, C)
if (rank.percentile > 75) {
return {
approved: false,
action: 'auto_reject',
reason: `Rank below threshold (${rank.percentile.toFixed(1)}%, Rank: ${rank.level})`,
rank
}
}
// 25% - 75% - 等待人工审核 (A-, B+, B, B-)
return {
approved: null,
action: 'manual_review',
reason: `Requires manual review (${rank.percentile.toFixed(1)}%, Rank: ${rank.level})`,
rank
}
}
/**
* 生成详细的评估报告
* @param {Object} stats - 用户统计数据
* @param {Object} evaluation - 评估结果
* @returns {string} - Markdown 格式的报告
*/
function generateReport (stats, evaluation) {
const { rank } = evaluation
const report = `## Developer Evaluation Report
**User**: @${stats.username} (${stats.name})
**Account Created**: ${new Date(stats.createdAt).toLocaleDateString()}
### GitHub Statistics
| Metric | Value | Weight | Median |
|--------|-------|--------|--------|
| 💻 Commits | ${stats.commits} | ${WEIGHTS.COMMITS} | ${MEDIANS.COMMITS} |
| 🔀 Pull Requests | ${stats.prs} | ${WEIGHTS.PRS} | ${MEDIANS.PRS} |
| 🐛 Issues | ${stats.issues} | ${WEIGHTS.ISSUES} | ${MEDIANS.ISSUES} |
| 👀 Code Reviews | ${stats.reviews} | ${WEIGHTS.REVIEWS} | ${MEDIANS.REVIEWS} |
| ⭐ Stars | ${stats.stars} | ${WEIGHTS.STARS} | ${MEDIANS.STARS} |
| 👥 Followers | ${stats.followers} | ${WEIGHTS.FOLLOWERS} | ${MEDIANS.FOLLOWERS} |
### Ranking Result
- **Level**: \`${rank.level}\`
- **Percentile**: \`${rank.percentile}%\` (Top ${rank.percentile.toFixed(1)}%)
- **Score**: \`${rank.score}/${TOTAL_WEIGHT}\`
### Decision
**Action**: \`${evaluation.action}\`
**Reason**: ${evaluation.reason}
`
return report
}
module.exports = {
calculateRank,
fetchUserStats,
evaluateUser,
generateReport,
RANK_THRESHOLDS,
MEDIANS,
WEIGHTS
}