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