diff --git a/gh_review_project/review_project.py b/gh_review_project/review_project.py index 1c07b74d..31e166fe 100644 --- a/gh_review_project/review_project.py +++ b/gh_review_project/review_project.py @@ -142,7 +142,7 @@ def _extract_data(cls, raw_data: dict) -> list: if "milestone" in item_data: item.milestone = item_data["milestone"]["title"] - if "assignee" in item_data: + if "assignees" in item_data: item.assignee = item_data["assignees"] item_list.append(item) @@ -309,7 +309,8 @@ class ProjectItem: number: number of the item in the repository title: title of the item repo: repository where the item is located - status: status of the item + status: status of the item in the project + state: item state (draft, open, merged, closed) milestone: title of the milestone """ @@ -325,6 +326,7 @@ def __init__( self.repo = repo self.status = None + self.state = None self.milestone = "None" self.assignee = None @@ -388,6 +390,16 @@ def add_comment(self, text: str, dry_run: bool = False) -> None: print(message) run_command(command) + def check_state(self) -> str: + """ + Fetch item state from gh and store to object + """ + command = f"gh {self.command_type} view {self.number} --repo='{PROJECT_OWNER}/{self.repo}' --json state" + output = run_command(command) + self.state = json.loads(output.stdout)["state"] + + return self.state + class PullRequest(ProjectItem): """ diff --git a/gh_review_project/set_milestone.py b/gh_review_project/set_milestone.py index 3abf209b..1995326a 100644 --- a/gh_review_project/set_milestone.py +++ b/gh_review_project/set_milestone.py @@ -33,15 +33,33 @@ def add_milestone( """ print_banner( - f"Setting closed pull requests with no milestone to {current_milestone}" + f"Setting merged pull requests with no milestone to {current_milestone}" ) closed_prs = reviews.get_milestone(milestone="None", status="closed") + archive_list = [] if closed_prs: for repo in closed_prs: for pr in closed_prs[repo]: - pr.modify_milestone(current_milestone, dry_run) + state = pr.check_state() + if state == "MERGED": + pr.modify_milestone(current_milestone, dry_run) + elif state == "CLOSED": + archive_list.append(pr) + else: + print( + f"PR {pr.number} in {pr.repo} is in state {state} with " + f"status {pr.status}. Please manually check it." + ) + + if archive_list: + print( + "\nThe following PRs were closed without merging. Archive them from the project:" + ) + for pr in archive_list: + pr.archive(REVIEW_ID, dry_run) + reviews.project_items.remove(pr) else: print("No closed pull requests without a milestone.")