|
| 1 | +import requests |
| 2 | +import sys |
| 3 | +import re |
| 4 | + |
| 5 | +if __name__ == "__main__": |
| 6 | + assert(len(sys.argv) == 4) |
| 7 | + repository = sys.argv[1] |
| 8 | + token = sys.argv[2] |
| 9 | + readme_path = sys.argv[3] |
| 10 | + |
| 11 | + headers = { |
| 12 | + "Authorization": f"token {token}" |
| 13 | + } |
| 14 | + |
| 15 | + # Get the latest commit of the day for the repository |
| 16 | + commit_url = f"https://api.github.com/repos/{repository}/commits" |
| 17 | + response = requests.get(commit_url, headers=headers) |
| 18 | + |
| 19 | + if not response.ok: |
| 20 | + print(f"Failed to fetch commits: {response.status_code} {response.text}") |
| 21 | + sys.exit(1) |
| 22 | + |
| 23 | + commits = response.json() |
| 24 | + |
| 25 | + # Assuming the first commit in the list is the most recent commit |
| 26 | + latest_commit = commits[0] |
| 27 | + commit_sha = latest_commit['sha'] |
| 28 | + commit_link = f"https://github.com/{repository}/commit/{commit_sha}" |
| 29 | + |
| 30 | + # Prepare the badge URL and commit link to update README |
| 31 | + badge_url = "https://img.shields.io/badge/GeeksforGeeks-Solution%20of%20the%20Day-blue" |
| 32 | + badge_link = f"[](https://github.com/{repository})" |
| 33 | + |
| 34 | + # Read the README file and update the sections for commit and badge |
| 35 | + with open(readme_path, "r") as readme: |
| 36 | + content = readme.read() |
| 37 | + |
| 38 | + # Update commit link |
| 39 | + content = re.sub( |
| 40 | + r"(?<=<!--START_SECTION:latest-commit-->).*?(?=<!--END_SECTION:latest-commit-->)", |
| 41 | + f"\n{commit_link}\n", |
| 42 | + content, |
| 43 | + flags=re.DOTALL |
| 44 | + ) |
| 45 | + |
| 46 | + # Update badge link |
| 47 | + content = re.sub( |
| 48 | + r"(?<=<!--START_SECTION:potd-badge-->).*?(?=<!--END_SECTION:potd-badge-->)", |
| 49 | + f"\n{badge_link}\n", |
| 50 | + content, |
| 51 | + flags=re.DOTALL |
| 52 | + ) |
| 53 | + |
| 54 | + # Write the updated content back to the README |
| 55 | + with open(readme_path, "w") as readme: |
| 56 | + readme.write(content) |
0 commit comments