|
1
|
|
|
import httplib |
|
2
|
|
|
|
|
3
|
|
|
import requests |
|
4
|
|
|
|
|
5
|
|
|
from st2actions.runners.pythonrunner import Action |
|
6
|
|
|
|
|
7
|
|
|
BASE_API_URL = 'https://circleci.com/api' |
|
8
|
|
|
API_VERSION = 'v1' |
|
9
|
|
|
API_URL = '%s/%s/' % (BASE_API_URL, API_VERSION) |
|
10
|
|
|
HEADER_ACCEPT = 'application/json' |
|
11
|
|
|
HEADER_CONTENT_TYPE = 'application/json' |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class CircleCI(Action): |
|
15
|
|
|
def _get_base_headers(self): |
|
16
|
|
|
headers = {} |
|
17
|
|
|
headers['Content-Type'] = HEADER_CONTENT_TYPE |
|
18
|
|
|
headers['Accept'] = HEADER_ACCEPT |
|
19
|
|
|
return headers |
|
20
|
|
|
|
|
21
|
|
|
def _get_auth_headers(self): |
|
22
|
|
|
headers = self._get_base_headers() |
|
23
|
|
|
token = self.config.get('token', None) |
|
24
|
|
|
|
|
25
|
|
|
if not token: |
|
26
|
|
|
raise Exception('Token not found in config file.') |
|
27
|
|
|
|
|
28
|
|
|
headers['circle-token'] = token |
|
29
|
|
|
return headers |
|
30
|
|
|
|
|
31
|
|
|
def _perform_request(self, path, method, data=None, requires_auth=True, |
|
32
|
|
|
extra_headers=None): |
|
33
|
|
|
url = API_URL + path |
|
34
|
|
|
|
|
35
|
|
|
headers = self._get_base_headers() |
|
36
|
|
|
if requires_auth: |
|
37
|
|
|
headers = self._get_auth_headers() |
|
38
|
|
|
|
|
39
|
|
|
if extra_headers: |
|
40
|
|
|
headers.update(extra_headers) |
|
41
|
|
|
|
|
42
|
|
|
if method == "GET": |
|
43
|
|
|
response = requests.get(url, headers=headers) |
|
44
|
|
|
elif method == 'POST': |
|
45
|
|
|
response = requests.post(url, headers=headers) |
|
46
|
|
|
elif method == 'PUT': |
|
47
|
|
|
response = requests.put(url, data=data, headers=headers) |
|
48
|
|
|
|
|
49
|
|
|
if response.status_code in [httplib.FORBIDDEN, httplib.UNAUTHORIZED]: |
|
50
|
|
|
msg = ('Invalid or missing Travis CI auth token. ' + |
|
51
|
|
|
'Make sure you have' |
|
52
|
|
|
'specified valid token in the config file') |
|
53
|
|
|
raise Exception(msg) |
|
54
|
|
|
|
|
55
|
|
|
return response |
|
56
|
|
|
|