diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/github_imports_controller.rb | 75 | ||||
-rw-r--r-- | app/helpers/projects_helper.rb | 16 | ||||
-rw-r--r-- | app/views/github_imports/create.js.haml | 18 | ||||
-rw-r--r-- | app/views/github_imports/status.html.haml | 41 | ||||
-rw-r--r-- | app/views/projects/new.html.haml | 13 | ||||
-rw-r--r-- | app/workers/repository_import_worker.rb | 8 |
6 files changed, 168 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/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 786a386c0ea..fef21368254 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -242,4 +242,20 @@ module ProjectsHelper url_params = is_newest ? {} : { version_id: version } project_wiki_path(proj, page, url_params) 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..ccd02acd761 100644 --- a/app/views/projects/new.html.haml +++ b/app/views/projects/new.html.haml @@ -27,7 +27,7 @@ .col-sm-10 = link_to "#", class: 'js-toggle-button' do %i.fa.fa-upload - %span Import existing repository? + %span Import existing repository by URL .js-toggle-content.hide .form-group.import-url-data = f.label :import_url, class: 'control-label' do @@ -39,7 +39,16 @@ %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 + = link_to status_github_import_path do + %i.fa.fa-github + Import projects from GitHub + + %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? |