forked from Azure/AI-Landing-Zones
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
47 lines (39 loc) · 1.57 KB
/
Copy pathmain.py
File metadata and controls
47 lines (39 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import re
import requests
def define_env(env):
@env.macro
def latest_release(repo="Azure/AI-Landing-Zones"):
url = f"https://github.com/{repo}/releases/latest"
r = requests.get(url, allow_redirects=True)
return r.url.split("/")[-1]
def _get_latest_prerelease(repo: str, prefer_rc: bool = True) -> dict | None:
api_url = f"https://api.github.com/repos/{repo}/releases?per_page=50"
try:
r = requests.get(
api_url,
headers={"Accept": "application/vnd.github+json"},
timeout=10,
)
r.raise_for_status()
releases = r.json() or []
except Exception:
return None
prereleases = [rel for rel in releases if rel.get("prerelease")]
if not prereleases:
return None
if prefer_rc:
rc_re = re.compile(r"(?:^|[-._])rc(?:[-._]|$)", re.IGNORECASE)
for rel in prereleases:
tag = rel.get("tag_name", "")
name = rel.get("name", "")
if rc_re.search(tag) or rc_re.search(name):
return rel
return prereleases[0]
@env.macro
def latest_release_candidate(repo: str = "Azure/AI-Landing-Zones") -> str:
rel = _get_latest_prerelease(repo)
return (rel or {}).get("tag_name", "")
@env.macro
def latest_release_candidate_url(repo: str = "Azure/AI-Landing-Zones") -> str:
rel = _get_latest_prerelease(repo)
return (rel or {}).get("html_url", f"https://github.com/{repo}/releases")