Skip to content

Commit 5ac8a7f

Browse files
committed
now?
1 parent 3ec485e commit 5ac8a7f

File tree

1 file changed

+66
-83
lines changed

1 file changed

+66
-83
lines changed

.github/workflows/gh_statistics_bot.yml

Lines changed: 66 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -59,131 +59,114 @@ jobs:
5959
}
6060
}
6161
62-
// Get commit statistics using GitHub's actual Pulse API
62+
// Get commit statistics using GitHub's recommended approach
6363
async function getMonthCommitStats(since, until) {
64-
console.log(`=== USING GITHUB PULSE API FOR ${since} TO ${until} ===`);
64+
console.log(`=== GETTING ALL COMMITS FROM ALL BRANCHES ${since} TO ${until} ===`);
6565
6666
try {
67-
// Use GitHub's Code Frequency API (shows additions/deletions over time)
68-
const codeFrequency = await github.rest.repos.getCodeFrequencyStats({
69-
owner: context.repo.owner,
70-
repo: context.repo.repo,
71-
});
72-
73-
console.log('*** CODE FREQUENCY DATA LENGTH ***', codeFrequency.data?.length);
74-
75-
// Use GitHub's Commit Activity API (shows commits per week)
76-
const commitActivity = await github.rest.repos.getCommitActivityStats({
77-
owner: context.repo.owner,
78-
repo: context.repo.repo,
79-
});
80-
81-
console.log('*** COMMIT ACTIVITY DATA LENGTH ***', commitActivity.data?.length);
82-
83-
// Use GitHub's Participation API (shows commit participation)
84-
const participation = await github.rest.repos.getParticipationStats({
67+
// Step 1: Get all branches in the repository
68+
console.log('*** STEP 1: LISTING ALL BRANCHES ***');
69+
const branches = await github.paginate(github.rest.repos.listBranches, {
8570
owner: context.repo.owner,
8671
repo: context.repo.repo,
72+
per_page: 100,
8773
});
8874
89-
console.log('*** PARTICIPATION DATA ***', participation.data);
75+
console.log(`*** FOUND ${branches.length} BRANCHES: ${branches.map(b => b.name).join(', ')} ***`);
9076
91-
// Use GitHub's Contributors API to get author info
92-
const contributors = await github.paginate(github.rest.repos.listContributors, {
77+
// Get default branch for main commit counting
78+
const repo = await github.rest.repos.get({
9379
owner: context.repo.owner,
9480
repo: context.repo.repo,
95-
per_page: 100,
9681
});
82+
const defaultBranch = repo.data.default_branch;
83+
console.log(`*** DEFAULT BRANCH: ${defaultBranch} ***`);
9784
98-
console.log('*** FOUND CONTRIBUTORS ***', contributors.length);
99-
100-
// For the specific month, we still need to use listCommits but with better logic
101-
// Get all commits using GraphQL-style approach via REST
85+
// Step 2: Fetch commits for each branch
86+
console.log('*** STEP 2: FETCHING COMMITS FROM EACH BRANCH ***');
87+
const allCommitShas = new Set();
88+
const allAuthors = new Set();
89+
let mainBranchCommits = [];
10290
103-
// Method 1: Try to use search API to find commits in date range
104-
const searchQuery = `repo:${context.repo.owner}/${context.repo.repo} author-date:${since.split('T')[0]}..${until.split('T')[0]}`;
105-
console.log('*** SEARCHING WITH QUERY ***', searchQuery);
106-
107-
let searchResults = null;
108-
try {
109-
searchResults = await github.rest.search.commits({
110-
q: searchQuery,
111-
sort: 'author-date',
112-
order: 'desc',
113-
per_page: 100,
114-
});
115-
console.log('*** SEARCH FOUND COMMITS ***', searchResults.data.total_count);
116-
} catch (searchError) {
117-
console.log('*** SEARCH API ERROR ***', searchError.message);
91+
for (const branch of branches) {
92+
try {
93+
console.log(`*** Fetching commits from branch: ${branch.name} ***`);
94+
95+
const branchCommits = await github.paginate(github.rest.repos.listCommits, {
96+
owner: context.repo.owner,
97+
repo: context.repo.repo,
98+
sha: branch.name,
99+
since,
100+
until,
101+
per_page: 100,
102+
});
103+
104+
console.log(`*** Found ${branchCommits.length} commits on ${branch.name} ***`);
105+
106+
// Add to our sets (automatically handles duplicates)
107+
branchCommits.forEach(commit => {
108+
allCommitShas.add(commit.sha);
109+
if (commit.author && commit.author.login) {
110+
allAuthors.add(commit.author.login);
111+
}
112+
});
113+
114+
// Keep main branch commits separate
115+
if (branch.name === defaultBranch) {
116+
mainBranchCommits = branchCommits;
117+
}
118+
119+
} catch (error) {
120+
console.log(`*** Error fetching commits from ${branch.name}: ${error.message} ***`);
121+
}
122+
123+
// Add delay to avoid rate limiting
124+
await new Promise(resolve => setTimeout(resolve, 100));
118125
}
119126
120-
// Method 2: Fallback to regular commits API
121-
const mainCommits = await github.paginate(github.rest.repos.listCommits, {
122-
owner: context.repo.owner,
123-
repo: context.repo.repo,
124-
sha: repo.data.default_branch,
125-
since,
126-
until,
127-
per_page: 100,
128-
});
129-
130-
console.log(`*** MAIN BRANCH COMMITS: ${mainCommits.length} ***`);
131-
132-
// Use search results if available, otherwise use estimates
133-
const totalCommitsAllBranches = searchResults?.data.total_count || mainCommits.length;
134-
const totalCommitsMain = mainCommits.length;
127+
console.log(`*** TOTAL UNIQUE COMMITS ACROSS ALL BRANCHES: ${allCommitShas.size} ***`);
128+
console.log(`*** COMMITS ON MAIN BRANCH: ${mainBranchCommits.length} ***`);
129+
console.log(`*** TOTAL UNIQUE AUTHORS: ${allAuthors.size} ***`);
130+
console.log(`*** AUTHORS: ${Array.from(allAuthors).join(', ')} ***`);
135131
136-
// Process main commits for detailed stats
137-
const authors = new Set();
132+
// Step 3: Get detailed stats from main branch commits
133+
console.log('*** STEP 3: GETTING DETAILED STATS FROM MAIN BRANCH ***');
138134
let totalAdditions = 0;
139135
let totalDeletions = 0;
140-
let fileChanges = 0;
136+
let mainFileChanges = 0;
141137
142-
for (const commit of mainCommits.slice(0, 50)) {
143-
if (commit.author && commit.author.login) {
144-
authors.add(commit.author.login);
145-
}
146-
138+
for (const commit of mainBranchCommits.slice(0, 50)) { // Limit to avoid rate limits
147139
try {
148140
const commitDetail = await github.rest.repos.getCommit({
149141
owner: context.repo.owner,
150142
repo: context.repo.repo,
151143
ref: commit.sha,
152144
});
153145
154-
fileChanges += commitDetail.data.files?.length || 0;
146+
mainFileChanges += commitDetail.data.files?.length || 0;
155147
totalAdditions += commitDetail.data.stats?.additions || 0;
156148
totalDeletions += commitDetail.data.stats?.deletions || 0;
157149
} catch (error) {
158150
console.log(`Error getting commit detail: ${error.message}`);
159151
}
160152
}
161153
162-
// If search worked, try to get all authors from search results
163-
if (searchResults && searchResults.data.items) {
164-
searchResults.data.items.forEach(commit => {
165-
if (commit.author && commit.author.login) {
166-
authors.add(commit.author.login);
167-
}
168-
});
169-
}
170-
171154
const result = {
172-
totalCommitsAllBranches: totalCommitsAllBranches,
173-
totalCommitsMain: totalCommitsMain,
174-
totalAuthors: authors.size,
175-
authorsList: Array.from(authors),
176-
filesChangedMain: fileChanges,
155+
totalCommitsAllBranches: allCommitShas.size,
156+
totalCommitsMain: mainBranchCommits.length,
157+
totalAuthors: allAuthors.size,
158+
authorsList: Array.from(allAuthors),
159+
filesChangedMain: mainFileChanges,
177160
linesAdded: totalAdditions,
178161
linesRemoved: totalDeletions,
179162
netLines: totalAdditions - totalDeletions,
180163
};
181164
182-
console.log('*** FINAL PULSE-BASED RESULT ***', JSON.stringify(result, null, 2));
165+
console.log('*** FINAL RESULT (FOLLOWING GITHUB DOCS) ***', JSON.stringify(result, null, 2));
183166
return result;
184167
185168
} catch (error) {
186-
console.log('*** ERROR WITH PULSE APIs ***', error.message);
169+
console.log('*** ERROR FOLLOWING GITHUB DOCS APPROACH ***', error.message);
187170
return {
188171
totalCommitsAllBranches: 0,
189172
totalCommitsMain: 0,

0 commit comments

Comments
 (0)