diff options
author | Valery Sizov <vsv2711@gmail.com> | 2014-12-31 16:07:48 +0300 |
---|---|---|
committer | Valery Sizov <valery@gitlab.com> | 2015-01-10 20:51:43 +0300 |
commit | a9f7fd2c1a7052247333b89f6a22a883b480370d (patch) | |
tree | a7321e528f9ac10f0e8f8f591dca6a4c83f09b9a /lib/gitlab | |
parent | d02a22ba21f91d2aa4f9cf716dc3aefcf7e7495e (diff) |
Github Importer
Diffstat (limited to 'lib/gitlab')
-rw-r--r-- | lib/gitlab/github/client.rb | 29 | ||||
-rw-r--r-- | lib/gitlab/github/importer.rb | 48 | ||||
-rw-r--r-- | lib/gitlab/github/project_creator.rb | 37 | ||||
-rw-r--r-- | lib/gitlab/regex.rb | 2 |
4 files changed, 115 insertions, 1 deletions
diff --git a/lib/gitlab/github/client.rb b/lib/gitlab/github/client.rb new file mode 100644 index 00000000000..c6935a0b0ba --- /dev/null +++ b/lib/gitlab/github/client.rb @@ -0,0 +1,29 @@ +module Gitlab + module Github + 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/importer.rb b/lib/gitlab/github/importer.rb new file mode 100644 index 00000000000..c72a1c25e9e --- /dev/null +++ b/lib/gitlab/github/importer.rb @@ -0,0 +1,48 @@ +module Gitlab + module Github + 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/project_creator.rb b/lib/gitlab/github/project_creator.rb new file mode 100644 index 00000000000..682ef389e44 --- /dev/null +++ b/lib/gitlab/github/project_creator.rb @@ -0,0 +1,37 @@ +module Gitlab + module Github + 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 + end + end + end +end diff --git a/lib/gitlab/regex.rb b/lib/gitlab/regex.rb index c4d0d85b7f5..cf6e260f257 100644 --- a/lib/gitlab/regex.rb +++ b/lib/gitlab/regex.rb @@ -11,7 +11,7 @@ module Gitlab end def project_name_regex - /\A[a-zA-Z0-9_][a-zA-Z0-9_\-\. ]*\z/ + /\A[a-zA-Z0-9_.][a-zA-Z0-9_\-\. ]*\z/ end def project_regex_message |