Last active
September 27, 2023 17:16
-
-
Save majorsilence/cef5127e444041944821a60a6c1f82f0 to your computer and use it in GitHub Desktop.
Copy github issues from one issue tracker to another
This file contains 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
from github import Github | |
from github import Auth | |
import time | |
# NOTE: this copies the issues. It does not transfer them. | |
# PYTHON SETUP: | |
# sudo apt update && apt install python3 | |
# wget https://bootstrap.pypa.io/get-ip.py | |
# sudo python3 get-ip.py | |
# pip install PyGithub | |
# pip install pyopenssl --upgrade | |
# Set your GitHub personal access token and repository information | |
source_repo_owner = '' | |
source_repo_name = '' | |
destination_repo_owner = '' | |
destination_repo_name = '' | |
access_token = '' | |
auth = Auth.Token(access_token) | |
# Initialize a PyGithub instance with your access token | |
g = Github(auth=auth) | |
# Get source and destination repositories | |
print (f"{source_repo_owner}/{source_repo_name}") | |
source_repo = g.get_repo(f"{source_repo_owner}/{source_repo_name}") | |
print(f"{destination_repo_owner}/{destination_repo_name}") | |
destination_repo = g.get_repo(f"{destination_repo_owner}/{destination_repo_name}") | |
# Get all open issues from the source repository | |
source_issues = source_repo.get_issues(state="open") | |
existing_issue_titles = [issue.title for issue in destination_repo.get_issues(state='open')] | |
count=0 | |
def transfer_issue_with_retry(source_issue, destination_repo, max_retries=5, retry_delay=20): | |
for attempt in range(max_retries): | |
try: | |
transfer_issue(source_issue, destination_repo) | |
return | |
except Exception as error: | |
print(f"Error transferring issue: {error}") | |
if attempt < max_retries - 1: | |
print(f"Retrying in {retry_delay * attempt} seconds...") | |
time.sleep(retry_delay * attempt) | |
else: | |
print(f"Max retries reached. Skipping issue.") | |
def transfer_issue(issue, destination_repo): | |
global count | |
if not issue.pull_request: | |
count += 1 | |
issue_title = issue.title | |
issue_body = issue.body | |
issue_labels = [label.name for label in issue.get_labels()] | |
issue_state = "open" if issue.state == "open" else "closed" | |
if "copied" not in issue_labels: | |
# Create a new issue in the destination repository | |
if issue_title not in existing_issue_titles: | |
new_issue = destination_repo.create_issue( | |
title=issue_title, | |
body=issue_body, | |
labels=issue_labels | |
) | |
if issue_state == "closed": | |
new_issue.edit(state="closed") | |
extra_labels = issue_labels + ['copied'] | |
new_issue.edit(labels=extra_labels) | |
print(f"{count}, {issue_state}, Issue '{issue_title}' transferred successfully.") | |
time.sleep(1) | |
else: | |
print(f"{count}, {issue_state}, Issue '{issue_title}' already exists in the destination repository.") | |
extra_labels = issue_labels + ['copied'] | |
issue.edit(labels=extra_labels) | |
time.sleep(1) | |
else: | |
print(f"Skipping already copied: {issue_title}") | |
else: | |
print(f"Skipping pull request: {issue.title}") | |
# Transfer issues to the destination repository | |
for issue in source_issues: | |
transfer_issue_with_retry(issue, destination_repo) | |
print("All issues copied.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment