socialforge/app/services/repositories_service.rb

96 lines
3.1 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#coding=utf-8
include RepositoriesHelper
class RepositoriesService
def createNewRepoBlockchain project, params_repository
status = -1
# 判断版本库创建者是否有同名版本库,避免版本库路径一致问题
unless is_sigle_identifier?(project.user_id, params_repository.first[1]) # first[1]表示identifier
return status, nil
else
attrs = {:attrs => {:identifier => params_repository.first[1]}, :attrs_extra => {}}
repo = Repository.factory('Git')
repo.safe_attributes = params_repository
if attrs[:attrs_extra].keys.any?
repo.merge_extra_info(attrs[:attrs_extra])
end
repo.project = project
repo.type = 'Repository::Gitlab'
repo.identifier = repo.identifier.downcase
repo.url = repo.identifier
repo.root_url = repo.url
repo.extra_info = repo.extra_info
ActiveRecord::Base.transaction do
begin
if repo.save!
Rails.logger.info("!!!!!! repo.save!成功")
s = Trustie::Gitlab::Sync.new
s.create_project(project, repo)
Rails.logger.info("!!!!!!!!! project.gpid is: .......")
Rails.logger.info(project.gpid.blank?)
Rails.logger.info(project.gpid)
raise "sync failed" if project.gpid.blank?
status = 0
return status, repo # 表示创建成功
else
return status, nil
end
rescue Exception => e
Rails.logger.error("create repository #{e.message}")
raise ActiveRecord::Rollback
return status, nil
end
end
end
end
# 根据repo_url找到所属project
def findProjectByRepoUrl repo_url
status = -1
# 解析ownername
owner_name = nil
repo_name = nil
begin
index_doublesplit = repo_url.index("//")
# gitlab_address = Redmine::Configuration['gitlab_address']
gitlab_address = repo_url[index_doublesplit + 2..repo_url.length - 1]
index_split = gitlab_address.index("/")
gitlab_address = gitlab_address[index_split + 1..gitlab_address.length - 1]
index_split = gitlab_address.index("/")
owner_name = gitlab_address[0..index_split - 1]
index_dotgit = gitlab_address.index(".git")
repo_name = gitlab_address[index_split + 1..index_dotgit - 1]
rescue => e
Rails.logger.error("!!!!!!!!!!!!error with the repo_url: " + repo_url)
return status, nil, nil
end
owner = User.find_by_login(owner_name)
if owner
projects = Project.find_all_by_user_id(owner.id)
if projects.length <= 0
return status, nil, nil
else
projects.each do |project|
gpid = project.gpid
if gpid
http_url_to_repo = Gitlab.client.project(gpid).try(:http_url_to_repo)
if http_url_to_repo
if repo_url == http_url_to_repo
status = 0
return status, project, repo_name # 表示找到了
end
end
end
end
return status, nil, nil # 表示没找到
end
else
return status, nil, nil # 表示数据库有问题没有对应的owner
end
end
end