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

GitLab.com integration: refactoring

parent 713bc152
class Importers::GithubsController < ApplicationController class Import::GithubController < ApplicationController
before_filter :github_auth, except: :callback before_filter :github_auth, except: :callback
rescue_from Octokit::Unauthorized, with: :github_unauthorized rescue_from Octokit::Unauthorized, with: :github_unauthorized
...@@ -7,7 +7,7 @@ class Importers::GithubsController < ApplicationController ...@@ -7,7 +7,7 @@ class Importers::GithubsController < ApplicationController
token = client.auth_code.get_token(params[:code]).token token = client.auth_code.get_token(params[:code]).token
current_user.github_access_token = token current_user.github_access_token = token
current_user.save current_user.save
redirect_to status_importers_github_url redirect_to status_import_github_url
end end
def status def status
...@@ -69,7 +69,7 @@ class Importers::GithubsController < ApplicationController ...@@ -69,7 +69,7 @@ class Importers::GithubsController < ApplicationController
def go_to_github_for_permissions def go_to_github_for_permissions
redirect_to client.auth_code.authorize_url({ redirect_to client.auth_code.authorize_url({
redirect_uri: callback_importers_github_url, redirect_uri: callback_import_github_url,
scope: "repo, user, user:email" scope: "repo, user, user:email"
}) })
end end
......
class Importers::GitlabsController < ApplicationController class Import::GitlabController < ApplicationController
before_filter :gitlab_auth, except: :callback before_filter :gitlab_auth, except: :callback
rescue_from OAuth2::Error, with: :gitlab_unauthorized rescue_from OAuth2::Error, with: :gitlab_unauthorized
def callback 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.gitlab_access_token = token
current_user.save current_user.save
redirect_to status_importers_gitlab_url redirect_to status_import_gitlab_url
end end
def status def status
...@@ -60,7 +60,7 @@ class Importers::GitlabsController < ApplicationController ...@@ -60,7 +60,7 @@ class Importers::GitlabsController < ApplicationController
end end
def go_to_gitlab_for_permissions 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 end
def gitlab_unauthorized def gitlab_unauthorized
......
...@@ -43,11 +43,11 @@ ...@@ -43,11 +43,11 @@
if tr.find(".import-target input").length > 0 if tr.find(".import-target input").length > 0
new_namespace = tr.find(".import-target input").prop("value") new_namespace = tr.find(".import-target input").prop("value")
tr.find(".import-target").empty().append(new_namespace + "/" + tr.find(".import-target").data("project_name")) 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 (-> setInterval (->
$.get "#{jobs_importers_github_path}", (data)-> $.get "#{jobs_import_github_path}", (data)->
$.each data, (i, job) -> $.each data, (i, job) ->
job_item = $("#project_" + job.id) job_item = $("#project_" + job.id)
status_field = job_item.find(".job-status") status_field = job_item.find(".job-status")
......
...@@ -43,11 +43,11 @@ ...@@ -43,11 +43,11 @@
if tr.find(".import-target input").length > 0 if tr.find(".import-target input").length > 0
new_namespace = tr.find(".import-target input").prop("value") new_namespace = tr.find(".import-target input").prop("value")
tr.find(".import-target").empty().append(new_namespace + "/" + tr.find(".import-target").data("project_name")) 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 (-> setInterval (->
$.get "#{jobs_importers_gitlab_path}", (data)-> $.get "#{jobs_import_gitlab_path}", (data)->
$.each data, (i, job) -> $.each data, (i, job) ->
job_item = $("#project_" + job.id) job_item = $("#project_" + job.id)
status_field = job_item.find(".job-status") status_field = job_item.find(".job-status")
......
...@@ -44,7 +44,7 @@ ...@@ -44,7 +44,7 @@
.col-sm-2 .col-sm-2
.col-sm-10 .col-sm-10
- if github_import_enabled? - if github_import_enabled?
= link_to status_importers_github_path do = link_to status_import_github_path do
%i.fa.fa-github %i.fa.fa-github
Import projects from GitHub Import projects from GitHub
- else - else
...@@ -57,7 +57,7 @@ ...@@ -57,7 +57,7 @@
.col-sm-2 .col-sm-2
.col-sm-10 .col-sm-10
- if gitlab_import_enabled? - if gitlab_import_enabled?
= link_to status_importers_gitlab_path do = link_to status_import_gitlab_path do
%i.fa.fa-heart %i.fa.fa-heart
Import projects from GitLab.com Import projects from GitLab.com
- else - else
......
...@@ -53,16 +53,16 @@ Gitlab::Application.routes.draw do ...@@ -53,16 +53,16 @@ Gitlab::Application.routes.draw do
# #
# Importers # Import
# #
namespace :importers do namespace :import do
resource :github, only: [:create, :new] do resource :github, only: [:create, :new], controller: :github do
get :status get :status
get :callback get :callback
get :jobs get :jobs
end end
resource :gitlab, only: [:create, :new] do resource :gitlab, only: [:create, :new], controller: :gitlab do
get :status get :status
get :callback get :callback
get :jobs get :jobs
......
...@@ -15,7 +15,7 @@ To enable the GitLab OmniAuth provider you must register your application with G ...@@ -15,7 +15,7 @@ To enable the GitLab OmniAuth provider you must register your application with G
- Redirect URI: - Redirect URI:
``` ```
http://gitlab.example.com/importers/gitlab/callback http://gitlab.example.com/import/gitlab/callback
http://gitlab.example.com/users/auth/gitlab/callback http://gitlab.example.com/users/auth/gitlab/callback
``` ```
......
...@@ -14,7 +14,7 @@ module Gitlab ...@@ -14,7 +14,7 @@ module Gitlab
private private
def config def config
Gitlab.config.omniauth.providers.select{|provider| provider.name == "github"}.first Gitlab.config.omniauth.providers.find{|provider| provider.name == "github"}
end end
def github_options def github_options
......
...@@ -5,6 +5,7 @@ module Gitlab ...@@ -5,6 +5,7 @@ module Gitlab
def initialize(project) def initialize(project)
@project = project @project = project
@formatter = Gitlab::ImportFormatter.new
end end
def execute def execute
...@@ -13,12 +14,14 @@ module Gitlab ...@@ -13,12 +14,14 @@ module Gitlab
#Issues && Comments #Issues && Comments
client.list_issues(project.import_source, state: :all).each do |issue| client.list_issues(project.import_source, state: :all).each do |issue|
if issue.pull_request.nil? 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 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| 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
end end
......
...@@ -13,7 +13,7 @@ module Gitlab ...@@ -13,7 +13,7 @@ module Gitlab
) )
if 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
end end
...@@ -67,7 +67,7 @@ module Gitlab ...@@ -67,7 +67,7 @@ module Gitlab
end end
def config def config
Gitlab.config.omniauth.providers.select{|provider| provider.name == "gitlab"}.first Gitlab.config.omniauth.providers.find{|provider| provider.name == "gitlab"}
end end
def github_options def github_options
......
...@@ -6,6 +6,7 @@ module Gitlab ...@@ -6,6 +6,7 @@ module Gitlab
def initialize(project) def initialize(project)
@project = project @project = project
@client = Client.new(project.creator.gitlab_access_token) @client = Client.new(project.creator.gitlab_access_token)
@formatter = Gitlab::ImportFormatter.new
end end
def execute def execute
...@@ -15,15 +16,16 @@ module Gitlab ...@@ -15,15 +16,16 @@ module Gitlab
issues = client.issues(project_identifier) issues = client.issues(project_identifier)
issues.each do |issue| 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"]) comments = client.issue_comments(project_identifier, issue["id"])
if comments.any? if comments.any?
body += "\n\n\n**Imported comments:**\n" body += @formatter.comments_header
end end
comments.each do |comment| 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 end
project.issues.create!( 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' require 'spec_helper'
describe Importers::GithubsController do describe Import::GithubController do
let(:user) { create(:user, github_access_token: 'asd123') } let(:user) { create(:user, github_access_token: 'asd123') }
before do before do
...@@ -16,7 +16,7 @@ describe Importers::GithubsController do ...@@ -16,7 +16,7 @@ describe Importers::GithubsController do
get :callback get :callback
user.reload.github_access_token.should == token 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
end end
......
require 'spec_helper' require 'spec_helper'
describe Importers::GitlabsController do describe Import::GitlabController do
let(:user) { create(:user, gitlab_access_token: 'asd123') } let(:user) { create(:user, gitlab_access_token: 'asd123') }
before do before do
...@@ -16,7 +16,7 @@ describe Importers::GitlabsController do ...@@ -16,7 +16,7 @@ describe Importers::GitlabsController do
get :callback get :callback
user.reload.gitlab_access_token.should == token 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
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