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