Skip to content

Commit 592aef6

Browse files
committed
init
1 parent 51afc2a commit 592aef6

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

.github/workflows/stats.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
name: pkg.pr.new stats
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
stats:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Set up Node.js
12+
uses: actions/setup-node@v3
13+
with:
14+
node-version: '16'
15+
16+
- name: Fetch and Count Data Iteratively
17+
uses: actions/github-script@v6
18+
with:
19+
script: |
20+
async function fetchAndCountData(url) {
21+
const counts = {
22+
templates: 0,
23+
packages: 0,
24+
orgs: new Set(),
25+
repos: new Set(),
26+
commits: 0,
27+
prNumbers: 0,
28+
branchNames: 0,
29+
};
30+
let cursor = null;
31+
32+
do {
33+
try {
34+
// Construct the URL with cursor if available
35+
const fetchUrl = cursor ? `${url}?cursor=${encodeURIComponent(cursor)}` : url;
36+
37+
// Fetch the data from the API
38+
const response = await fetch(fetchUrl);
39+
40+
if (!response.ok) {
41+
throw new Error(`HTTP error! status: ${response.status}`);
42+
}
43+
44+
const responseData = await response.json();
45+
46+
// Process the current page of data
47+
if (responseData.data) {
48+
for (const item of responseData.data) {
49+
if (item.type === "template") {
50+
counts.templates += 1;
51+
counts.orgs.add(item.org);
52+
counts.repos.add(item.repo);
53+
} else if (item.type === "package") {
54+
counts.packages += 1;
55+
counts.orgs.add(item.org);
56+
counts.repos.add(item.repo);
57+
counts.commits += 1; // Count one commit per package
58+
} else if (item.type === "cursor") {
59+
counts.orgs.add(item.org);
60+
counts.repos.add(item.repo);
61+
62+
// Count PR numbers and branch names
63+
if (/^\d+$/.test(item.ref)) {
64+
counts.prNumbers += 1;
65+
} else {
66+
counts.branchNames += 1;
67+
}
68+
}
69+
}
70+
}
71+
72+
// Update the cursor for the next page
73+
cursor = responseData.nextCursor || null;
74+
} catch (error) {
75+
core.setFailed(`Error fetching data: ${error.message}`);
76+
cursor = null; // Exit the loop on error
77+
}
78+
} while (cursor);
79+
80+
return {
81+
templates: counts.templates,
82+
packages: counts.packages,
83+
orgs: counts.orgs.size,
84+
repos: counts.repos.size,
85+
commits: counts.commits,
86+
prNumbers: counts.prNumbers,
87+
branchNames: counts.branchNames,
88+
};
89+
}
90+
91+
const url = "https://e853059f.stackblitz-cr.pages.dev/stats";
92+
const counts = await fetchAndCountData(url);
93+
94+
console.log("Counts:", counts);

0 commit comments

Comments
 (0)