From dbcbbf262b731e30446167fbd10ef081d195c862 Mon Sep 17 00:00:00 2001
From: Ahmad Sherif <me@ahmadsherif.com>
Date: Tue, 27 Sep 2016 19:35:39 +0200
Subject: [PATCH] Speed up label-applying process for GitHub importing

* No need to re-fetch issues from GH to read their labels, the labels
  are already there from the index request.
* No need to look up labels on the database for every application, so we
  cache them.
---
 lib/gitlab/github_import/importer.rb           | 18 +++++++++---------
 spec/lib/gitlab/github_import/importer_spec.rb | 11 ++++++-----
 2 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/lib/gitlab/github_import/importer.rb b/lib/gitlab/github_import/importer.rb
index f1adec8212b..31fba36de7e 100644
--- a/lib/gitlab/github_import/importer.rb
+++ b/lib/gitlab/github_import/importer.rb
@@ -10,6 +10,7 @@ module Gitlab
         @repo     = project.import_source
         @repo_url = project.import_url
         @errors   = []
+        @labels   = {}
 
         if credentials
           @client = Client.new(credentials[:user])
@@ -49,7 +50,8 @@ module Gitlab
         client.labels(repo, per_page: 100) do |labels|
           labels.each do |raw|
             begin
-              LabelFormatter.new(project, raw).create!
+              label = LabelFormatter.new(project, raw).create!
+              @labels[label.title] = label.id
             rescue => e
               errors << { type: :label, url: Gitlab::UrlSanitizer.sanitize(raw.url), errors: e.message }
             end
@@ -77,7 +79,7 @@ module Gitlab
             if gh_issue.valid?
               begin
                 issue = gh_issue.create!
-                apply_labels(issue)
+                apply_labels(issue, raw)
                 import_comments(issue) if gh_issue.has_comments?
               rescue => e
                 errors << { type: :issue, url: Gitlab::UrlSanitizer.sanitize(raw.url), errors: e.message }
@@ -98,7 +100,7 @@ module Gitlab
               restore_target_branch(pull_request) unless pull_request.target_branch_exists?
 
               merge_request = pull_request.create!
-              apply_labels(merge_request)
+              apply_labels(merge_request, raw)
               import_comments(merge_request)
               import_comments_on_diff(merge_request)
             rescue => e
@@ -131,12 +133,10 @@ module Gitlab
         project.repository.after_remove_branch
       end
 
-      def apply_labels(issuable)
-        issue = client.issue(repo, issuable.iid)
-
-        if issue.labels.count > 0
-          label_ids = issue.labels
-            .map { |attrs| project.labels.find_by(title: attrs.name).try(:id) }
+      def apply_labels(issuable, raw_issuable)
+        if raw_issuable.labels.count > 0
+          label_ids = raw_issuable.labels
+            .map { |attrs| @labels[attrs.name] }
             .compact
 
           issuable.update_attribute(:label_ids, label_ids)
diff --git a/spec/lib/gitlab/github_import/importer_spec.rb b/spec/lib/gitlab/github_import/importer_spec.rb
index 553c849c9b4..844c081fc7f 100644
--- a/spec/lib/gitlab/github_import/importer_spec.rb
+++ b/spec/lib/gitlab/github_import/importer_spec.rb
@@ -57,7 +57,8 @@ describe Gitlab::GithubImport::Importer, lib: true do
           created_at: created_at,
           updated_at: updated_at,
           closed_at: nil,
-          url: 'https://api.github.com/repos/octocat/Hello-World/issues/1347'
+          url: 'https://api.github.com/repos/octocat/Hello-World/issues/1347',
+          labels: [double(name: 'Label #1')],
         )
       end
 
@@ -75,7 +76,8 @@ describe Gitlab::GithubImport::Importer, lib: true do
           created_at: created_at,
           updated_at: updated_at,
           closed_at: nil,
-          url: 'https://api.github.com/repos/octocat/Hello-World/issues/1348'
+          url: 'https://api.github.com/repos/octocat/Hello-World/issues/1348',
+          labels: [double(name: 'Label #2')],
         )
       end
 
@@ -94,7 +96,8 @@ describe Gitlab::GithubImport::Importer, lib: true do
           updated_at: updated_at,
           closed_at: nil,
           merged_at: nil,
-          url: 'https://api.github.com/repos/octocat/Hello-World/pulls/1347'
+          url: 'https://api.github.com/repos/octocat/Hello-World/pulls/1347',
+          labels: [double(name: 'Label #3')],
         )
       end
 
@@ -148,9 +151,7 @@ describe Gitlab::GithubImport::Importer, lib: true do
           errors: [
             { type: :label, url: "https://api.github.com/repos/octocat/Hello-World/labels/bug", errors: "Validation failed: Title can't be blank, Title is invalid" },
             { type: :milestone, url: "https://api.github.com/repos/octocat/Hello-World/milestones/1", errors: "Validation failed: Title has already been taken" },
-            { type: :issue, url: "https://api.github.com/repos/octocat/Hello-World/issues/1347", errors: "Invalid Repository. Use user/repo format." },
             { type: :issue, url: "https://api.github.com/repos/octocat/Hello-World/issues/1348", errors: "Validation failed: Title can't be blank, Title is too short (minimum is 0 characters)" },
-            { type: :pull_request, url: "https://api.github.com/repos/octocat/Hello-World/pulls/1347", errors: "Invalid Repository. Use user/repo format." },
             { type: :pull_request, url: "https://api.github.com/repos/octocat/Hello-World/pulls/1347", errors: "Validation failed: Validate branches Cannot Create: This merge request already exists: [\"New feature\"]" },
             { type: :wiki, errors: "Gitlab::Shell::Error" },
             { type: :release, url: 'https://api.github.com/repos/octocat/Hello-World/releases/2', errors: "Validation failed: Description can't be blank" }
-- 
GitLab