project同步

This commit is contained in:
guange 2015-07-12 15:56:15 +08:00
parent 797ed5081d
commit 3614c6ee9b
4 changed files with 83 additions and 7 deletions

View File

@ -848,6 +848,10 @@ class Project < ActiveRecord::Base
end
end
def owner
User.find(self.user_id)
end
private
def after_parent_changed(parent_was)
@ -1162,5 +1166,7 @@ class Project < ActiveRecord::Base
:forge_act_id => self.id,:forge_act_type => "ProjectCreateInfo")
fa.save!
end
end

View File

@ -0,0 +1,5 @@
class AddGitlabUserIdToUsers < ActiveRecord::Migration
def change
add_column :users, :gid, :integer
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20150619060110) do
ActiveRecord::Schema.define(:version => 20150712063406) do
create_table "activities", :force => true do |t|
t.integer "act_id", :null => false
@ -1429,6 +1429,7 @@ ActiveRecord::Schema.define(:version => 20150619060110) do
t.string "identity_url"
t.string "mail_notification", :default => "", :null => false
t.string "salt", :limit => 64
t.integer "gid"
end
add_index "users", ["auth_source_id"], :name => "index_users_on_auth_source_id"

View File

@ -1,11 +1,75 @@
namespace :gitlab do
desc "sync users to gitlab"
task :sync => :environment do
User.where(login: 'guange').find_each do |user|
g = Gitlab.client
unless g.get("/users?search=#{user.mail}")
g.create_user(user.mail, user.hashed_password, name: user.show_name, username: user.login)
namespace :sync do
module Helper
def self.change_password(uid, en_pwd, salt)
g = Gitlab.client
g.edit_user(uid, :encrypted_password=>en_pwd, :password_salt=>salt)
end
end
desc "sync users to gitlab"
task :users => :environment do
# User.where(username: 'root').find_each do |user|
User.where(login: 'guange1').find_each do |user|
begin
g = Gitlab.client
u = g.get("/users?search=#{user.mail}").first
unless u
u = g.create_user(user.mail, user.hashed_password, name: user.show_name, username: user.login)
user.gid = u.id
user.save!
puts "create user #{user.login}"
end
Helper.change_password(u.id, user.hashed_password, user.salt)
rescue => e
puts e
end
end
end
desc "update user password"
task :password => :environment do
Helper.change_password(1,'5188b7a65acf294ee7deceb397b6f9c62214ea50','dcb8d9fffabec60c2d0d1030b679fbbb')
end
desc "sync projects to gitlab"
task :projects => :environment do
Project.where(id: 505).find_each do |project|
g = Gitlab.client
gid = project.owner.gid
raise "unknow gid" unless gid
path = project.repositories.where(:is_default => true).first.root_url.split('/').last
path = path.split('.').first
raise "unknow path" unless path
gproject = g.create_project(project.identifier,
path: path,
description: project.description,
wiki_enabled: false,
wall_enabled: false,
issues_enabled: false,
snippets_enabled: false,
public: false,
user_id: gid,
import_url: 'https://github.com/gitlabhq/gitlab-cli.git'
)
# add team members
#
GUEST = 10
REPORTER = 20
DEVELOPER = 30
MASTER = 40
OWNER = 50
project.members.each do |m|
g.add_team_member(gproject.id, m.user.gid, DEVELOPER)
end
end
end
end
end