方法重构

This commit is contained in:
guange 2015-10-16 16:57:45 +08:00
parent 9266eccdff
commit 9e8e77f3e1
3 changed files with 110 additions and 83 deletions

View File

@ -497,27 +497,6 @@ ActiveRecord::Schema.define(:version => 20151013023237) do
add_index "documents", ["created_on"], :name => "index_documents_on_created_on" add_index "documents", ["created_on"], :name => "index_documents_on_created_on"
add_index "documents", ["project_id"], :name => "documents_project_id" 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| create_table "dts", :force => true do |t|
t.string "IPLineCode" t.string "IPLineCode"
t.string "Description" t.string "Description"
@ -1330,9 +1309,9 @@ ActiveRecord::Schema.define(:version => 20151013023237) do
create_table "student_work_tests", :force => true do |t| create_table "student_work_tests", :force => true do |t|
t.integer "student_work_id" t.integer "student_work_id"
t.datetime "created_at", :null => false t.integer "status"
t.datetime "updated_at", :null => false t.datetime "created_at", :null => false
t.integer "status", :default => 9 t.datetime "updated_at", :null => false
t.text "results" t.text "results"
t.text "src" t.text "src"
end end

View File

@ -1,80 +1,42 @@
require 'trustie/gitlab/sync'
namespace :gitlab do namespace :gitlab do
namespace :sync 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" desc "sync users to gitlab"
task :users => :environment do task :users => :environment do
# User.where(username: 'root').find_each do |user| # User.where(username: 'root').find_each do |user|
s = Trustie::Gitlab::Sync.new
User.where(login: 'guange1').find_each do |user| User.where(login: 'guange1').find_each do |user|
begin s.sync_user(user)
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
end end
end end
desc "update user password" desc "update user password"
task :password => :environment do task :password => :environment do
Helper.change_password(1,'5188b7a65acf294ee7deceb397b6f9c62214ea50','dcb8d9fffabec60c2d0d1030b679fbbb') s = Trustie::Gitlab::Sync.new
s.change_password(1,'5188b7a65acf294ee7deceb397b6f9c62214ea50','dcb8d9fffabec60c2d0d1030b679fbbb')
end end
desc "sync projects to gitlab" desc "sync projects to gitlab"
task :projects => :environment do task :projects => :environment do
s = Trustie::Gitlab::Sync.new
Project.where(id: 505).find_each do |project| Project.where(id: 505).find_each do |project|
g = Gitlab.client s.sync_project(project, path: 'trustie', import_url: 'http://xianbo_trustie2:1234@repository.trustie.net/xianbo/trustie2.git')
gid = project.owner.gid end
raise "unknow gid" unless gid end
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
project.members.each do |m| desc "remove all projects"
g.add_team_member(gproject.id, m.user.gid, DEVELOPER) 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 end
end end

View File

@ -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