summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorValery Sizov <valery@gitlab.com>2015-01-27 15:37:19 -0800
committerValery Sizov <valery@gitlab.com>2015-02-05 12:50:34 -0800
commit5194214e3a2f97accf0c8119b4cb39fd4fcef5db (patch)
tree7320257c4470abadd90a0f17fd92f408143b6bb1 /lib
parent0a9cab4ee65f2b42c56989698c401cab60d68b53 (diff)
downloadgitlab-ce-5194214e3a2f97accf0c8119b4cb39fd4fcef5db.tar.gz
GitLab integration. Importer
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/gitlab_import/client.rb82
-rw-r--r--lib/gitlab/gitlab_import/importer.rb48
-rw-r--r--lib/gitlab/gitlab_import/project_creator.rb39
3 files changed, 169 insertions, 0 deletions
diff --git a/lib/gitlab/gitlab_import/client.rb b/lib/gitlab/gitlab_import/client.rb
new file mode 100644
index 00000000000..64e369e9c12
--- /dev/null
+++ b/lib/gitlab/gitlab_import/client.rb
@@ -0,0 +1,82 @@
+module Gitlab
+ module GitlabImport
+ class Client
+ attr_reader :client, :api
+
+ PER_PAGE = 100
+
+ def initialize(access_token)
+ @client = ::OAuth2::Client.new(
+ config.app_id,
+ config.app_secret,
+ github_options
+ )
+
+ if access_token
+ @api = OAuth2::AccessToken.from_hash(@client, :access_token => access_token)
+ end
+ end
+
+ def authorize_url(redirect_uri)
+ client.auth_code.authorize_url({
+ redirect_uri: redirect_uri,
+ scope: "api"
+ })
+ end
+
+ def get_token(code, redirect_uri)
+ client.auth_code.get_token(code, redirect_uri: redirect_uri).token
+ end
+
+ def issues(project_identifier)
+ lazy_page_iterator(PER_PAGE) do |page|
+ api.get("/api/v3/projects/#{project_identifier}/issues?per_page=#{PER_PAGE}&page=#{page}").parsed
+ end
+ end
+
+ def issue_comments(project_identifier, issue_id)
+ lazy_page_iterator(PER_PAGE) do |page|
+ api.get("/api/v3/projects/#{project_identifier}/issues/#{issue_id}/notes?per_page=#{PER_PAGE}&page=#{page}").parsed
+ end
+ end
+
+ def project(id)
+ api.get("/api/v3/projects/#{id}").parsed
+ end
+
+ def projects
+ lazy_page_iterator(PER_PAGE) do |page|
+ api.get("/api/v3/projects?per_page=#{PER_PAGE}&page=#{page}").parsed
+ end
+ end
+
+ private
+
+ def lazy_page_iterator(per_page)
+ Enumerator.new do |y|
+ page = 1
+ loop do
+ items = yield(page)
+ items.each do |item|
+ y << item
+ end
+ break if items.empty? || items.size < per_page
+ page += 1
+ end
+ end
+ end
+
+ def config
+ Gitlab.config.omniauth.providers.select{|provider| provider.name == "gitlab"}.first
+ end
+
+ def github_options
+ {
+ site: 'https://gitlab.com/',
+ authorize_url: 'oauth/authorize',
+ token_url: 'oauth/token'
+ }
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/gitlab_import/importer.rb b/lib/gitlab/gitlab_import/importer.rb
new file mode 100644
index 00000000000..3e9087a556c
--- /dev/null
+++ b/lib/gitlab/gitlab_import/importer.rb
@@ -0,0 +1,48 @@
+module Gitlab
+ module GitlabImport
+ class Importer
+ attr_reader :project, :client
+
+ def initialize(project)
+ @project = project
+ @client = Client.new(project.creator.gitlab_access_token)
+ end
+
+ def execute
+ project_identifier = URI.encode(project.import_source, '/')
+
+ #Issues && Comments
+ issues = client.issues(project_identifier)
+
+ issues.each do |issue|
+ body = "*Created by: #{issue["author"]["name"]}*\n\n#{issue["description"]}"
+
+
+ comments = client.issue_comments(project_identifier, issue["id"])
+ if comments.any?
+ body += "\n\n\n**Imported comments:**\n"
+ end
+ comments.each do |comment|
+ body += "\n\n*By #{comment["author"]["name"]} on #{comment["created_at"]}*\n\n#{comment["body"]}"
+ end
+
+ project.issues.create!(
+ description: body,
+ title: issue["title"],
+ state: issue["state"],
+ author_id: gl_user_id(project, issue["author"]["id"])
+ )
+ end
+
+ true
+ end
+
+ private
+
+ def gl_user_id(project, gitlab_id)
+ user = User.joins(:identities).find_by("identities.extern_uid = ?", gitlab_id.to_s)
+ (user && user.id) || project.creator_id
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/gitlab_import/project_creator.rb b/lib/gitlab/gitlab_import/project_creator.rb
new file mode 100644
index 00000000000..affd828e816
--- /dev/null
+++ b/lib/gitlab/gitlab_import/project_creator.rb
@@ -0,0 +1,39 @@
+module Gitlab
+ module GitlabImport
+ 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["path"],
+ description: repo["description"],
+ namespace: namespace,
+ creator: current_user,
+ visibility_level: repo["visibility_level"],
+ import_type: "gitlab",
+ import_source: repo["path_with_namespace"],
+ import_url: repo["http_url_to_repo"]#.sub("://", "://oauth2@#{current_user.gitlab_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