socialforge/lib/tasks/gitlab_sync.rake

51 lines
1.7 KiB
Ruby
Raw Permalink Normal View History

2016-12-13 17:21:58 +08:00
namespace :gitlab do
desc "make sure trustie'data consistent with gitlab's data"
2016-12-14 10:08:41 +08:00
task :sync_data => :environment do
2016-12-13 17:21:58 +08:00
s = Trustie::Gitlab::Sync.new
g = Gitlab.client
2016-12-14 13:32:25 +08:00
projects = Project.where("gpid is not null and status=?", 1)
projects.each do |project|
# sync members and roles
begin
2016-12-13 17:21:58 +08:00
if project.members.count != g.team_members(project.gpid).count
2016-12-14 10:08:41 +08:00
project.members.each do |m|
2016-12-14 13:32:25 +08:00
begin
gid = m.user.gid
if gid.nil?
gid = s.sync_user(m.user).id
end
access_level = m.roles[0].position == 3 ? 40 : (m.roles[0].position == 4 ? 30 : 20)
# sync project's members
g.add_team_member(project.gpid, gid, access_level)
# sync members' roles
g.edit_team_member(project.gpid, gid, access_level)
rescue Exception => e
DataException.create(:message => e.message, :container_id => project.id, :container_type => "Project")
puts e
2016-12-14 10:08:41 +08:00
end
end
2016-12-13 17:21:58 +08:00
end
2016-12-14 13:32:25 +08:00
rescue Exception => e
puts e
2016-12-13 17:21:58 +08:00
end
2016-12-14 13:32:25 +08:00
end
2016-12-13 17:21:58 +08:00
2016-12-14 13:32:25 +08:00
users = User.where("gid is not null and status =?", 1)
users.each do |user|
begin
2016-12-13 17:21:58 +08:00
# sync username
g.edit_user(user.gid, :username => user.login) if user.login != g.user(user.gid).try(:username)
# sync email
g.edit_user(user.gid, :email => user.mail) if user.mail != g.user(user.gid).try(:email)
# sync password
options = {:encrypted_password=> user.hashed_password, :password_salt=> user.salt}
2016-12-14 10:08:41 +08:00
g.put("/users/ext/#{user.gid}", :body => options)
2016-12-14 13:32:25 +08:00
rescue Exception => e
puts e
2016-12-13 17:21:58 +08:00
end
end
end
end