diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 8f90dd65b..e9c6df64e 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -32,9 +32,9 @@ 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] + 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 ] @@ -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,16 +266,22 @@ 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? - 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 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/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 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 diff --git a/db/schema.rb b/db/schema.rb index 9432cff39..de210ce22 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,4 +1,3 @@ -<<<<<<< HEAD # encoding: UTF-8 # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to @@ -1313,9 +1312,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 @@ -3443,4 +3442,3 @@ ActiveRecord::Schema.define(:version => 20151014023806) do end end ->>>>>>> szzh diff --git a/lib/redmine/scm/adapters/gitlab_adapter.rb b/lib/redmine/scm/adapters/gitlab_adapter.rb index cad6583f6..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 = 11 + @project = Repository.find_by_url(url).project.gpid @path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding end diff --git a/lib/tasks/gitlab.rake b/lib/tasks/gitlab.rake index 0f6f78bb6..ffa81e912 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| - 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 = Trustie::Gitlab::Sync.new + User.find_each do |user| + 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 - 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 = Trustie::Gitlab::Sync.new + Project.where(id: ENV["PROJECT_ID"]).find_each do |project| + s.sync_project(project, path: ENV["REP_NAME"], import_url: project.repository.url) + 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..597e01a36 --- /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 + 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