From 996fede1dfd6f58fe483b3d63300adeb6db1af3f Mon Sep 17 00:00:00 2001 From: Het Patel <124852522+Hunterdii@users.noreply.github.com> Date: Wed, 6 Nov 2024 22:01:24 +0530 Subject: [PATCH] Update update_solution.py --- src/update_solution.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/update_solution.py b/src/update_solution.py index 4f636c58..bc2e2f5d 100644 --- a/src/update_solution.py +++ b/src/update_solution.py @@ -2,22 +2,23 @@ import json import sys import re -from datetime import datetime if __name__ == "__main__": - assert(len(sys.argv) == 4) - handle = sys.argv[1] - token = sys.argv[2] - readmePath = sys.argv[3] + assert(len(sys.argv) == 3) # Adjusted to take only the token and README path + token = sys.argv[1] + readmePath = sys.argv[2] headers = { "Authorization": f"token {token}" } - # Get the latest commit details - commit_url = f"https://api.github.com/repos/{handle}/{sys.argv[1]}/commits?sha=main" + # Set the target repository to the specific repo + repo_name = "Hunterdii/GeeksforGeeks-POTD" + commit_url = f"https://api.github.com/repos/{repo_name}/commits?sha=main" + + # Fetch the latest commit details response = requests.get(commit_url, headers=headers) - + if response.status_code != 200: print(f"Error fetching commit details: {response.text}") sys.exit(1) @@ -27,23 +28,23 @@ commit_message = commit_data['commit']['message'] commit_date = commit_data['commit']['committer']['date'] - # Extract the question name or solution identifier from the commit message (example: "01(Nov) Solution Name") + # Extract the question name or solution identifier from the commit message solution_identifier = commit_message.split(":")[0] # Assuming commit message starts with the identifier - # Generate the badge URL dynamically based on the solution + # Generate the badge URL dynamically based on the solution identifier badge_url = f"https://img.shields.io/badge/Solution-{solution_identifier}-blue" - badge_link = f"[![Today's Solution]({badge_url})](https://github.com/{handle}/{sys.argv[1]}/commit/{commit_sha})" + badge_link = f"[![Today's Solution]({badge_url})](https://github.com/{repo_name}/commit/{commit_sha})" # Prepare the commit link - commit_link = f"Commit URL: https://github.com/{handle}/{sys.argv[1]}/commit/{commit_sha}" + commit_link = f"Commit URL: https://github.com/{repo_name}/commit/{commit_sha}" # Update README with the new commit and badge with open(readmePath, "r") as readme: content = readme.read() # Update the commit link and the badge in the README file - new_content = re.sub(r"(?<=)[\s\S]*(?=)", commit_link, content) - new_content = re.sub(r"(?<=)[\s\S]*(?=)", badge_link, new_content) + new_content = re.sub(r"(?<=)[\s\S]*(?=)", f" {commit_link} ", content) + new_content = re.sub(r"(?<=)[\s\S]*(?=)", f" {badge_link} ", new_content) # Write the updated content back to README with open(readmePath, "w") as readme: