|
21 | 21 | # THE SOFTWARE.
|
22 | 22 |
|
23 | 23 | import argparse
|
| 24 | +import base64 |
24 | 25 | import datetime
|
25 | 26 | import inspect
|
26 | 27 | import json
|
@@ -104,29 +105,74 @@ def get_contributors(repo):
|
104 | 105 |
|
105 | 106 | return contributors, reviewers, merged_pr_count
|
106 | 107 |
|
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 |
110 | 116 | """
|
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 | + |
130 | 176 |
|
131 | 177 | if __name__ == "__main__":
|
132 | 178 | 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
|
155 | 201 | sys.exit()
|
156 | 202 |
|
157 | 203 | 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") |
159 | 205 |
|
160 | 206 | startup_message = [
|
161 | 207 | "Run Date: {}".format(run_time.strftime("%d %B %Y, %I:%M%p"))
|
162 | 208 | ]
|
163 | 209 |
|
164 |
| - output_filename = os.path.join(cp_org_dir, "_data/libraries.json") |
| 210 | + output_filename = "" |
165 | 211 | local_file_output = False
|
166 | 212 | if cmd_line_args.output_file:
|
167 | 213 | output_filename = os.path.abspath(cmd_line_args.output_file)
|
168 | 214 | 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)) |
170 | 216 |
|
171 | 217 | print("\n".join(startup_message))
|
172 | 218 |
|
@@ -274,9 +320,10 @@ def update_json_file(working_directory, cp_org_dir, output_filename, json_string
|
274 | 320 | json_obj = json.dumps(build_json, indent=2)
|
275 | 321 |
|
276 | 322 | if "TRAVIS" in os.environ:
|
277 |
| - update_json_file(working_directory, cp_org_dir, output_filename, build_json) |
| 323 | + update_json_file(json_obj) |
278 | 324 | else:
|
| 325 | + #update_json_file(json_obj) |
279 | 326 | if local_file_output:
|
280 | 327 | with open(output_filename, "w") as json_file:
|
281 | 328 | json.dump(build_json, json_file, indent=2)
|
282 |
| - print(json.dumps(build_json, indent=2)) |
| 329 | + print(json_obj) |
0 commit comments