From 9e8e77f3e1595188d5ab5f42523feff925c1f9c4 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Fri, 16 Oct 2015 16:57:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B9=E6=B3=95=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/schema.rb | 27 ++---------- lib/tasks/gitlab.rake | 80 ++++++++++------------------------- lib/trustie/gitlab/sync.rb | 86 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 83 deletions(-) create mode 100644 lib/trustie/gitlab/sync.rb diff --git a/db/schema.rb b/db/schema.rb index 78d0c5fd6..e1d43a74d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -497,27 +497,6 @@ ActiveRecord::Schema.define(:version => 20151013023237) do add_index "documents", ["created_on"], :name => "index_documents_on_created_on" add_index "documents", ["project_id"], :name => "documents_project_id" - create_table "dts", :primary_key => "Num", :force => true do |t| - t.string "Defect", :limit => 50 - t.string "Category", :limit => 50 - t.string "File" - t.string "Method" - t.string "Module", :limit => 20 - t.string "Variable", :limit => 50 - t.integer "StartLine" - t.integer "IPLine" - t.string "IPLineCode", :limit => 200 - t.string "Judge", :limit => 15 - t.integer "Review", :limit => 1 - t.string "Description" - t.text "PreConditions", :limit => 2147483647 - t.text "TraceInfo", :limit => 2147483647 - t.text "Code", :limit => 2147483647 - t.integer "project_id" - t.datetime "created_at" - t.datetime "updated_at" - t.integer "id", :null => false -======= create_table "dts", :force => true do |t| t.string "IPLineCode" t.string "Description" @@ -1330,9 +1309,9 @@ ActiveRecord::Schema.define(:version => 20151013023237) do create_table "student_work_tests", :force => true do |t| t.integer "student_work_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false - t.integer "status", :default => 9 + t.integer "status" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.text "results" t.text "src" end diff --git a/lib/tasks/gitlab.rake b/lib/tasks/gitlab.rake index 0f6f78bb6..e21b5f329 100644 --- a/lib/tasks/gitlab.rake +++ b/lib/tasks/gitlab.rake @@ -1,80 +1,42 @@ +require 'trustie/gitlab/sync' + namespace :gitlab do namespace :sync do - - module Helper - def self.change_password(uid, en_pwd, salt) - g = Gitlab.client - options = {:encrypted_password=>en_pwd, :password_salt=>salt} - g.put("/users/ext/#{uid}", :body => options) - # 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| + s = Trustie::Gitlab::Sync.new 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, confirm: "true") - 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 + s.sync_user(user) end end desc "update user password" task :password => :environment do - Helper.change_password(1,'5188b7a65acf294ee7deceb397b6f9c62214ea50','dcb8d9fffabec60c2d0d1030b679fbbb') + s = Trustie::Gitlab::Sync.new + s.change_password(1,'5188b7a65acf294ee7deceb397b6f9c62214ea50','dcb8d9fffabec60c2d0d1030b679fbbb') end - desc "sync projects to gitlab" task :projects => :environment do + s = Trustie::Gitlab::Sync.new 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 - - # import url http://xianbo_trustie2:1234@repository.trustie.net/xianbo/trustie2.git - # can use password - 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' - ) - project.gpid = gproject.id - project.save! - puts "Successfully created #{project.name}" - # add team members - # - GUEST = 10 - REPORTER = 20 - DEVELOPER = 30 - MASTER = 40 - OWNER = 50 + s.sync_project(project, path: 'trustie', import_url: 'http://xianbo_trustie2:1234@repository.trustie.net/xianbo/trustie2.git') + end + end - project.members.each do |m| - g.add_team_member(gproject.id, m.user.gid, DEVELOPER) + desc "remove all projects" + task :remove_all_projects => :environment do + g = Gitlab.client + 100.times do + g.projects(scope: 'all').each do |p| + puts p.id + begin + g.delete_project(p.id) + rescue => e + puts e end - + end end end diff --git a/lib/trustie/gitlab/sync.rb b/lib/trustie/gitlab/sync.rb new file mode 100644 index 000000000..91c241cea --- /dev/null +++ b/lib/trustie/gitlab/sync.rb @@ -0,0 +1,86 @@ +module Trustie + module Gitlab + module UserLevel + GUEST = 10 + REPORTER = 20 + DEVELOPER = 30 + MASTER = 40 + OWNER = 50 + end + + class Sync + attr :g + + def initialize + @g = ::Gitlab.client + end + + def change_password(uid, en_pwd, salt) + options = {:encrypted_password=>en_pwd, :password_salt=>salt} + self.g.put("/users/ext/#{uid}", :body => options) + # g.edit_user(uid, :encrypted_password=>en_pwd, :password_salt=>salt) + end + + def sync_user(user) + begin + u = self.g.get("/users?search=#{user.mail}").first + unless u + u = self.g.create_user(user.mail, user.hashed_password, name: user.show_name, username: user.login, confirm: "true") + 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 + + + def sync_project(project, opt={}) + gid = project.owner.gid + 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 + self.g.add_team_member(gproject.id, m.user.gid, UserLevel::DEVELOPER) + rescue => e + puts e + end + end + end + + def remove_project + end + + end + end +end \ No newline at end of file