Browse files

Reduce API requests resulting from issue labels.

- Gather labels in advance and assign them all when creating an issue
- Cache Label objects to avoid calls to get_label
  • Loading branch information...
1 parent af4b192 commit 982e60fe73b37f73e22040d8baf8d81f6fd06071 @dnschnur dnschnur committed Sep 27, 2012
Showing with 36 additions and 30 deletions.
  1. +36 −30 migrateissues.py
View
66 migrateissues.py
@@ -23,14 +23,14 @@
GOOGLE_MAX_RESULTS = 25
-# Mapping from Google Code issue labels to Github tags
+# Mapping from Google Code issue labels to Github labels
LABEL_MAPPING = {
'Type-Defect' : "bug",
'Type-Enhancement' : "enhancement"
}
-# Mapping from Google Code issue states to Github tags
+# Mapping from Google Code issue states to Github labels
STATE_MAPPING = {
'invalid': "invalid",
@@ -55,9 +55,11 @@ def github_label(name, color = "FFFFFF"):
""" Returns the Github label with the given name, creating it if necessary. """
- try: return github_repo.get_label(name)
- except GithubException:
- return github_repo.create_label(name, color)
+ try: return label_cache[name]
+ except KeyError:
+ try: return label_cache.setdefault(name, github_repo.get_label(name))
+ except GithubException:
+ return label_cache.setdefault(name, github_repo.create_label(name, color))
def parse_gcode_id(id_text):
@@ -87,43 +89,45 @@ def add_issue_to_github(issue):
content = issue.content.text
date = parse_gcode_date(issue.published.text)
- header = "_Original author: %s (%s)_" % (author, date)
- body = github_escape("%s\n\n%s\n\n\n_Original issue: %s_" % (header, content, link))
+ # Build a list of labels to apply to the new issue, including an 'imported' tag that
+ # we can use to identify this issue as one that's passed through migration.
- output("Adding issue %s" % gid)
+ labels = ["imported"]
- github_issue = None
+ # Convert Google Code labels to Github labels where possible
- if not options.dry_run:
+ if issue.label:
+ for label in issue.label:
+ if label.text.startswith("Priority-") and options.omit_priority:
+ continue
+ labels.append(LABEL_MAPPING.get(label.text, label.text))
- github_issue = github_repo.create_issue(title, body = body.encode("utf-8"))
- github_issue.edit(state = issue.state.text)
+ # Add additional labels based on the issue's state
- # Add an 'imported' tag so it's easy to identify issues that we created
+ if status in STATE_MAPPING:
+ labels.append(STATE_MAPPING[status])
- github_issue.add_to_labels(github_label("imported"))
+ # Add the new Github issue with its labels and a header identifying it as migrated
- # Add additional tags based on the issue's state
+ github_issue = None
- if status in STATE_MAPPING:
- github_issue.add_to_labels(github_label(status))
+ header = "_Original author: %s (%s)_" % (author, date)
+ body = github_escape("%s\n\n%s\n\n\n_Original issue: %s_" % (header, content, link))
- # Assigns issues that originally had an owner to the current user
+ output("Adding issue %s" % gid)
- if issue.owner and options.assign_owner and not options.dry_run:
- github_issue.edit(assignee = github.get_user(github_user.login))
+ if not options.dry_run:
+ github_labels = [ github_label(label) for label in labels ]
+ github_issue = github_repo.create_issue(title, body = body.encode("utf-8"), labels = github_labels)
+ if issue.state.text != "open":
+ github_issue.edit(state = issue.state.text)
- # Convert Google Code labels to Github tags where possible
+ # Assigns issues that originally had an owner to the current user
- if issue.label:
- output(", adding labels")
- for label in issue.label:
- if label.text.startswith("Priority-") and options.omit_priority:
- continue
- label_text = LABEL_MAPPING.get(label.text, label.text)
- if not options.dry_run:
- github_issue.add_to_labels(github_label(label_text))
- output(".")
+ if issue.owner and options.assign_owner:
+ assignee = github.get_user(github_user.login)
+ if not options.dry_run:
+ github_issue.edit(assignee = assignee)
return github_issue
@@ -250,6 +254,8 @@ def get_existing_github_issues():
parser.print_help()
sys.exit()
+ label_cache = {} # Cache Github tags, to avoid unnecessary API requests
+
google_project_name, github_user_name, github_project = args
github_password = getpass.getpass("Github password: ")

0 comments on commit 982e60f

Please sign in to comment.