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 01/10] =?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 From 765d2b875d5779a2bee611c694185e4a8901a919 Mon Sep 17 00:00:00 2001 From: huang Date: Fri, 16 Oct 2015 20:06:03 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E8=BF=98=E5=8E=9F=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/repositories_controller.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 3552c0624..72af09448 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -259,13 +259,13 @@ update #if( !User.current.member_of?(@project) || @project.hidden_repo) @repository.fetch_changesets if Setting.autofetch_changesets? && @path.empty? - g = Gitlab.client - project = g.project(11) + #g = Gitlab.client + #project = g.project(11) # rr = g.trees(project.id, @path) # r = g.get ("/projects/#{@project}/repository/tree") # :name, :path, :kind, :size, :lastrev, :changeset - # @entries = @repository.entries(@path, @rev) - @entries = g.trees(project.id, @path) + @entries = @repository.entries(@path, @rev) + #@entries = g.trees(project.id, @path) @changeset = @repository.find_changeset_by_name(@rev) #@project_path_cut = RepositoriesHelper::PROJECT_PATH_CUT From 3a21e9f2b035552da23217275b75503213083cab Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Sun, 18 Oct 2015 13:16:58 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/gitlab.rake | 2 +- lib/trustie/gitlab/sync.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tasks/gitlab.rake b/lib/tasks/gitlab.rake index e21b5f329..9529ce4c2 100644 --- a/lib/tasks/gitlab.rake +++ b/lib/tasks/gitlab.rake @@ -6,7 +6,7 @@ namespace :gitlab do 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| + User.find_each do |user| s.sync_user(user) end end diff --git a/lib/trustie/gitlab/sync.rb b/lib/trustie/gitlab/sync.rb index 91c241cea..597e01a36 100644 --- a/lib/trustie/gitlab/sync.rb +++ b/lib/trustie/gitlab/sync.rb @@ -30,7 +30,7 @@ module Trustie user.save! puts "create user #{user.login}" end - Helper.change_password(u.id, user.hashed_password, user.salt) + change_password(u.id, user.hashed_password, user.salt) rescue => e puts e end From b80a0d6366b44431a1a3fb41f7bf5bf6d86d07b9 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Sun, 18 Oct 2015 13:25:10 +0800 Subject: [PATCH 04/10] . --- lib/tasks/gitlab.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/gitlab.rake b/lib/tasks/gitlab.rake index 9529ce4c2..348e7ffed 100644 --- a/lib/tasks/gitlab.rake +++ b/lib/tasks/gitlab.rake @@ -20,7 +20,7 @@ namespace :gitlab do desc "sync projects to gitlab" task :projects => :environment do s = Trustie::Gitlab::Sync.new - Project.where(id: 505).find_each do |project| + Project.where(id: ENV["PROJECT_ID"]).find_each do |project| s.sync_project(project, path: 'trustie', import_url: 'http://xianbo_trustie2:1234@repository.trustie.net/xianbo/trustie2.git') end end From 07084a94c5796e9f6e253d0ddb2190414486a0f5 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Sun, 18 Oct 2015 13:50:59 +0800 Subject: [PATCH 05/10] . --- lib/tasks/gitlab.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/gitlab.rake b/lib/tasks/gitlab.rake index 348e7ffed..ffa81e912 100644 --- a/lib/tasks/gitlab.rake +++ b/lib/tasks/gitlab.rake @@ -21,7 +21,7 @@ namespace :gitlab do task :projects => :environment do s = Trustie::Gitlab::Sync.new Project.where(id: ENV["PROJECT_ID"]).find_each do |project| - s.sync_project(project, path: 'trustie', import_url: 'http://xianbo_trustie2:1234@repository.trustie.net/xianbo/trustie2.git') + s.sync_project(project, path: ENV["REP_NAME"], import_url: project.repository.url) end end From f1af041f24388400f7b98e3769d5254f2fe59a20 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Sun, 18 Oct 2015 13:56:10 +0800 Subject: [PATCH 06/10] =?UTF-8?q?gitlab=20key=20=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/initializers/gitlab_config.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/initializers/gitlab_config.rb b/config/initializers/gitlab_config.rb index 61fc12287..75edc8eff 100644 --- a/config/initializers/gitlab_config.rb +++ b/config/initializers/gitlab_config.rb @@ -2,7 +2,7 @@ Gitlab.configure do |config| # config.endpoint = 'http://192.168.41.130:3000/trustie/api/v3' # API endpoint URL, default: ENV['GITLAB_API_ENDPOINT'] # config.private_token = 'cK15gUDwvt8EEkzwQ_63' # user's private token, default: ENV['GITLAB_API_PRIVATE_TOKEN'] config.endpoint = 'http://git.trustie.net/trustie/api/v3' # API endpoint URL, default: ENV['GITLAB_API_ENDPOINT'] - config.private_token = 'fxm19wjRAs4REgTJwgtn' # user's private token, default: ENV['GITLAB_API_PRIVATE_TOKEN'] + config.private_token = 'fPc_gBmEiSANve8TCfxW' # 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 From acbd690bc651e266066a1c82a8a0f7d796c8f780 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Sun, 18 Oct 2015 21:04:04 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/repositories_controller.rb | 18 +++++++++++++++++- app/views/repositories/to_gitlab.html.erb | 4 ++++ config/routes.rb | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 app/views/repositories/to_gitlab.html.erb diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 72af09448..492efbf75 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -32,7 +32,7 @@ class RepositoriesController < ApplicationController before_filter :find_project_by_project_id, :only => [:new, :create, :newrepo] before_filter :find_repository, :only => [:edit, :update, :destroy, :committers] - before_filter :find_project_repository, :except => [:new, :create, :newcreate, :edit, :update, :destroy, :committers, :newrepo] + before_filter :find_project_repository, :except => [:new, :create, :newcreate, :edit, :update, :destroy, :committers, :newrepo,:to_gitlab] before_filter :find_changeset, :only => [:revision, :add_related_issue, :remove_related_issue] before_filter :authorize , :except => [:newrepo,:newcreate,:fork] accept_rss_auth :revisions @@ -247,6 +247,16 @@ update redirect_to settings_project_url(@project, :tab => 'repositories') end + def to_gitlab + @project = Project.find(params[:project_id]) + @repository = Repository.find(params[:id]) + s = Trustie::Gitlab::Sync.new + s.sync_project(@project, path: params[:repo_name], import_url: @repository.url) + @repository.type = 'Repository::Gitlab' + @repository.save + redirect_to :controller => 'repositories', :action => 'show', :id => @project.id, to: 'gitlab' + end + def show ## TODO: the below will move to filter, done. if !User.current.member_of?(@project) @@ -256,6 +266,12 @@ update end end + unless @repository && @repository.type == 'Repository::Gitlab' + # redirect_to to_gitlab_project_repository_path(@project, @repository) + render :to_gitlab + return + end + #if( !User.current.member_of?(@project) || @project.hidden_repo) @repository.fetch_changesets if Setting.autofetch_changesets? && @path.empty? diff --git a/app/views/repositories/to_gitlab.html.erb b/app/views/repositories/to_gitlab.html.erb new file mode 100644 index 000000000..eccf7259d --- /dev/null +++ b/app/views/repositories/to_gitlab.html.erb @@ -0,0 +1,4 @@ +<%= form_for(@repository, url: to_gitlab_project_repository_path(@project, @repository)) do |f| %> + + +<% end %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 810c22e7a..7a07c86db 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -533,6 +533,7 @@ RedmineApp::Application.routes.draw do resources :repositories, :except => [:index, :show] do member do get 'newrepo', :via => [:get, :post] + put 'to_gitlab' # get 'create', :via=>[:get, :post] end end From 74fd461facbc06d95b8583f855ddbf26e994f97f Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Sun, 18 Oct 2015 21:16:03 +0800 Subject: [PATCH 08/10] . --- app/controllers/repositories_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 492efbf75..442f6d22c 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -34,7 +34,7 @@ class RepositoriesController < ApplicationController before_filter :find_repository, :only => [:edit, :update, :destroy, :committers] before_filter :find_project_repository, :except => [:new, :create, :newcreate, :edit, :update, :destroy, :committers, :newrepo,:to_gitlab] before_filter :find_changeset, :only => [:revision, :add_related_issue, :remove_related_issue] - before_filter :authorize , :except => [:newrepo,:newcreate,:fork] + before_filter :authorize , :except => [:newrepo,:newcreate,:fork, :to_gitlab] accept_rss_auth :revisions # hidden repositories filter // 隐藏代码过滤器 before_filter :check_hidden_repo, :only => [:show, :stats, :revisions, :revision, :diff ] From 98bd46ceb4b7f975c39e174f2b79b09c00c4bbe9 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Sun, 18 Oct 2015 21:24:59 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E9=80=9A=E8=BF=87url=E8=AF=86=E5=88=ABpr?= =?UTF-8?q?oject?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/redmine/scm/adapters/gitlab_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/redmine/scm/adapters/gitlab_adapter.rb b/lib/redmine/scm/adapters/gitlab_adapter.rb index cad6583f6..449de7ede 100644 --- a/lib/redmine/scm/adapters/gitlab_adapter.rb +++ b/lib/redmine/scm/adapters/gitlab_adapter.rb @@ -16,7 +16,7 @@ module Redmine def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil) super @g = Gitlab.client - @project = 11 + @project = Project.find_by_url(url).gpid @path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding end From 65e6444272547292621ae60e52c2295457f00380 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Sun, 18 Oct 2015 21:31:00 +0800 Subject: [PATCH 10/10] . --- lib/redmine/scm/adapters/gitlab_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/redmine/scm/adapters/gitlab_adapter.rb b/lib/redmine/scm/adapters/gitlab_adapter.rb index 449de7ede..404243c89 100644 --- a/lib/redmine/scm/adapters/gitlab_adapter.rb +++ b/lib/redmine/scm/adapters/gitlab_adapter.rb @@ -16,7 +16,7 @@ module Redmine def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil) super @g = Gitlab.client - @project = Project.find_by_url(url).gpid + @project = Repository.find_by_url(url).project.gpid @path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding end