Skip to content

Instantly share code, notes, and snippets.

@crwilcox
Created August 19, 2015 00:39
Show Gist options
  • Save crwilcox/96c4e419f93481017664 to your computer and use it in GitHub Desktop.
Save crwilcox/96c4e419f93481017664 to your computer and use it in GitHub Desktop.
A script to migrate issues with a certain label from one repo to another and close the original issue. There is no error handling. This could very well fail but it worked for what I needed it for.
# Quick and Dirty script to create issues in a new repo and
# close them in the old one. There is no error handling.
# Set these variables as appropriate
GITHUB_ACCESS_TOKEN = 'INSERT ACCESS TOKEN HERE'
SOURCE_REPO = 'Azure/some-source-repo'
DEST_REPO = 'Azure/some-dest-repo'
LABEL_TO_MOVE = 'Interesting Label'
LABEL_TO_LABEL_IN_DEST = 'Migrated from {}'.format(SOURCE_REPO)
# Below here there shouldn't be much to change
import github
issue_description_format = '''
### Description:
{}
##### Original Issue: {}
### Comments ({}):
'''
comment_format = '''#### @{} commented on {}:
> {}
'''
g = github.Github(GITHUB_ACCESS_TOKEN)
source_repo = g.get_repo(SOURCE_REPO)
destination_repo = g.get_repo(DEST_REPO)
labels_to_move = [source_repo.get_label(LABEL_TO_MOVE)]
source_issues = source_repo.get_issues(state = 'open', labels = labels_to_move)
destination_repo.create_label(LABEL_TO_LABEL_IN_DEST, 'EE82EE')
labels_to_add_to_issues = [destination_repo.get_label(LABEL_TO_LABEL_IN_DEST)]
for issue in source_issues:
issue_title = issue.title
issue_description = issue_description_format.format(issue.body, issue.html_url, issue.comments)
for comment in issue.get_comments():
issue_description += comment_format.format(comment.user.login, comment.created_at, comment.body)
#print(issue_description)
dest_issue = destination_repo.create_issue(issue_title, body = issue_description, labels = labels_to_add_to_issues)
issue.create_comment("Issue Migrated to {}".format(dest_issue.html_url))
issue.edit(state='closed')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment