You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pgfqe6ch8/lib/trustie/gitlab/sync.rb

179 lines
5.5 KiB

6 years ago
#coding=utf-8
require_relative 'helper'
module Trustie
module Gitlab
module UserLevel
GUEST = 10
REPORTER = 20
DEVELOPER = 30
MASTER = 40
OWNER = 50
# 项目公开和私有
PUBLIC = 20
PRIVATE = 0
end
class Sync
attr :g
include Helper
def initialize
@g = ::Gitlab.client
end
def sync_user(user)
u = add_user(user)
if (!("day,none,all".include? user.mail_notification) or user.mail_notification.blank?)
user.mail_notification = "day"
end
user.save!
u
end
def create_project(project, repository)
gid = project.owner.gid
unless gid
gid = sync_user(project.owner).id
end
raise "unknow gid" unless gid
gproject = g.create_project(repository.identifier,
path: repository.identifier,
description: false,
wiki_enabled: false,
wall_enabled: false,
issues_enabled: false,
snippets_enabled: false,
public: true,
user_id: gid,
visibility_level: project.is_public? ? UserLevel::PUBLIC : UserLevel::PRIVATE
)
project.gpid = gproject.id
project.save!
# 创建的时候一并同步成员及角色
project.members.each do |m|
begin
gid = m.user.gid
unless gid
gid = sync_user(m.user).id
end
self.g.add_team_member(gproject.id, gid, get_gitlab_role(m))
rescue => e
puts e
end
end
project
end
def create_shixun(shixun, repository)
user = User.find_by_mail("educoder@163.com")
gid = user.gid
unless gid
gid = sync_user(user).id
end
raise "unknow gid" unless gid
gshixun = g.create_project(repository.identifier,
path: repository.identifier,
description: false,
wiki_enabled: false,
wall_enabled: false,
issues_enabled: false,
snippets_enabled: false,
public: false,
user_id: gid,
visibility_level: UserLevel::PUBLIC
)
shixun.gpid = gshixun.id
shixun.save!
# 创建的时候一并同步成员及角色
shixun.shixun_members.each do |m|
begin
gid = m.user.gid
unless gid
gid = sync_user(m.user).id
end
self.g.add_team_member(gshixun.id, gid, shixun_gitlab_role(m.role))
rescue => e
puts e
end
end
end
def sync_project(project, opt={})
gid = project.owner.gid
unless gid
gid = sync_user(project.owner).id
end
raise "unknow gid" unless gid
path = opt[:path]
raise "unknow path" unless path
import_url = opt[:import_url]
raise "unknow import_url" unless import_url
if opt[:password]
import_url.sub('@', ":#{opt[:password]}@")
end
# import url http://xianbo_trustie2:1234@repository.trustie.net/xianbo/trustie2.git
# can use password
gproject = self.g.create_project(path,
path: path,
description: false,
wiki_enabled: false,
wall_enabled: false,
issues_enabled: false,
snippets_enabled: false,
merge_requests_enabled: true,
public: false,
user_id: gid,
import_url: import_url,
visibility_level: project.is_public? ? UserLevel::PUBLIC : UserLevel::PRIVATE
)
project.gpid = gproject.id
project.save!
puts "Successfully created #{project.name}"
# add team members
#
project.members.each do |m|
begin
gid = m.user.gid
unless gid
gid = sync_user(m.user).id
end
self.g.add_team_member(gproject.id, gid, get_gitlab_role(m))
rescue => e
puts e
end
end
end
def only_members(project)
project.members.each do |m|
begin
gid = m.user.gid
if gid.nil?
gid = sync_user(m.user).id
end
self.g.add_team_member(project.gpid, gid, get_gitlab_role(m))
rescue => e
puts e
end
end
end
def remove_project
end
end
end
end