Commit 33349dd5 authored by Valery Sizov's avatar Valery Sizov
Browse files

GitLab.com integration: refactoring

parent 713bc152
class Importers::GithubsController < ApplicationController
class Import::GithubController < ApplicationController
before_filter :github_auth, except: :callback
rescue_from Octokit::Unauthorized, with: :github_unauthorized
......@@ -7,7 +7,7 @@ def callback
token = client.auth_code.get_token(params[:code]).token
current_user.github_access_token = token
current_user.save
redirect_to status_importers_github_url
redirect_to status_import_github_url
end
def status
......@@ -69,7 +69,7 @@ def github_auth
def go_to_github_for_permissions
redirect_to client.auth_code.authorize_url({
redirect_uri: callback_importers_github_url,
redirect_uri: callback_import_github_url,
scope: "repo, user, user:email"
})
end
......
class Importers::GitlabsController < ApplicationController
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_importers_gitlab_url)
token = client.get_token(params[:code], callback_import_gitlab_url)
current_user.gitlab_access_token = token
current_user.save
redirect_to status_importers_gitlab_url
redirect_to status_import_gitlab_url
end
def status
......@@ -60,7 +60,7 @@ def gitlab_auth
end
def go_to_gitlab_for_permissions
redirect_to client.authorize_url(callback_importers_gitlab_url)
redirect_to client.authorize_url(callback_import_gitlab_url)
end
def gitlab_unauthorized
......
......@@ -43,11 +43,11 @@
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 "#{importers_github_url}", {repo_id: id, new_namespace: new_namespace}, dataType: 'script'
$.post "#{import_github_url}", {repo_id: id, new_namespace: new_namespace}, dataType: 'script'
setInterval (->
$.get "#{jobs_importers_github_path}", (data)->
$.get "#{jobs_import_github_path}", (data)->
$.each data, (i, job) ->
job_item = $("#project_" + job.id)
status_field = job_item.find(".job-status")
......
......@@ -43,11 +43,11 @@
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 "#{importers_gitlab_url}", {repo_id: id, new_namespace: new_namespace}, dataType: 'script'
$.post "#{import_gitlab_url}", {repo_id: id, new_namespace: new_namespace}, dataType: 'script'
setInterval (->
$.get "#{jobs_importers_gitlab_path}", (data)->
$.get "#{jobs_import_gitlab_path}", (data)->
$.each data, (i, job) ->
job_item = $("#project_" + job.id)
status_field = job_item.find(".job-status")
......
......@@ -44,7 +44,7 @@
.col-sm-2
.col-sm-10
- if github_import_enabled?
= link_to status_importers_github_path do
= link_to status_import_github_path do
%i.fa.fa-github
Import projects from GitHub
- else
......@@ -57,7 +57,7 @@
.col-sm-2
.col-sm-10
- if gitlab_import_enabled?
= link_to status_importers_gitlab_path do
= link_to status_import_gitlab_path do
%i.fa.fa-heart
Import projects from GitLab.com
- else
......
......@@ -53,16 +53,16 @@
#
# Importers
# Import
#
namespace :importers do
resource :github, only: [:create, :new] do
namespace :import do
resource :github, only: [:create, :new], controller: :github do
get :status
get :callback
get :jobs
end
resource :gitlab, only: [:create, :new] do
resource :gitlab, only: [:create, :new], controller: :gitlab do
get :status
get :callback
get :jobs
......
......@@ -15,7 +15,7 @@ To enable the GitLab OmniAuth provider you must register your application with G
- Redirect URI:
```
http://gitlab.example.com/importers/gitlab/callback
http://gitlab.example.com/import/gitlab/callback
http://gitlab.example.com/users/auth/gitlab/callback
```
......
......@@ -14,7 +14,7 @@ def initialize
private
def config
Gitlab.config.omniauth.providers.select{|provider| provider.name == "github"}.first
Gitlab.config.omniauth.providers.find{|provider| provider.name == "github"}
end
def github_options
......
......@@ -5,6 +5,7 @@ class Importer
def initialize(project)
@project = project
@formatter = Gitlab::ImportFormatter.new
end
def execute
......@@ -13,12 +14,14 @@ def execute
#Issues && Comments
client.list_issues(project.import_source, state: :all).each do |issue|
if issue.pull_request.nil?
body = "*Created by: #{issue.user.login}*\n\n#{issue.body}"
body = @formatter.author_line(issue.user.login, issue.body)
if issue.comments > 0
body += "\n\n\n**Imported comments:**\n"
body += @formatter.comments_header
client.issue_comments(project.import_source, issue.number).each do |c|
body += "\n\n*By #{c.user.login} on #{c.created_at}*\n\n#{c.body}"
body += @formatter.comment_to_md(c.user.login, c.created_at, c.body)
end
end
......
......@@ -13,7 +13,7 @@ def initialize(access_token)
)
if access_token
@api = OAuth2::AccessToken.from_hash(@client, :access_token => access_token)
@api = OAuth2::AccessToken.from_hash(@client, access_token: access_token)
end
end
......@@ -67,7 +67,7 @@ def lazy_page_iterator(per_page)
end
def config
Gitlab.config.omniauth.providers.select{|provider| provider.name == "gitlab"}.first
Gitlab.config.omniauth.providers.find{|provider| provider.name == "gitlab"}
end
def github_options
......
......@@ -6,6 +6,7 @@ class Importer
def initialize(project)
@project = project
@client = Client.new(project.creator.gitlab_access_token)
@formatter = Gitlab::ImportFormatter.new
end
def execute
......@@ -15,15 +16,16 @@ def execute
issues = client.issues(project_identifier)
issues.each do |issue|
body = "*Created by: #{issue["author"]["name"]}*\n\n#{issue["description"]}"
body = @formatter.author_line(issue["author"]["name"], issue["description"])
comments = client.issue_comments(project_identifier, issue["id"])
if comments.any?
body += "\n\n\n**Imported comments:**\n"
body += @formatter.comments_header
end
comments.each do |comment|
body += "\n\n*By #{comment["author"]["name"]} on #{comment["created_at"]}*\n\n#{comment["body"]}"
body += @formatter.comment_to_md(comment["author"]["name"], comment["created_at"], comment["body"])
end
project.issues.create!(
......
module Gitlab
class ImportFormatter
def comment_to_md(author, date, body)
"\n\n*By #{author} on #{date}*\n\n#{body}"
end
def comments_header
"\n\n\n**Imported comments:**\n"
end
def author_line(author, body)
"*Created by: #{author}*\n\n#{body}"
end
end
end
\ No newline at end of file
require 'spec_helper'
describe Importers::GithubsController do
describe Import::GithubController do
let(:user) { create(:user, github_access_token: 'asd123') }
before do
......@@ -16,7 +16,7 @@
get :callback
user.reload.github_access_token.should == token
controller.should redirect_to(status_importers_github_url)
controller.should redirect_to(status_import_github_url)
end
end
......
require 'spec_helper'
describe Importers::GitlabsController do
describe Import::GitlabController do
let(:user) { create(:user, gitlab_access_token: 'asd123') }
before do
......@@ -16,7 +16,7 @@
get :callback
user.reload.gitlab_access_token.should == token
controller.should redirect_to(status_importers_gitlab_url)
controller.should redirect_to(status_import_gitlab_url)
end
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment