GitLab.com integration: refactoring

This commit is contained in:
Valery Sizov 2015-02-02 17:01:07 -08:00
parent 713bc152bd
commit 33349dd549
16 changed files with 52 additions and 32 deletions

View file

@ -1,4 +1,4 @@
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 @@ class Importers::GithubsController < ApplicationController
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 @@ class Importers::GithubsController < ApplicationController
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

View file

@ -1,13 +1,13 @@
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 @@ class Importers::GitlabsController < ApplicationController
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

View file

@ -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")

View file

@ -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")

View file

@ -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

View file

@ -53,16 +53,16 @@ Gitlab::Application.routes.draw do
#
# 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

View file

@ -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
```

View file

@ -14,7 +14,7 @@ module Gitlab
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

View file

@ -5,6 +5,7 @@ module Gitlab
def initialize(project)
@project = project
@formatter = Gitlab::ImportFormatter.new
end
def execute
@ -13,12 +14,14 @@ module Gitlab
#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

View file

@ -13,7 +13,7 @@ module Gitlab
)
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 @@ module Gitlab
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

View file

@ -6,6 +6,7 @@ module Gitlab
def initialize(project)
@project = project
@client = Client.new(project.creator.gitlab_access_token)
@formatter = Gitlab::ImportFormatter.new
end
def execute
@ -15,15 +16,16 @@ module Gitlab
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!(

View file

@ -0,0 +1,15 @@
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

View file

@ -1,6 +1,6 @@
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 @@ describe Importers::GithubsController do
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

View file

@ -1,6 +1,6 @@
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 @@ describe Importers::GitlabsController do
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