-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcci.py
More file actions
78 lines (62 loc) · 1.88 KB
/
Copy pathcci.py
File metadata and controls
78 lines (62 loc) · 1.88 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
# Common actions for interacting with the CCI API
import requests
import json
import os
import yaml
Token = os.getenv("CIRCLECI_TOKEN")
# Get data from an endpoint and return JSON dict, auto inject token
def get_data(endpoint, slug=""):
if slug != "" and is_valid_slug(slug) is False:
return {}
url = endpoint.format(slug=slug)
r = requests.get(
url,
params={
"circle-token": Token},
headers={
"Accept": "application/json"})
data = r.json()
if not isinstance(data, list) and 'message' in data.keys(
) and data['message'] == 'Not Found':
return {}
return data
# Post dict to endpoint, automatically inject token
def post_data(endpoint, slug="", data={}):
if slug != "" and is_valid_slug(slug) is False:
return {}
url = endpoint.format(slug=slug)
r = requests.post(
url,
data=data,
params={
"circle-token": Token},
headers={
"Accept": "application/json"})
data = r.json()
if 'message' in data.keys() and data['message'] == 'Not Found':
return {}
return data
# Validate project slug
def is_valid_slug(project):
if project == "":
return False
decon = project.split("/")
if len(decon) < 3:
return False
if len(decon[0]) == 2:
if decon[0] != "gh" and decon[0] != "bb":
return False
else:
if decon[0] != "github" and decon[0] != "bitbucket":
return False
return True
# Validate API/token setup.
def validate_setup():
if Token is None or Token == "":
return "token unset"
logged_in = get_data("https://circleci.com/api/v1.1/me")
if 'message' in logged_in and logged_in['message'] == "You must log in first.":
return "cannot authenticate with api"
return True
def parse_config(config):
return yaml.safe_load(config)