-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathado_api.py
More file actions
79 lines (68 loc) · 2.44 KB
/
Copy pathado_api.py
File metadata and controls
79 lines (68 loc) · 2.44 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import requests
from base64 import b64encode
from config import ADO_ORGANIZATION, ADO_PROJECT, ADO_PIPELINE_ID, ADO_PAT
def call_ado_api(min_time: str = None, max_time: str = None) -> str:
"""
Queries Azure DevOps for the latest successful build in the configured pipeline.
Args:
min_time: ISO8601 start of time window (optional)
max_time: ISO8601 end of time window (optional)
Returns:
Build number string (e.g. '2026.3.0.71') or None if not found.
"""
url = (
f"https://dev.azure.com/{ADO_ORGANIZATION}/{ADO_PROJECT}"
f"/_apis/build/builds"
f"?definitions={ADO_PIPELINE_ID}"
f"&statusFilter=completed"
f"&resultFilter=succeeded"
f"&$top=1"
f"&api-version=7.0"
)
if min_time:
url += f"&minTime={min_time}"
if max_time:
url += f"&maxTime={max_time}"
token = b64encode(f":{ADO_PAT}".encode()).decode()
headers = {"Authorization": f"Basic {token}"}
try:
response = requests.get(url, headers=headers, timeout=15)
response.raise_for_status()
data = response.json()
if data.get("value"):
return data["value"][0]["buildNumber"]
return None
except requests.RequestException as e:
print(f"[ado_api] ERROR querying ADO: {e}")
return None
def get_build_history(top: int = 10) -> list:
"""
Returns the last N successful builds from Azure DevOps.
Used by the deployment history tool in mcp_server.py.
"""
url = (
f"https://dev.azure.com/{ADO_ORGANIZATION}/{ADO_PROJECT}"
f"/_apis/build/builds"
f"?definitions={ADO_PIPELINE_ID}"
f"&statusFilter=completed"
f"&resultFilter=succeeded"
f"&$top={top}"
f"&api-version=7.0"
)
token = b64encode(f":{ADO_PAT}".encode()).decode()
headers = {"Authorization": f"Basic {token}"}
try:
response = requests.get(url, headers=headers, timeout=15)
response.raise_for_status()
data = response.json()
builds = []
for b in data.get("value", []):
builds.append({
"buildNumber": b.get("buildNumber"),
"finishTime": b.get("finishTime"),
"requestedBy": b.get("requestedBy", {}).get("displayName", "unknown")
})
return builds
except requests.RequestException as e:
print(f"[ado_api] ERROR fetching build history: {e}")
return []