2015-02-05 13:31:36 -05:00
|
|
|
class Import::GithubController < Import::BaseController
|
2015-02-17 16:52:32 -05:00
|
|
|
before_filter :verify_github_import_enabled
|
2014-12-31 08:07:48 -05:00
|
|
|
before_filter :github_auth, except: :callback
|
|
|
|
|
|
|
|
rescue_from Octokit::Unauthorized, with: :github_unauthorized
|
2015-01-13 14:44:17 -05:00
|
|
|
|
2014-12-31 08:07:48 -05:00
|
|
|
def callback
|
2015-02-05 19:57:27 -05:00
|
|
|
token = client.get_token(params[:code])
|
2014-12-31 08:07:48 -05:00
|
|
|
current_user.github_access_token = token
|
|
|
|
current_user.save
|
2015-02-02 20:01:07 -05:00
|
|
|
redirect_to status_import_github_url
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
2015-02-05 19:57:27 -05:00
|
|
|
@repos = client.repos
|
|
|
|
client.orgs.each do |org|
|
2015-03-12 08:47:15 -04:00
|
|
|
@repos += client.org_repos(org.login)
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@already_added_projects = current_user.created_projects.where(import_type: "github")
|
|
|
|
already_added_projects_names = @already_added_projects.pluck(:import_source)
|
|
|
|
|
2015-02-02 21:25:33 -05:00
|
|
|
@repos.reject!{ |repo| already_added_projects_names.include? repo.full_name }
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
2015-01-20 14:52:55 -05:00
|
|
|
def jobs
|
2015-02-02 22:30:09 -05:00
|
|
|
jobs = current_user.created_projects.where(import_type: "github").to_json(only: [:id, :import_status])
|
2015-01-20 14:52:55 -05:00
|
|
|
render json: jobs
|
|
|
|
end
|
|
|
|
|
2014-12-31 08:07:48 -05:00
|
|
|
def create
|
|
|
|
@repo_id = params[:repo_id].to_i
|
2015-02-05 19:57:27 -05:00
|
|
|
repo = client.repo(@repo_id)
|
2015-02-05 13:31:36 -05:00
|
|
|
@target_namespace = params[:new_namespace].presence || repo.owner.login
|
|
|
|
@project_name = repo.name
|
|
|
|
|
|
|
|
namespace = get_or_create_namespace || (render and return)
|
2014-12-31 08:07:48 -05:00
|
|
|
|
2015-02-02 17:26:29 -05:00
|
|
|
@project = Gitlab::GithubImport::ProjectCreator.new(repo, namespace, current_user).execute
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def client
|
2015-02-05 19:57:27 -05:00
|
|
|
@client ||= Gitlab::GithubImport::Client.new(current_user.github_access_token)
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
2015-02-17 16:52:32 -05:00
|
|
|
def verify_github_import_enabled
|
|
|
|
not_found! unless github_import_enabled?
|
|
|
|
end
|
|
|
|
|
2014-12-31 08:07:48 -05:00
|
|
|
def github_auth
|
|
|
|
if current_user.github_access_token.blank?
|
2015-01-18 10:29:37 -05:00
|
|
|
go_to_github_for_permissions
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-18 10:29:37 -05:00
|
|
|
def go_to_github_for_permissions
|
2015-02-05 19:57:27 -05:00
|
|
|
redirect_to client.authorize_url(callback_import_github_url)
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def github_unauthorized
|
2015-01-18 10:29:37 -05:00
|
|
|
go_to_github_for_permissions
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
end
|