Skip to content

Commit

Permalink
Update update_solution.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Hunterdii authored Nov 6, 2024
1 parent 4e5488d commit d5dc5cf
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/update_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@
import json
import sys
import re
from datetime import datetime

if __name__ == "__main__":
assert(len(sys.argv) == 4) # Adjusted to expect three arguments + the script name
handle = "Hunterdii" # The fixed GitHub user handle
token = sys.argv[1]
readmePath = sys.argv[2]
assert(len(sys.argv) == 4)
handle = sys.argv[1] # GitHub username (e.g., Hunterdii)
token = sys.argv[2] # GitHub token for authentication
readmePath = sys.argv[3] # Path to the README.md file

headers = {
"Authorization": f"token {token}"
}

# 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
# Correct the repo name and fetch the latest commit details from the correct repository
repo_name = "GeeksforGeeks-POTD" # Specify the correct repo name here
commit_url = f"https://api.github.com/repos/{handle}/{repo_name}/commits?sha=main"
response = requests.get(commit_url, headers=headers)

if response.status_code != 200:
print(f"Error fetching commit details: {response.text}")
sys.exit(1)
Expand All @@ -29,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
# Extract the question name or solution identifier from the commit message (example: "01(Nov) Solution Name")
solution_identifier = commit_message.split(":")[0] # Assuming commit message starts with the identifier

# Generate the badge URL dynamically based on the solution identifier
# Generate the badge URL dynamically based on the solution
badge_url = f"https://img.shields.io/badge/Solution-{solution_identifier}-blue"
badge_link = f"[![Today's Solution]({badge_url})](https://github.com/{repo_name}/commit/{commit_sha})"
badge_link = f"[![Today's Solution]({badge_url})](https://github.com/{handle}/{repo_name}/commit/{commit_sha})"

# Prepare the commit link
commit_link = f"Commit URL: https://github.com/{repo_name}/commit/{commit_sha}"
commit_link = f"Commit URL: https://github.com/{handle}/{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"(?<=<!--START_SECTION:latest-commit-->)[\s\S]*(?=<!--END_SECTION:latest-commit-->)", f" {commit_link} ", content)
new_content = re.sub(r"(?<=<!--START_SECTION:potd-badge-->)[\s\S]*(?=<!--END_SECTION:potd-badge-->)", f" {badge_link} ", new_content)
new_content = re.sub(r"(?<=<!--START_SECTION:latest-commit-->)[\s\S]*(?=<!--END_SECTION:latest-commit-->)", commit_link, content)
new_content = re.sub(r"(?<=<!--START_SECTION:potd-badge-->)[\s\S]*(?=<!--END_SECTION:potd-badge-->)", badge_link, new_content)

# Write the updated content back to README
with open(readmePath, "w") as readme:
Expand Down

0 comments on commit d5dc5cf

Please sign in to comment.