Skip to content

Commit 1939062

Browse files
authored
Merge pull request #97 from sommersoft/cp_org_pr
Submit PRs To circuitpython-org Repo
2 parents 2d75cf2 + 992c819 commit 1939062

File tree

1 file changed

+74
-27
lines changed

1 file changed

+74
-27
lines changed

adabot/update_cp_org_libraries.py

Lines changed: 74 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# THE SOFTWARE.
2222

2323
import argparse
24+
import base64
2425
import datetime
2526
import inspect
2627
import json
@@ -104,29 +105,74 @@ def get_contributors(repo):
104105

105106
return contributors, reviewers, merged_pr_count
106107

107-
def update_json_file(working_directory, cp_org_dir, output_filename, json_string):
108-
""" Clone the circuitpython-org repo, update libraries.json, and push the updates
109-
in a commit.
108+
def update_json_file(json_string):
109+
""" Uses GitHub API to do the following:
110+
- Creates branch on fork 'adafruit-adabot/circuipython-org'
111+
- Updates '_data/libraries.json'
112+
- Creates pull request from fork to upstream
113+
114+
Note: adapted from Scott Shawcroft's code found here
115+
https://github.com/adafruit/circuitpython/blob/master/tools/build_board_info.py
110116
"""
111-
if "TRAVIS" in os.environ:
112-
if not os.path.isdir(cp_org_dir):
113-
os.makedirs(cp_org_dir, exist_ok=True)
114-
git_url = ("https://"
115-
+ os.environ["ADABOT_GITHUB_ACCESS_TOKEN"]
116-
+ "@github.com/adafruit/circuitpython-org.git")
117-
git.clone("-o", "adafruit", git_url, cp_org_dir)
118-
os.chdir(cp_org_dir)
119-
git.pull()
120-
git.submodule("update", "--init", "--recursive")
121-
122-
with open(output_filename, "w") as json_file:
123-
json.dump(json_string, json_file, indent=2)
124-
125-
commit_day = datetime.date.strftime(datetime.date.today(), "%Y-%m-%d")
126-
commit_msg = "adabot: auto-update of libraries.json ({})".format(commit_day)
127-
git.commit("-a", "-m", commit_msg)
128-
git_push = git.push("adafruit", "master")
129-
print(git_push)
117+
master_url = "/repos/adafruit/circuitpython-org/"
118+
fork_url = "/repos/adafruit-adabot/circuitpython-org/"
119+
commit_date = datetime.date.today()
120+
branch_name = "libraries_update_" + commit_date.strftime("%d-%b-%y")
121+
122+
response = github.get(master_url + "git/refs/heads/master")
123+
if not response.ok:
124+
raise RuntimeError(
125+
"Failed to retrieve master sha:\n{}".format(response.text)
126+
)
127+
commit_sha = response.json()["object"]["sha"]
128+
129+
response = github.get(
130+
master_url + "contents/_data/libraries.json?ref=" + commit_sha
131+
)
132+
if not response.ok:
133+
raise RuntimeError(
134+
"Failed to retrieve libraries.json sha:\n{}".format(response.text)
135+
)
136+
blob_sha = response.json()["sha"]
137+
138+
branch_info = {
139+
"ref": "refs/heads/" + branch_name,
140+
"sha": commit_sha
141+
}
142+
response = github.post(fork_url + "git/refs", json=branch_info)
143+
if not response.ok and response.json()["message"] != "Reference already exists":
144+
raise RuntimeError(
145+
"Failed to create branch:\n{}".format(response.text)
146+
)
147+
148+
commit_msg = "Automated Libraries update for {}".format(commit_date.strftime("%d-%b-%y"))
149+
content = json_string.encode("utf-8") + b"\n"
150+
update_json = {
151+
"message": commit_msg,
152+
"content": base64.b64encode(content).decode("utf-8"),
153+
"sha": blob_sha,
154+
"branch": branch_name
155+
}
156+
response = github.put(fork_url + "contents/_data/libraries.json",
157+
json=update_json)
158+
if not response.ok:
159+
raise RuntimeError(
160+
"Failed to update libraries.json:\n{}".format(response.text)
161+
)
162+
163+
pr_info = {
164+
"title": commit_msg,
165+
"head": "adafruit-adabot:" + branch_name,
166+
"base": "master",
167+
"body": commit_msg,
168+
"maintainer_can_modify": True
169+
}
170+
response = github.post(master_url + "pulls", json=pr_info)
171+
if not response.ok:
172+
raise RuntimeError(
173+
"Failed to create pull request:\n{}".format(response.text)
174+
)
175+
130176

131177
if __name__ == "__main__":
132178
cmd_line_args = cmd_line_parser.parse_args()
@@ -155,18 +201,18 @@ def update_json_file(working_directory, cp_org_dir, output_filename, json_string
155201
sys.exit()
156202

157203
working_directory = os.path.abspath(os.getcwd())
158-
cp_org_dir = os.path.join(working_directory, ".cp_org")
204+
#cp_org_dir = os.path.join(working_directory, ".cp_org")
159205

160206
startup_message = [
161207
"Run Date: {}".format(run_time.strftime("%d %B %Y, %I:%M%p"))
162208
]
163209

164-
output_filename = os.path.join(cp_org_dir, "_data/libraries.json")
210+
output_filename = ""
165211
local_file_output = False
166212
if cmd_line_args.output_file:
167213
output_filename = os.path.abspath(cmd_line_args.output_file)
168214
local_file_output = True
169-
startup_message.append(" - Output will be saved to: {}".format(output_filename))
215+
startup_message.append(" - Output will be saved to: {}".format(output_filename))
170216

171217
print("\n".join(startup_message))
172218

@@ -274,9 +320,10 @@ def update_json_file(working_directory, cp_org_dir, output_filename, json_string
274320
json_obj = json.dumps(build_json, indent=2)
275321

276322
if "TRAVIS" in os.environ:
277-
update_json_file(working_directory, cp_org_dir, output_filename, build_json)
323+
update_json_file(json_obj)
278324
else:
325+
#update_json_file(json_obj)
279326
if local_file_output:
280327
with open(output_filename, "w") as json_file:
281328
json.dump(build_json, json_file, indent=2)
282-
print(json.dumps(build_json, indent=2))
329+
print(json_obj)

0 commit comments

Comments
 (0)