可以通过gitlab创建项目

This commit is contained in:
guange 2015-10-21 17:13:07 +08:00
parent 65e6444272
commit 11d1cac40d
10 changed files with 140 additions and 119 deletions

View File

@ -38,7 +38,8 @@ class ApplicationController < ActionController::Base
protect_from_forgery protect_from_forgery
def handle_unverified_request def handle_unverified_request
super super
cookies.delete(autologin_cookie_name) raise(ActionController::InvalidAuthenticityToken)
# cookies.delete(autologin_cookie_name)
end end
before_filter :find_first_page before_filter :find_first_page

View File

@ -115,74 +115,34 @@ update
} }
def create def create
if params[:repository_scm].to_s == 'Gitlab' attrs = pickup_extra_info
# add by nwb @repository = Repository.factory('Git')
# 增加对gitlab版本库的支持 @repository.safe_attributes = params[:repository]
attrs = pickup_extra_info if attrs[:attrs_extra].keys.any?
@repository = Repository.factory('Git') @repository.merge_extra_info(attrs[:attrs_extra])
@repository.safe_attributes = params[:repository] end
if attrs[:attrs_extra].keys.any? @repository.project = @project
@repository.merge_extra_info(attrs[:attrs_extra]) @repository.type = 'Repository::Gitlab'
end @repository.url = @repository.identifier
@repository.project = @project if request.post? && @repository.save
if request.post? && @repository.save g = ::Gitlab.client
redirect_to settings_project_url(@project, :tab => 'repositories') gid = @project.owner.gid
else gproject = g.create_project(@repository.identifier,
redirect_to settings_project_url(@project, :tab => 'repositories') path: @repository.identifier,
end description: @project.description,
else # 原逻辑 wiki_enabled: false,
##xianbo wall_enabled: false,
@root_path=RepositoriesHelper::ROOT_PATH issues_enabled: false,
@repository_name=User.current.login.to_s+"/"+params[:repository][:identifier]+".git" snippets_enabled: false,
@project_path=@root_path+"htdocs/"+@repository_name public: false,
@repository_tag=params[:repository][:upassword] || params[:repository][:password] user_id: gid
@repo_name=User.current.login.to_s+"_"+params[:repository][:identifier] )
logger.info "htpasswd -mb "+@root_path+"htdocs/user.passwd "+@repo_name+": "+@repository_tag @project.gpid = gproject.id
logger.info "the value of create repository"+@root_path+": "+@repository_name+": "+@project_path+": "+@repo_name @project.save!
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
if request.post? && @repository.save
if(params[:repository_scm]=="Git")
system "htpasswd -mb "+@root_path+"htdocs/user.passwd "+@repo_name+" "+@repository_tag
system "echo -e '"+@repo_name+"-write:"+
" "+@repo_name+"' >> "+@root_path+"htdocs/group.passwd"
system "mkdir "+@root_path+"htdocs/"+User.current.login.to_s
system "git init --bare "+@project_path
system "mv "+@project_path+"/hooks/post-update{.sample,}"
system "chmod a+x "+@project_path+"/hooks/post-update"
system "echo -e 'Allow from all \n Order Deny,Allow \n "+
"<Limit PUT POST DELETE PROPPATCH MKCOL COPY MOVE LOCK UNLOCK> \n"+
"Require group "+@repo_name+"-write \n "+
"</Limit> \n ' >> "+
@root_path+"htdocs/"+ @repository_name+"/.htaccess"
system "cd "+@project_path+" ;git update-server-info"
File.open(@project_path+"/hooks/post-update", "w+") do |f|
f.write(HOOK_TEMPLATE)
end
@repository.update_attributes(:login => User.current.login.to_s)
end
redirect_to settings_project_url(@project, :tab => 'repositories',:repository_error_message=>@repository.errors.full_messages)
else if(@repository_tag.blank?)
#render :action => 'newrepo', :layout =>'base_projects'
redirect_to settings_project_url(@project, :tab => 'repositories',:repository => "pswd_is_null",:repository_error_message=>@repository.errors.full_messages)
else
redirect_to settings_project_url(@project, :tab => 'repositories',:repository => @repository,:repository_error_message=>@repository.errors.full_messages)
end
end
redirect_to settings_project_url(@project, :tab => 'repositories')
else
redirect_to settings_project_url(@project, :tab => 'repositories',:repository_error_message=>@repository.errors.full_messages)
end end
end end
@ -266,7 +226,7 @@ update
end end
end end
unless @repository && @repository.type == 'Repository::Gitlab' unless @repository.gitlab?
# redirect_to to_gitlab_project_repository_path(@project, @repository) # redirect_to to_gitlab_project_repository_path(@project, @repository)
render :to_gitlab render :to_gitlab
return return

View File

@ -57,6 +57,11 @@ class Repository < ActiveRecord::Base
safe_attributes 'url', safe_attributes 'url',
:if => lambda {|repository, user| repository.new_record?} :if => lambda {|repository, user| repository.new_record?}
def gitlab?
self.type == 'Repository::Gitlab'
end
def repo_create_validation def repo_create_validation
unless Setting.enabled_scm.include?(self.class.name.demodulize) unless Setting.enabled_scm.include?(self.class.name.demodulize)
errors.add(:type, :invalid) errors.add(:type, :invalid)

View File

@ -73,13 +73,6 @@
<span class="c_grey"><%= l(:text_scm_command_not_available) %></span> <span class="c_grey"><%= l(:text_scm_command_not_available) %></span>
<% end %> <% end %>
</li> </li>
<% unless judge_main_repository(@project) %>
<li>
<label class="label02"><%=l(:field_repository_is_default)%></label>
<%= f.check_box :is_default, :label => "", :no_label => true %></p>
</li>
<% end %>
<li > <li >
<input type="text" style="display: none"/> <!--阻止表单自动填充 --> <input type="text" style="display: none"/> <!--阻止表单自动填充 -->
<input type="password" style="display: none"/> <!--阻止表单自动填充 --> <input type="password" style="display: none"/> <!--阻止表单自动填充 -->
@ -89,11 +82,6 @@
<span class="c_grey"><%=l(:text_length_between,:min=>1,:max=>254)<<l(:text_project_identifier_info) %></span> <span class="c_grey"><%=l(:text_length_between,:min=>1,:max=>254)<<l(:text_project_identifier_info) %></span>
<% end %> <% end %>
</li> </li>
<li >
<label class="label02"><span class="c_red">*</span><%=l(:label_password)%></label>
<%= f.password_field :upassword, :label=> "", :no_label => true%>
<span class="c_grey"><%= l(:label_upassword_info)%></span>
</li>
<div class="cl"></div> <div class="cl"></div>
</ul> </ul>
<a href="#" onclick="$('#repository-form').submit();" class="blue_btn fl ml110"><%=l(:button_save)%></a> <a href="#" onclick="$('#repository-form').submit();" class="blue_btn fl ml110"><%=l(:button_save)%></a>

View File

@ -31,12 +31,7 @@
<%= f.text_field :login, :size => 30 %> <%= f.text_field :login, :size => 30 %>
<input type="text" hidden="hidden"> <input type="text" hidden="hidden">
</p> </p>
<p>
<%= f.password_field :password, :size => 30, :name => 'ignore',
:value => ((@repository.new_record? || @repository.password.blank?) ? '' : ('x'*15)),
:onfocus => "this.value=''; this.name='repository[password]';",
:onchange => "this.name='repository[password]';" %>
</p>
</div> </div>
<!--Ended by tanxianbo--> <!--Ended by tanxianbo-->
<p> <p>

View File

@ -26,7 +26,6 @@ border:none
<em class="info error"><%= l(:text_scm_command_not_available) %></em> <em class="info error"><%= l(:text_scm_command_not_available) %></em>
<% end %> <% end %>
</p> </p>
<p><%= f.check_box :is_default, :label => :field_repository_is_default %></p>
<p> <p>
<input id="googleinputcache" size="30" type="text"> <input id="googleinputcache" size="30" type="text">
@ -36,8 +35,6 @@ border:none
<%= l(:text_repository_identifier_info).html_safe %></em> <%= l(:text_repository_identifier_info).html_safe %></em>
<% end %></p> <% end %></p>
<!-- <p><%#= f.text_field :url, :size => 60, :required => true,:readonly=>true, :class=>'textbg'%></p> --> <!-- <p><%#= f.text_field :url, :size => 60, :required => true,:readonly=>true, :class=>'textbg'%></p> -->
<p><%= f.password_field :upassword, :required =>true, :label=> :field_password %>
<em class="info"><%= l(:label_upassword_info)%></em></p>
</div> </div>
<p> <p>
<%= submit_tag(@repository.new_record? ? l(:button_create) : l(:button_save)) %> <%= submit_tag(@repository.new_record? ? l(:button_create) : l(:button_save)) %>

View File

@ -0,0 +1,37 @@
#coding=utf-8
module Trustie
module Gitlab
module Helper
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 add_user(user)
u = nil
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
end
change_password(u.id, user.hashed_password, user.salt)
rescue => e
puts e
end
return u
end
def del_user(user)
## gitlab unimplement
end
end
end
end

View File

@ -14,27 +14,41 @@ module Trustie
end end
def change_gitlab_member def change_gitlab_member
if self.member.project_id == 2 if isGitlabProject?
@g ||= ::Gitlab.client @g ||= ::Gitlab.client
@g.edit_team_member(11, self.member.user.gid, self.role.to_gitlab_role ) @g.edit_team_member(project.gpid, self.member.user.gid, self.role.to_gitlab_role )
end end
end end
def add_gitlab_member def add_gitlab_member
if self.member.project_id == 2 if isGitlabProject?
@g ||= ::Gitlab.client @g ||= ::Gitlab.client
@g.add_team_member(11, self.member.user.gid, self.role.to_gitlab_role ) @g.add_team_member(project.gpid, self.member.user.gid, self.role.to_gitlab_role )
end end
end end
def delete_gitlab_member def delete_gitlab_member
if member.roles.count <=1 if isGitlabProject?
if self.member.project_id == 2 if member.roles.count <=1
@g ||= ::Gitlab.client @g ||= ::Gitlab.client
@g.remove_team_member(11, self.member.user.gid) @g.remove_team_member(project.gpid, self.member.user.gid)
end end
end end
end end
private
def project
self.member.project
end
def repository
project.repository
end
def isGitlabProject?
repository && repository.gitlab?
end
end end
end end

View File

@ -0,0 +1,37 @@
#coding=utf-8
#
#
require_relative 'helper'
module Trustie
module Gitlab
module ManageUser
include Helper
def self.included(base)
base.class_eval {
before_create :add_gitlab_user
before_destroy :delete_gitlab_user
before_save :change_gitlab_user
}
end
def add_gitlab_user
add_user(self)
end
def delete_gitlab_user
del_user(self)
end
def change_gitlab_user
change_password(self.gid, self.hashed_password, self.salt)
end
private
def g
@g ||= ::Gitlab.client
end
end
end
end

View File

@ -1,3 +1,7 @@
#coding=utf-8
require_relative 'helper'
module Trustie module Trustie
module Gitlab module Gitlab
module UserLevel module UserLevel
@ -10,33 +14,17 @@ module Trustie
class Sync class Sync
attr :g attr :g
include Helper
def initialize def initialize
@g = ::Gitlab.client @g = ::Gitlab.client
end 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) def sync_user(user)
begin u = add_user(user)
u = self.g.get("/users?search=#{user.mail}").first user.save! if u
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 end
def sync_project(project, opt={}) def sync_project(project, opt={})
gid = project.owner.gid gid = project.owner.gid
raise "unknow gid" unless gid raise "unknow gid" unless gid
@ -48,7 +36,6 @@ module Trustie
if opt[:password] if opt[:password]
import_url.sub('@', ":#{opt[:password]}@") import_url.sub('@', ":#{opt[:password]}@")
end end
# import url http://xianbo_trustie2:1234@repository.trustie.net/xianbo/trustie2.git # import url http://xianbo_trustie2:1234@repository.trustie.net/xianbo/trustie2.git
# can use password # can use password
@ -80,7 +67,7 @@ module Trustie
def remove_project def remove_project
end end
end end
end end
end end