Skip to content

Commit 454b281

Browse files
authored
upload release assets (#96)
* test release upload * test upload release * install uritemplate * install requests * opt out from travis
1 parent 5b2b688 commit 454b281

File tree

4 files changed

+119
-3
lines changed

4 files changed

+119
-3
lines changed

.github/workflows/githubci.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323

2424
- name: Install Toolchains
2525
run: |
26-
pip3 install adafruit-nrfutil
26+
pip3 install adafruit-nrfutil uritemplate requests
2727
wget https://www.nordicsemi.com/api/sitecore/Products/DownloadPlatform --post-data=fileid=8F19D314130548209E75EFFADD9348DB -O cli-tools.tar
2828
tar -xv -f cli-tools.tar
2929
echo "::add-path::$GITHUB_WORKSPACE/mergehex"
@@ -34,6 +34,11 @@ jobs:
3434
- name: Build
3535
run: python3 tools/build_all.py
3636

37-
- name: Upload release
37+
- name: Upload Release Asset
3838
if: github.event_name == 'release' && (github.event.action == 'published' || github.event.action == 'rerequested')
39-
run: echo Upload... Bin files
39+
working-directory: tools
40+
env:
41+
UPLOAD_URL: ${{ github.event.release.upload_url }}
42+
ADABOT_GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
run: "[ -z \"$ADABOT_GITHUB_ACCESS_TOKEN\" ] || python3 -u upload_release_files.py"
44+
File renamed without changes.

tools/github_requests.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
import os
31+
32+
import requests
33+
34+
35+
def _fix_url(url):
36+
if url.startswith("/"):
37+
url = "https://api.github.com" + url
38+
return url
39+
40+
def _fix_kwargs(kwargs):
41+
api_version = "application/vnd.github.scarlet-witch-preview+json;application/vnd.github.hellcat-preview+json"
42+
if "headers" in kwargs:
43+
if "Accept" in kwargs["headers"]:
44+
kwargs["headers"]["Accept"] += ";" + api_version
45+
else:
46+
kwargs["headers"]["Accept"] = api_version
47+
else:
48+
kwargs["headers"] = {"Accept": "application/vnd.github.hellcat-preview+json"}
49+
if "ADABOT_GITHUB_ACCESS_TOKEN" in os.environ and "auth" not in kwargs:
50+
access_token = os.environ["ADABOT_GITHUB_ACCESS_TOKEN"]
51+
if "params" in kwargs:
52+
kwargs["params"]["access_token"] = access_token
53+
else:
54+
kwargs["params"] = {"access_token": access_token}
55+
if "timeout" not in kwargs:
56+
kwargs["timeout"] = 30
57+
return kwargs
58+
59+
def get(url, **kwargs):
60+
response = requests.get(_fix_url(url), **_fix_kwargs(kwargs))
61+
remaining = int(response.headers["X-RateLimit-Remaining"])
62+
if remaining % 100 == 0:
63+
print(remaining, "requests remaining this hour")
64+
return response
65+
66+
def post(url, **kwargs):
67+
return requests.post(_fix_url(url), **_fix_kwargs(kwargs))
68+
69+
def put(url, **kwargs):
70+
return requests.put(_fix_url(url), **_fix_kwargs(kwargs))
71+
72+
def patch(url, **kwargs):
73+
return requests.patch(_fix_url(url), **_fix_kwargs(kwargs))
74+
75+
def delete(url, **kwargs):
76+
return requests.delete(_fix_url(url), **_fix_kwargs(kwargs))

tools/upload_release_files.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#! /usr/bin/env python3
2+
3+
import os
4+
import os.path
5+
import sys
6+
import uritemplate
7+
8+
sys.path.append("adabot")
9+
import github_requests as github
10+
11+
exit_status = 0
12+
13+
for dirpath, dirnames, filenames in os.walk("../bin"):
14+
if not filenames:
15+
continue
16+
for filename in filenames:
17+
full_filename = os.path.join(dirpath, filename)
18+
label = filename.replace("adafruit-circuitpython-", "")
19+
url_vars = {}
20+
url_vars["name"] = filename
21+
url_vars["label"] = label
22+
url = uritemplate.expand(os.environ["UPLOAD_URL"], url_vars)
23+
headers = {"content-type": "application/octet-stream"}
24+
print(url)
25+
with open(full_filename, "rb") as f:
26+
response = github.post(url, data=f, headers=headers)
27+
if not response.ok:
28+
if response.status_code == 422 and response.json().get("errors", [{"code":""}])[0]["code"] == "already_exists":
29+
print("File already uploaded. Skipping.")
30+
continue
31+
print("Upload of {} failed with {}.".format(filename, response.status_code))
32+
print(response.text)
33+
sys.exit(response.status_code)
34+
35+
sys.exit(exit_status)

0 commit comments

Comments
 (0)