Skip to content

Commit cf19712

Browse files
Merge pull request #53 from Andre-Diamond:main
Main
2 parents 70ae4b6 + da964df commit cf19712

File tree

1 file changed

+101
-19
lines changed
  • apps/shared-backend/github-actions/github-stats-action

1 file changed

+101
-19
lines changed

apps/shared-backend/github-actions/github-stats-action/getStats.js

Lines changed: 101 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,28 @@ async function fetchAllMissingCommits(existingShasSet) {
180180
if (!resp.ok) throw { status: resp.status, message: await resp.text() }
181181
return resp.json()
182182
})
183-
results.push(details)
183+
// Minify commit payload to only what the Netlify function needs
184+
const filesCount = Array.isArray(details.files) ? details.files.length : 0
185+
const parents = Array.isArray(details.parents) ? details.parents.map((p) => ({ sha: p.sha })) : []
186+
results.push({
187+
sha: details.sha,
188+
author: details.author ? { login: details.author.login, avatar_url: details.author.avatar_url } : null,
189+
committer: details.committer ? { login: details.committer.login, avatar_url: details.committer.avatar_url } : null,
190+
commit: {
191+
message: details.commit?.message ?? null,
192+
author: { date: details.commit?.author?.date ?? null }
193+
},
194+
stats: details.stats
195+
? {
196+
additions: details.stats.additions,
197+
deletions: details.stats.deletions,
198+
total: details.stats.total
199+
}
200+
: null,
201+
// Only send a small array to preserve length semantics expected by the function
202+
files: filesCount ? Array(filesCount).fill(0) : [],
203+
parents
204+
})
184205
}
185206
}
186207
page += 1
@@ -264,26 +285,87 @@ async function main() {
264285
const missingIssues = await fetchAllMissingIssues(existingIssueNumbers)
265286
console.log(`🧮 Missing issues: ${missingIssues.length}`)
266287

267-
const payload = {
268-
org: parsedOrg,
269-
repo: parsedRepo,
270-
commits: missingCommits,
271-
pulls: missingPulls,
272-
issues: missingIssues,
288+
// Helper to chunk arrays
289+
const chunkArray = (arr, size) => {
290+
const chunks = []
291+
for (let i = 0; i < arr.length; i += size) chunks.push(arr.slice(i, i + size))
292+
return chunks
273293
}
274294

275-
if (
276-
(missingCommits && missingCommits.length) ||
277-
(missingPulls && missingPulls.length) ||
278-
(missingIssues && missingIssues.length)
279-
) {
280-
console.log('📡 Sending new data to Netlify background function...')
281-
const response = await makeRequest(parsedFunctionUrl, {
282-
method: 'POST',
283-
body: JSON.stringify(payload),
284-
})
285-
console.log(`✅ Background function response: ${JSON.stringify(response, null, 2)}`)
286-
} else {
295+
// Use conservative batch sizes to avoid gateway limits
296+
const COMMIT_BATCH_SIZE = 25
297+
const PULL_BATCH_SIZE = 20
298+
const ISSUE_BATCH_SIZE = 50
299+
300+
let sentSomething = false
301+
302+
if (missingCommits.length > 0) {
303+
console.log(`📡 Sending ${missingCommits.length} commits in batches of ${COMMIT_BATCH_SIZE}...`)
304+
const commitBatches = chunkArray(missingCommits, COMMIT_BATCH_SIZE)
305+
for (let i = 0; i < commitBatches.length; i++) {
306+
const batch = commitBatches[i]
307+
console.log(` ➤ Commit batch ${i + 1}/${commitBatches.length} (size=${batch.length})`)
308+
const payload = {
309+
org: parsedOrg,
310+
repo: parsedRepo,
311+
commits: batch,
312+
pulls: [],
313+
issues: []
314+
}
315+
const response = await makeRequest(parsedFunctionUrl, {
316+
method: 'POST',
317+
body: JSON.stringify(payload)
318+
})
319+
console.log(` ✅ Response: ${JSON.stringify(response)}`)
320+
sentSomething = true
321+
}
322+
}
323+
324+
if (missingPulls.length > 0) {
325+
console.log(`📡 Sending ${missingPulls.length} pull requests in batches of ${PULL_BATCH_SIZE}...`)
326+
const pullBatches = chunkArray(missingPulls, PULL_BATCH_SIZE)
327+
for (let i = 0; i < pullBatches.length; i++) {
328+
const batch = pullBatches[i]
329+
console.log(` ➤ PR batch ${i + 1}/${pullBatches.length} (size=${batch.length})`)
330+
const payload = {
331+
org: parsedOrg,
332+
repo: parsedRepo,
333+
commits: [],
334+
pulls: batch,
335+
issues: []
336+
}
337+
const response = await makeRequest(parsedFunctionUrl, {
338+
method: 'POST',
339+
body: JSON.stringify(payload)
340+
})
341+
console.log(` ✅ Response: ${JSON.stringify(response)}`)
342+
sentSomething = true
343+
}
344+
}
345+
346+
if (missingIssues.length > 0) {
347+
console.log(`📡 Sending ${missingIssues.length} issues in batches of ${ISSUE_BATCH_SIZE}...`)
348+
const issueBatches = chunkArray(missingIssues, ISSUE_BATCH_SIZE)
349+
for (let i = 0; i < issueBatches.length; i++) {
350+
const batch = issueBatches[i]
351+
console.log(` ➤ Issue batch ${i + 1}/${issueBatches.length} (size=${batch.length})`)
352+
const payload = {
353+
org: parsedOrg,
354+
repo: parsedRepo,
355+
commits: [],
356+
pulls: [],
357+
issues: batch
358+
}
359+
const response = await makeRequest(parsedFunctionUrl, {
360+
method: 'POST',
361+
body: JSON.stringify(payload)
362+
})
363+
console.log(` ✅ Response: ${JSON.stringify(response)}`)
364+
sentSomething = true
365+
}
366+
}
367+
368+
if (!sentSomething) {
287369
console.log('✅ No new data to send')
288370
}
289371

0 commit comments

Comments
 (0)