mirror of
https://github.com/JonasunderscoreJones/turbo-octo-potato.git
synced 2025-10-25 19:19:19 +02:00
Added project version support
Added the ability to fetch project versions and sort alphabetically
This commit is contained in:
parent
ca0f6bbac5
commit
f50b812e71
1 changed files with 56 additions and 17 deletions
|
|
@ -2,15 +2,25 @@ import os
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
# Load environment variables from .env file
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
GITHUB_API_TOKEN = os.getenv("GITHUB_API_TOKEN")
|
||||||
|
|
||||||
# GitHub API base URL
|
# GitHub API base URL
|
||||||
BASE_URL = "https://api.github.com/repos/"
|
BASE_URL = "https://api.github.com/repos/"
|
||||||
|
|
||||||
# Function to get the default branch name for a GitHub repository
|
# Function to get the default branch name for a GitHub repository
|
||||||
def get_default_branch(repo):
|
def get_default_branch(repo, access_token):
|
||||||
try:
|
try:
|
||||||
url = f"{BASE_URL}{repo}"
|
url = f"{BASE_URL}{repo}"
|
||||||
response = requests.get(url)
|
headers = {
|
||||||
|
"Authorization": f"Bearer {access_token}",
|
||||||
|
"Accept": "application/json",
|
||||||
|
}
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
repo_data = response.json()
|
repo_data = response.json()
|
||||||
default_branch = repo_data["default_branch"]
|
default_branch = repo_data["default_branch"]
|
||||||
|
|
@ -20,20 +30,43 @@ def get_default_branch(repo):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Function to get the timestamp of the last commit date for a GitHub repository
|
# Function to get the timestamp of the last commit date for a GitHub repository
|
||||||
def get_last_commit_timestamp(repo):
|
def get_last_commit_timestamp(repo, access_token):
|
||||||
default_branch = get_default_branch(repo)
|
default_branch = get_default_branch(repo, access_token)
|
||||||
if default_branch:
|
if default_branch:
|
||||||
try:
|
|
||||||
url = f"{BASE_URL}{repo}/commits/{default_branch}"
|
url = f"{BASE_URL}{repo}/commits/{default_branch}"
|
||||||
response = requests.get(url)
|
headers = {
|
||||||
|
"Authorization": f"Bearer {access_token}",
|
||||||
|
"X-GitHub-Api-Version": "2022-11-28",
|
||||||
|
"Accept": "application/json",
|
||||||
|
}
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
commit_data = response.json()
|
commit_data = response.json()
|
||||||
last_commit_date = commit_data["commit"]["committer"]["date"]
|
last_commit_date = commit_data["commit"]["author"]["date"]
|
||||||
# Convert last_commit_date to Unix timestamp
|
# Convert last_commit_date to Unix timestamp
|
||||||
timestamp = datetime.strptime(last_commit_date, '%Y-%m-%dT%H:%M:%SZ').timestamp()
|
timestamp = datetime.strptime(last_commit_date, '%Y-%m-%dT%H:%M:%SZ').timestamp()
|
||||||
return int(timestamp)
|
return int(timestamp)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_last_release_version(repo, access_token):
|
||||||
|
try:
|
||||||
|
url = f"{BASE_URL}{repo}/releases/latest"
|
||||||
|
headers = {
|
||||||
|
"Authorization": f"Bearer {access_token}",
|
||||||
|
"Accept": "application/json",
|
||||||
|
}
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
|
|
||||||
|
# Check if the response status code indicates that the release does not exist
|
||||||
|
if response.status_code == 404:
|
||||||
|
return None
|
||||||
|
|
||||||
|
response.raise_for_status()
|
||||||
|
release_data = response.json()
|
||||||
|
last_release_version = release_data["tag_name"]
|
||||||
|
return last_release_version
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error fetching last commit date for {repo}: {str(e)}")
|
print(f"Error fetching last release version for {repo}: {str(e)}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Path to the projects.json file
|
# Path to the projects.json file
|
||||||
|
|
@ -47,12 +80,18 @@ with open(projects_json_path, "r") as file:
|
||||||
for project in projects_data:
|
for project in projects_data:
|
||||||
gh_api = project.get("gh_api")
|
gh_api = project.get("gh_api")
|
||||||
if gh_api:
|
if gh_api:
|
||||||
last_commit_timestamp = get_last_commit_timestamp(gh_api)
|
last_commit_timestamp = get_last_commit_timestamp(gh_api, GITHUB_API_TOKEN)
|
||||||
|
last_release_version = get_last_release_version(gh_api, GITHUB_API_TOKEN)
|
||||||
if last_commit_timestamp:
|
if last_commit_timestamp:
|
||||||
project["last_update"] = last_commit_timestamp
|
project["last_update"] = last_commit_timestamp
|
||||||
|
if last_release_version:
|
||||||
|
project["version"] = last_release_version.replace("v", "")
|
||||||
|
|
||||||
|
# sort projects alphabetically
|
||||||
|
projects_data = sorted(projects_data, key=lambda x: x["title"])
|
||||||
|
|
||||||
# Save the updated data back to the projects.json file
|
# Save the updated data back to the projects.json file
|
||||||
with open(projects_json_path, "w") as file:
|
with open(projects_json_path, "w") as file:
|
||||||
json.dump(projects_data, file, indent=2)
|
json.dump(projects_data, file, indent=2)
|
||||||
|
|
||||||
print("Updated projects.json with last_update (Unix timestamp) information.")
|
print("Updated projects.json")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue