Commit fdb960fc authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

Bitbucket import: removed most of the code

There aren't Mercurial repositories to import from Bitbucket
any more: our divergence with upstream on this is now just a
drag.

Removal is partial in order not to jeopardize the installation
of 0.18.0rc1 on foss.h.n (second OAuth, worker queue registration
etc.). In all cases, behaviour on such configurations will be
tested and adjusted before release.
parent e3b221981d54
......@@ -30,12 +30,11 @@
super
end
def import_form
target_namespace = Namespace.find(params[:namespace_id].to_i)
begin
project = create_from_bb_rpath(params[:repo_id].to_s, target_namespace)
rescue StandardError => err
redirect_to(status_import_bitbucket_path, status: 303, alert: "Error: #{err}")
return
end
def create
bitbucket_client = Bitbucket::Client.new(credentials)
repo_id = params[:repo_id].to_s
name = repo_id.gsub('___', '/')
repo = bitbucket_client.repo(name)
project_name = params[:new_name].presence || repo.name
......@@ -41,15 +40,6 @@
if project.nil?
redirect_to(status_import_bitbucket_path,
status: 303,
alert: _('This namespace has already been taken! Please choose another one.'),
)
elsif project.persisted?
redirect_to(status_import_bitbucket_path, status: 303, notice: _('Project import started'))
else
err = project_save_error(project)
Rails.logger.info("testgr Bitbucket controller project err=#{err}")
redirect_to(status_import_bitbucket_path, status: 303, alert: "Error: #{err}")
end
end
repo_owner = repo.owner
repo_owner = current_user.username if repo_owner == bitbucket_client.user.username
namespace_path = params[:new_namespace].presence || repo_owner
target_namespace = find_or_create_namespace(namespace_path, current_user)
......@@ -55,12 +45,13 @@
def create
repo_id = params[:repo_id].to_s
project = create_from_bb_rpath(repo_id.gsub('___', '/'))
if project.nil?
render json: { errors: _('This namespace has already been taken! Please choose another one.') }, status: :unprocessable_entity
else
if current_user.can?(:create_projects, target_namespace)
# The token in a session can be expired, we need to get most recent one because
# Bitbucket::Connection class refreshes it.
session[:bitbucket_token] = bitbucket_client.connection.token
project = Gitlab::BitbucketImport::ProjectCreator.new(repo, project_name, target_namespace, current_user, credentials).execute
if project.persisted?
render json: ProjectSerializer.new.represent(project, serializer: :import)
else
render json: { errors: project_save_error(project) }, status: :unprocessable_entity
end
......@@ -62,48 +53,10 @@
if project.persisted?
render json: ProjectSerializer.new.represent(project, serializer: :import)
else
render json: { errors: project_save_error(project) }, status: :unprocessable_entity
end
end
end
# name is the rpath in Bitbucket Cloud
def create_from_bb_rpath(name, target_namespace = nil)
bitbucket_client = Bitbucket::Client.new(credentials)
repo = bitbucket_client.repo(name)
project_name = params[:new_name].presence || repo.name
if target_namespace.nil?
repo_owner = repo.owner
repo_owner = current_user.username if repo_owner == bitbucket_client.user.username
namespace_path = params[:new_namespace].presence || repo_owner
target_namespace = find_or_create_namespace(namespace_path, current_user)
end
# The token in a session can be expired, we need to get most recent one because
# Bitbucket::Connection class refreshes it.
session[:bitbucket_token] = bitbucket_client.connection.token
project = Project.where(namespace_id: target_namespace.id,
path: project_name).first
if !project.nil?
record_url = URI(project.import_url)
current_url = URI(repo.clone_url)
if record_url.host != current_url.host || record_url.path != current_url.path
raise StandardError,
"Found project '#{project_name}' in target namespace "\
"'#{target_namespace.full_path}', but its previous import URL "\
"#{record_url} doesn't match the expected #{current_url}"
end
project.create_or_update_import_data(credentials: credentials)
project.save
Rails.logger.info("Bitbucket controller: scheduling asynchronous "\
"reimport for project #{project.id}")
BitbucketReimportWorker.perform_async(project.id)
project
elsif current_user.can?(:create_projects, target_namespace)
Gitlab::BitbucketImport::ProjectCreator.new(repo, project_name, target_namespace, current_user, credentials).execute
else
render json: { errors: _('This namespace has already been taken! Please choose another one.') }, status: :unprocessable_entity
end
end
......@@ -140,8 +93,7 @@
end
def provider
Gitlab::Auth::OAuth::Provider.config_for('bitbucket_import',
fallback='bitbucket')
Gitlab::Auth::OAuth::Provider.config_for('bitbucket')
end
def client
......
......@@ -18,11 +18,6 @@
get_collection(path, :comment)
end
def issue_attachments(repo, issue_id)
path = "/repositories/#{repo}/issues/#{issue_id}/attachments"
get_collection(path, :attachment)
end
def pull_requests(repo)
path = "/repositories/#{repo}/pullrequests?state=ALL"
get_collection(path, :pull_request)
......
......@@ -26,18 +26,6 @@
response.parsed
end
# Return a response for an absolute URL
#
# This is useful for parts of the Bitbucket API (e.g, attachments) that
# provide a full URL.
#
# Returns response object
def raw_get(absolute_url, follow_redirect = true)
refresh! if expired?
opts = follow_redirect ? {} : {redirect_count: connection.client.options[:max_redirects]}
connection.get(absolute_url, opts)
end
delegate :expired?, to: :connection
def refresh!
......@@ -71,8 +59,7 @@
end
def provider
Gitlab::Auth::OAuth::Provider.config_for('bitbucket_import',
fallback='bitbucket')
Gitlab::Auth::OAuth::Provider.config_for('bitbucket')
end
def options
......
......@@ -31,12 +31,7 @@
end
def fetch_next_page
batching_extra = {pagelen: PAGE_LENGTH}
# TODO maybe better to have an explicit attribute
# (but it's also true that the default sorting just can't work with
# attachments)
batching_extra[:sort] = :created_on unless type == :attachment
parsed_response = connection.get(next_url, batching_extra)
parsed_response = connection.get(next_url, pagelen: PAGE_LENGTH, sort: :created_on)
Page.new(parsed_response, type)
end
end
......
# frozen_string_literal: true
module Bitbucket
module Representation
class Attachment < Representation::Base
def name
raw.fetch('name')
end
def retrieval_url
# honestly, don't know why the URL should be a list. I don't
# give up easily on documentation, but Bitbucket API documentation
# is inscrutable, couldn't find a reference for attachments.
raw.fetch('links').fetch('self').fetch('href')[0]
end
end
end
end
......@@ -4,14 +4,7 @@
module Representation
class Comment < Representation::Base
def author
user&.fetch('nickname', nil)
end
def author_info
# gracinet: would be simpler to unmark user() as public
# but this way gives us more uniformity with issues and
# probably less conflicts down the road.
raw.fetch('user', nil)
user['nickname']
end
def note
......
......@@ -17,10 +17,6 @@
raw.dig('reporter', 'nickname')
end
def author_info
raw.fetch('reporter', nil)
end
def description
raw.fetch('content', {}).fetch('raw', nil)
end
......
......@@ -4,11 +4,7 @@
module Representation
class PullRequest < Representation::Base
def author
raw.dig('author', 'nickname')
end
def author_info
raw.fetch('author', nil)
raw.fetch('author', {}).fetch('nickname', nil)
end
def description
......@@ -43,19 +39,7 @@
end
def source_branch_name
source_branch.dig('branch', 'name')
end
def source_project_full_name
source_branch.dig('repository', 'full_name')
end
def source_project_uuid
source_branch.dig('repository', 'uuid')
end
def source_project_url
source_branch.dig('repository', 'links', 'html', 'href')
source_branch.fetch('branch', {}).fetch('name', nil)
end
def source_branch_sha
......@@ -59,7 +43,7 @@
end
def source_branch_sha
source_branch.dig('commit', 'hash')
source_branch.fetch('commit', {}).fetch('hash', nil)
end
def target_branch_name
......@@ -63,7 +47,7 @@
end
def target_branch_name
target_branch.dig('branch', 'name')
target_branch.fetch('branch', {}).fetch('name', nil)
end
def target_branch_sha
......@@ -67,15 +51,7 @@
end
def target_branch_sha
target_branch.dig('commit', 'hash')
end
def target_project_full_name
target_branch.dig('repository', 'full_name')
end
def target_project_uuid
target_branch.dig('repository', 'uuid')
target_branch.fetch('commit', {}).fetch('hash', nil)
end
private
......
......@@ -47,8 +47,4 @@
raw['name']
end
def main_branch
raw['mainbranch']
end
def valid?
......@@ -54,5 +50,5 @@
def valid?
raw['scm'] == 'hg'
raw['scm'] == 'git'
end
def has_wiki?
......
This diff is collapsed.
......@@ -18,7 +18,7 @@
end
def import_url
project.import_url.sub(/(\.git)?\z/, '\1/wiki')
project.import_url.sub(/\.git\z/, ".git/wiki")
end
end
end
......
Supports Markdown
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