summaryrefslogtreecommitdiff
path: root/app/controllers/import
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2016-12-15 17:31:14 +0100
committerRémy Coutable <remy@rymai.me>2016-12-19 17:35:51 +0100
commit103114e3d73819f76bed9d8ad1bbdb8964875579 (patch)
tree3457c48ed93ee0266acf6f459e5a9c312e3d42b4 /app/controllers/import
parent5d4531db2555d3051fc47e9268728a670ece95f9 (diff)
downloadgitlab-ce-103114e3d73819f76bed9d8ad1bbdb8964875579.tar.gz
Rename Gogs to Gitea, DRY the controller and improve views
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'app/controllers/import')
-rw-r--r--app/controllers/import/gitea_controller.rb41
-rw-r--r--app/controllers/import/github_controller.rb91
-rw-r--r--app/controllers/import/gogs_controller.rb76
3 files changed, 98 insertions, 110 deletions
diff --git a/app/controllers/import/gitea_controller.rb b/app/controllers/import/gitea_controller.rb
new file mode 100644
index 00000000000..c82a20be04c
--- /dev/null
+++ b/app/controllers/import/gitea_controller.rb
@@ -0,0 +1,41 @@
+class Import::GiteaController < Import::GithubController
+ def new
+ if session[:access_token].present? && session[:host_url].present?
+ redirect_to status_import_url
+ end
+ end
+
+ def personal_access_token
+ session[:host_url] = params[:gitea_host_url]
+ super
+ end
+
+ def status
+ @gitea_root_url = session[:host_url]
+ super
+ end
+
+ private
+
+ # Overriden methods
+ def provider
+ :gitea
+ end
+
+ # Gitea is not yet an OAuth provider
+ # See https://github.com/go-gitea/gitea/issues/27
+ def logged_in_with_provider?
+ false
+ end
+
+ def provider_auth
+ if session[:access_token].blank? || session[:host_url].blank?
+ redirect_to new_import_gitea_url,
+ alert: 'You need to specify both an Access Token and a Host URL.'
+ end
+ end
+
+ def client_options
+ { host: session[:host_url], api_version: 'v1' }
+ end
+end
diff --git a/app/controllers/import/github_controller.rb b/app/controllers/import/github_controller.rb
index ee7d498c59c..343ca51e510 100644
--- a/app/controllers/import/github_controller.rb
+++ b/app/controllers/import/github_controller.rb
@@ -1,39 +1,37 @@
class Import::GithubController < Import::BaseController
- before_action :verify_github_import_enabled
- before_action :github_auth, only: [:status, :jobs, :create]
+ before_action :verify_import_enabled
+ before_action :provider_auth, only: [:status, :jobs, :create]
- rescue_from Octokit::Unauthorized, with: :github_unauthorized
-
- helper_method :logged_in_with_github?
+ rescue_from Octokit::Unauthorized, with: :provider_unauthorized
def new
- if logged_in_with_github?
- go_to_github_for_permissions
- elsif session[:github_access_token]
- redirect_to status_import_github_url
+ if logged_in_with_provider?
+ go_to_provider_for_permissions
+ elsif session[:access_token]
+ redirect_to status_import_url
end
end
def callback
- session[:github_access_token] = client.get_token(params[:code])
- redirect_to status_import_github_url
+ session[:access_token] = client.get_token(params[:code])
+ redirect_to status_import_url
end
def personal_access_token
- session[:github_access_token] = params[:personal_access_token]
- redirect_to status_import_github_url
+ session[:access_token] = params[:personal_access_token]
+ redirect_to status_import_url
end
def status
@repos = client.repos
- @already_added_projects = current_user.created_projects.where(import_type: "github")
+ @already_added_projects = current_user.created_projects.where(import_type: provider)
already_added_projects_names = @already_added_projects.pluck(:import_source)
- @repos.reject!{ |repo| already_added_projects_names.include? repo.full_name }
+ @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])
+ jobs = current_user.created_projects.where(import_type: provider).to_json(only: [:id, :import_status])
render json: jobs
end
@@ -44,8 +42,8 @@ class Import::GithubController < Import::BaseController
namespace_path = params[:target_namespace].presence || current_user.namespace_path
@target_namespace = find_or_create_namespace(namespace_path, current_user.namespace_path)
- if current_user.can?(:create_projects, @target_namespace)
- @project = Gitlab::GithubImport::ProjectCreator.new(repo, @project_name, @target_namespace, current_user, access_params).execute
+ if can?(current_user, :create_projects, @target_namespace)
+ @project = Gitlab::GithubImport::ProjectCreator.new(repo, @project_name, @target_namespace, current_user, access_params, type: provider).execute
else
render 'unauthorized'
end
@@ -54,34 +52,59 @@ class Import::GithubController < Import::BaseController
private
def client
- @client ||= Gitlab::GithubImport::Client.new(session[:github_access_token])
+ @client ||= Gitlab::GithubImport::Client.new(session[:access_token], client_options)
end
- def verify_github_import_enabled
- render_404 unless github_import_enabled?
+ def verify_import_enabled
+ render_404 unless import_enabled?
end
- def github_auth
- if session[:github_access_token].blank?
- go_to_github_for_permissions
- end
+ def go_to_provider_for_permissions
+ redirect_to client.authorize_url(callback_import_url)
+ end
+
+ def import_enabled?
+ __send__("#{provider}_import_enabled?")
end
- def go_to_github_for_permissions
- redirect_to client.authorize_url(callback_import_github_url)
+ def new_import_url
+ public_send("new_import_#{provider}_url")
end
- def github_unauthorized
- session[:github_access_token] = nil
- redirect_to new_import_github_url,
- alert: 'Access denied to your GitHub account.'
+ def status_import_url
+ public_send("status_import_#{provider}_url")
end
- def logged_in_with_github?
- current_user.identities.exists?(provider: 'github')
+ def callback_import_url
+ public_send("callback_import_#{provider}_url")
+ end
+
+ def provider_unauthorized
+ session[:access_token] = nil
+ redirect_to new_import_url,
+ alert: "Access denied to your #{Gitlab::ImportSources.options.key(provider.to_s)} account."
end
def access_params
- { github_access_token: session[:github_access_token] }
+ { github_access_token: session[:access_token] }
+ end
+
+ # The following methods are overriden in subclasses
+ def provider
+ :github
+ end
+
+ def logged_in_with_provider?
+ current_user.identities.exists?(provider: provider)
+ end
+
+ def provider_auth
+ if session[:access_token].blank?
+ go_to_provider_for_permissions
+ end
+ end
+
+ def client_options
+ {}
end
end
diff --git a/app/controllers/import/gogs_controller.rb b/app/controllers/import/gogs_controller.rb
deleted file mode 100644
index 4caf7d2605a..00000000000
--- a/app/controllers/import/gogs_controller.rb
+++ /dev/null
@@ -1,76 +0,0 @@
-class Import::GogsController < Import::BaseController
- before_action :verify_gogs_import_enabled
- before_action :gogs_auth, only: [:status, :jobs, :create]
-
- rescue_from Octokit::Unauthorized, with: :gogs_unauthorized
-
- helper_method :logged_in_with_gogs?
-
- def new
- if session[:gogs_access_token]
- redirect_to status_import_gogs_url
- end
- end
-
- def personal_access_token
- session[:gogs_access_token] = params[:personal_access_token]
- session[:gogs_host_url] = params[:gogs_host_url]
- redirect_to status_import_gogs_url
- end
-
- def status
- @repos = client.repos
- @already_added_projects = current_user.created_projects.where(import_type: "gogs")
- already_added_projects_names = @already_added_projects.pluck(:import_source)
-
- @gogs_root_url = session[:gogs_host_url]
-
- @repos.reject!{ |repo| already_added_projects_names.include? repo.full_name }
- end
-
- def jobs
- jobs = current_user.created_projects.where(import_type: "gogs").to_json(only: [:id, :import_status])
- render json: jobs
- end
-
- def create
- @repo_id = params[:repo_id].to_i
- repo = client.repo(@repo_id)
- @project_name = params[:new_name].presence || repo.name
- namespace_path = params[:target_namespace].presence || current_user.namespace_path
- @target_namespace = find_or_create_namespace(namespace_path, current_user.namespace_path)
-
- if current_user.can?(:create_projects, @target_namespace)
- @project = Gitlab::GithubImport::ProjectCreator.new(repo, @project_name, @target_namespace, current_user, access_params, type: 'gogs').execute
- else
- render 'unauthorized'
- end
- end
-
- private
-
- def client
- @client ||= Gitlab::GithubImport::Client.new(session[:gogs_access_token], host: session[:gogs_host_url], api_version: 'v1')
- end
-
- def verify_gogs_import_enabled
- render_404 unless gogs_import_enabled?
- end
-
- def gogs_auth
- if session[:gogs_access_token].blank? || session[:gogs_host_url].blank?
- redirect_to new_import_gogs_url,
- alert: 'You need to specify both an Access Token and a Host URL.'
- end
- end
-
- def gogs_unauthorized
- session[:gogs_access_token] = nil
- redirect_to new_import_gogs_url,
- alert: 'Access denied to your Gogs account.'
- end
-
- def access_params
- { github_access_token: session[:gogs_access_token] }
- end
-end