summaryrefslogtreecommitdiff
path: root/lib/gitlab/github_import
diff options
context:
space:
mode:
authorValery Sizov <valery@gitlab.com>2015-02-02 14:26:29 -0800
committerValery Sizov <valery@gitlab.com>2015-02-05 12:50:34 -0800
commit18231b0bb353fffa77b492e4b04fa61c9b3a25bb (patch)
tree6f00378c2d998382edae9752866be1a44b50ca03 /lib/gitlab/github_import
parent7ddba92a394b5e5fd67eda50b6ee25b38e53e7b9 (diff)
downloadgitlab-ce-18231b0bb353fffa77b492e4b04fa61c9b3a25bb.tar.gz
GitLab.com integration: refactoring
Diffstat (limited to 'lib/gitlab/github_import')
-rw-r--r--lib/gitlab/github_import/client.rb29
-rw-r--r--lib/gitlab/github_import/importer.rb48
-rw-r--r--lib/gitlab/github_import/project_creator.rb39
3 files changed, 116 insertions, 0 deletions
diff --git a/lib/gitlab/github_import/client.rb b/lib/gitlab/github_import/client.rb
new file mode 100644
index 00000000000..2e454e7c10f
--- /dev/null
+++ b/lib/gitlab/github_import/client.rb
@@ -0,0 +1,29 @@
+module Gitlab
+ module GithubImport
+ class Client
+ attr_reader :client
+
+ def initialize
+ @client = ::OAuth2::Client.new(
+ config.app_id,
+ config.app_secret,
+ github_options
+ )
+ end
+
+ private
+
+ def config
+ Gitlab.config.omniauth.providers.select{|provider| provider.name == "github"}.first
+ end
+
+ def github_options
+ {
+ site: 'https://api.github.com',
+ authorize_url: 'https://github.com/login/oauth/authorize',
+ token_url: 'https://github.com/login/oauth/access_token'
+ }
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/github_import/importer.rb b/lib/gitlab/github_import/importer.rb
new file mode 100644
index 00000000000..180ad6c3018
--- /dev/null
+++ b/lib/gitlab/github_import/importer.rb
@@ -0,0 +1,48 @@
+module Gitlab
+ module GithubImport
+ class Importer
+ attr_reader :project
+
+ def initialize(project)
+ @project = project
+ end
+
+ def execute
+ client = octo_client(project.creator.github_access_token)
+
+ #Issues && Comments
+ client.list_issues(project.import_source, state: :all).each do |issue|
+ if issue.pull_request.nil?
+ body = "*Created by: #{issue.user.login}*\n\n#{issue.body}"
+
+ if issue.comments > 0
+ body += "\n\n\n**Imported comments:**\n"
+ client.issue_comments(project.import_source, issue.number).each do |c|
+ body += "\n\n*By #{c.user.login} on #{c.created_at}*\n\n#{c.body}"
+ end
+ end
+
+ project.issues.create!(
+ description: body,
+ title: issue.title,
+ state: issue.state == 'closed' ? 'closed' : 'opened',
+ author_id: gl_user_id(project, issue.user.id)
+ )
+ end
+ end
+ end
+
+ private
+
+ def octo_client(access_token)
+ ::Octokit.auto_paginate = true
+ ::Octokit::Client.new(access_token: access_token)
+ end
+
+ def gl_user_id(project, github_id)
+ user = User.joins(:identities).find_by("identities.extern_uid = ?", github_id.to_s)
+ (user && user.id) || project.creator_id
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/github_import/project_creator.rb b/lib/gitlab/github_import/project_creator.rb
new file mode 100644
index 00000000000..9439ca6cbf4
--- /dev/null
+++ b/lib/gitlab/github_import/project_creator.rb
@@ -0,0 +1,39 @@
+module Gitlab
+ module GithubImport
+ class ProjectCreator
+ attr_reader :repo, :namespace, :current_user
+
+ def initialize(repo, namespace, current_user)
+ @repo = repo
+ @namespace = namespace
+ @current_user = current_user
+ end
+
+ def execute
+ @project = Project.new(
+ name: repo.name,
+ path: repo.name,
+ description: repo.description,
+ namespace: namespace,
+ creator: current_user,
+ visibility_level: repo.private ? Gitlab::VisibilityLevel::PRIVATE : Gitlab::VisibilityLevel::PUBLIC,
+ import_type: "github",
+ import_source: repo.full_name,
+ import_url: repo.clone_url.sub("https://", "https://#{current_user.github_access_token}@")
+ )
+
+ if @project.save!
+ @project.reload
+
+ if @project.import_failed?
+ @project.import_retry
+ else
+ @project.import_start
+ end
+ end
+
+ @project
+ end
+ end
+ end
+end