-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtrigger_pipelines.py
More file actions
131 lines (110 loc) · 4.71 KB
/
trigger_pipelines.py
File metadata and controls
131 lines (110 loc) · 4.71 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os
import json
import time
from json import JSONDecodeError
from typing import Dict, Any
import requests
class AzureDevOps:
def __init__(self):
self.base_url = "https://dev.azure.com/NHSD-APIM/API Platform/_apis/pipelines"
self.client_id = os.environ["AZ_CLIENT_ID"]
self.client_secret = os.environ["AZ_CLIENT_SECRET"]
self.client_tenant = os.environ["AZ_CLIENT_TENANT"]
self.access_token = self._get_access_token()
self.notify_commit_sha = os.environ["NOTIFY_COMMIT_SHA"]
self.utils_pr_number = os.environ["UTILS_PR_NUMBER"]
self.notify_github_repo = "NHSDigital/api-management-utils"
self.api_request_delay = 60
@staticmethod
def print_response(response: requests.Response, note: str, verbose: bool = True) -> None:
if verbose:
print(note)
try:
print(json.dumps(response.json(), indent=2))
except json.decoder.JSONDecodeError:
print(response.content.decode())
def run_pipeline(self,
service: str,
pipeline_type: str,
pipeline_id: int,
pipeline_branch: str) -> int:
run_url = self.base_url + f"/{pipeline_id}/runs"
request_body = self._build_request_body(pipeline_branch)
response = self.api_request(
run_url,
json=request_body,
method='post',
)
self.print_response(response, f"Initial request to {run_url}")
result = "failed"
if response.status_code == 200:
result = self._check_pipeline_response(response)
print(f"Result of {service} {pipeline_type} pipeline: {result}")
else:
print(f"Triggering pipeline: {service} {pipeline_type} failed, status code: {response.status_code}")
return result
def _check_pipeline_response(self, response: requests.Response):
delay = 0
state_url = response.json()["_links"]["self"]["href"]
# print("response check from our Start", response.status_code, response.json()["state"])
while response.status_code == 200 and response.json()["state"] == "inProgress":
time.sleep(self.api_request_delay)
delay = delay + self.api_request_delay
response = self.api_request(state_url)
return response.json()["result"]
def _build_request_body(self, pipeline_branch: str):
return {
"resources": {
"repositories": {
"common": {
"repository": {
"fullName": "NHSDigital/api-management-utils",
"type": "gitHub",
},
"refName": f"refs/pull/{self.utils_pr_number}/merge",
},
"self": {"refName": f"{pipeline_branch}"},
}
}
}
def _get_access_token(self):
url = f"https://login.microsoftonline.com/{self.client_tenant}/oauth2/v2.0/token"
data = {
"client_id": self.client_id,
"client_secret": self.client_secret,
"grant_type": "client_credentials",
"scope": "https://app.vssps.visualstudio.com/.default",
}
headers = {"Content-Type": "application/x-www-form-urlencoded"}
res = requests.post(url=url, data=data, headers=headers)
res.raise_for_status()
return res.json()["access_token"]
def api_request(
self,
uri,
params: Dict[str, Any] = None,
headers: Dict[str, Any] = None,
api_version: str = "6.0-preview.1",
method: str = "get",
max_tries: int = 5,
**kwargs,
):
def get_headers():
_headers = {"Accept": "application/json", "Authorization": f"Bearer {self.access_token}"}
_headers.update(headers or {})
return _headers
_params = {"api-version": api_version, "NOTIFY_GITHUB_REPOSITORY": self.notify_github_repo, "NOTIFY_COMMIT_SHA": self.notify_commit_sha, "UTILS_PR_NUMBER": self.utils_pr_number}
_params.update(params or {})
action = getattr(requests, method)
result = action(uri, params=_params, headers=get_headers(), **kwargs)
tries = 0
while result.status_code not in (200, 201, 202, 204):
tries += 1
if tries > max_tries:
break
if result.status_code in (203, 401):
print("REFRESHING ACCESS TOKEN...")
self.access_token = self._get_access_token()
time.sleep(0.5 * tries)
result = action(uri, params=_params, headers=get_headers(), **kwargs)
return result