Created
March 25, 2025 09:56
-
-
Save pen-pal/db4ddf62eeafa12624b6f1704c388de7 to your computer and use it in GitHub Desktop.
copy issues and comments from org1/repo1 to org2/repo2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| # GitHub personal access token | |
| TOKEN = "your-token" | |
| # Source and destination repositories | |
| SOURCE_REPO = 'org1/repo1' | |
| DEST_REPO = 'org2/repo2' | |
| # Headers for authorization | |
| headers = {'Authorization': f'token {TOKEN}'} | |
| # Function to fetch comments for a specific issue from the source repo | |
| def get_comments(issue_number): | |
| comments_url = f'https://api.github.com/repos/{SOURCE_REPO}/issues/{issue_number}/comments' | |
| response = requests.get(comments_url, headers=headers) | |
| # Check if the response was successful (status code 200) | |
| if response.status_code != 200: | |
| print(f"Failed to fetch comments for issue {issue_number}: {response.status_code}") | |
| print("Error details:", response.json()) | |
| return [] | |
| # Parse the JSON response | |
| comments = response.json() | |
| return comments | |
| # Function to create comments for the specific issue in the destination repo | |
| def create_comments(issue_number, comments): | |
| for comment in comments: | |
| comment_data = { | |
| 'body': comment['body'] | |
| } | |
| comment_url = f'https://api.github.com/repos/{DEST_REPO}/issues/{issue_number}/comments' | |
| response = requests.post(comment_url, headers=headers, json=comment_data) | |
| if response.status_code == 201: | |
| print(f"Comment added to issue {issue_number}!") | |
| else: | |
| print(f"Failed to add comment to issue {issue_number}") | |
| # Fetch issues from source repo | |
| issues_url = f'https://api.github.com/repos/{SOURCE_REPO}/issues' | |
| issues = requests.get(issues_url, headers=headers).json() | |
| # Debugging: print the issues to check what is being returned | |
| print("Issues response:", issues) | |
| # Check if the issues is a list | |
| if isinstance(issues, list): | |
| # Migrate each issue to the destination repo | |
| for issue in issues: | |
| # Debugging: print each issue to check its structure | |
| print("Issue:", issue) | |
| if isinstance(issue, dict): # Ensure we're working with a dictionary | |
| issue_data = { | |
| 'title': issue['title'], | |
| 'body': issue['body'], | |
| 'labels': [label['name'] for label in issue['labels']], | |
| 'assignees': [assignee['login'] for assignee in issue['assignees']], | |
| } | |
| # Create the issue in the destination repo | |
| create_url = f'https://api.github.com/repos/{DEST_REPO}/issues' | |
| response = requests.post(create_url, headers=headers, json=issue_data) | |
| if response.status_code == 201: | |
| print(f"Issue {issue['title']} migrated successfully!") | |
| # Get the new issue number from the response | |
| new_issue_number = response.json()['number'] | |
| # Fetch the comments for the specific issue in the source repo | |
| comments = get_comments(issue['number']) | |
| # Post the comments to the new issue in the destination repo if there are any | |
| if comments: | |
| create_comments(new_issue_number, comments) | |
| else: | |
| print(f"No comments found for issue {issue['title']} in {SOURCE_REPO}.") | |
| else: | |
| print(f"Failed to migrate issue {issue['title']}") | |
| else: | |
| print(f"Unexpected issue format: {issue}") | |
| else: | |
| print("Error: The response is not a list of issues.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment