Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
56cb4762
Commit
56cb4762
authored
Aug 22, 2016
by
Douglas Barbosa Alexandre
Committed by
Stan Hu
Nov 21, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring Bitbucket import controller to use the new OAuth2 client
parent
411d0a93
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
51 deletions
+67
-51
app/controllers/import/bitbucket_controller.rb
app/controllers/import/bitbucket_controller.rb
+43
-34
app/views/import/bitbucket/status.html.haml
app/views/import/bitbucket/status.html.haml
+18
-17
lib/bitbucket/error/unauthorized.rb
lib/bitbucket/error/unauthorized.rb
+6
-0
No files found.
app/controllers/import/bitbucket_controller.rb
View file @
56cb4762
...
...
@@ -3,49 +3,54 @@ class Import::BitbucketController < Import::BaseController
before_action
:bitbucket_auth
,
except: :callback
rescue_from
OAuth
::
Error
,
with: :bitbucket_unauthorized
rescue_from
Gitlab
::
BitbucketImport
::
Client
::
Unauthorized
,
with: :bitbucket_unauthorized
rescue_from
Bitbucket
::
Error
::
Unauthorized
,
with: :bitbucket_unauthorized
def
callback
request_token
=
session
.
delete
(
:oauth_request_token
)
raise
"Session expired!"
if
request_token
.
nil?
response
=
client
.
auth_code
.
get_token
(
params
[
:code
],
redirect_uri:
callback_import_bitbucket_url
)
request_token
.
symbolize_keys!
access_token
=
client
.
get_token
(
request_token
,
params
[
:oauth_verifier
],
callback_import_bitbucket_url
)
session
[
:bitbucket_access_token
]
=
access_token
.
token
session
[
:bitbucket_access_token_secret
]
=
access_token
.
secret
session
[
:bitbucket_token
]
=
response
.
token
session
[
:bitbucket_expires_at
]
=
response
.
expires_at
session
[
:bitbucket_expires_in
]
=
response
.
expires_in
session
[
:bitbucket_refresh_token
]
=
response
.
refresh_token
redirect_to
status_import_bitbucket_url
end
def
status
@repos
=
client
.
projects
@incompatible_repos
=
client
.
incompatible_project
s
client
=
Bitbucket
::
Client
.
new
(
credentials
)
repos
=
client
.
repo
s
@already_added_projects
=
current_user
.
created_projects
.
where
(
import_type:
"bitbucket"
)
@repos
=
repos
.
select
(
&
:valid?
)
@incompatible_repos
=
repos
.
reject
(
&
:valid?
)
@already_added_projects
=
current_user
.
created_projects
.
where
(
import_type:
'bitbucket'
)
already_added_projects_names
=
@already_added_projects
.
pluck
(
:import_source
)
@repos
.
to_a
.
reject!
{
|
repo
|
already_added_projects_names
.
include?
"
#{
repo
[
"owner"
]
}
/
#{
repo
[
"slug"
]
}
"
}
@repos
.
to_a
.
reject!
{
|
repo
|
already_added_projects_names
.
include?
(
repo
.
full_name
)
}
end
def
jobs
jobs
=
current_user
.
created_projects
.
where
(
import_type:
"bitbucket"
).
to_json
(
only:
[
:id
,
:import_status
])
render
json:
jobs
render
json:
current_user
.
created_projects
.
where
(
import_type:
'bitbucket'
)
.
to_json
(
only:
[
:id
,
:import_status
])
end
def
create
client
=
Bitbucket
::
Client
.
new
(
credentials
)
@repo_id
=
params
[
:repo_id
].
to_s
repo
=
client
.
project
(
@repo_id
.
gsub
(
'___'
,
'/'
)
)
@project_name
=
repo
[
'slug'
]
@
target_namespace
=
find_or_create_namespace
(
repo
[
'owner'
],
client
.
user
[
'user'
][
'username'
])
name
=
@repo_id
.
to_s
.
gsub
(
'___'
,
'/'
)
repo
=
client
.
repo
(
name
)
@
project_name
=
repo
.
name
unless
Gitlab
::
BitbucketImport
::
KeyAdder
.
new
(
repo
,
current_user
,
access_params
).
execute
render
'deploy_key'
and
return
end
repo_owner
=
repo
.
owner
repo_owner
=
current_user
.
username
if
repo_owner
==
client
.
user
.
username
@target_namespace
=
params
[
:new_namespace
].
presence
||
repo_owner
namespace
=
find_or_create_namespace
(
target_namespace_name
,
repo_owner
)
if
current_user
.
can?
(
:create_projects
,
@target_namespace
)
@project
=
Gitlab
::
BitbucketImport
::
ProjectCreator
.
new
(
repo
,
@target_namespace
,
current_user
,
access_param
s
).
execute
@project
=
Gitlab
::
BitbucketImport
::
ProjectCreator
.
new
(
repo
,
namespace
,
current_user
,
credential
s
).
execute
else
render
'unauthorized'
end
...
...
@@ -54,8 +59,15 @@ class Import::BitbucketController < Import::BaseController
private
def
client
@client
||=
Gitlab
::
BitbucketImport
::
Client
.
new
(
session
[
:bitbucket_access_token
],
session
[
:bitbucket_access_token_secret
])
@client
||=
OAuth2
::
Client
.
new
(
provider
.
app_id
,
provider
.
app_secret
,
options
)
end
def
provider
Gitlab
.
config
.
omniauth
.
providers
.
find
{
|
provider
|
provider
.
name
==
'bitbucket'
}
end
def
options
OmniAuth
::
Strategies
::
Bitbucket
.
default_options
[
:client_options
].
deep_symbolize_keys
end
def
verify_bitbucket_import_enabled
...
...
@@ -63,26 +75,23 @@ class Import::BitbucketController < Import::BaseController
end
def
bitbucket_auth
if
session
[
:bitbucket_access_token
].
blank?
go_to_bitbucket_for_permissions
end
go_to_bitbucket_for_permissions
if
session
[
:bitbucket_token
].
blank?
end
def
go_to_bitbucket_for_permissions
request_token
=
client
.
request_token
(
callback_import_bitbucket_url
)
session
[
:oauth_request_token
]
=
request_token
redirect_to
client
.
authorize_url
(
request_token
,
callback_import_bitbucket_url
)
redirect_to
client
.
auth_code
.
authorize_url
(
redirect_uri:
callback_import_bitbucket_url
)
end
def
bitbucket_unauthorized
go_to_bitbucket_for_permissions
end
def
access_param
s
def
credential
s
{
bitbucket_access_token:
session
[
:bitbucket_access_token
],
bitbucket_access_token_secret:
session
[
:bitbucket_access_token_secret
]
token:
session
[
:bitbucket_token
],
expires_at:
session
[
:bitbucket_expires_at
],
expires_in:
session
[
:bitbucket_expires_in
],
refresh_token:
session
[
:bitbucket_refresh_token
]
}
end
end
app/views/import/bitbucket/status.html.haml
View file @
56cb4762
-
page_title
"Bitbucket import"
-
header_title
"Projects"
,
root_path
-
page_title
'Bitbucket import'
-
header_title
'Projects'
,
root_path
%h3
.page-title
%i
.fa.fa-bitbucket
Import projects from Bitbucket
...
...
@@ -10,13 +11,13 @@
%hr
%p
-
if
@incompatible_repos
.
any?
=
button_tag
class:
"btn btn-import btn-success js-import-all"
do
=
button_tag
class:
'btn btn-import btn-success js-import-all'
do
Import all compatible projects
=
icon
(
"spinner spin"
,
class:
"loading-icon"
)
=
icon
(
'spinner spin'
,
class:
'loading-icon'
)
-
else
=
button_tag
class:
"btn btn-success js-import-all"
do
=
button_tag
class:
'btn btn-success js-import-all'
do
Import all projects
=
icon
(
"spinner spin"
,
class:
"loading-icon"
)
=
icon
(
'spinner spin'
,
class:
'loading-icon'
)
.table-responsive
%table
.table.import-jobs
...
...
@@ -32,7 +33,7 @@
-
@already_added_projects
.
each
do
|
project
|
%tr
{
id:
"project_#{project.id}"
,
class:
"#{project_status_css_class(project.import_status)}"
}
%td
=
link_to
project
.
import_source
,
"https://bitbucket.org/
#{
project
.
import_source
}
"
,
target:
"_blank"
=
link_to
project
.
import_source
,
'https://bitbucket.org/#{project.import_source}'
,
target:
'_blank'
%td
=
link_to
project
.
path_with_namespace
,
[
project
.
namespace
.
becomes
(
Namespace
),
project
]
%td
.job-status
...
...
@@ -47,31 +48,31 @@
=
project
.
human_import_status_name
-
@repos
.
each
do
|
repo
|
%tr
{
id:
"repo_#{repo
["
owner
"]}___#{repo["
slug
"]
}"
}
%tr
{
id:
"repo_#{repo
.owner}___#{repo.slug
}"
}
%td
=
link_to
"
#{
repo
[
"owner"
]
}
/
#{
repo
[
"slug"
]
}
"
,
"https://bitbucket.org/
#{
repo
[
"owner"
]
}
/
#{
repo
[
"slug"
]
}
"
,
target:
"_blank"
=
link_to
"
#{
repo
.
full_name
}
"
,
"https://bitbucket.org/
#{
repo
.
full_name
}
"
,
target:
"_blank"
%td
.import-target
=
import_project_target
(
repo
[
'owner'
],
repo
[
'slug'
])
=
"
#{
repo
.
full_name
}
"
%td
.import-actions.job-status
=
button_tag
class:
"btn btn-import js-add-to-import"
do
=
button_tag
class:
'btn btn-import js-add-to-import'
do
Import
=
icon
(
"spinner spin"
,
class:
"loading-icon"
)
=
icon
(
'spinner spin'
,
class:
'loading-icon'
)
-
@incompatible_repos
.
each
do
|
repo
|
%tr
{
id:
"repo_#{repo
["
owner
"]}___#{repo["
slug
"]
}"
}
%tr
{
id:
"repo_#{repo
.owner}___#{repo.slug
}"
}
%td
=
link_to
"
#{
repo
[
"owner"
]
}
/
#{
repo
[
"slug"
]
}
"
,
"https://bitbucket.org/
#{
repo
[
"owner"
]
}
/
#{
repo
[
"slug"
]
}
"
,
target:
"_blank"
=
link_to
"
#{
repo
.
full_name
}
"
,
"https://bitbucket.org/
#{
repo
.
full_name
}
"
,
target:
'_blank'
%td
.import-target
%td
.import-actions-job-status
=
label_tag
"Incompatible Project"
,
nil
,
class:
"label label-danger"
=
label_tag
'Incompatible Project'
,
nil
,
class:
'label label-danger'
-
if
@incompatible_repos
.
any?
%p
One or more of your Bitbucket projects cannot be imported into GitLab
directly because they use Subversion or Mercurial for version control,
rather than Git. Please convert
=
link_to
"them to Git,"
,
"https://www.atlassian.com/git/tutorials/migrating-overview"
=
link_to
'them to Git,'
,
'https://www.atlassian.com/git/tutorials/migrating-overview'
and go through the
=
link_to
"import flow"
,
status_import_bitbucket_path
,
"data-no-turbolink"
=>
"true"
=
link_to
'import flow'
,
status_import_bitbucket_path
,
'data-no-turbolink'
=>
'true'
again.
.js-importer-status
{
data:
{
jobs_import_path:
"#{jobs_import_bitbucket_path}"
,
import_path:
"#{import_bitbucket_path}"
}
}
lib/bitbucket/error/unauthorized.rb
0 → 100644
View file @
56cb4762
module
Bitbucket
module
Error
class
Unauthorized
<
StandardError
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment