forked from TanStack/tanstack.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.ts
More file actions
394 lines (359 loc) · 10.2 KB
/
Copy pathgithub.ts
File metadata and controls
394 lines (359 loc) · 10.2 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import axios from 'axios'
import { Octokit } from '@octokit/rest'
import { graphql } from '@octokit/graphql'
export const GITHUB_ORG = 'TanStack'
export const octokit = new Octokit({
auth: process.env.GITHUB_AUTH_TOKEN,
userAgent: 'TanStack.com',
})
export const graphqlWithAuth = graphql.defaults({
headers: {
authorization: `token ${process.env.GITHUB_AUTH_TOKEN}`,
},
})
const githubClientID = 'Iv1.3aa8d13a4a3fde91'
const githubClientSecret = 'e2340f390f956b6fbfb9c6f85100d6cfe07f29a8'
export async function exchangeGithubCodeForToken({
code,
state,
redirectUrl,
}: {
code: string
state: string
redirectUrl: string
}) {
try {
const { data } = await axios.post(
'https://github.com/login/oauth/access_token',
{
client_id: githubClientID,
client_secret: githubClientSecret,
code,
redirect_uri: redirectUrl,
state,
},
{
headers: {
Accept: 'application/json',
},
}
)
return data.access_token
} catch (err) {
console.error(err)
throw new Error('Unable to authenticate with GitHub. Please log in again.')
}
}
export async function getTeamBySlug(slug: string) {
const teams = await octokit.teams.list({
org: GITHUB_ORG,
})
const sponsorsTeam = teams.data.find((x) => x.slug === slug)
if (!sponsorsTeam) {
throw new Error(`Cannot find team "${slug}" in the organization`)
}
return sponsorsTeam
}
// GitHub contributor stats - commented out due to performance/accuracy concerns
/*
export interface ContributorStats {
username: string
totalCommits: number
totalPullRequests: number
totalIssues: number
totalReviews: number
firstContribution: string | null
lastContribution: string | null
repositories: Array<{
name: string
commits: number
pullRequests: number
issues: number
reviews: number
}>
}
export async function getContributorStats(
username: string
): Promise<ContributorStats> {
try {
// Use GraphQL for TanStack organization stats
const query = `
query($username: String!, $org: String!) {
user(login: $username) {
contributionsCollection(organizationID: $org) {
totalCommitContributions
totalPullRequestReviewContributions
totalIssueContributions
totalPullRequestContributions
contributionCalendar {
totalContributions
}
}
}
organization(login: $org) {
id
}
}
`
const result = (await graphqlWithAuth(query, {
username,
org: GITHUB_ORG,
})) as any
const user = result.user
return {
username,
totalCommits:
user?.contributionsCollection?.totalCommitContributions || 0,
totalPullRequests:
user?.contributionsCollection?.totalPullRequestContributions || 0,
totalIssues: user?.contributionsCollection?.totalIssueContributions || 0,
totalReviews:
user?.contributionsCollection?.totalPullRequestReviewContributions || 0,
firstContribution: null, // GraphQL doesn't provide this easily
lastContribution: null, // GraphQL doesn't provide this easily
repositories: [], // Not tracking per-repo anymore
}
} catch (error) {
console.error(`Error fetching stats for ${username}:`, error)
return {
username,
totalCommits: 0,
totalPullRequests: 0,
totalIssues: 0,
totalReviews: 0,
firstContribution: null,
lastContribution: null,
repositories: [],
}
}
}
async function getContributorStatsForRepo(username: string, repoName: string) {
const stats = {
commits: 0,
pullRequests: 0,
issues: 0,
reviews: 0,
dates: [] as string[],
}
try {
// Get commits by the user
const commits = await octokit.repos.listCommits({
owner: GITHUB_ORG,
repo: repoName,
author: username,
per_page: 100,
})
stats.commits = commits.data.length
stats.dates.push(
...commits.data
.map((commit) => commit.commit.author?.date || '')
.filter(Boolean)
)
// Get pull requests by the user
const pullRequests = await octokit.pulls.list({
owner: GITHUB_ORG,
repo: repoName,
state: 'all',
per_page: 100,
})
const userPRs = pullRequests.data.filter(
(pr) => pr.user?.login === username
)
stats.pullRequests = userPRs.length
stats.dates.push(...userPRs.map((pr) => pr.created_at).filter(Boolean))
// Get issues by the user
const issues = await octokit.issues.listForRepo({
owner: GITHUB_ORG,
repo: repoName,
state: 'all',
per_page: 100,
})
const userIssues = issues.data.filter(
(issue) => issue.user?.login === username
)
stats.issues = userIssues.length
stats.dates.push(
...userIssues.map((issue) => issue.created_at).filter(Boolean)
)
// Get reviews by the user
const reviews = await octokit.pulls.listReviews({
owner: GITHUB_ORG,
repo: repoName,
pull_number: 1, // We'll need to iterate through all PRs for reviews
})
// Note: This is a simplified approach. For accurate review counts,
// we'd need to iterate through all PRs and get reviews for each
const userReviews = reviews.data.filter(
(review) => review.user?.login === username
)
stats.reviews = userReviews.length
stats.dates.push(
...userReviews.map((review) => review.submitted_at).filter(Boolean)
)
} catch (error) {
console.error(`Error fetching stats for ${username} in ${repoName}:`, error)
}
return stats
}
export async function getContributorStatsForLibrary(
username: string,
libraryRepo: string
): Promise<ContributorStats> {
try {
const repoStats = await getContributorStatsForRepo(username, libraryRepo)
return {
username,
totalCommits: repoStats.commits,
totalPullRequests: repoStats.pullRequests,
totalIssues: repoStats.issues,
totalReviews: repoStats.reviews,
firstContribution:
repoStats.dates.length > 0 ? repoStats.dates.sort()[0] : null,
lastContribution:
repoStats.dates.length > 0 ? repoStats.dates.sort().reverse()[0] : null,
repositories: [
{
name: libraryRepo,
commits: repoStats.commits,
pullRequests: repoStats.pullRequests,
issues: repoStats.issues,
reviews: repoStats.reviews,
},
],
}
} catch (error) {
console.error(
`Error fetching library stats for ${username} in ${libraryRepo}:`,
error
)
return {
username,
totalCommits: 0,
totalPullRequests: 0,
totalIssues: 0,
totalReviews: 0,
firstContribution: null,
lastContribution: null,
repositories: [],
}
}
}
// GraphQL approach for more efficient data fetching
export async function getContributorStatsGraphQL(
username: string
): Promise<ContributorStats> {
try {
const query = `
query($username: String!, $org: String!) {
user(login: $username) {
contributionsCollection {
totalCommitContributions
totalPullRequestReviewContributions
totalIssueContributions
totalPullRequestContributions
contributionCalendar {
totalContributions
}
}
}
organization(login: $org) {
repositories(first: 100, privacy: PUBLIC) {
nodes {
name
defaultBranchRef {
target {
... on Commit {
history(author: { login: $username }) {
totalCount
}
}
}
}
pullRequests(first: 100, states: [OPEN, MERGED, CLOSED]) {
nodes {
author {
login
}
createdAt
}
}
issues(first: 100, states: [OPEN, CLOSED]) {
nodes {
author {
login
}
createdAt
}
}
}
}
}
}
`
const result = await graphqlWithAuth(query, {
username,
org: GITHUB_ORG,
})
const user = result.user
const org = result.organization
const stats: ContributorStats = {
username,
totalCommits:
user?.contributionsCollection?.totalCommitContributions || 0,
totalPullRequests:
user?.contributionsCollection?.totalPullRequestContributions || 0,
totalIssues: user?.contributionsCollection?.totalIssueContributions || 0,
totalReviews:
user?.contributionsCollection?.totalPullRequestReviewContributions || 0,
firstContribution: null,
lastContribution: null,
repositories: [],
}
// Process repository-specific data
if (org?.repositories?.nodes) {
for (const repo of org.repositories.nodes) {
const commits = repo.defaultBranchRef?.target?.history?.totalCount || 0
const pullRequests =
repo.pullRequests?.nodes?.filter(
(pr) => pr.author?.login === username
).length || 0
const issues =
repo.issues?.nodes?.filter(
(issue) => issue.author?.login === username
).length || 0
if (commits > 0 || pullRequests > 0 || issues > 0) {
stats.repositories.push({
name: repo.name,
commits,
pullRequests,
issues,
reviews: 0, // Would need additional query for reviews
})
}
}
}
return stats
} catch (error) {
console.error(`Error fetching GraphQL stats for ${username}:`, error)
return {
username,
totalCommits: 0,
totalPullRequests: 0,
totalIssues: 0,
totalReviews: 0,
firstContribution: null,
lastContribution: null,
repositories: [],
}
}
}
// Batch fetch stats for multiple contributors
export async function getBatchContributorStats(
usernames: string[]
): Promise<ContributorStats[]> {
const stats = await Promise.all(
usernames.map((username) => getContributorStats(username))
)
return stats
}
*/