-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.ts
188 lines (166 loc) · 6.04 KB
/
index.ts
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
import * as fs from "fs"
import type { AnalyzedPR, ContributorStats } from "./lib/types"
import { getRepos } from "./lib/getRepos"
import { generateMarkdown } from "./lib/generateMarkdown"
import { getMergedPRs } from "./lib/getMergedPRs"
import { getAllPRs } from "./lib/getAllPRs"
import { getBountiedIssues } from "./lib/getBountiedIssues"
import { getIssuesCreated } from "./lib/getIssuesCreated"
import { analyzePRWithClaude } from "./lib/analyzePRWithClaude"
import { getLastWednesday } from "./lib/date-utils"
export async function generateOverview(startDate: string) {
const startDateString = startDate
const repos = await getRepos()
const mergedPrsWithAnalysis: AnalyzedPR[] = []
const contributorData: Record<string, ContributorStats> = {}
for (const repo of repos) {
console.log(`Analyzing ${repo}`)
const prsWithReviews = await getAllPRs(repo, startDate)
console.log(`Found ${prsWithReviews.length} total PRs`)
for (const pr of prsWithReviews) {
if (pr.user.login.includes("renovate")) {
continue
}
const contributor = pr.user.login
if (!contributorData[contributor]) {
contributorData[contributor] = {
reviewsReceived: 0,
rejectionsReceived: 0,
approvalsReceived: 0,
approvalsGiven: 0,
rejectionsGiven: 0,
prsOpened: 0,
prsMerged: 0,
issuesCreated: 0,
bountiedIssuesCount: 0,
bountiedIssuesTotal: 0,
}
}
contributorData[contributor].reviewsReceived += pr.reviewsReceived
contributorData[contributor].rejectionsReceived += pr.rejectionsReceived
contributorData[contributor].approvalsReceived += pr.approvalsReceived
contributorData[contributor].prsOpened += 1
if (pr.reviewsByUser) {
Object.entries(pr.reviewsByUser).forEach(
([reviewer, reviewerStats]) => {
if (!contributorData[reviewer]) {
contributorData[reviewer] = {
reviewsReceived: 0,
rejectionsReceived: 0,
approvalsReceived: 0,
approvalsGiven: 0,
rejectionsGiven: 0,
prsOpened: 0,
prsMerged: 0,
issuesCreated: 0,
bountiedIssuesCount: 0,
bountiedIssuesTotal: 0,
}
}
contributorData[reviewer].approvalsGiven +=
reviewerStats.approvalsGiven
contributorData[reviewer].rejectionsGiven +=
reviewerStats.rejectionsGiven
},
)
}
if (pr.isClosed && pr.merged_at)
contributorData[contributor].prsMerged += 1
}
const mergedPrs = await getMergedPRs(repo, startDateString)
console.log(`Found ${mergedPrs.length} merged PRs`)
for (const pr of mergedPrs) {
if (pr.user.login.includes("renovate")) {
continue
}
const analysis = await analyzePRWithClaude(pr, repo)
mergedPrsWithAnalysis.push(analysis)
}
// Fetch and process bountied issues for all contributors in parallel
const bountiedIssuesPromises = Object.keys(contributorData).map(
async (contributor) => {
const bountiedIssues = await getBountiedIssues(
repo,
contributor,
startDateString,
)
contributorData[contributor].bountiedIssuesCount =
(contributorData[contributor].bountiedIssuesCount || 0) +
bountiedIssues.length
contributorData[contributor].bountiedIssuesTotal =
(contributorData[contributor].bountiedIssuesTotal || 0) +
bountiedIssues.reduce((total, issue) => total + issue.amount, 0)
},
)
// Wait for all bounty fetching to complete
await Promise.all(bountiedIssuesPromises)
const getIssuesCreatedPromises = Object.keys(contributorData).map(
async (contributor) => {
const issuesCreated = await getIssuesCreated(
repo,
contributor,
startDateString,
)
contributorData[contributor].issuesCreated =
(contributorData[contributor].issuesCreated || 0) + issuesCreated
},
)
await Promise.all(getIssuesCreatedPromises)
}
// Data processing complete
await generateAndWriteFiles(
mergedPrsWithAnalysis,
contributorData,
startDateString,
)
}
async function generateAndWriteFiles(
mergedPrsWithAnalysis: AnalyzedPR[],
contributorData: Record<string, ContributorStats>,
startDateString: string,
) {
// Group PRs by contributor
const contributorPRs = mergedPrsWithAnalysis.reduce(
(acc, pr) => {
if (!acc[pr.contributor]) {
acc[pr.contributor] = []
}
acc[pr.contributor].push(pr)
return acc
},
{} as Record<string, AnalyzedPR[]>,
)
// Sort each contributor's PRs by impact
const impactOrder = { Major: 3, Minor: 2, Tiny: 1 }
for (const contributor in contributorPRs) {
contributorPRs[contributor].sort(
(a, b) => impactOrder[b.impact] - impactOrder[a.impact],
)
}
// Flatten the sorted PRs back into a single array
const sortedPRs = Object.values(contributorPRs).flat()
const markdown = await generateMarkdown(
sortedPRs,
contributorData,
startDateString,
)
fs.writeFileSync(`contribution-overviews/${startDateString}.md`, markdown)
console.log(`Generated contribution-overviews/${startDateString}.md`)
fs.writeFileSync(
`contribution-overviews/${startDateString}.json`,
JSON.stringify(contributorData, null, 2),
)
console.log(`Generated contribution-overviews/${startDateString}.json`)
// Edit the README.md file
const readme = fs.readFileSync("README.md", "utf8")
const updatedReadme = readme.replace(
/<!-- START_CURRENT_WEEK -->[\s\S]*<!-- END_CURRENT_WEEK -->/m,
`<!-- START_CURRENT_WEEK -->\n\n${markdown}\n\n<!-- END_CURRENT_WEEK -->`,
)
fs.writeFileSync("README.md", updatedReadme)
}
export async function generateWeeklyOverview() {
const weekStart = getLastWednesday(new Date())
const weekStartString = weekStart.toISOString().split("T")[0]
await generateOverview(weekStartString)
}