main
py 87 lines 2.81 KB
Raw
1 from typing import Any
2 from typing import Dict
3 from typing import Optional
4
5 import httpx
6 from loguru import logger
7 from packaging.version import Version
8
9 # Current version - update this with each release
10 CURRENT_VERSION = "0.1.68"
11 VERSION_CHECK_URL = "https://api.github.com/repos/socfortress/CoPilot/releases/latest"
12
13
14 async def get_latest_version() -> Optional[Dict[str, Any]]:
15 """
16 Fetch the latest version from GitHub releases
17
18 Returns:
19 dict: Release information including version, url, and body or None if unable to fetch
20 """
21 try:
22 async with httpx.AsyncClient() as client:
23 response = await client.get(VERSION_CHECK_URL, timeout=5.0)
24 if response.status_code == 200:
25 data = response.json()
26 return {
27 "tag_name": data.get("tag_name", "").lstrip("v"),
28 "html_url": data.get("html_url", ""),
29 "published_at": data.get("published_at", ""),
30 "body": data.get("body", ""),
31 "name": data.get("name", "").lstrip("v"),
32 }
33 except Exception as e:
34 logger.warning(f"Failed to fetch latest version: {e}")
35 return None
36
37
38 async def check_version_outdated() -> dict:
39 """
40 Check if current version is outdated
41
42 Returns:
43 dict: Version check results
44 """
45 release_info = await get_latest_version()
46
47 if not release_info:
48 return {
49 "success": False,
50 "message": "Unable to check for updates",
51 "current_version": CURRENT_VERSION,
52 "latest_version": None,
53 "is_outdated": False,
54 "release_url": None,
55 "release_notes": None,
56 "published_at": None,
57 }
58
59 latest_version = release_info.get("tag_name")
60
61 try:
62 current = Version(CURRENT_VERSION)
63 latest = Version(latest_version)
64 is_outdated = current < latest
65
66 return {
67 "success": True,
68 "message": f"New version v{latest_version} available!" if is_outdated else "You're up to date",
69 "current_version": CURRENT_VERSION,
70 "latest_version": latest_version,
71 "is_outdated": is_outdated,
72 "release_url": release_info.get("html_url"),
73 "release_notes": release_info.get("body"),
74 "published_at": release_info.get("published_at"),
75 }
76 except Exception as e:
77 logger.error(f"Error comparing versions: {e}")
78 return {
79 "success": False,
80 "message": "Error checking version",
81 "current_version": CURRENT_VERSION,
82 "latest_version": latest_version,
83 "is_outdated": False,
84 "release_url": release_info.get("html_url"),
85 "release_notes": None,
86 "published_at": None,
87 }