8mo ago
Loading comments...
For some reason, i work across bitbucket, github, private repos and i wanted to build a actiity log that would mix bitbucket and github. Although for security purpose, i could not use my main org repo for activity but it was a fun exercise to do anyway.
Main executor that reads from config and knows which sources to use
fetch branches and process the commits
1 import { GitHubFetcher } from './github-commits.mjs';
2 import { BitbucketFetcher } from './bitbucket-commits.mjs';
3 import { GikiRepo } from './giki-private-commits.mjs';
4 // Configuration
5 const CONFIG = {
6 github: {
7 enabled: process.env.NEXT_PUBLIC_ENABLE_GITHUB === 'true',
8 username: process.env.NEXT_PUBLIC_GITHUB_USERNAME
9 },
10 bitbucket: {
11 enabled: process.env.NEXT_PUBLIC_ENABLE_BITBUCKET === 'true'
12 },
13 gikiPrivate: {
14 enabled: process.env.GIKI_PRIVATE === 'true'
15 },
16 dateRange: {
17 days: 365
18 }
19 };
20
21 async function fetchGitHubData(startDate) {
22 if (!process.env.GITHUB_TOKEN) {
23 return {};
24 }
25
26 console.log('Reading GitHub commits...');
27 const github = new GitHubFetcher(process.env.GITHUB_TOKEN, CONFIG.github.username);
28 return github.fetchCommits(startDate);
29 }
30
31 function getActivityLevel(count, maxCount) {
32 if (count === 0) return 0;
33 const percentile = (count / maxCount) * 100;
34 if (percentile <= 15) return 1;
35 if (percentile <= 35) return 2;
36 if (percentile <= 65) return 3;
37 return 4;
38 }
39
40 const dailyTotals = allDates.map(date => {
41 return (githubCommits[date] || 0) + (bitbucketCommits[date] || 0);
42 });
43 const maxDailyCommits = Math.max(...dailyTotals, 1); // Avoid division by zero
44
45 console.log('\nActivity Statistics:');
46 console.log('Max commits in a day:', maxDailyCommits);
47 console.log('Total days with activity:', dailyTotals.filter(x => x > 0).length);
48 console.log('Average daily commits:', (dailyTotals.reduce((a, b) => a + b, 0) / dailyTotals.length).toFixed(2));
49
50 const commitData = {};
51 allDates.forEach(date => {
52 const total = (githubCommits[date] || 0) + (bitbucketCommits[date] || 0);
53 commitData[date] = {
54 level: getActivityLevel(total, maxDailyCommits)
55 };
56 });
57
1 async fetchCommits(startDate) {
2 try {
3 console.log('🔍 Fetching Bitbucket repositories...');
4 const repos = await this.fetchRepositories();
5 console.log(`Found ${repos.length} repositories`);
6
7 let allCommits = {};
8
9 for (const repo of repos) {
10 console.log(`\n📂 Processing repository: ${repo.name}`);
11
12 // First get all branches
13 const branches = await this.fetchBranches(repo.full_name);
14 console.log(`Found ${branches.length} branches in ${repo.name}`);
15
16 // Fetch commits from each branch
17 for (const branch of branches) {
18 console.log(` Fetching commits from branch: ${branch.name}`);
19 const branchCommits = await this.fetchBranchCommits(repo.full_name, branch.name, startDate);
20
21 // Merge commits into overall counts
22 Object.entries(branchCommits).forEach(([date, count]) => {
23 allCommits[date] = (allCommits[date] || 0) + count;
24 });
25 }
26 }
27
28 return allCommits;
29 } catch (error) {
30 console.error('Error in main fetch process:', error);
31 return {};
32 }
33 }
34
35
1 async fetchBranchCommits(repoFullName, branchName, startDate) {
2 try {
3 let commits = {};
4 let url = `https://api.bitbucket.org/2.0/repositories/${repoFullName}/commits/${branchName}`;
5
6 while (url) {
7 const response = await fetch(url, {
8 headers: {
9 'Authorization': this.authHeader
10 }
11 });
12
13 if (!response.ok) {
14 throw new Error(`Error fetching commits: ${response.statusText}`);
15 }
16
17 const data = await response.json();
18
19 // Process commits
20 if (data.values) {
21 const processedCommits = this.processCommits(data.values, startDate);
22 Object.entries(processedCommits).forEach(([date, count]) => {
23 commits[date] = (commits[date] || 0) + count;
24 });
25 }
26 // Check if we need to continue fetching (pagination)
27 url = data.next;
28 }
29
30 return commits;
31 } catch (error) {
32 console.error(`Error fetching commits for ${repoFullName}/${branchName}:`, error.message);
33 return {};
34 }
35 }
36
37 processCommits(commits = [], startDate) {
38 const commitsByDate = {};
39 const startDateTime = new Date(startDate).getTime();
40
41 commits.forEach(commit => {
42 const date = new Date(commit.date).toISOString().split('T')[0];
43 if (new Date(date).getTime() >= startDateTime) {
44 commitsByDate[date] = (commitsByDate[date] || 0) + 1;
45 }
46 });
47
48 return commitsByDate;
49 }
50
51
1,513 views
No comments yet.