Compare commits

...

9 Commits

Author SHA1 Message Date
  huang 098fd6c276 Merge branch 'szzh' into gitlab 11 years ago
  huang f4be7b51e7 新建项目 11 years ago
  huang 5e034f74d8 项目同步(未完) 11 years ago
  huang c4d6ded1f6 Merge branch 'gitlab' of http://repository.trustie.net/xianbo/trustie2 into gitlab 11 years ago
  guange 006ed66d7a 新注册用户可以同步到gitlab 11 years ago
  huang 687186faf2 初始化 11 years ago
  huang cd10d90f13 版本库--统计页面的样式 11 years ago
  guange 3614c6ee9b project同步 11 years ago
  guange 797ed5081d 同步登录 11 years ago
11 changed files with 199 additions and 65 deletions
Unified View
  1. +1
    -0
      Gemfile
  2. +6
    -6
      app/controllers/repositories_controller.rb
  3. +23
    -0
      app/helpers/repositories_helper.rb
  4. +40
    -0
      app/models/project.rb
  5. +12
    -0
      app/models/user.rb
  6. +1
    -1
      app/views/projects/_development_group.html.erb
  7. +8
    -0
      config/application.rb
  8. +7
    -0
      config/initializers/gitlab_config.rb
  9. +5
    -0
      db/migrate/20150712063406_add_gitlab_user_id_to_users.rb
  10. +16
    -58
      db/schema.rb
  11. +80
    -0
      lib/tasks/gitlab.rake

+ 1
- 0
Gemfile View File

@@ -6,6 +6,7 @@ unless RUBY_PLATFORM =~ /w32/
gem 'iconv' gem 'iconv'
end end
gem 'gitlab'
gem 'rest-client' gem 'rest-client'
gem "mysql2", "= 0.3.18" gem "mysql2", "= 0.3.18"
gem 'redis-rails' gem 'redis-rails'


+ 6
- 6
app/controllers/repositories_controller.rb View File

@@ -251,12 +251,12 @@ update
end end
end end
if params[:to] == 'gitlab'
g = Gitlab.client
g.post('/session', body: {email: User.current.mail, password: User.current.hashed_password})
redirect_to "http://192.168.41.130:3000/gitlab-org/gitlab-shell/tree/master"
return
end
# if params[:to] == 'gitlab'
# g = Gitlab.client
# g.post('/session', body: {email: User.current.mail, password: User.current.hashed_password})
# redirect_to "http://192.168.41.130:3000/gitlab-org/gitlab-shell/tree/master"
# return
# end
#if( !User.current.member_of?(@project) || @project.hidden_repo) #if( !User.current.member_of?(@project) || @project.hidden_repo)
@repository.fetch_changesets if Setting.autofetch_changesets? && @path.empty? @repository.fetch_changesets if Setting.autofetch_changesets? && @path.empty?


+ 23
- 0
app/helpers/repositories_helper.rb View File

@@ -26,6 +26,29 @@ module RepositoriesHelper
PROJECT_PATH_CUT = 40 PROJECT_PATH_CUT = 40
REPO_IP_ADDRESS = Setting.host_repository REPO_IP_ADDRESS = Setting.host_repository


def sync_repo_gitlab
@root_path=RepositoriesHelper::ROOT_PATH
@repository_name=User.current.login.to_s+"/"+params[:repository][:identifier]+".git"
@project_path=@root_path+"htdocs/"+@repository_name
@repository_tag=params[:repository][:upassword] || params[:repository][:password]
@repo_name=User.current.login.to_s+"_"+params[:repository][:identifier]
logger.info "htpasswd -mb "+@root_path+"htdocs/user.passwd "+@repo_name+": "+@repository_tag
logger.info "the value of create repository"+@root_path+": "+@repository_name+": "+@project_path+": "+@repo_name
attrs = pickup_extra_info
if((@repository_tag!="")&&params[:repository_scm]=="Git")
params[:repository][:url]=@project_path
end
###xianbo
@repository = Repository.factory(params[:repository_scm])
@repository.safe_attributes = params[:repository]
if attrs[:attrs_extra].keys.any?
@repository.merge_extra_info(attrs[:attrs_extra])
end

@repository.project = @project
@repository.save
end

def format_revision(revision) def format_revision(revision)
if revision.respond_to? :format_identifier if revision.respond_to? :format_identifier
revision.format_identifier revision.format_identifier


+ 40
- 0
app/models/project.rb View File

@@ -134,6 +134,9 @@ class Project < ActiveRecord::Base
# 创建project之后默认创建一个board,之后的board去掉了board的概念 # 创建project之后默认创建一个board,之后的board去掉了board的概念
after_create :create_board_sync,:acts_as_forge_activities after_create :create_board_sync,:acts_as_forge_activities
before_destroy :delete_all_members before_destroy :delete_all_members
# 同步项目
before_create :sync_gitlab_project
def remove_references_before_destroy def remove_references_before_destroy
return if self.id.nil? return if self.id.nil?
Watcher.delete_all ['watchable_id = ?', id] Watcher.delete_all ['watchable_id = ?', id]
@@ -849,7 +852,42 @@ class Project < ActiveRecord::Base
end end
end end
def owner
User.find(self.user_id)
end
private private
def sync_gitlab_project
project = self
# sync_repo_gitlab
g = Gitlab.client
gid = project.owner.id
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, # home/pdl/...htdocs/xianbo/trustie.git
description: project.description,
default_branch: true,
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
project.members.each do |m|
g.add_team_member(gproject.id, m.user.gid, DEVELOPER)
end
end
def after_parent_changed(parent_was) def after_parent_changed(parent_was)
remove_inherited_member_roles remove_inherited_member_roles
@@ -1163,5 +1201,7 @@ class Project < ActiveRecord::Base
:forge_act_id => self.id,:forge_act_type => "ProjectCreateInfo") :forge_act_id => self.id,:forge_act_type => "ProjectCreateInfo")
fa.save! fa.save!
end end
end end

+ 12
- 0
app/models/user.rb View File

@@ -203,6 +203,7 @@ class User < Principal
# added by fq # added by fq
after_create :act_as_activity after_create :act_as_activity
# end # end
before_create :sync_gitlab_user
scope :in_group, lambda {|group| scope :in_group, lambda {|group|
group_id = group.is_a?(Group) ? group.id : group.to_i group_id = group.is_a?(Group) ? group.id : group.to_i
@@ -994,6 +995,17 @@ class User < Principal
end end
private
def sync_gitlab_user
user = self
g = Gitlab.client
u = g.get("/users?search=#{user.mail}").first
unless u
u = g.create_user(user.mail, user.password, name: user.show_name, username: user.login, confirm: "true")
self.gid = u.id
puts "create user #{user.login}"
end
end
end end


+ 1
- 1
app/views/projects/_development_group.html.erb View File

@@ -41,7 +41,7 @@
<%# --版本库被设置成私有、module中设置不显示、没有创建版本库 三种情况不显示-- %> <%# --版本库被设置成私有、module中设置不显示、没有创建版本库 三种情况不显示-- %>
<% if visible_repository?(@project) %> <% if visible_repository?(@project) %>
<div class="subNav"> <div class="subNav">
<%= link_to l(:project_module_repository), {:controller => 'repositories', :action => 'show', :id => @project.id}, :class => "f14 c_blue02" %>
<%= link_to l(:project_module_repository), {:controller => 'repositories', :action => 'show', :id => @project.id, to: 'gitlab'}, :class => "f14 c_blue02" %>
<a class="subnav_num">(<%= @project.repositories.count %>)</a> <a class="subnav_num">(<%= @project.repositories.count %>)</a>
</div> </div>
<% end %> <% end %>


+ 8
- 0
config/application.rb View File

@@ -69,6 +69,14 @@ module RedmineApp


config.action_view.sanitized_allowed_tags = 'div', 'p', 'span', 'img', 'embed' config.action_view.sanitized_allowed_tags = 'div', 'p', 'span', 'img', 'embed'


config.before_initialize do
puts "before initialize"
end

config.after_initialize do
puts "after initialize"
end

if File.exists?(File.join(File.dirname(__FILE__), 'additional_environment.rb')) if File.exists?(File.join(File.dirname(__FILE__), 'additional_environment.rb'))
instance_eval File.read(File.join(File.dirname(__FILE__), 'additional_environment.rb')) instance_eval File.read(File.join(File.dirname(__FILE__), 'additional_environment.rb'))
end end


+ 7
- 0
config/initializers/gitlab_config.rb View File

@@ -0,0 +1,7 @@
Gitlab.configure do |config|
config.endpoint = 'http://git.trustie.net/api/v3' # API endpoint URL, default: ENV['GITLAB_API_ENDPOINT']
config.private_token = 'fxm19wjRAs4REgTJwgtn' # user's private token, default: ENV['GITLAB_API_PRIVATE_TOKEN']
# Optional
# config.user_agent = 'Custom User Agent' # user agent, default: 'Gitlab Ruby Gem [version]'
# config.sudo = 'user' # username for sudo mode, default: nil
end

+ 5
- 0
db/migrate/20150712063406_add_gitlab_user_id_to_users.rb View File

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

+ 16
- 58
db/schema.rb View File

@@ -11,14 +11,12 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.


ActiveRecord::Schema.define(:version => 20150719092427) do
ActiveRecord::Schema.define(:version => 20150712063406) do


create_table "activities", :force => true do |t| create_table "activities", :force => true do |t|
t.integer "act_id", :null => false
t.string "act_type", :null => false
t.integer "user_id", :null => false
t.integer "activity_container_id"
t.string "activity_container_type", :default => ""
t.integer "act_id", :null => false
t.string "act_type", :null => false
t.integer "user_id", :null => false
end end


add_index "activities", ["act_id", "act_type"], :name => "index_activities_on_act_id_and_act_type" add_index "activities", ["act_id", "act_type"], :name => "index_activities_on_act_id_and_act_type"
@@ -475,25 +473,6 @@ ActiveRecord::Schema.define(:version => 20150719092427) 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", :force => true do |t|
t.string "IPLineCode"
t.string "Description"
t.string "Num"
t.string "Variable"
t.string "TraceInfo"
t.string "Method"
t.string "File"
t.string "IPLine"
t.string "Review"
t.string "Category"
t.string "Defect"
t.string "PreConditions"
t.string "StartLine"
t.integer "project_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "enabled_modules", :force => true do |t| create_table "enabled_modules", :force => true do |t|
t.integer "project_id" t.integer "project_id"
t.string "name", :null => false t.string "name", :null => false
@@ -607,10 +586,8 @@ ActiveRecord::Schema.define(:version => 20150719092427) do
t.string "language" t.string "language"
t.text "standard_code", :limit => 2147483647 t.text "standard_code", :limit => 2147483647
t.integer "homework_common_id" t.integer "homework_common_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.float "ta_proportion", :default => 0.1
t.integer "question_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end end


create_table "homework_evaluations", :force => true do |t| create_table "homework_evaluations", :force => true do |t|
@@ -1242,28 +1219,20 @@ ActiveRecord::Schema.define(:version => 20150719092427) do
t.integer "project_id" t.integer "project_id"
end end


create_table "student_work_tests", :force => true do |t|
t.integer "student_work_id"
t.integer "homework_test_id"
t.integer "result"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "student_works", :force => true do |t| create_table "student_works", :force => true do |t|
t.string "name" t.string "name"
t.text "description", :limit => 2147483647
t.text "description"
t.integer "homework_common_id" t.integer "homework_common_id"
t.integer "user_id" t.integer "user_id"
t.float "final_score" t.float "final_score"
t.float "teacher_score" t.float "teacher_score"
t.float "student_score" t.float "student_score"
t.float "teaching_asistant_score" t.float "teaching_asistant_score"
t.integer "project_id", :default => 0
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "late_penalty", :default => 0
t.integer "absence_penalty", :default => 0
t.integer "project_id", :default => 0
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "late_penalty", :default => 0
t.integer "absence_penalty", :default => 0
end end


create_table "student_works_evaluation_distributions", :force => true do |t| create_table "student_works_evaluation_distributions", :force => true do |t|
@@ -1361,7 +1330,7 @@ ActiveRecord::Schema.define(:version => 20150719092427) do
end end


create_table "user_extensions", :force => true do |t| create_table "user_extensions", :force => true do |t|
t.integer "user_id", :null => false
t.integer "user_id", :null => false
t.date "birthday" t.date "birthday"
t.string "brief_introduction" t.string "brief_introduction"
t.integer "gender" t.integer "gender"
@@ -1369,8 +1338,8 @@ ActiveRecord::Schema.define(:version => 20150719092427) do
t.string "occupation" t.string "occupation"
t.integer "work_experience" t.integer "work_experience"
t.integer "zip_code" t.integer "zip_code"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "technical_title" t.string "technical_title"
t.integer "identity" t.integer "identity"
t.string "student_id" t.string "student_id"
@@ -1378,7 +1347,6 @@ ActiveRecord::Schema.define(:version => 20150719092427) do
t.string "student_realname" t.string "student_realname"
t.string "location_city" t.string "location_city"
t.integer "school_id" t.integer "school_id"
t.string "description", :default => ""
end end


create_table "user_grades", :force => true do |t| create_table "user_grades", :force => true do |t|
@@ -1463,6 +1431,7 @@ ActiveRecord::Schema.define(:version => 20150719092427) do
t.string "identity_url" t.string "identity_url"
t.string "mail_notification", :default => "", :null => false t.string "mail_notification", :default => "", :null => false
t.string "salt", :limit => 64 t.string "salt", :limit => 64
t.integer "gid"
end end


add_index "users", ["auth_source_id"], :name => "index_users_on_auth_source_id" add_index "users", ["auth_source_id"], :name => "index_users_on_auth_source_id"
@@ -1484,17 +1453,6 @@ ActiveRecord::Schema.define(:version => 20150719092427) do
add_index "versions", ["project_id"], :name => "versions_project_id" add_index "versions", ["project_id"], :name => "versions_project_id"
add_index "versions", ["sharing"], :name => "index_versions_on_sharing" add_index "versions", ["sharing"], :name => "index_versions_on_sharing"


create_table "visitors", :force => true do |t|
t.integer "user_id"
t.integer "master_id"
t.datetime "updated_on"
t.datetime "created_on"
end

add_index "visitors", ["master_id"], :name => "index_visitors_master_id"
add_index "visitors", ["updated_on"], :name => "index_visitors_updated_on"
add_index "visitors", ["user_id"], :name => "index_visitors_user_id"

create_table "watchers", :force => true do |t| create_table "watchers", :force => true do |t|
t.string "watchable_type", :default => "", :null => false t.string "watchable_type", :default => "", :null => false
t.integer "watchable_id", :default => 0, :null => false t.integer "watchable_id", :default => 0, :null => false


+ 80
- 0
lib/tasks/gitlab.rake View File

@@ -0,0 +1,80 @@
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|
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
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
# 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'
)

# 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

Loading…
Cancel
Save