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
diff options
context:
space:
mode:
authorValery Sizov <valery@gitlab.com>2015-02-03 04:01:07 +0300
committerValery Sizov <valery@gitlab.com>2015-02-05 23:50:34 +0300
commit33349dd54928a0b074b4ae3ebfabf214799fc085 (patch)
tree593a14d0f6df812410349f353e252e3a58d8cacd /app/controllers/import
parent713bc152bde5396bb95a1555907bcd9a2847839d (diff)
GitLab.com integration: refactoring
Diffstat (limited to 'app/controllers/import')
-rw-r--r--app/controllers/import/github_controller.rb80
-rw-r--r--app/controllers/import/gitlab_controller.rb69
2 files changed, 149 insertions, 0 deletions
diff --git a/app/controllers/import/github_controller.rb b/app/controllers/import/github_controller.rb
new file mode 100644
index 00000000000..3f0461ead51
--- /dev/null
+++ b/app/controllers/import/github_controller.rb
@@ -0,0 +1,80 @@
+class Import::GithubController < 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_import_github_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 jobs
+ jobs = current_user.created_projects.where(import_type: "github").to_json(only: [:id, :import_status])
+ render json: jobs
+ 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
+
+ @project = Gitlab::GithubImport::ProjectCreator.new(repo, namespace, current_user).execute
+ end
+
+ private
+
+ def client
+ @client ||= Gitlab::GithubImport::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_github_for_permissions
+ end
+ end
+
+ def go_to_github_for_permissions
+ redirect_to client.auth_code.authorize_url({
+ redirect_uri: callback_import_github_url,
+ scope: "repo, user, user:email"
+ })
+ end
+
+ def github_unauthorized
+ go_to_github_for_permissions
+ end
+end
diff --git a/app/controllers/import/gitlab_controller.rb b/app/controllers/import/gitlab_controller.rb
new file mode 100644
index 00000000000..3712af6f024
--- /dev/null
+++ b/app/controllers/import/gitlab_controller.rb
@@ -0,0 +1,69 @@
+class Import::GitlabController < ApplicationController
+ before_filter :gitlab_auth, except: :callback
+
+ rescue_from OAuth2::Error, with: :gitlab_unauthorized
+
+ def callback
+ token = client.get_token(params[:code], callback_import_gitlab_url)
+ current_user.gitlab_access_token = token
+ current_user.save
+ redirect_to status_import_gitlab_url
+ end
+
+ def status
+ @repos = client.projects
+
+ @already_added_projects = current_user.created_projects.where(import_type: "gitlab")
+ already_added_projects_names = @already_added_projects.pluck(:import_source)
+
+ @repos.to_a.reject!{|repo| already_added_projects_names.include? repo["path_with_namespace"]}
+ end
+
+ def jobs
+ jobs = current_user.created_projects.where(import_type: "gitlab").to_json(:only => [:id, :import_status])
+ render json: jobs
+ end
+
+ 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)
+
+ 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
+
+ @project = Gitlab::GitlabImport::ProjectCreator.new(repo, namespace, current_user).execute
+ end
+
+ private
+
+ def client
+ @client ||= Gitlab::GitlabImport::Client.new(current_user.gitlab_access_token)
+ end
+
+ def gitlab_auth
+ if current_user.gitlab_access_token.blank?
+ go_to_gitlab_for_permissions
+ end
+ end
+
+ def go_to_gitlab_for_permissions
+ redirect_to client.authorize_url(callback_import_gitlab_url)
+ end
+
+ def gitlab_unauthorized
+ go_to_gitlab_for_permissions
+ end
+end