-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweekly-summary.js
More file actions
166 lines (138 loc) Β· 4.47 KB
/
Copy pathweekly-summary.js
File metadata and controls
166 lines (138 loc) Β· 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { Octokit } from '@octokit/rest';
import fetch from 'node-fetch';
import dotenv from 'dotenv';
dotenv.config();
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const GITHUB_USERNAME = process.env.GITHUB_USERNAME || 'timo-t-q';
const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL;
const octokit = new Octokit({
auth: GITHUB_TOKEN
});
function getWeekStart() {
const now = new Date();
const dayOfWeek = now.getUTCDay();
const daysToSubtract = dayOfWeek === 0 ? 6 : dayOfWeek - 1;
const weekStart = new Date(now);
weekStart.setUTCDate(now.getUTCDate() - daysToSubtract);
weekStart.setUTCHours(0, 0, 0, 0);
return weekStart;
}
async function getWeeklyCommits() {
const since = getWeekStart().toISOString();
const until = new Date().toISOString();
console.log(`Fetching commits for ${GITHUB_USERNAME} from ${since}...`);
let allCommits = [];
let page = 1;
while (true) {
const { data } = await octokit.search.commits({
q: `author:${GITHUB_USERNAME} committer-date:${since}..${until}`,
sort: 'committer-date',
order: 'desc',
per_page: 100,
page
});
if (data.items.length === 0) break;
allCommits.push(...data.items);
page++;
if (data.items.length < 100) break;
}
return allCommits;
}
function calculateStats(commits) {
const days = {};
const repos = {};
let streak = 0;
commits.forEach(commit => {
const date = new Date(commit.commit.committer.date);
const dayKey = date.toISOString().split('T')[0];
days[dayKey] = (days[dayKey] || 0) + 1;
const repoName = commit.repository.full_name;
repos[repoName] = (repos[repoName] || 0) + 1;
});
const sortedDays = Object.entries(days).sort((a, b) => b[1] - a[1]);
const mostProductiveDay = sortedDays[0] ? { date: sortedDays[0][0], count: sortedDays[0][1] } : null;
const sortedRepos = Object.entries(repos).sort((a, b) => b[1] - a[1]);
const favoriteRepo = sortedRepos[0] ? { name: sortedRepos[0][0], count: sortedRepos[0][1] } : null;
const today = new Date().toISOString().split('T')[0];
let currentStreak = 0;
let checkDate = new Date();
while (true) {
const dateKey = checkDate.toISOString().split('T')[0];
if (days[dateKey]) {
currentStreak++;
checkDate.setUTCDate(checkDate.getUTCDate() - 1);
} else {
break;
}
}
return {
totalCommits: commits.length,
mostProductiveDay,
favoriteRepo,
currentStreak
};
}
async function sendWeeklySummary(stats) {
if (!DISCORD_WEBHOOK_URL) {
console.log('No Discord webhook URL, skipping');
return;
}
const embed = {
embeds: [{
title: 'π Weekly Commit Summary',
description: `Here's your coding activity for the week!`,
color: 0x57F287,
fields: [
{
name: 'π Total Commits',
value: `${stats.totalCommits} commits this week`,
inline: true
},
{
name: 'π₯ Current Streak',
value: `${stats.currentStreak} day(s)`,
inline: true
},
{
name: 'π Most Productive Day',
value: stats.mostProductiveDay ? `${stats.mostProductiveDay.date}: ${stats.mostProductiveDay.count} commits` : 'N/A',
inline: true
},
{
name: 'π Favorite Repo',
value: stats.favoriteRepo ? `${stats.favoriteRepo.name}: ${stats.favoriteRepo.count} commits` : 'N/A',
inline: true
}
],
timestamp: new Date().toISOString(),
footer: {
text: 'Weekly Commit Summary'
}
}]
};
try {
const response = await fetch(DISCORD_WEBHOOK_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(embed)
});
if (response.ok) {
console.log('β
Weekly summary sent!');
} else {
console.error('β Failed to send:', response.status);
}
} catch (error) {
console.error('β Error:', error.message);
}
}
async function main() {
console.log('π Weekly Commit Summary starting...');
const commits = await getWeeklyCommits();
const stats = calculateStats(commits);
console.log(`Total: ${stats.totalCommits} commits`);
console.log(`Streak: ${stats.currentStreak} days`);
console.log(`Most productive: ${stats.mostProductiveDay?.date} (${stats.mostProductiveDay?.count})`);
console.log(`Favorite repo: ${stats.favoriteRepo?.name} (${stats.favoriteRepo?.count})`);
await sendWeeklySummary(stats);
}
main();