Skip to content

Commit

Permalink
Merge pull request #113 from sommersoft/cporg_issue_labels
Browse files Browse the repository at this point in the history
Cirpy.org: Add Label Data to Issues
  • Loading branch information
kattni authored Oct 11, 2019
2 parents b2f6591 + fed3de3 commit 8bf52d0
Showing 1 changed file with 13 additions and 75 deletions.
88 changes: 13 additions & 75 deletions adabot/update_cp_org_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@
# THE SOFTWARE.

import argparse
import base64
import datetime
import inspect
import json
import os
import re
import sh
from sh.contrib import git
import sys

from adabot.lib import common_funcs
from adabot.lib import circuitpython_library_validators as cpy_vals
Expand All @@ -49,6 +45,7 @@

sort_re = re.compile("(?<=\(Open\s)(.+)(?=\sdays)")


def get_open_issues_and_prs(repo):
""" Retreive all of the open issues (minus pull requests) for the repo.
"""
Expand All @@ -70,12 +67,23 @@ def get_open_issues_and_prs(repo):
issue_title = "{0} (Open {1} days)".format(issue["title"],
days_open.days)
if "pull_request" not in issue: # ignore pull requests
open_issues.append({issue["html_url"]: issue_title})
issue_labels = ["None"]
if len(issue["labels"]) != 0:
issue_labels = [label["name"] for label in issue["labels"]]

issue_dict = {
"title": issue_title,
"url": issue["html_url"],
"labels": issue_labels,
}

open_issues.append(issue_dict)
else:
open_pull_requests.append({issue["html_url"]: issue_title})

return open_issues, open_pull_requests


def get_contributors(repo):
contributors = []
reviewers = []
Expand Down Expand Up @@ -115,74 +123,6 @@ def get_contributors(repo):

return contributors, reviewers, merged_pr_count

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)
)


if __name__ == "__main__":
cmd_line_args = cmd_line_parser.parse_args()
Expand All @@ -192,7 +132,6 @@ def update_json_file(json_string):
run_time = datetime.datetime.now()

working_directory = os.path.abspath(os.getcwd())
#cp_org_dir = os.path.join(working_directory, ".cp_org")

startup_message = [
"Run Date: {}".format(run_time.strftime("%d %B %Y, %I:%M%p"))
Expand Down Expand Up @@ -310,7 +249,6 @@ def update_json_file(json_string):
}
json_obj = json.dumps(build_json, indent=2)

#update_json_file(json_obj)
if local_file_output:
with open(output_filename, "w") as json_file:
json.dump(build_json, json_file, indent=2)
Expand Down

0 comments on commit 8bf52d0

Please sign in to comment.