gitlab.com importer: refactorig
This commit is contained in:
parent
2d5765bd2c
commit
1ac20698a5
14 changed files with 85 additions and 140 deletions
31
app/assets/javascripts/importer_status.js.coffee
Normal file
31
app/assets/javascripts/importer_status.js.coffee
Normal file
|
@ -0,0 +1,31 @@
|
|||
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
|
21
app/controllers/import/base_controller.rb
Normal file
21
app/controllers/import/base_controller.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
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
|
|
@ -1,4 +1,4 @@
|
|||
class Import::GithubController < ApplicationController
|
||||
class Import::GithubController < Import::BaseController
|
||||
before_filter :github_auth, except: :callback
|
||||
|
||||
rescue_from Octokit::Unauthorized, with: :github_unauthorized
|
||||
|
@ -30,22 +30,10 @@ class Import::GithubController < ApplicationController
|
|||
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
|
||||
@target_namespace = params[:new_namespace].presence || repo.owner.login
|
||||
@project_name = repo.name
|
||||
|
||||
namespace = get_or_create_namespace || (render and return)
|
||||
|
||||
@project = Gitlab::GithubImport::ProjectCreator.new(repo, namespace, current_user).execute
|
||||
end
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
class Import::GitlabController < ApplicationController
|
||||
class Import::GitlabController < Import::BaseController
|
||||
before_filter :gitlab_auth, except: :callback
|
||||
|
||||
rescue_from OAuth2::Error, with: :gitlab_unauthorized
|
||||
|
@ -27,22 +27,10 @@ class Import::GitlabController < ApplicationController
|
|||
def create
|
||||
@repo_id = params[:repo_id].to_i
|
||||
repo = client.project(@repo_id)
|
||||
target_namespace = params[:new_namespace].presence || repo["namespace"]["path"]
|
||||
existing_namespace = Namespace.find_by("path = ? OR name = ?", target_namespace, target_namespace)
|
||||
@target_namespace = params[:new_namespace].presence || repo["namespace"]["path"]
|
||||
@project_name = repo["name"]
|
||||
|
||||
if existing_namespace
|
||||
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
|
||||
namespace = get_or_create_namespace || (render and return)
|
||||
|
||||
@project = Gitlab::GitlabImport::ProjectCreator.new(repo, namespace, current_user).execute
|
||||
end
|
||||
|
|
|
@ -34,30 +34,6 @@
|
|||
%td.import-actions.job-status
|
||||
= 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 "#{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
|
||||
$ ->
|
||||
new ImporterStatus("#{jobs_import_github_path}", "#{import_github_path}")
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
- 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 @@
|
|||
%td.import-actions.job-status
|
||||
= 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 "#{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
|
||||
$ ->
|
||||
new ImporterStatus("#{jobs_import_gitlab_path}", "#{import_gitlab_url}")
|
||||
|
|
|
@ -6,17 +6,4 @@
|
|||
%h3 GitHub OAuth import
|
||||
.modal-body
|
||||
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'
|
||||
|
||||
|
||||
: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();
|
||||
})
|
||||
})
|
||||
= link_to 'How to setup integration with GitHub', 'https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/integration/github.md'
|
|
@ -6,17 +6,4 @@
|
|||
%h3 GitLab OAuth import
|
||||
.modal-body
|
||||
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'
|
||||
|
||||
|
||||
: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();
|
||||
})
|
||||
})
|
||||
= link_to 'How to setup integration with GitLab', 'https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/integration/gitlab.md'
|
|
@ -92,3 +92,11 @@
|
|||
%i.fa.fa-spinner.fa-spin
|
||||
Creating project & repository.
|
||||
%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()
|
|
@ -10,13 +10,13 @@ class RepositoryImportWorker
|
|||
project.path_with_namespace,
|
||||
project.import_url)
|
||||
|
||||
if project.import_type == 'github'
|
||||
result_of_data_import = Gitlab::GithubImport::Importer.new(project).execute
|
||||
elsif project.import_type == 'gitlab'
|
||||
result_of_data_import = Gitlab::GitlabImport::Importer.new(project).execute
|
||||
else
|
||||
result_of_data_import = true
|
||||
end
|
||||
result_of_data_import = if project.import_type == 'github'
|
||||
Gitlab::GithubImport::Importer.new(project).execute
|
||||
elsif project.import_type == 'gitlab'
|
||||
Gitlab::GitlabImport::Importer.new(project).execute
|
||||
else
|
||||
true
|
||||
end
|
||||
|
||||
if result && result_of_data_import
|
||||
project.import_finish
|
||||
|
|
|
@ -43,7 +43,8 @@ module Gitlab
|
|||
end
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
|
|
|
@ -42,7 +42,7 @@ module Gitlab
|
|||
private
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue