2020-10-22 13:39:55 +02:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from zerver.lib.cache import cache_with_key
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-10-22 13:39:55 +02:00
|
|
|
def get_latest_github_release_version_for_repo(repo: str) -> str:
|
|
|
|
api_url = f"https://api.github.com/repos/zulip/{repo}/releases/latest"
|
|
|
|
try:
|
|
|
|
return requests.get(api_url).json()["tag_name"]
|
|
|
|
except (requests.RequestException, json.JSONDecodeError, KeyError):
|
|
|
|
logger.error("Unable to fetch the latest release version from GitHub %s", api_url)
|
|
|
|
return ""
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-10-22 13:39:55 +02:00
|
|
|
def verify_release_download_link(link: str) -> bool:
|
|
|
|
try:
|
|
|
|
requests.head(link).raise_for_status()
|
|
|
|
return True
|
|
|
|
except requests.RequestException:
|
|
|
|
logger.error("App download link is broken %s", link)
|
|
|
|
return False
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-10-22 13:39:55 +02:00
|
|
|
PLATFORM_TO_SETUP_FILE = {
|
|
|
|
"linux": "Zulip-{version}-x86_64.AppImage",
|
|
|
|
"mac": "Zulip-{version}.dmg",
|
2021-05-06 00:39:52 +02:00
|
|
|
"mac-arm64": "Zulip-{version}-arm64.dmg",
|
2020-10-22 13:39:55 +02:00
|
|
|
"windows": "Zulip-Web-Setup-{version}.exe",
|
|
|
|
}
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-10-22 13:39:55 +02:00
|
|
|
class InvalidPlatform(Exception):
|
|
|
|
pass
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
@cache_with_key(lambda platform: f"download_link:{platform}", timeout=60 * 30)
|
2020-10-22 13:39:55 +02:00
|
|
|
def get_latest_github_release_download_link_for_platform(platform: str) -> str:
|
|
|
|
if platform not in PLATFORM_TO_SETUP_FILE:
|
|
|
|
raise InvalidPlatform()
|
|
|
|
|
|
|
|
latest_version = get_latest_github_release_version_for_repo("zulip-desktop")
|
|
|
|
if latest_version:
|
|
|
|
if latest_version[0] in ["v", "V"]:
|
|
|
|
latest_version = latest_version[1:]
|
|
|
|
setup_file = PLATFORM_TO_SETUP_FILE[platform].format(version=latest_version)
|
|
|
|
link = f"https://github.com/zulip/zulip-desktop/releases/download/v{latest_version}/{setup_file}"
|
|
|
|
if verify_release_download_link(link):
|
|
|
|
return link
|
|
|
|
return "https://github.com/zulip/zulip-desktop/releases/latest"
|