|
| 1 | +import requests |
| 2 | +import json |
| 3 | +import sys |
| 4 | +import re |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +if __name__ == "__main__": |
| 8 | + assert(len(sys.argv) == 4) |
| 9 | + handle = sys.argv[1] |
| 10 | + token = sys.argv[2] |
| 11 | + readmePath = sys.argv[3] |
| 12 | + |
| 13 | + headers = { |
| 14 | + "Authorization": f"token {token}" |
| 15 | + } |
| 16 | + |
| 17 | + # Get the latest commit details |
| 18 | + commit_url = f"https://api.github.com/repos/{handle}/{sys.argv[1]}/commits?sha=main" |
| 19 | + response = requests.get(commit_url, headers=headers) |
| 20 | + |
| 21 | + if response.status_code != 200: |
| 22 | + print(f"Error fetching commit details: {response.text}") |
| 23 | + sys.exit(1) |
| 24 | + |
| 25 | + commit_data = response.json()[0] |
| 26 | + commit_sha = commit_data['sha'] |
| 27 | + commit_message = commit_data['commit']['message'] |
| 28 | + commit_date = commit_data['commit']['committer']['date'] |
| 29 | + |
| 30 | + # Extract the question name or solution identifier from the commit message (example: "01(Nov) Solution Name") |
| 31 | + solution_identifier = commit_message.split(":")[0] # Assuming commit message starts with the identifier |
| 32 | + |
| 33 | + # Generate the badge URL dynamically based on the solution |
| 34 | + badge_url = f"https://img.shields.io/badge/Solution-{solution_identifier}-blue" |
| 35 | + badge_link = f"[](https://github.com/{handle}/{sys.argv[1]}/commit/{commit_sha})" |
| 36 | + |
| 37 | + # Prepare the commit link |
| 38 | + commit_link = f"Commit URL: https://github.com/{handle}/{sys.argv[1]}/commit/{commit_sha}" |
| 39 | + |
| 40 | + # Update README with the new commit and badge |
| 41 | + with open(readmePath, "r") as readme: |
| 42 | + content = readme.read() |
| 43 | + |
| 44 | + # Update the commit link and the badge in the README file |
| 45 | + new_content = re.sub(r"(?<=<!--START_SECTION:latest-commit-->)[\s\S]*(?=<!--END_SECTION:latest-commit-->)", commit_link, content) |
| 46 | + new_content = re.sub(r"(?<=<!--START_SECTION:potd-badge-->)[\s\S]*(?=<!--END_SECTION:potd-badge-->)", badge_link, new_content) |
| 47 | + |
| 48 | + # Write the updated content back to README |
| 49 | + with open(readmePath, "w") as readme: |
| 50 | + readme.write(new_content) |
| 51 | + |
| 52 | + print("Successfully updated README with the latest commit and badge.") |
0 commit comments