Skip to content

Commit 1ae3f39

Browse files
committed
Add Travis checking and enabling.
1 parent 9119968 commit 1ae3f39

File tree

4 files changed

+149
-23
lines changed

4 files changed

+149
-23
lines changed

adabot/circuitpython_bundle.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
# THE SOFTWARE.
2222

2323
from adabot import github_requests as github
24-
import json
2524
import os
2625
import subprocess
2726
import shlex
@@ -269,7 +268,7 @@ def new_release(bundle, bundle_path):
269268
"draft": False,
270269
"prerelease": False}
271270

272-
response = github.post("/repos/adafruit/" + bundle + "/releases", data=json.dumps(release))
271+
response = github.post("/repos/adafruit/" + bundle + "/releases", json=release)
273272
if not response.ok:
274273
print(response.request.url)
275274
print(response.text)

adabot/circuitpython_libraries.py

+74-14
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,89 @@
2121
# THE SOFTWARE.
2222

2323
from adabot import github_requests as github
24+
from adabot import travis_requests as travis
2425
import sys
2526

2627
def list_repos():
27-
result = github.get("/search/repositories?q=Adafruit_CircuitPython+in%3Aname")
28-
return result.json()["items"]
28+
result = github.get("/search/repositories",
29+
params={"q":"Adafruit_CircuitPython in:name",
30+
"per_page": 100})
31+
result = result.json()
32+
if result["total_count"] > len(result["items"]):
33+
print("Implement pagination of results!!!")
34+
return result["items"]
2935

30-
def validate_teams(repo):
31-
if repo["owner"]["login"] != "adafruit":
36+
def validate_repo(repo):
37+
if not (repo["owner"]["login"] == "adafruit" and
38+
repo["name"].startswith("Adafruit_CircuitPython")):
3239
return True
33-
result = github.get(repo["teams_url"])
34-
ok = False
35-
for team in result.json():
36-
ok = ok or team["name"] == "CircuitPythonLibrarians"
37-
if not ok:
38-
print(repo["full_name"], "missing CircuitPythonLibrarians team.")
40+
full_repo = github.get("/repos/" + repo["full_name"])
41+
if not full_repo.ok:
42+
print("Unable to pull repo details")
43+
return False
44+
ok = True
45+
if repo["has_wiki"]:
46+
print("Wiki should be disabled " + repo["full_name"])
47+
ok = False
48+
if not repo["license"]:
49+
print(repo["full_name"], "missing license.")
50+
ok = False
51+
if not repo["permissions"]["push"]:
52+
print(repo["full_name"], "likely missing CircuitPythonLibrarians team.")
53+
ok = False
3954
return ok
4055

41-
validators = [validate_teams]
56+
def validate_travis(repo):
57+
if not (repo["owner"]["login"] == "adafruit" and
58+
repo["name"].startswith("Adafruit_CircuitPython")):
59+
return True
60+
repo_url = "/repo/" + repo["owner"]["login"] + "%2F" + repo["name"]
61+
result = travis.get(repo_url)
62+
if not result.ok:
63+
print(result, result.request.url, result.request.headers)
64+
print(result.text)
65+
print("Travis error with repo:", repo["full_name"])
66+
return False
67+
result = result.json()
68+
if not result["active"]:
69+
activate = travis.post(repo_url + "/activate")
70+
if not activate.ok:
71+
print(activate, activate.text)
72+
print("Unable to enable Travis build for " + repo["full_name"])
73+
return False
74+
75+
env_variables = travis.get(repo_url + "/env_vars")
76+
if not env_variables.ok:
77+
print(env_variables, env_variables.text)
78+
print(env_variables.request.headers)
79+
print("Unable to read Travis env variables for " + repo["full_name"])
80+
return False
81+
env_variables = env_variables.json()
82+
found_token = False
83+
for var in env_variables["env_vars"]:
84+
found_token = found_token or var["name"] == "GITHUB_TOKEN"
85+
if not found_token:
86+
print("Unable to find GITHUB_TOKEN env variable for " + repo["full_name"])
87+
return False
88+
89+
validators = [validate_repo, validate_travis]
4290

4391
def validate_repo(repo):
92+
ok = True
4493
for validator in validators:
45-
validator(repo)
94+
ok = validator(repo) and ok
95+
return ok
4696

4797
if __name__ == "__main__":
48-
for repo in list_repos():
49-
validate_repo(repo)
98+
repos = list_repos()
99+
print("Found {} repos to check.".format(len(repos)))
100+
github_user = github.get("/user").json()
101+
print("Running GitHub checks as " + github_user["login"])
102+
travis_user = travis.get("/user").json()
103+
print("Running Travis checks as " + travis_user["login"])
104+
need_work = 0
105+
for repo in repos:
106+
if not validate_repo(repo):
107+
need_work += 1
108+
print()
109+
print("{} out of {} repos need work.".format(need_work, len(repos)))

adabot/github_requests.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,25 @@
3232
import requests
3333

3434
def _fix_url(url):
35-
if "ADABOT_GITHUB_ACCESS_TOKEN" in os.environ:
36-
if "?" in url:
37-
url += "&"
38-
else:
39-
url += "?"
40-
url += "access_token=" + os.environ["ADABOT_GITHUB_ACCESS_TOKEN"]
4135
if url.startswith("/"):
4236
url = "https://api.github.com" + url
4337
return url
4438

4539
def _fix_kwargs(kwargs):
46-
api_version = "application/vnd.github.hellcat-preview+json"
40+
api_version = "application/vnd.github.scarlet-witch-preview+json;application/vnd.github.hellcat-preview+json"
4741
if "headers" in kwargs:
4842
if "Accept" in kwargs["headers"]:
4943
kwargs["headers"]["Accept"] += ";" + api_version
5044
else:
5145
kwargs["headers"]["Accept"] = api_version
5246
else:
5347
kwargs["headers"] = {"Accept": "application/vnd.github.hellcat-preview+json"}
48+
if "ADABOT_GITHUB_ACCESS_TOKEN" in os.environ:
49+
access_token = os.environ["ADABOT_GITHUB_ACCESS_TOKEN"]
50+
if "params" in kwargs:
51+
kwargs["params"]["access_token"] = access_token
52+
else:
53+
kwargs["params"] = {"access_token": access_token}
5454
return kwargs
5555

5656
def get(url, **kwargs):

adabot/travis_requests.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_adabot`
24+
====================================================
25+
26+
TODO(description)
27+
28+
* Author(s): Scott Shawcroft
29+
"""
30+
31+
import os
32+
import requests
33+
import sys
34+
35+
def _fix_url(url):
36+
if url.startswith("/"):
37+
url = "https://api.travis-ci.org" + url
38+
return url
39+
40+
def _auth_token():
41+
if not "ADABOT_TRAVIS_ACCESS_TOKEN" in os.environ:
42+
print("Please configure the ADABOT_TRAVIS_ACCESS_TOKEN environment variable.")
43+
return "token "
44+
return "token {}".format(os.environ["ADABOT_TRAVIS_ACCESS_TOKEN"])
45+
46+
def _fix_kwargs(kwargs):
47+
user_agent = "AdafruitAdabot"
48+
if "headers" in kwargs:
49+
kwargs["headers"]["Authorization"] = _auth_token()
50+
kwargs["headers"]["User-Agent"] = user_agent
51+
kwargs["headers"]["Travis-API-Version"] = "3"
52+
else:
53+
kwargs["headers"] = {
54+
"Authorization": _auth_token(),
55+
"User-Agent": user_agent,
56+
"Travis-API-Version": "3"
57+
}
58+
return kwargs
59+
60+
def get(url, **kwargs):
61+
return requests.get(_fix_url(url), **_fix_kwargs(kwargs))
62+
63+
def post(url, **kwargs):
64+
return requests.post(_fix_url(url), **_fix_kwargs(kwargs))
65+
66+
def put(url, **kwargs):
67+
return requests.put(_fix_url(url), **_fix_kwargs(kwargs))

0 commit comments

Comments
 (0)