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 dadf53c commit 996fede
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/update_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"(?<=<!--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)
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)

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

0 comments on commit 996fede

Please sign in to comment.