refactor: extract issue handling into dedicated functions

This commit is contained in:
Paul Gauthier (aider) 2024-11-05 07:02:24 -08:00
parent e8c153f72f
commit 17351e8f91

View file

@ -130,53 +130,33 @@ def find_unlabeled_with_paul_comments(issues):
return unlabeled_issues return unlabeled_issues
def main(): def handle_unlabeled_issues(all_issues, auto_yes):
parser = argparse.ArgumentParser(description="Handle duplicate GitHub issues") print("\nFinding unlabeled issues with paul-gauthier comments...")
parser.add_argument( unlabeled_issues = find_unlabeled_with_paul_comments(all_issues)
"--yes", action="store_true", help="Automatically close duplicates without prompting"
)
parser.add_argument(
"--find-unlabeled",
action="store_true",
help="Find unlabeled issues with paul-gauthier comments",
)
args = parser.parse_args()
if not TOKEN: if not unlabeled_issues:
print("Error: Missing GITHUB_TOKEN environment variable. Please check your .env file.") print("No unlabeled issues with paul-gauthier comments found.")
return return
all_issues = get_issues("all") print(f"\nFound {len(unlabeled_issues)} unlabeled issues with paul-gauthier comments:")
for issue in unlabeled_issues:
print(f" - #{issue['number']}: {issue['title']} {issue['html_url']}")
if args.find_unlabeled: if not auto_yes:
#ai refactor this into a function... confirm = input("\nDo you want to add the 'question' label to these issues? (y/n): ")
print("\nFinding unlabeled issues with paul-gauthier comments...") if confirm.lower() != "y":
unlabeled_issues = find_unlabeled_with_paul_comments(all_issues) print("Skipping labeling.")
if not unlabeled_issues:
print("No unlabeled issues with paul-gauthier comments found.")
return return
print(f"\nFound {len(unlabeled_issues)} unlabeled issues with paul-gauthier comments:") print("\nAdding 'question' label to issues...")
for issue in unlabeled_issues: for issue in unlabeled_issues:
print(f" - #{issue['number']}: {issue['title']} {issue['html_url']}") url = f"{GITHUB_API_URL}/repos/{REPO_OWNER}/{REPO_NAME}/issues/{issue['number']}"
response = requests.patch(url, headers=headers, json={"labels": ["question"]})
response.raise_for_status()
print(f" - Added 'question' label to #{issue['number']}")
if not args.yes:
confirm = input("\nDo you want to add the 'question' label to these issues? (y/n): ")
if confirm.lower() != "y":
print("Skipping labeling.")
return
print("\nAdding 'question' label to issues...") def handle_duplicate_issues(all_issues, auto_yes):
for issue in unlabeled_issues:
url = f"{GITHUB_API_URL}/repos/{REPO_OWNER}/{REPO_NAME}/issues/{issue['number']}"
response = requests.patch(url, headers=headers, json={"labels": ["question"]})
response.raise_for_status()
print(f" - Added 'question' label to #{issue['number']}")
return
# ... to here
# ai also refactor this into its own function...
open_issues = [issue for issue in all_issues if issue["state"] == "open"] open_issues = [issue for issue in all_issues if issue["state"] == "open"]
grouped_open_issues = group_issues_by_subject(open_issues) grouped_open_issues = group_issues_by_subject(open_issues)
@ -202,21 +182,43 @@ def main():
f" {oldest_issue['html_url']} ({oldest_issue['state']})" f" {oldest_issue['html_url']} ({oldest_issue['state']})"
) )
if not args.yes: if not auto_yes:
# Confirmation prompt
confirm = input("Do you want to comment and close duplicate issues? (y/n): ") confirm = input("Do you want to comment and close duplicate issues? (y/n): ")
if confirm.lower() != "y": if confirm.lower() != "y":
print("Skipping this group of issues.") print("Skipping this group of issues.")
continue continue
# Comment and close duplicate issues
for issue in issues: for issue in issues:
if issue["number"] != oldest_issue["number"]: if issue["number"] != oldest_issue["number"]:
comment_and_close_duplicate(issue, oldest_issue) comment_and_close_duplicate(issue, oldest_issue)
if oldest_issue["state"] == "open": if oldest_issue["state"] == "open":
print(f"Oldest issue #{oldest_issue['number']} left open") print(f"Oldest issue #{oldest_issue['number']} left open")
# ai ... to here!
def main():
parser = argparse.ArgumentParser(description="Handle duplicate GitHub issues")
parser.add_argument(
"--yes", action="store_true", help="Automatically close duplicates without prompting"
)
parser.add_argument(
"--find-unlabeled",
action="store_true",
help="Find unlabeled issues with paul-gauthier comments",
)
args = parser.parse_args()
if not TOKEN:
print("Error: Missing GITHUB_TOKEN environment variable. Please check your .env file.")
return
all_issues = get_issues("all")
if args.find_unlabeled:
handle_unlabeled_issues(all_issues, args.yes)
return
handle_duplicate_issues(all_issues, args.yes)
if __name__ == "__main__": if __name__ == "__main__":