From 6d667b9666e429f279afbf1435b1cee090bf6f9b Mon Sep 17 00:00:00 2001 From: Het Patel <124852522+Hunterdii@users.noreply.github.com> Date: Wed, 6 Nov 2024 23:41:52 +0530 Subject: [PATCH] Update update_commit.py --- src/update_commit.py | 43 ++++++++++--------------------------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/src/update_commit.py b/src/update_commit.py index 992c8362..f0a7f263 100644 --- a/src/update_commit.py +++ b/src/update_commit.py @@ -4,52 +4,29 @@ from datetime import datetime if __name__ == "__main__": - assert(len(sys.argv) == 4) + assert(len(sys.argv) == 5) repository = sys.argv[1] token = sys.argv[2] readme_path = sys.argv[3] + file_name = sys.argv[4] # The filename is now passed dynamically - # GitHub API to get the latest commit from the repository - api_url = f"https://api.github.com/repos/{repository}/commits" - - headers = { - "Authorization": f"token {token}" - } - - # Get the latest commit - response = requests.get(api_url, headers=headers) - if response.status_code != 200: - raise Exception(f"Error fetching commits: {response.status_code}, {response.text}") - - commits = response.json() - latest_commit = commits[0] # Get the most recent commit - - # Extract the commit date and message - commit_date = latest_commit['commit']['author']['date'] - commit_message = latest_commit['commit']['message'] - - # Extract the solution filename from the commit message (assumed format) - # Example: 'Add solution for 07(Nov) Insert in Sorted way in a Sorted DLL' - # We need to extract the file name: '07(Nov) Insert in Sorted way in a Sorted DLL.md' - - # The commit message is expected to have the solution's file name in a consistent format. - solution_filename = f"{commit_message.split(' ')[-1]}.md" - - # Prepare the solution URL based on the commit + # Get the current date today = datetime.today() - month = today.strftime("%b") # Current month (e.g., Nov) + day_of_month = today.strftime("%d") # Get current day of the month (e.g., 06) + month = today.strftime("%b") # Get current month (e.g., Nov) - solution_url = f"https://github.com/{repository}/blob/main/{month}%202024%20GFG%20SOLUTION/{solution_filename}" + # Prepare the solution URL for today (dynamically from the file_name) + solution_url = f"https://github.com/{repository}/blob/main/November%202024%20GFG%20SOLUTION/{file_name}" # Prepare the badge URL and commit link to update README badge_url = "https://img.shields.io/badge/GeeksforGeeks-Solution%20of%20the%20Day-blue" - badge_link = f"[![Today's POTD Solution]({badge_url})]({solution_url})" # This makes the badge link to the solution + badge_link = f"[![Today's POTD Solution]({badge_url})]({solution_url})" # This makes the badge link to the solution for today - # Read the README file and update the sections for the badge + # Read the README file and update the sections for commit and badge with open(readme_path, "r") as readme: content = readme.read() - # Update the badge link in the README + # Update badge link content = re.sub( r"(?<=).*?(?=)", f"\n{badge_link}\n",