Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorValery Sizov <vsv2711@gmail.com>2014-12-31 16:07:48 +0300
committerValery Sizov <valery@gitlab.com>2015-01-10 20:51:43 +0300
commita9f7fd2c1a7052247333b89f6a22a883b480370d (patch)
treea7321e528f9ac10f0e8f8f591dca6a4c83f09b9a /app
parentd02a22ba21f91d2aa4f9cf716dc3aefcf7e7495e (diff)
Github Importer
Diffstat (limited to 'app')
-rw-r--r--app/controllers/github_imports_controller.rb75
-rw-r--r--app/controllers/omniauth_callbacks_controller.rb2
-rw-r--r--app/helpers/projects_helper.rb16
-rw-r--r--app/views/github_imports/create.js.haml18
-rw-r--r--app/views/github_imports/status.html.haml41
-rw-r--r--app/views/projects/new.html.haml10
-rw-r--r--app/workers/repository_import_worker.rb8
7 files changed, 167 insertions, 3 deletions
diff --git a/app/controllers/github_imports_controller.rb b/app/controllers/github_imports_controller.rb
new file mode 100644
index 00000000000..97a2637b1eb
--- /dev/null
+++ b/app/controllers/github_imports_controller.rb
@@ -0,0 +1,75 @@
+class GithubImportsController < ApplicationController
+ before_filter :github_auth, except: :callback
+
+ rescue_from Octokit::Unauthorized, with: :github_unauthorized
+
+ def callback
+ token = client.auth_code.get_token(params[:code]).token
+ current_user.github_access_token = token
+ current_user.save
+ redirect_to status_github_import_url
+ end
+
+ def status
+ @repos = octo_client.repos
+ octo_client.orgs.each do |org|
+ @repos += octo_client.repos(org.login)
+ end
+
+ @already_added_projects = current_user.created_projects.where(import_type: "github")
+ already_added_projects_names = @already_added_projects.pluck(:import_source)
+
+ @repos.reject!{|repo| already_added_projects_names.include? repo.full_name}
+ end
+
+ def create
+ @repo_id = params[:repo_id].to_i
+ repo = octo_client.repo(@repo_id)
+ target_namespace = params[:new_namespace].presence || repo.owner.login
+ existing_namespace = Namespace.find_by("path = ? OR name = ?", target_namespace, target_namespace)
+
+ if existing_namespace
+ if existing_namespace.owner == current_user
+ namespace = existing_namespace
+ else
+ @already_been_taken = true
+ @target_namespace = target_namespace
+ @project_name = repo.name
+ render and return
+ end
+ else
+ namespace = Group.create(name: target_namespace, path: target_namespace, owner: current_user)
+ namespace.add_owner(current_user)
+ end
+
+ Gitlab::Github::ProjectCreator.new(repo, namespace, current_user).execute
+ end
+
+ private
+
+ def client
+ @client ||= Gitlab::Github::Client.new.client
+ end
+
+ def octo_client
+ Octokit.auto_paginate = true
+ @octo_client ||= Octokit::Client.new(:access_token => current_user.github_access_token)
+ end
+
+ def github_auth
+ if current_user.github_access_token.blank?
+ go_to_gihub_for_permissions
+ end
+ end
+
+ def go_to_gihub_for_permissions
+ redirect_to client.auth_code.authorize_url({
+ redirect_uri: callback_github_import_url,
+ scope: "repo, user, user:email"
+ })
+ end
+
+ def github_unauthorized
+ go_to_gihub_for_permissions
+ end
+end
diff --git a/app/controllers/omniauth_callbacks_controller.rb b/app/controllers/omniauth_callbacks_controller.rb
index 3e984e5007a..442a1cf7518 100644
--- a/app/controllers/omniauth_callbacks_controller.rb
+++ b/app/controllers/omniauth_callbacks_controller.rb
@@ -65,7 +65,7 @@ class OmniauthCallbacksController < Devise::OmniauthCallbacksController
redirect_to omniauth_error_path(oauth['provider'], error: error_message) and return
end
end
- rescue ForbiddenAction => e
+ rescue Gitlab::OAuth::ForbiddenAction => e
flash[:notice] = e.message
redirect_to new_user_session_path
end
diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb
index e489d431e84..39d6be06383 100644
--- a/app/helpers/projects_helper.rb
+++ b/app/helpers/projects_helper.rb
@@ -237,4 +237,20 @@ module ProjectsHelper
result.password = '*****' if result.password.present?
result
end
+
+ def project_status_css_class(status)
+ case status
+ when "started"
+ "active"
+ when "failed"
+ "danger"
+ when "finished"
+ "success"
+ end
+ end
+
+ def github_import_enabled?
+ Gitlab.config.omniauth.enabled && enabled_oauth_providers.include?(:github)
+ end
end
+
diff --git a/app/views/github_imports/create.js.haml b/app/views/github_imports/create.js.haml
new file mode 100644
index 00000000000..e354c2da4dd
--- /dev/null
+++ b/app/views/github_imports/create.js.haml
@@ -0,0 +1,18 @@
+- if @already_been_taken
+ :plain
+ target_field = $("tr#repo_#{@repo_id} .import-target")
+ origin_target = target_field.text()
+ project_name = "#{@project_name}"
+ origin_namespace = "#{@target_namespace}"
+ target_field.empty()
+ target_field.append("<p class='alert alert-danger'>This namespace already been taken! Please choose another one</p>")
+ target_field.append("<input type='text' name='target_namespace' />")
+ target_field.append("/" + project_name)
+ target_field.data("project_name", project_name)
+ target_field.find('input').prop("value", origin_namespace)
+- else
+ :plain
+ $("table.import-jobs tbody").prepend($("tr#repo_#{@repo_id}"))
+ $("tr#repo_#{@repo_id}").addClass("active").find(".import-actions").text("started")
+
+ \ No newline at end of file
diff --git a/app/views/github_imports/status.html.haml b/app/views/github_imports/status.html.haml
new file mode 100644
index 00000000000..6a196cae39d
--- /dev/null
+++ b/app/views/github_imports/status.html.haml
@@ -0,0 +1,41 @@
+%h3.page-title
+ Import repositories from github
+
+%hr
+%h4
+ Select projects you want to import.
+
+%table.table.table-bordered.import-jobs
+ %thead
+ %tr
+ %th From GitHub
+ %th To GitLab
+ %th Status
+ %tbody
+ - @already_added_projects.each do |repo|
+ %tr{id: "repo_#{repo.id}", class: "#{project_status_css_class(repo.import_status)}"}
+ %td= repo.import_source
+ %td= repo.name_with_namespace
+ %td= repo.human_import_status_name
+
+ - @repos.each do |repo|
+ %tr{id: "repo_#{repo.id}"}
+ %td= repo.full_name
+ %td.import-target
+ = repo.full_name
+ %td.import-actions
+ = button_tag "Add", class: "btn btn-add-to-import"
+
+
+:coffeescript
+ $(".btn-add-to-import").click () ->
+ new_namespace = null
+ tr = $(this).closest("tr")
+ id = tr.attr("id").replace("repo_", "")
+ if tr.find(".import-target input").length > 0
+ new_namespace = tr.find(".import-target input").prop("value")
+ tr.find(".import-target").empty().append(new_namespace + "/" + tr.find(".import-target").data("project_name"))
+ $.post "#{github_import_url}", {repo_id: id, new_namespace: new_namespace}, dataType: 'script'
+
+
+
diff --git a/app/views/projects/new.html.haml b/app/views/projects/new.html.haml
index f320a2b505e..88c1f725703 100644
--- a/app/views/projects/new.html.haml
+++ b/app/views/projects/new.html.haml
@@ -39,7 +39,15 @@
%br
The import will time out after 4 minutes. For big repositories, use a clone/push combination.
For SVN repositories, check #{link_to "this migrating from SVN doc.", "http://doc.gitlab.com/ce/workflow/migrating_from_svn.html"}
- %hr
+
+ - if github_import_enabled?
+ .project-import.form-group
+ .col-sm-2
+ .col-sm-10
+ %i.fa.fa-bars
+ = link_to "Import projects from github", status_github_import_path
+
+ %hr.prepend-botton-10
.form-group
= f.label :description, class: 'control-label' do
diff --git a/app/workers/repository_import_worker.rb b/app/workers/repository_import_worker.rb
index 01586150cd2..0bcc42bc62c 100644
--- a/app/workers/repository_import_worker.rb
+++ b/app/workers/repository_import_worker.rb
@@ -10,7 +10,13 @@ class RepositoryImportWorker
project.path_with_namespace,
project.import_url)
- if result
+ if project.import_type == 'github'
+ result_of_data_import = Gitlab::Github::Importer.new(project).execute
+ else
+ result_of_data_import = true
+ end
+
+ if result && result_of_data_import
project.import_finish
project.save
project.satellite.create unless project.satellite.exists?