Commit 1ac20698 authored by Valery Sizov's avatar Valery Sizov

gitlab.com importer: refactorig

parent 2d5765bd
class @ImporterStatus
constructor: (@jobs_url, @import_url) ->
this.initStatusPage()
this.setAutoUpdate()
initStatusPage: ->
$(".btn-add-to-import").click (event) =>
new_namespace = null
tr = $(event.currentTarget).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 @import_url, {repo_id: id, new_namespace: new_namespace}, dataType: 'script'
setAutoUpdate: ->
setInterval (=>
$.get @jobs_url, (data) =>
$.each data, (i, job) =>
job_item = $("#project_" + job.id)
status_field = job_item.find(".job-status")
if job.import_status == 'finished'
job_item.removeClass("active").addClass("success")
status_field.html('<span class="cgreen"><i class="fa fa-check"></i> done</span>')
else if job.import_status == 'started'
status_field.html("<i class='fa fa-spinner fa-spin'></i> started")
else
status_field.html(job.import_status)
), 4000
\ No newline at end of file
class Import::BaseController < ApplicationController
private
def get_or_create_namespace
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
return false
end
else
namespace = Group.create(name: @target_namespace, path: @target_namespace, owner: current_user)
namespace.add_owner(current_user)
namespace
end
end
end
class Import::GithubController < ApplicationController class Import::GithubController < Import::BaseController
before_filter :github_auth, except: :callback before_filter :github_auth, except: :callback
rescue_from Octokit::Unauthorized, with: :github_unauthorized rescue_from Octokit::Unauthorized, with: :github_unauthorized
...@@ -30,22 +30,10 @@ class Import::GithubController < ApplicationController ...@@ -30,22 +30,10 @@ class Import::GithubController < ApplicationController
def create def create
@repo_id = params[:repo_id].to_i @repo_id = params[:repo_id].to_i
repo = octo_client.repo(@repo_id) repo = octo_client.repo(@repo_id)
target_namespace = params[:new_namespace].presence || repo.owner.login @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 @project_name = repo.name
render and return
end namespace = get_or_create_namespace || (render and return)
else
namespace = Group.create(name: target_namespace, path: target_namespace, owner: current_user)
namespace.add_owner(current_user)
end
@project = Gitlab::GithubImport::ProjectCreator.new(repo, namespace, current_user).execute @project = Gitlab::GithubImport::ProjectCreator.new(repo, namespace, current_user).execute
end end
......
class Import::GitlabController < ApplicationController class Import::GitlabController < Import::BaseController
before_filter :gitlab_auth, except: :callback before_filter :gitlab_auth, except: :callback
rescue_from OAuth2::Error, with: :gitlab_unauthorized rescue_from OAuth2::Error, with: :gitlab_unauthorized
...@@ -27,22 +27,10 @@ class Import::GitlabController < ApplicationController ...@@ -27,22 +27,10 @@ class Import::GitlabController < ApplicationController
def create def create
@repo_id = params[:repo_id].to_i @repo_id = params[:repo_id].to_i
repo = client.project(@repo_id) repo = client.project(@repo_id)
target_namespace = params[:new_namespace].presence || repo["namespace"]["path"] @target_namespace = params[:new_namespace].presence || repo["namespace"]["path"]
existing_namespace = Namespace.find_by("path = ? OR name = ?", target_namespace, target_namespace) @project_name = repo["name"]
if existing_namespace namespace = get_or_create_namespace || (render and return)
if existing_namespace.owner == current_user
namespace = existing_namespace
else
@already_been_taken = true
@target_namespace = target_namespace
@project_name = repo["path"]
render and return
end
else
namespace = Group.create(name: target_namespace, path: target_namespace, owner: current_user)
namespace.add_owner(current_user)
end
@project = Gitlab::GitlabImport::ProjectCreator.new(repo, namespace, current_user).execute @project = Gitlab::GitlabImport::ProjectCreator.new(repo, namespace, current_user).execute
end end
......
...@@ -34,30 +34,6 @@ ...@@ -34,30 +34,6 @@
%td.import-actions.job-status %td.import-actions.job-status
= button_tag "Add", class: "btn btn-add-to-import" = button_tag "Add", class: "btn btn-add-to-import"
:coffeescript :coffeescript
$(".btn-add-to-import").click () -> $ ->
new_namespace = null new ImporterStatus("#{jobs_import_github_path}", "#{import_github_path}")
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 "#{import_github_url}", {repo_id: id, new_namespace: new_namespace}, dataType: 'script'
setInterval (->
$.get "#{jobs_import_github_path}", (data)->
$.each data, (i, job) ->
job_item = $("#project_" + job.id)
status_field = job_item.find(".job-status")
if job.import_status == 'finished'
job_item.removeClass("active").addClass("success")
status_field.html('<span class="cgreen"><i class="fa fa-check"></i> done</span>')
else if job.import_status == 'started'
status_field.html("<i class='fa fa-spinner fa-spin'></i> started")
else
status_field.html(job.import_status)
), 4000
- 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
job = $("tr#repo_#{@repo_id}")
job.attr("id", "project_#{@project.id}")
$("table.import-jobs tbody").prepend(job)
job.addClass("active").find(".import-actions").html("<i class='fa fa-spinner fa-spin'></i> started")
...@@ -34,30 +34,6 @@ ...@@ -34,30 +34,6 @@
%td.import-actions.job-status %td.import-actions.job-status
= button_tag "Add", class: "btn btn-add-to-import" = button_tag "Add", class: "btn btn-add-to-import"
:coffeescript :coffeescript
$(".btn-add-to-import").click () -> $ ->
new_namespace = null new ImporterStatus("#{jobs_import_gitlab_path}", "#{import_gitlab_url}")
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 "#{import_gitlab_url}", {repo_id: id, new_namespace: new_namespace}, dataType: 'script'
setInterval (->
$.get "#{jobs_import_gitlab_path}", (data)->
$.each data, (i, job) ->
job_item = $("#project_" + job.id)
status_field = job_item.find(".job-status")
if job.import_status == 'finished'
job_item.removeClass("active").addClass("success")
status_field.html('<span class="cgreen"><i class="fa fa-check"></i> done</span>')
else if job.import_status == 'started'
status_field.html("<i class='fa fa-spinner fa-spin'></i> started")
else
status_field.html(job.import_status)
), 4000
...@@ -7,16 +7,3 @@ ...@@ -7,16 +7,3 @@
.modal-body .modal-body
You need to setup integration with GitHub first. You need to setup integration with GitHub first.
= link_to 'How to setup integration with GitHub', 'https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/integration/github.md' = link_to 'How to setup integration with GitHub', 'https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/integration/github.md'
:javascript
$(function(){
var import_modal = $('#github_import_modal').modal({modal: true, show:false});
$('.how_to_import_link').bind("click", function(e){
e.preventDefault();
import_modal.show();
});
$('.modal-header .close').bind("click", function(){
import_modal.hide();
})
})
...@@ -7,16 +7,3 @@ ...@@ -7,16 +7,3 @@
.modal-body .modal-body
You need to setup integration with GitLab first. You need to setup integration with GitLab first.
= link_to 'How to setup integration with GitLab', 'https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/integration/gitlab.md' = link_to 'How to setup integration with GitLab', 'https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/integration/gitlab.md'
:javascript
$(function(){
var import_modal = $('#gitlab_import_modal').modal({modal: true, show:false});
$('.how_to_import_link').bind("click", function(e){
e.preventDefault();
import_modal.show();
});
$('.modal-header .close').bind("click", function(){
import_modal.hide();
})
})
...@@ -92,3 +92,11 @@ ...@@ -92,3 +92,11 @@
%i.fa.fa-spinner.fa-spin %i.fa.fa-spinner.fa-spin
Creating project &amp; repository. Creating project &amp; repository.
%p Please wait a moment, this page will automatically refresh when ready. %p Please wait a moment, this page will automatically refresh when ready.
:coffeescript
$ ->
$('.how_to_import_link').bind 'click', (e) ->
e.preventDefault()
import_modal = $(this).parent().find(".modal").show()
$('.modal-header .close').bind 'click', ->
$(".modal").hide()
\ No newline at end of file
...@@ -10,12 +10,12 @@ class RepositoryImportWorker ...@@ -10,12 +10,12 @@ class RepositoryImportWorker
project.path_with_namespace, project.path_with_namespace,
project.import_url) project.import_url)
if project.import_type == 'github' result_of_data_import = if project.import_type == 'github'
result_of_data_import = Gitlab::GithubImport::Importer.new(project).execute Gitlab::GithubImport::Importer.new(project).execute
elsif project.import_type == 'gitlab' elsif project.import_type == 'gitlab'
result_of_data_import = Gitlab::GitlabImport::Importer.new(project).execute Gitlab::GitlabImport::Importer.new(project).execute
else else
result_of_data_import = true true
end end
if result && result_of_data_import if result && result_of_data_import
......
...@@ -43,7 +43,8 @@ module Gitlab ...@@ -43,7 +43,8 @@ module Gitlab
end end
def gl_user_id(project, github_id) def gl_user_id(project, github_id)
user = User.joins(:identities).find_by("identities.extern_uid = ?", github_id.to_s) user = User.joins(:identities).
find_by("identities.extern_uid = ? AND identities.provider = 'github'", github_id.to_s)
(user && user.id) || project.creator_id (user && user.id) || project.creator_id
end end
end end
......
...@@ -42,7 +42,7 @@ module Gitlab ...@@ -42,7 +42,7 @@ module Gitlab
private private
def gl_user_id(project, gitlab_id) def gl_user_id(project, gitlab_id)
user = User.joins(:identities).find_by("identities.extern_uid = ?", gitlab_id.to_s) user = User.joins(:identities).find_by("identities.extern_uid = ? AND identities.provider = 'gitlab'", gitlab_id.to_s)
(user && user.id) || project.creator_id (user && user.id) || project.creator_id
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment