Skip to content

Commit

Permalink
Update update_commit.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Hunterdii authored Nov 6, 2024
1 parent 0ab8ff1 commit 6d667b9
Showing 1 changed file with 10 additions and 33 deletions.
43 changes: 10 additions & 33 deletions src/update_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"(?<=<!--START_SECTION:potd-badge-->).*?(?=<!--END_SECTION:potd-badge-->)",
f"\n{badge_link}\n",
Expand Down

0 comments on commit 6d667b9

Please sign in to comment.