Skip to content

Commit 1d03f6f

Browse files
committed
add count of merged prs; finalize auto updating using direct commits
1 parent 7f1c7d8 commit 1d03f6f

File tree

1 file changed

+38
-40
lines changed

1 file changed

+38
-40
lines changed

adabot/update_cp_org_libraries.py

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -99,48 +99,47 @@ def get_open_issues_and_prs(repo):
9999
def get_contributors(repo):
100100
contributors = []
101101
reviewers = []
102+
merged_pr_count = 0
102103
params = {"state":"closed", "sort":"updated", "direction":"desc"}
103104
result = github.get("/repos/adafruit/" + repo["name"] + "/pulls", params=params)
104-
if not result.ok:
105-
return [], []
106-
107-
today_minus_seven = datetime.datetime.today() - datetime.timedelta(days=7)
108-
prs = result.json()
109-
for pr in prs:
110-
merged_at = datetime.datetime.min
111-
if "merged_at" in pr:
112-
if pr["merged_at"] is None:
105+
if result.ok:
106+
today_minus_seven = datetime.datetime.today() - datetime.timedelta(days=7)
107+
prs = result.json()
108+
for pr in prs:
109+
merged_at = datetime.datetime.min
110+
if "merged_at" in pr:
111+
if pr["merged_at"] is None:
112+
continue
113+
merged_at = datetime.datetime.strptime(pr["merged_at"], "%Y-%m-%dT%H:%M:%SZ")
114+
else:
113115
continue
114-
merged_at = datetime.datetime.strptime(pr["merged_at"], "%Y-%m-%dT%H:%M:%SZ")
115-
else:
116-
continue
117-
if merged_at < today_minus_seven:
118-
continue
119-
contributors.append(pr["user"]["login"])
116+
if merged_at < today_minus_seven:
117+
continue
118+
contributors.append(pr["user"]["login"])
119+
merged_pr_count += 1
120120

121-
# get reviewers (merged_by, and any others)
122-
single_pr = github.get(pr["url"])
123-
if not single_pr.ok:
124-
continue
125-
pr_info = single_pr.json()
126-
reviewers.append(pr_info["merged_by"]["login"])
127-
pr_reviews = github.get(str(pr_info["url"]) + "/reviews")
128-
if not pr_reviews.ok:
129-
continue
130-
for review in pr_reviews.json():
131-
if review["state"].lower() == "approved":
132-
reviewers.append(review["user"]["login"])
121+
# get reviewers (merged_by, and any others)
122+
single_pr = github.get(pr["url"])
123+
if not single_pr.ok:
124+
continue
125+
pr_info = single_pr.json()
126+
reviewers.append(pr_info["merged_by"]["login"])
127+
pr_reviews = github.get(str(pr_info["url"]) + "/reviews")
128+
if not pr_reviews.ok:
129+
continue
130+
for review in pr_reviews.json():
131+
if review["state"].lower() == "approved":
132+
reviewers.append(review["user"]["login"])
133133

134-
return contributors, reviewers
134+
return contributors, reviewers, merged_pr_count
135135

136136
def update_json_file(working_directory, cp_org_dir, output_filename, json_string):
137137
""" Clone the circuitpython-org repo, update libraries.json, and push the updates
138-
in a commit/pull request.
138+
in a commit.
139139
"""
140140
if not os.path.isdir(cp_org_dir):
141141
os.makedirs(cp_org_dir, exist_ok=True)
142142
if "TRAVIS" in os.environ:
143-
#git_url = "https://" + os.environ["ADABOT_GITHUB_ACCESS_TOKEN"] + "@github.com/adafruit-adabot/circuitpython-org.git"
144143
git_url = "https://" + os.environ["ADABOT_GITHUB_ACCESS_TOKEN"] + "@github.com/adafruit/circuitpython-org.git"
145144
git.clone("-o", "adafruit", git_url, cp_org_dir)
146145
else:
@@ -149,8 +148,6 @@ def update_json_file(working_directory, cp_org_dir, output_filename, json_string
149148
os.chdir(cp_org_dir)
150149
git.pull()
151150
git.submodule("update", "--init", "--recursive")
152-
#check_branch = git.branch("-r", "--list")
153-
#print("branch result:", check_branch.split("\n"))
154151

155152
with open(output_filename, "w") as json_file:
156153
json.dump(json_string, json_file, indent=2)
@@ -159,7 +156,8 @@ def update_json_file(working_directory, cp_org_dir, output_filename, json_string
159156
commit_day = date.date.strftime(datetime.datetime.today(), "%Y-%m-%d")
160157
commit_msg = "adabot: auto-update of libraries.json ({})".format(commit_day)
161158
git.commit("-a", "-m", commit_msg)
162-
git.push()
159+
git_push = git.push("adafruit", "master")
160+
print(git_push)
163161

164162
if __name__ == "__main__":
165163
cmd_line_args = cmd_line_parser.parse_args()
@@ -193,7 +191,6 @@ def update_json_file(working_directory, cp_org_dir, output_filename, json_string
193191
local_file_output = False
194192
if cmd_line_args.output_file:
195193
output_filename = os.path.abspath(cmd_line_args.output_file)
196-
print(output_filename)
197194
local_file_output = True
198195
startup_message.append(" - Output will be saved to: {}".format(output_filename))
199196

@@ -207,6 +204,7 @@ def update_json_file(working_directory, cp_org_dir, output_filename, json_string
207204
open_prs_by_repo = {}
208205
contributors = []
209206
reviewers = []
207+
merged_pr_count_total = 0
210208
repos_by_error = {}
211209

212210
default_validators = [vals[1] for vals in inspect.getmembers(cpy_vals.library_validator) if vals[0].startswith("validate")]
@@ -233,11 +231,12 @@ def update_json_file(working_directory, cp_org_dir, output_filename, json_string
233231
open_prs_by_repo[repo_name] = check_prs
234232

235233
# get the contributors and reviewers for the last week
236-
get_contribs, get_revs = get_contributors(repo)
234+
get_contribs, get_revs, get_merge_count = get_contributors(repo)
237235
if get_contribs:
238236
contributors.extend(get_contribs)
239237
if get_revs:
240238
reviewers.extend(get_revs)
239+
merged_pr_count_total += get_merge_count
241240

242241
# run repo validators to check for infrastructure errors
243242
errors = validator.run_repo_validation(repo)
@@ -282,19 +281,18 @@ def update_json_file(working_directory, cp_org_dir, output_filename, json_string
282281
"updated_at": run_time.strftime("%Y-%m-%dT%H:%M:%SZ"),
283282
"contributors": [contrib for contrib in set(contributors)],
284283
"reviewers": [rev for rev in set(reviewers)],
284+
"merged_pr_count": str(merged_pr_count_total),
285285
"library_updates": {"new": sorted_new_list, "updated": sorted_updated_list},
286286
"open_issues": sorted_issues_list,
287287
"pull_requests": sorted_prs_list,
288-
"repo_infrastructure_errors": sorted_repos_by_error
288+
"repo_infrastructure_errors": sorted_repos_by_error,
289289
}
290290
json_obj = json.dumps(build_json, indent=2)
291291

292292
if "TRAVIS" in os.environ:
293-
# WIP: will finish after final deployment is determined.
294-
#update_json_file(working_directory, cp_org_dir, output_filename, build_json)
295-
print()
293+
update_json_file(working_directory, cp_org_dir, output_filename, build_json)
296294
else:
297295
if local_file_output:
298296
with open(output_filename, "w") as json_file:
299297
json.dump(build_json, json_file, indent=2)
300-
print(json.dumps(build_json, indent=2))
298+
print(json.dumps(build_json, indent=2))

0 commit comments

Comments
 (0)