Close opened issues when adding migration comment #2
1 changed files with 75 additions and 3 deletions
|
|
@ -194,6 +194,60 @@ def add_comment_to_issue(
|
|||
return False
|
||||
|
||||
|
||||
def close_issue(
|
||||
api_base: str, project: str, issue_id: int, api_token: str
|
||||
) -> bool:
|
||||
"""
|
||||
Close a Pagure issue.
|
||||
|
||||
Args:
|
||||
api_base: Pagure API base URL
|
||||
project: Pagure project path
|
||||
issue_id: Issue number
|
||||
api_token: Pagure API token
|
||||
|
||||
Returns:
|
||||
True if successful, False otherwise
|
||||
"""
|
||||
url = f"{api_base}/{project}/issue/{issue_id}/status"
|
||||
headers = {"Authorization": f"token {api_token}"}
|
||||
data = {"status": "Closed"}
|
||||
|
||||
response = requests.post(url, headers=headers, data=data, timeout=30)
|
||||
|
||||
if response.status_code == 200:
|
||||
return True
|
||||
else:
|
||||
print(f" Error closing issue: {response.status_code} - {response.text}")
|
||||
return False
|
||||
|
||||
|
||||
def issue_is_opened(
|
||||
api_base: str, project: str, issue_id: int, api_token: str
|
||||
) -> bool:
|
||||
"""
|
||||
Check if issue is opened.
|
||||
|
||||
Args:
|
||||
api_base: Pagure API base URL
|
||||
project: Pagure project path
|
||||
issue_id: Issue number
|
||||
api_token: Pagure API token
|
||||
|
||||
Returns:
|
||||
True if issue is opened, False otherwise
|
||||
"""
|
||||
url = f"{api_base}/{project}/issue/{issue_id}"
|
||||
headers = {"Authorization": f"token {api_token}"}
|
||||
|
||||
response = requests.get(url, headers=headers, timeout=30)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
status = data.get("status", [])
|
||||
return status and status == "Open"
|
||||
|
||||
|
||||
def construct_forge_issue_url(forge_url: str, issue_id: int) -> str:
|
||||
"""
|
||||
Construct the Forge issue URL from the project URL and issue ID.
|
||||
|
|
@ -218,7 +272,7 @@ def process_issues(
|
|||
issue_numbers: list[int] | None,
|
||||
dry_run: bool,
|
||||
delay: float,
|
||||
) -> tuple[int, int, int]:
|
||||
) -> tuple[int, int, int, int, int]:
|
||||
"""
|
||||
Process issues and add migration comments.
|
||||
|
||||
|
|
@ -235,6 +289,8 @@ def process_issues(
|
|||
Tuple of (processed, skipped, failed) counts
|
||||
"""
|
||||
processed = 0
|
||||
closed = 0
|
||||
failed_to_close = 0
|
||||
skipped = 0
|
||||
failed = 0
|
||||
|
||||
|
|
@ -276,10 +332,24 @@ def process_issues(
|
|||
# Delay after API call to avoid rate limiting
|
||||
if delay > 0:
|
||||
time.sleep(delay)
|
||||
|
||||
if issue_is_opened(api_base, project, issue_id, api_token):
|
||||
print(f" [DRY RUN] Issue is opened")
|
||||
print(f" [DRY RUN] Closing the issue")
|
||||
else:
|
||||
print(f" [DRY RUN] Issue is closed")
|
||||
|
||||
else:
|
||||
print(f" Adding migration comment linking to: {forge_issue_url}")
|
||||
if add_comment_to_issue(api_base, project, issue_id, comment, api_token):
|
||||
print(" Success!")
|
||||
if issue_is_opened(api_base, project, issue_id, api_token):
|
||||
if close_issue(api_base, project, issue_id, api_token):
|
||||
print(" Issue closed!")
|
||||
closed += 1
|
||||
else:
|
||||
failed_to_close += 1
|
||||
|
||||
processed += 1
|
||||
else:
|
||||
failed += 1
|
||||
|
|
@ -288,7 +358,7 @@ def process_issues(
|
|||
if delay > 0:
|
||||
time.sleep(delay)
|
||||
|
||||
return processed, skipped, failed
|
||||
return processed, skipped, failed, closed, failed_to_close
|
||||
|
||||
|
||||
def main():
|
||||
|
|
@ -371,7 +441,7 @@ Examples:
|
|||
print()
|
||||
|
||||
try:
|
||||
processed, skipped, failed = process_issues(
|
||||
processed, skipped, failed, closed, failed_to_close = process_issues(
|
||||
api_base=api_base,
|
||||
project=project,
|
||||
forge_url=args.forge_url,
|
||||
|
|
@ -390,8 +460,10 @@ Examples:
|
|||
print(f"\n{'=' * 40}")
|
||||
print("Summary:")
|
||||
print(f" Processed: {processed}")
|
||||
print(f" Closed: {closed}")
|
||||
print(f" Skipped (already migrated): {skipped}")
|
||||
print(f" Failed: {failed}")
|
||||
print(f" Failed to close: {failed_to_close}")
|
||||
|
||||
if failed > 0:
|
||||
sys.exit(1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue