2015-10-21 17:13:07 +08:00
|
|
|
#coding=utf-8
|
|
|
|
|
|
|
|
require_relative 'helper'
|
|
|
|
|
2015-10-16 16:57:45 +08:00
|
|
|
module Trustie
|
|
|
|
module Gitlab
|
|
|
|
module UserLevel
|
|
|
|
GUEST = 10
|
|
|
|
REPORTER = 20
|
|
|
|
DEVELOPER = 30
|
|
|
|
MASTER = 40
|
|
|
|
OWNER = 50
|
|
|
|
end
|
|
|
|
|
|
|
|
class Sync
|
|
|
|
attr :g
|
2015-10-21 17:13:07 +08:00
|
|
|
include Helper
|
2015-10-16 16:57:45 +08:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
@g = ::Gitlab.client
|
|
|
|
end
|
|
|
|
|
|
|
|
def sync_user(user)
|
2015-10-21 17:13:07 +08:00
|
|
|
u = add_user(user)
|
2015-10-29 15:56:41 +08:00
|
|
|
if (!("day,none,all".include? user.mail_notification) or user.mail_notification.blank?)
|
2015-10-29 12:33:22 +08:00
|
|
|
user.mail_notification = "day"
|
|
|
|
end
|
|
|
|
user.save!
|
2015-11-01 22:25:42 +08:00
|
|
|
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: project.description,
|
|
|
|
wiki_enabled: false,
|
|
|
|
wall_enabled: false,
|
|
|
|
issues_enabled: false,
|
|
|
|
snippets_enabled: false,
|
|
|
|
public: false,
|
|
|
|
user_id: gid
|
|
|
|
)
|
|
|
|
project.gpid = gproject.id
|
|
|
|
project.save!
|
2015-10-16 16:57:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def sync_project(project, opt={})
|
|
|
|
gid = project.owner.gid
|
2015-11-01 22:25:42 +08:00
|
|
|
unless gid
|
|
|
|
gid = sync_user(project.owner).id
|
|
|
|
end
|
|
|
|
|
2015-10-16 16:57:45 +08:00
|
|
|
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: project.description,
|
|
|
|
wiki_enabled: false,
|
|
|
|
wall_enabled: false,
|
|
|
|
issues_enabled: false,
|
|
|
|
snippets_enabled: false,
|
|
|
|
public: false,
|
|
|
|
user_id: gid,
|
|
|
|
import_url: import_url
|
|
|
|
)
|
|
|
|
project.gpid = gproject.id
|
|
|
|
project.save!
|
|
|
|
puts "Successfully created #{project.name}"
|
|
|
|
# add team members
|
|
|
|
#
|
|
|
|
|
|
|
|
project.members.each do |m|
|
|
|
|
begin
|
2015-11-01 22:25:42 +08:00
|
|
|
gid = m.user.gid
|
|
|
|
unless gid
|
|
|
|
gid = sync_user(m.user).id
|
|
|
|
end
|
|
|
|
self.g.add_team_member(gproject.id, gid, UserLevel::DEVELOPER)
|
2015-10-16 16:57:45 +08:00
|
|
|
rescue => e
|
|
|
|
puts e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_project
|
|
|
|
end
|
|
|
|
end
|
2015-10-21 17:13:07 +08:00
|
|
|
|
2015-10-16 16:57:45 +08:00
|
|
|
end
|
|
|
|
end
|