From fa8da00b503537402efaf08b0bcec9a1b1f8726a Mon Sep 17 00:00:00 2001 From: Het Patel <124852522+Hunterdii@users.noreply.github.com> Date: Wed, 6 Nov 2024 23:27:51 +0530 Subject: [PATCH] Update update_commit.py --- src/update_commit.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/update_commit.py b/src/update_commit.py index ee386f08..60abbc0e 100644 --- a/src/update_commit.py +++ b/src/update_commit.py @@ -1,7 +1,9 @@ import requests import sys import re -from datetime import datetime + +# GitHub API to fetch the latest commit +GITHUB_API_URL = "https://api.github.com" if __name__ == "__main__": assert(len(sys.argv) == 4) @@ -9,34 +11,32 @@ token = sys.argv[2] readme_path = sys.argv[3] - # Get the current date - today = datetime.today() - 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) - - # Prepare the solution filename for today (e.g., 06(Nov)Root to leaf paths sum.md) - today_solution_filename = f"{day_of_month}({month}){today.strftime('%A')}.md" + # Fetch the latest commit information for the repository + commits_url = f"{GITHUB_API_URL}/repos/{repository}/commits?per_page=1" + headers = {"Authorization": f"token {token}"} + response = requests.get(commits_url, headers=headers) + response.raise_for_status() + commit_data = response.json()[0] # Get the latest commit - # Prepare the commit URL for today (we don't need to change this as it will be dynamically picked from the latest commit) - solution_url = f"https://github.com/{repository}/blob/main/{month}%202024%20GFG%20SOLUTION/{today_solution_filename}" + # Extract the latest commit's file name and URL + commit_message = commit_data["commit"]["message"] + commit_date = commit_data["commit"]["committer"]["date"] + + # Assume the file is in the `GFG SOLUTION` folder and match the file name based on the commit message (assuming this naming convention is consistent) + solution_filename = commit_message.strip().replace(" ", "%20") + ".md" + + # Construct the solution URL + solution_url = f"https://github.com/{repository}/blob/main/November%202024%20GFG%20SOLUTION/{solution_filename}" # 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 for today + badge_link = f"[![Today's POTD Solution]({badge_url})]({solution_url})" # This makes the badge link to the solution - # Read the README file and update the sections for commit and badge + # Read the README file and update the sections for the commit and badge with open(readme_path, "r") as readme: content = readme.read() - # Update today's solution link (for the commit) - # content = re.sub( - # r"(?<=).*?(?=)", - # f"\n{solution_url}\n", - # content, - # flags=re.DOTALL - # ) - - # Update badge link + # Update the badge link in the README content = re.sub( r"(?<=).*?(?=)", f"\n{badge_link}\n",