From 3614c6ee9b9980c27e845b616ab15c9b803a06fe Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Sun, 12 Jul 2015 15:56:15 +0800 Subject: [PATCH] =?UTF-8?q?project=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/project.rb | 6 ++ ...50712063406_add_gitlab_user_id_to_users.rb | 5 ++ db/schema.rb | 3 +- lib/tasks/gitlab.rake | 76 +++++++++++++++++-- 4 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20150712063406_add_gitlab_user_id_to_users.rb diff --git a/app/models/project.rb b/app/models/project.rb index 49ed2d0ee..49d46a93a 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -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 diff --git a/db/migrate/20150712063406_add_gitlab_user_id_to_users.rb b/db/migrate/20150712063406_add_gitlab_user_id_to_users.rb new file mode 100644 index 000000000..f6e2d60cd --- /dev/null +++ b/db/migrate/20150712063406_add_gitlab_user_id_to_users.rb @@ -0,0 +1,5 @@ +class AddGitlabUserIdToUsers < ActiveRecord::Migration + def change + add_column :users, :gid, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 420432355..bc2f2abfe 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/lib/tasks/gitlab.rake b/lib/tasks/gitlab.rake index b0b64ff48..2d53eaa0a 100644 --- a/lib/tasks/gitlab.rake +++ b/lib/tasks/gitlab.rake @@ -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