Skip to content

Commit 7bc994b

Browse files
authored
Check state before changing Milestone (#202)
* add merge check * remove from project * add error check
1 parent 69bd273 commit 7bc994b

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

gh_review_project/review_project.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def _extract_data(cls, raw_data: dict) -> list:
142142
if "milestone" in item_data:
143143
item.milestone = item_data["milestone"]["title"]
144144

145-
if "assignee" in item_data:
145+
if "assignees" in item_data:
146146
item.assignee = item_data["assignees"]
147147

148148
item_list.append(item)
@@ -309,7 +309,8 @@ class ProjectItem:
309309
number: number of the item in the repository
310310
title: title of the item
311311
repo: repository where the item is located
312-
status: status of the item
312+
status: status of the item in the project
313+
state: item state (draft, open, merged, closed)
313314
milestone: title of the milestone
314315
"""
315316

@@ -325,6 +326,7 @@ def __init__(
325326
self.repo = repo
326327

327328
self.status = None
329+
self.state = None
328330
self.milestone = "None"
329331
self.assignee = None
330332

@@ -388,6 +390,16 @@ def add_comment(self, text: str, dry_run: bool = False) -> None:
388390
print(message)
389391
run_command(command)
390392

393+
def check_state(self) -> str:
394+
"""
395+
Fetch item state from gh and store to object
396+
"""
397+
command = f"gh {self.command_type} view {self.number} --repo='{PROJECT_OWNER}/{self.repo}' --json state"
398+
output = run_command(command)
399+
self.state = json.loads(output.stdout)["state"]
400+
401+
return self.state
402+
391403

392404
class PullRequest(ProjectItem):
393405
"""

gh_review_project/set_milestone.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,33 @@ def add_milestone(
3333
"""
3434

3535
print_banner(
36-
f"Setting closed pull requests with no milestone to {current_milestone}"
36+
f"Setting merged pull requests with no milestone to {current_milestone}"
3737
)
3838

3939
closed_prs = reviews.get_milestone(milestone="None", status="closed")
4040

41+
archive_list = []
4142
if closed_prs:
4243
for repo in closed_prs:
4344
for pr in closed_prs[repo]:
44-
pr.modify_milestone(current_milestone, dry_run)
45+
state = pr.check_state()
46+
if state == "MERGED":
47+
pr.modify_milestone(current_milestone, dry_run)
48+
elif state == "CLOSED":
49+
archive_list.append(pr)
50+
else:
51+
print(
52+
f"PR {pr.number} in {pr.repo} is in state {state} with "
53+
f"status {pr.status}. Please manually check it."
54+
)
55+
56+
if archive_list:
57+
print(
58+
"\nThe following PRs were closed without merging. Archive them from the project:"
59+
)
60+
for pr in archive_list:
61+
pr.archive(REVIEW_ID, dry_run)
62+
reviews.project_items.remove(pr)
4563
else:
4664
print("No closed pull requests without a milestone.")
4765

0 commit comments

Comments
 (0)