-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-activity-summary.sh
More file actions
executable file
·462 lines (374 loc) · 16.1 KB
/
github-activity-summary.sh
File metadata and controls
executable file
·462 lines (374 loc) · 16.1 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/bin/bash
# GitHub Activity Summary Script
# Usage: ./github-activity-summary.sh <username> [since_date] [github_token]
# Example: ./github-activity-summary.sh etnt 2024-05-01 ghp_your_token_here
set -e
# Configuration
USERNAME="${1:-}"
SINCE_DATE="${2:-$(date -v-6m +%Y-%m-%d)}" # Default to 6 months ago
GITHUB_TOKEN="${3:-}"
OUTPUT_DIR="github-summary-$(date +%Y%m%d)"
PER_PAGE=100
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_section() {
echo -e "\n${BLUE}=== $1 ===${NC}"
}
# Function to make authenticated API calls
api_call() {
local url="$1"
local headers=(-H "Accept: application/vnd.github+json")
if [[ -n "$GITHUB_TOKEN" ]]; then
headers+=(-H "Authorization: token $GITHUB_TOKEN")
fi
curl -s "${headers[@]}" "$url"
}
# Function to check if jq is installed
check_dependencies() {
if ! command -v jq &> /dev/null; then
print_error "jq is required but not installed. Please install it with: brew install jq"
exit 1
fi
if ! command -v curl &> /dev/null; then
print_error "curl is required but not installed."
exit 1
fi
}
# Function to show help
show_help() {
cat << 'EOF'
GitHub Activity Summary Script
==============================
DESCRIPTION:
Generates a comprehensive report of your GitHub activities for performance
evaluation purposes. Collects data on repositories, commits, pull requests,
issues, and other activities.
USAGE:
./github-activity-summary.sh <username> [since_date] [github_token]
./github-activity-summary.sh --help
PARAMETERS:
username (Required) Your GitHub username
since_date (Optional) Start date in YYYY-MM-DD format
Default: 6 months ago
github_token (Optional) GitHub Personal Access Token
Recommended for private repos and higher rate limits
EXAMPLES:
# Basic usage - last 6 months of public activity
./github-activity-summary.sh johndoe
# Specific time period
./github-activity-summary.sh johndoe 2024-01-01
# With authentication token
./github-activity-summary.sh johndoe 2024-01-01 ghp_xxxxxxxxxxxx
GITHUB TOKEN SETUP:
For best results, create a Personal Access Token (PAT):
1. Go to GitHub.com → Settings (top-right profile menu)
2. Click "Developer settings" (bottom of left sidebar)
3. Click "Personal access tokens" → "Tokens (classic)"
4. Click "Generate new token" → "Generate new token (classic)"
5. Set these options:
• Note: "GitHub Activity Summary Script"
• Expiration: 30 days (or as needed)
• Scopes to select:
☑ repo (Full control of private repositories)
☑ read:org (Read org and team membership)
☑ read:user (Read user profile data)
6. Click "Generate token"
7. COPY THE TOKEN IMMEDIATELY (you won't see it again!)
8. Use it as the third parameter to this script
Token format: ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SECURITY NOTES:
• Treat tokens like passwords - never share them
• Revoke tokens after use in GitHub Settings
• Use short expiration periods
• Don't commit tokens to version control
RATE LIMITS:
Without token: 60 requests/hour (may hit limits with many repos)
With token: 5,000 requests/hour (recommended)
OUTPUT:
Creates a timestamped directory containing:
• GitHub_Activity_Report.md - Main professional report
• *.json files - Raw API data
• *_summary.txt files - Human-readable summaries
• statistics.txt - Detailed metrics
REQUIREMENTS:
• curl (pre-installed on macOS)
• jq (install with: brew install jq)
TROUBLESHOOTING:
• "jq command not found" → brew install jq
• "Rate limit exceeded" → Use a GitHub token
• "Empty results" → Check username spelling and date format
• "Invalid date" → Use YYYY-MM-DD format (e.g., 2024-01-01)
For more information, see README.md
EOF
}
# Function to validate inputs
validate_inputs() {
if [[ -z "$USERNAME" ]]; then
print_error "Username is required"
echo ""
echo "Quick usage: $0 <username> [since_date] [github_token]"
echo "For detailed help: $0 --help"
exit 1
fi
# Validate date format (YYYY-MM-DD)
if ! date -j -f "%Y-%m-%d" "$SINCE_DATE" >/dev/null 2>&1; then
print_error "Invalid date format. Use YYYY-MM-DD (e.g., 2024-01-01)"
echo "For help: $0 --help"
exit 1
fi
}
# Function to create output directory
setup_output_dir() {
mkdir -p "$OUTPUT_DIR"
print_status "Created output directory: $OUTPUT_DIR"
}
# Function to get user's repositories
get_repositories() {
print_section "Fetching Repositories"
local repos_file="$OUTPUT_DIR/repositories.json"
local repos_summary="$OUTPUT_DIR/repositories_summary.txt"
api_call "https://api.github.com/users/$USERNAME/repos?per_page=$PER_PAGE&sort=updated" > "$repos_file"
# Check if we got valid JSON
if ! jq empty "$repos_file" 2>/dev/null; then
print_error "Failed to fetch repositories or invalid response"
return 1
fi
# Generate repository summary
jq -r '.[] | select(.updated_at >= "'$SINCE_DATE'T00:00:00Z") |
"• \(.name) (\(.language // "N/A")) - Updated: \(.updated_at[:10]) - Stars: \(.stargazers_count) - \(.description // "No description")"' \
"$repos_file" > "$repos_summary"
local repo_count=$(jq '[.[] | select(.updated_at >= "'$SINCE_DATE'T00:00:00Z")] | length' "$repos_file")
print_status "Found $repo_count repositories active since $SINCE_DATE"
}
# Function to get commits across all repositories
get_commits() {
print_section "Fetching Commits"
local commits_file="$OUTPUT_DIR/all_commits.json"
local commits_summary="$OUTPUT_DIR/commits_summary.txt"
# Clear previous results
echo "[]" > "$commits_file"
# Get list of repositories and fetch commits for each
jq -r '.[] | select(.updated_at >= "'$SINCE_DATE'T00:00:00Z") | .name' "$OUTPUT_DIR/repositories.json" | while read -r repo; do
if [[ -n "$repo" ]]; then
print_status "Fetching commits from: $repo"
local repo_commits=$(api_call "https://api.github.com/repos/$USERNAME/$repo/commits?author=$USERNAME&since=${SINCE_DATE}T00:00:00Z&per_page=$PER_PAGE")
if jq empty <<< "$repo_commits" 2>/dev/null && [[ "$repo_commits" != "[]" ]]; then
# Add repository name to each commit and merge with existing commits
echo "$repo_commits" | jq --arg repo "$repo" 'map(. + {repository: $repo})' | \
jq -s '.[0] + .[1]' "$commits_file" - > "${commits_file}.tmp" && mv "${commits_file}.tmp" "$commits_file"
fi
fi
done
# Generate commits summary
jq -r '.[] | "• [\(.repository)] \(.commit.message | split("\n")[0]) - \(.commit.author.date[:10]) - \(.html_url)"' \
"$commits_file" | sort -r > "$commits_summary"
local commit_count=$(jq 'length' "$commits_file")
print_status "Found $commit_count commits since $SINCE_DATE"
}
# Function to get pull requests and issues
get_prs_and_issues() {
print_section "Fetching Pull Requests and Issues"
local prs_file="$OUTPUT_DIR/pull_requests.json"
local issues_file="$OUTPUT_DIR/issues.json"
local prs_summary="$OUTPUT_DIR/prs_summary.txt"
local issues_summary="$OUTPUT_DIR/issues_summary.txt"
# Get PRs authored by user
api_call "https://api.github.com/search/issues?q=author:$USERNAME+is:pr+created:>=$SINCE_DATE&per_page=$PER_PAGE" | \
jq '.items' > "$prs_file"
# Get Issues authored by user
api_call "https://api.github.com/search/issues?q=author:$USERNAME+is:issue+created:>=$SINCE_DATE&per_page=$PER_PAGE" | \
jq '.items' > "$issues_file"
# Generate PR summary
jq -r '.[] | "• [\(.state | ascii_upcase)] \(.title) - Created: \(.created_at[:10]) - \(.html_url)"' \
"$prs_file" > "$prs_summary"
# Generate Issues summary
jq -r '.[] | "• [\(.state | ascii_upcase)] \(.title) - Created: \(.created_at[:10]) - \(.html_url)"' \
"$issues_file" > "$issues_summary"
local pr_count=$(jq 'length' "$prs_file")
local issue_count=$(jq 'length' "$issues_file")
print_status "Found $pr_count pull requests and $issue_count issues since $SINCE_DATE"
}
# Function to get recent activity events
get_recent_events() {
print_section "Fetching Recent Activity Events"
local events_file="$OUTPUT_DIR/recent_events.json"
local events_summary="$OUTPUT_DIR/events_summary.txt"
api_call "https://api.github.com/users/$USERNAME/events?per_page=$PER_PAGE" > "$events_file"
# Filter events since the specified date and generate summary
jq --arg since_date "$SINCE_DATE" -r '
.[] |
select(.created_at >= ($since_date + "T00:00:00Z")) |
"• \(.type) on \(.repo.name) - \(.created_at[:10]) - \(
if .type == "PushEvent" then
"Pushed \(.payload.commits | length) commit(s)"
elif .type == "PullRequestEvent" then
"PR: \(.payload.action) - \(.payload.pull_request.title)"
elif .type == "IssuesEvent" then
"Issue: \(.payload.action) - \(.payload.issue.title)"
elif .type == "CreateEvent" then
"Created \(.payload.ref_type): \(.payload.ref // .repo.name)"
elif .type == "ReleaseEvent" then
"Released: \(.payload.release.tag_name)"
else
.type
end
)"' "$events_file" > "$events_summary"
local event_count=$(jq --arg since_date "$SINCE_DATE" '[.[] | select(.created_at >= ($since_date + "T00:00:00Z"))] | length' "$events_file")
print_status "Found $event_count activity events since $SINCE_DATE"
}
# Function to generate statistics
generate_statistics() {
print_section "Generating Statistics"
local stats_file="$OUTPUT_DIR/statistics.txt"
{
echo "GitHub Activity Statistics for $USERNAME"
echo "Period: $SINCE_DATE to $(date +%Y-%m-%d)"
echo "Generated: $(date)"
echo ""
echo "SUMMARY:"
local repo_count=$(jq '[.[] | select(.updated_at >= "'$SINCE_DATE'T00:00:00Z")] | length' "$OUTPUT_DIR/repositories.json" 2>/dev/null || echo "0")
local commit_count=$(jq 'length' "$OUTPUT_DIR/all_commits.json" 2>/dev/null || echo "0")
local pr_count=$(jq 'length' "$OUTPUT_DIR/pull_requests.json" 2>/dev/null || echo "0")
local issue_count=$(jq 'length' "$OUTPUT_DIR/issues.json" 2>/dev/null || echo "0")
echo "• Active Repositories: $repo_count"
echo "• Total Commits: $commit_count"
echo "• Pull Requests: $pr_count"
echo "• Issues Created: $issue_count"
echo ""
echo "LANGUAGE BREAKDOWN:"
jq -r '.[] | select(.updated_at >= "'$SINCE_DATE'T00:00:00Z") | .language // "Unknown"' \
"$OUTPUT_DIR/repositories.json" 2>/dev/null | sort | uniq -c | sort -nr | \
awk '{printf "• %-15s: %d repositories\n", $2, $1}' || echo "No language data available"
echo ""
echo "TOP REPOSITORIES BY ACTIVITY:"
jq -r '.[] | select(.updated_at >= "'$SINCE_DATE'T00:00:00Z") |
"\(.stargazers_count)\t\(.name)\t\(.updated_at[:10])\t\(.language // "N/A")"' \
"$OUTPUT_DIR/repositories.json" 2>/dev/null | sort -nr | head -10 | \
awk -F'\t' '{printf "• %-25s (Stars: %2d) - %s - %s\n", $2, $1, $3, $4}' || echo "No repository data available"
} > "$stats_file"
print_status "Statistics saved to $stats_file"
}
# Function to generate final report
generate_report() {
print_section "Generating Final Report"
local report_file="$OUTPUT_DIR/GitHub_Activity_Report.md"
{
echo "# GitHub Activity Report"
echo ""
echo "**Developer:** $USERNAME"
echo "**Period:** $SINCE_DATE to $(date +%Y-%m-%d)"
echo "**Generated:** $(date)"
echo ""
if [[ -f "$OUTPUT_DIR/statistics.txt" ]]; then
echo "## Executive Summary"
echo ""
echo '```'
tail -n +5 "$OUTPUT_DIR/statistics.txt"
echo '```'
echo ""
fi
if [[ -f "$OUTPUT_DIR/repositories_summary.txt" && -s "$OUTPUT_DIR/repositories_summary.txt" ]]; then
echo "## Active Repositories"
echo ""
cat "$OUTPUT_DIR/repositories_summary.txt"
echo ""
fi
if [[ -f "$OUTPUT_DIR/commits_summary.txt" && -s "$OUTPUT_DIR/commits_summary.txt" ]]; then
echo "## Recent Commits"
echo ""
head -20 "$OUTPUT_DIR/commits_summary.txt"
if [[ $(wc -l < "$OUTPUT_DIR/commits_summary.txt") -gt 20 ]]; then
echo ""
echo "*...showing first 20 commits. See commits_summary.txt for complete list.*"
fi
echo ""
fi
if [[ -f "$OUTPUT_DIR/prs_summary.txt" && -s "$OUTPUT_DIR/prs_summary.txt" ]]; then
echo "## Pull Requests"
echo ""
cat "$OUTPUT_DIR/prs_summary.txt"
echo ""
fi
if [[ -f "$OUTPUT_DIR/issues_summary.txt" && -s "$OUTPUT_DIR/issues_summary.txt" ]]; then
echo "## Issues Created"
echo ""
cat "$OUTPUT_DIR/issues_summary.txt"
echo ""
fi
if [[ -f "$OUTPUT_DIR/events_summary.txt" && -s "$OUTPUT_DIR/events_summary.txt" ]]; then
echo "## Recent Activity Events"
echo ""
head -15 "$OUTPUT_DIR/events_summary.txt"
if [[ $(wc -l < "$OUTPUT_DIR/events_summary.txt") -gt 15 ]]; then
echo ""
echo "*...showing first 15 events. See events_summary.txt for complete list.*"
fi
echo ""
fi
echo "## Files Generated"
echo ""
echo "This report includes the following detailed files:"
echo ""
for file in "$OUTPUT_DIR"/*.{json,txt}; do
if [[ -f "$file" ]]; then
echo "• \`$(basename "$file")\` - $(wc -l < "$file" 2>/dev/null || echo "0") lines"
fi
done
echo ""
echo "---"
echo "*Report generated by github-activity-summary.sh*"
} > "$report_file"
print_status "Final report saved to $report_file"
}
# Main function
main() {
# Check for help first, before any processing
if [[ "$USERNAME" == "--help" || "$USERNAME" == "-h" || "$USERNAME" == "help" ]]; then
show_help
exit 0
fi
print_status "GitHub Activity Summary Script"
print_status "Analyzing activity for user: $USERNAME since $SINCE_DATE"
if [[ -n "$GITHUB_TOKEN" ]]; then
print_status "Using authenticated requests (higher rate limits)"
else
print_warning "No GitHub token provided. Using unauthenticated requests (60/hour rate limit)"
print_warning "For private repos or higher rate limits, provide a token as the third argument"
print_warning "Run '$0 --help' for detailed token setup instructions"
fi
check_dependencies
validate_inputs
setup_output_dir
# Fetch all data
get_repositories
get_commits
get_prs_and_issues
get_recent_events
# Generate summaries
generate_statistics
generate_report
print_section "Summary Complete!"
print_status "All files saved to: $OUTPUT_DIR"
print_status "Main report: $OUTPUT_DIR/GitHub_Activity_Report.md"
echo ""
echo "📊 Quick Stats:"
if [[ -f "$OUTPUT_DIR/statistics.txt" ]]; then
grep -A 10 "SUMMARY:" "$OUTPUT_DIR/statistics.txt" | tail -n +2
fi
}
# Run main function with all arguments
main "$@"