Skip to content

Commit a601aa9

Browse files
committed
fix: Use Compare API for commit count when Events API lacks data
GitHub Events API sometimes returns PushEvents without commits array. Now uses Compare API (before...head) to get actual commit count and messages.
1 parent 80a6ba0 commit a601aa9

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

services/github_activity.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,41 @@ def parse_event(event):
115115
commits_data = payload.get('commits', [])
116116
commits_count = len(commits_data)
117117

118+
# If commits array is empty but we have head/before SHAs, try Compare API
119+
# GitHub Events API sometimes doesn't include commits array
120+
if commits_count == 0:
121+
head_sha = payload.get('head')
122+
before_sha = payload.get('before')
123+
124+
# If we have both SHAs, try to get commit count from Compare API
125+
if head_sha and before_sha and before_sha != '0000000000000000000000000000000000000000':
126+
try:
127+
headers = {'Accept': 'application/vnd.github.v3+json'}
128+
app_token = os.getenv('GITHUB_TOKEN')
129+
if app_token:
130+
headers['Authorization'] = f'token {app_token}'
131+
132+
compare_url = f"{GITHUB_API}/repos/{repo}/compare/{before_sha}...{head_sha}"
133+
compare_resp = requests.get(compare_url, headers=headers, timeout=5)
134+
135+
if compare_resp.status_code == 200:
136+
compare_data = compare_resp.json()
137+
commits_count = compare_data.get('total_commits', 0)
138+
# Get commit messages from compare
139+
compare_commits = compare_data.get('commits', [])
140+
commits_data = compare_commits # Use for message extraction
141+
logger.info(f"Got {commits_count} commits from Compare API for {repo}")
142+
except Exception as e:
143+
logger.warning(f"Compare API failed for {repo}: {e}")
144+
# Fall back to assuming at least 1 commit since there was a push
145+
commits_count = 1
146+
118147
# Extract commit messages for personalized posts (Pro feature)
119148
# Limit to 5 messages, truncate each to 100 chars
120149
commit_messages = []
121150
for commit in commits_data[:5]:
122-
message = commit.get('message', '')
151+
# Handle both Events API format and Compare API format
152+
message = commit.get('message') or commit.get('commit', {}).get('message', '')
123153
# Take first line only and truncate
124154
first_line = message.split('\n')[0][:100]
125155
if first_line:

0 commit comments

Comments
 (0)