2020-10-22 13:39:55 +02:00
|
|
|
import json
|
|
|
|
import logging
|
2021-06-29 21:26:01 +02:00
|
|
|
from typing import Any
|
2020-10-22 13:39:55 +02:00
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from zerver.lib.cache import cache_with_key
|
2021-05-07 03:54:25 +02:00
|
|
|
from zerver.lib.outgoing_http import OutgoingSession
|
2020-10-22 13:39:55 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-05-07 03:54:25 +02:00
|
|
|
class GithubSession(OutgoingSession):
|
2021-06-29 21:26:01 +02:00
|
|
|
def __init__(self, **kwargs: Any) -> None:
|
|
|
|
super().__init__(role="github", timeout=5, **kwargs)
|
2021-05-07 03:54:25 +02: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:
|
2021-05-07 03:54:25 +02:00
|
|
|
return GithubSession().get(api_url).json()["tag_name"]
|
2020-10-22 13:39:55 +02:00
|
|
|
except (requests.RequestException, json.JSONDecodeError, KeyError):
|
2021-08-04 01:29:52 +02:00
|
|
|
logger.exception(
|
|
|
|
"Unable to fetch the latest release version from GitHub %s", api_url, stack_info=True
|
|
|
|
)
|
2020-10-22 13:39:55 +02:00
|
|
|
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:
|
2021-05-07 03:54:25 +02:00
|
|
|
GithubSession().head(link).raise_for_status()
|
2020-10-22 13:39:55 +02:00
|
|
|
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",
|
2021-07-22 03:13:11 +02:00
|
|
|
"mac": "Zulip-{version}-x64.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)
|
2021-08-20 03:25:07 +02:00
|
|
|
link = f"https://desktop-download.zulip.com/v{latest_version}/{setup_file}"
|
2020-10-22 13:39:55 +02:00
|
|
|
if verify_release_download_link(link):
|
|
|
|
return link
|
|
|
|
return "https://github.com/zulip/zulip-desktop/releases/latest"
|