I was recently asked by Scott D’Angelo on how he could quickly copy or move issues from one GitHub repo to another, in this case the landing repo was on GitHub Enterprise. The solution? …
PyGitHub of course!
The quick and dirty script
Nothing fancy needed here, just a few lines of Python. We start by reading in API keys with
os, we’ll need those to connect to public GitHub and GitHub Enterprise.
Then we loop over the result of
create_issue() along the way with the second session.
import os from github import Github GH_TOKEN = os.environ.get('GH_TOKEN') g = Github(GH_TOKEN) pub_repo = g.get_repo('IBM/some-repo') GHE_TOKEN = os.environ.get('GHE_TOKEN') ghe = Github(base_url="https://github.enterprise.com/api/v3", login_or_token=GHE_TOKEN) ghe_repo = ghe.get_repo('Developer-Advocacy/some-repo') for issue in pub_repo.get_issues(): ret_issue = ghe_repo.create_issue(title=issue.title, body=issue.body) if issue.comments: comments = issue.get_comments() for comment in comments: ret_issue.create_comment(comment.body)
That’s it, pretty easy huh? Credit goes to Scott for going the extra mile and writing in code to copy the issue comments, too. Note that issues and issue comments will appear to be created by whomever created the API key.