|
1 |
| -import requests |
2 | 1 | import sys
|
3 | 2 | import re
|
| 3 | +import requests |
4 | 4 |
|
5 |
| -def get_latest_commit(repo): |
| 5 | +def fetch_latest_commit(repo, token, file_pattern): |
| 6 | + headers = {"Authorization": f"token {token}"} |
6 | 7 | url = f"https://api.github.com/repos/{repo}/commits"
|
7 |
| - response = requests.get(url) |
8 |
| - if response.status_code != 200: |
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 |
| 8 | + params = {"path": file_pattern, "per_page": 1} |
| 9 | + response = requests.get(url, headers=headers, params=params) |
| 10 | + |
| 11 | + if response.ok and response.json(): |
| 12 | + commit_data = response.json()[0] |
| 13 | + commit_url = commit_data["html_url"] |
| 14 | + return commit_url |
| 15 | + else: |
| 16 | + print("Error fetching commit data:", response.status_code, response.text) |
| 17 | + sys.exit(1) |
14 | 18 |
|
15 |
| -def update_readme(readme_path, commit_url, commit_message): |
| 19 | +def update_readme(readme_path, commit_url): |
| 20 | + badge_url = "https://img.shields.io/badge/GeeksforGeeks-Solution%20of%20the%20Day-blue" |
| 21 | + badge_link = f"[]({commit_url})" |
| 22 | + |
16 | 23 | with open(readme_path, "r") as file:
|
17 | 24 | 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\-\->)", |
| 25 | + |
| 26 | + new_content = re.sub( |
| 27 | + r"(?<=<!--START_SECTION:latest-commit-->)[\s\S]*(?=<!--END_SECTION:latest-commit-->)", |
26 | 28 | f"\n{commit_url}\n",
|
27 | 29 | content
|
28 | 30 | )
|
29 |
| - updated_content = re.sub( |
30 |
| - r"(?<=<!\-\-START_SECTION:potd\-badge\-\->)[\s\S]*(?=<!\-\-END_SECTION:potd\-badge\-\->)", |
| 31 | + |
| 32 | + new_content = re.sub( |
| 33 | + r"(?<=<!--START_SECTION:potd-badge-->)[\s\S]*(?=<!--END_SECTION:potd-badge-->)", |
31 | 34 | f"\n{badge_link}\n",
|
32 |
| - updated_content |
| 35 | + new_content |
33 | 36 | )
|
34 | 37 |
|
35 | 38 | with open(readme_path, "w") as file:
|
36 |
| - file.write(updated_content) |
| 39 | + file.write(new_content) |
37 | 40 |
|
38 | 41 | if __name__ == "__main__":
|
39 | 42 | repo = sys.argv[1]
|
40 |
| - readme_path = sys.argv[2] |
| 43 | + token = sys.argv[2] |
| 44 | + readme_path = sys.argv[3] |
| 45 | + file_pattern = sys.argv[4] # Example pattern: "01(Nov)question name.md" |
41 | 46 |
|
42 |
| - commit_url, commit_message = get_latest_commit(repo) |
43 |
| - update_readme(readme_path, commit_url, commit_message) |
| 47 | + commit_url = fetch_latest_commit(repo, token, file_pattern) |
| 48 | + update_readme(readme_path, commit_url) |
0 commit comments