def update_json_file(json_string): """ Uses GitHub API to do the following: - Creates branch on fork 'adafruit-adabot/circuipython-org' - Updates '_data/libraries.json' - Creates pull request from fork to upstream Note: adapted from Scott Shawcroft's code found here https://github.com/adafruit/circuitpython/blob/master/tools/build_board_info.py """ master_url = "/repos/adafruit/circuitpython-org/" fork_url = "/repos/adafruit-adabot/circuitpython-org/" commit_date = datetime.date.today() branch_name = "libraries_update_" + commit_date.strftime("%d-%b-%y") response = github.get(master_url + "git/refs/heads/master") if not response.ok: raise RuntimeError("Failed to retrieve master sha:\n{}".format( response.text)) commit_sha = response.json()["object"]["sha"] response = github.get(master_url + "contents/_data/libraries.json?ref=" + commit_sha) if not response.ok: raise RuntimeError("Failed to retrieve libraries.json sha:\n{}".format( response.text)) blob_sha = response.json()["sha"] branch_info = {"ref": "refs/heads/" + branch_name, "sha": commit_sha} response = github.post(fork_url + "git/refs", json=branch_info) if not response.ok and response.json( )["message"] != "Reference already exists": raise RuntimeError("Failed to create branch:\n{}".format( response.text)) commit_msg = "Automated Libraries update for {}".format( commit_date.strftime("%d-%b-%y")) content = json_string.encode("utf-8") + b"\n" update_json = { "message": commit_msg, "content": base64.b64encode(content).decode("utf-8"), "sha": blob_sha, "branch": branch_name } response = github.put(fork_url + "contents/_data/libraries.json", json=update_json) if not response.ok: raise RuntimeError("Failed to update libraries.json:\n{}".format( response.text)) pr_info = { "title": commit_msg, "head": "adafruit-adabot:" + branch_name, "base": "master", "body": commit_msg, "maintainer_can_modify": True } response = github.post(master_url + "pulls", json=pr_info) if not response.ok: raise RuntimeError("Failed to create pull request:\n{}".format( response.text))
def create_pr(changes, updated, git_info): commit_sha, original_blob_sha = git_info branch_name = "new_release_" + changes["new_release"] updated = json.dumps(updated, sort_keys=True, indent=4).encode("utf-8") + b"\n" print(updated.decode("utf-8")) pr_title = "Automated website update for release {}".format( changes["new_release"]) boards = "" if changes["new_boards"]: boards = "New boards:\n* " + "\n* ".join(changes["new_boards"]) languages = "" if changes["new_languages"]: languages = "New languages:\n* " + "\n* ".join( changes["new_languages"]) message = "Automated website update for release {} by AdaBot.\n\n{}\n\n{}\n".format( changes["new_release"], boards, languages) create_branch = {"ref": "refs/heads/" + branch_name, "sha": commit_sha} response = github.post("/repos/adafruit-adabot/circuitpython-org/git/refs", json=create_branch) if not response.ok and response.json( )["message"] != "Reference already exists": print("unable to create branch") print(response.text) return update_file = { "message": message, "content": base64.b64encode(updated).decode("utf-8"), "sha": original_blob_sha, "branch": branch_name } response = github.put( "/repos/adafruit-adabot/circuitpython-org/contents/_data/files.json", json=update_file) if not response.ok: print("unable to post new file") print(response.text) return pr_info = { "title": pr_title, "head": "adafruit-adabot:" + branch_name, "base": "master", "body": message, "maintainer_can_modify": True } response = github.post("/repos/adafruit/circuitpython-org/pulls", json=pr_info) if not response.ok: print("unable to create pr") print(response.text) return print(changes) print(pr_info)
def create_pr(changes, updated, git_info, user): commit_sha, original_blob_sha = git_info branch_name = "new_release_" + changes["new_release"] # Convert the dictionary to a list of boards. Liquid templates only handle arrays. updated_list = [] all_ids = sorted(updated.keys()) for id in all_ids: info = updated[id] info["id"] = id updated_list.append(info) updated = json.dumps(updated_list, sort_keys=True, indent=4).encode("utf-8") + b"\n" #print(updated.decode("utf-8")) pr_title = "Automated website update for release {}".format( changes["new_release"]) boards = "" if changes["new_boards"]: boards = "New boards:\n* " + "\n* ".join(changes["new_boards"]) languages = "" if changes["new_languages"]: languages = "New languages:\n* " + "\n* ".join( changes["new_languages"]) message = "Automated website update for release {} by Blinka.\n\n{}\n\n{}\n".format( changes["new_release"], boards, languages) create_branch = {"ref": "refs/heads/" + branch_name, "sha": commit_sha} response = github.post("/repos/{}/circuitpython-org/git/refs".format(user), json=create_branch) if not response.ok and response.json( )["message"] != "Reference already exists": print("unable to create branch") print(response.text) return update_file = { "message": message, "content": base64.b64encode(updated).decode("utf-8"), "sha": original_blob_sha, "branch": branch_name } response = github.put( "/repos/{}/circuitpython-org/contents/_data/files.json".format(user), json=update_file) if not response.ok: print("unable to post new file") print(response.text) return pr_info = { "title": pr_title, "head": user + ":" + branch_name, "base": "master", "body": message, "maintainer_can_modify": True } response = github.post("/repos/adafruit/circuitpython-org/pulls", json=pr_info) if not response.ok: print("unable to create pr") print(response.text) return print(changes) print(pr_info)