commit 2b18ab8bf03355f43ca4f362b626171dc3d040b7
parent d57b3412668e7cc1a48542b8741b88da99f34268
Author: Amit Dutta <amitdutta4255@gmail.com>
Date: Thu, 28 May 2026 07:52:55 +0530
Refactor commit fetching to use events API
Diffstat:
1 file changed, 32 insertions(+), 66 deletions(-)
diff --git a/.github/scripts/update_commits.py b/.github/scripts/update_commits.py
@@ -8,82 +8,48 @@ USERNAME = "notamitgamer"
def fetch_recent_commits():
commits_data = []
try:
- # 1. Get the repos you recently pushed to
- repos_url = f"https://api.github.com/users/{USERNAME}/repos?sort=pushed&per_page=5"
- req = urllib.request.Request(repos_url)
+ # Events API captures activity across ALL repos, including orgs
+ events_url = f"https://api.github.com/users/{USERNAME}/events/public?per_page=100"
+ req = urllib.request.Request(events_url)
req.add_header('User-Agent', f'{USERNAME}-readme-updater')
with urllib.request.urlopen(req) as response:
- repos = json.loads(response.read().decode())
+ events = json.loads(response.read().decode())
- # 2. For each repo, get your recent commits
- for repo in repos:
- repo_name = repo['name']
- # Query commits authored specifically by your username
- commits_url = f"https://api.github.com/repos/{USERNAME}/{repo_name}/commits?author={USERNAME}&per_page=5"
-
- try:
- c_req = urllib.request.Request(commits_url)
- c_req.add_header('User-Agent', f'{USERNAME}-readme-updater')
- with urllib.request.urlopen(c_req) as c_response:
- repo_commits = json.loads(c_response.read().decode())
-
- for commit in repo_commits:
- # Parse date for sorting
- date_str = commit['commit']['author']['date']
- date_obj = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%SZ")
-
- # Extract message and the direct web URL
- msg = commit['commit']['message'].split('\n')[0]
- url = commit['html_url']
-
- commits_data.append({
- 'date': date_obj,
- 'markdown': f"- [{msg}]({url}) in [{repo_name}](https://github.com/{USERNAME}/{repo_name})"
- })
- except Exception as e:
- print(f"Skipping commits for {repo_name} due to error: {e}")
+ for event in events:
+ # Only look at PushEvents (covers direct pushes + merged PRs)
+ if event.get('type') != 'PushEvent':
continue
- # 3. Sort all collected commits by date descending (newest first)
- commits_data.sort(key=lambda x: x['date'], reverse=True)
-
- # 4. Extract just the markdown strings for the top 5
- final_commits = [c['markdown'] for c in commits_data[:5]]
-
- print(f"✅ Successfully found {len(final_commits)} recent commits across your repositories.")
- return final_commits
+ repo_name = event['repo']['name'] # e.g. "is-a-dev/register"
+ commits = event['payload'].get('commits', [])
- except Exception as e:
- print(f"Error fetching data: {e}")
- return []
+ for commit in commits:
+ # Filter to only YOUR commits by email or name
+ author = commit.get('author', {})
+ if author.get('name', '').lower() != USERNAME.lower() and \
+ USERNAME.lower() not in author.get('email', '').lower():
+ continue
-def update_readme(commits):
- if not commits:
- print("No commits found to update.")
- return
+ date_str = event['created_at']
+ date_obj = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%SZ")
- # Format the commits back to standard markdown bullet points
- commit_list_md = "\n".join(commits)
+ msg = commit['message'].split('\n')[0]
+ sha = commit['sha']
+ commit_url = f"https://github.com/{repo_name}/commit/{sha}"
+ repo_url = f"https://github.com/{repo_name}"
- # Read the current README.md
- with open('README.md', 'r', encoding='utf-8') as f:
- readme = f.read()
+ commits_data.append({
+ 'date': date_obj,
+ 'markdown': f"- [{msg}]({commit_url}) in [{repo_name}]({repo_url})"
+ })
- # Use regex to replace the tags and everything in between them
- updated_readme = re.sub(
- r"<!-- START_RECENT_COMMITS -->.*?<!-- END_RECENT_COMMITS -->",
- f"<!-- START_RECENT_COMMITS -->\n{commit_list_md}\n<!-- END_RECENT_COMMITS -->",
- readme,
- flags=re.DOTALL
- )
+ commits_data.sort(key=lambda x: x['date'], reverse=True)
+ final_commits = [c['markdown'] for c in commits_data[:5]]
- # Write the updated content back to README.md
- with open('README.md', 'w', encoding='utf-8') as f:
- f.write(updated_readme)
-
- print("README.md successfully updated!")
+ print(f"Found {len(final_commits)} recent commits across all repositories.")
+ return final_commits
-if __name__ == "__main__":
- recent_commits = fetch_recent_commits()
- update_readme(recent_commits)
+ except Exception as e:
+ print(f"Error fetching data: {e}")
+ return []