1.支持已有gitlab账号在新trustie数据库连接gitlab

2.gitlab版本库管理、界面更新
GitlabVersion
nwb 11 years ago
parent 7fffbfaea5
commit 08676e1b6f

@ -77,6 +77,7 @@ class ProjectsController < ApplicationController
helper :words helper :words
### added by william ### added by william
include ActsAsTaggableOn::TagsHelper include ActsAsTaggableOn::TagsHelper
include GitlabHelper
def find_project_repository def find_project_repository
unless @project.repositories.nil? unless @project.repositories.nil?

@ -41,7 +41,7 @@ class RepositoriesController < ApplicationController
helper :repositories helper :repositories
include GitlabHelper include GitlabHelper
#@root_path = RepositoriesHelper::ROOT_PATH #@root_path = RepositoriesHelper::ROOT_PATH
rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed
def new def new
@ -100,6 +100,8 @@ class RepositoriesController < ApplicationController
if params[:repository_scm].to_s == 'Git' if params[:repository_scm].to_s == 'Git'
# add by nwb # add by nwb
# 增加对gitlab版本库的支持 # 增加对gitlab版本库的支持
user = User.current
@gitlab_user = GitlabUser.find_by_user_id(user.id)
attrs = pickup_extra_info attrs = pickup_extra_info
@repository = Repository.factory('Git') @repository = Repository.factory('Git')
@ -129,20 +131,23 @@ class RepositoriesController < ApplicationController
end end
# 判断gitlab用户是否存在不存在则创建 # 判断gitlab用户是否存在不存在则创建
user = User.current if @gitlab_user.nil?
gitlab_user = GitlabUser.find_by_user_id(user.id)
if gitlab_user.nil?
if !create_user(params[:repository][:gitlab_user],user.show_name,params[:repository][:upassword],params[:repository][:gitlab_email],user.id) if !create_user(params[:repository][:gitlab_user],user.show_name,params[:repository][:upassword],params[:repository][:gitlab_email],user.id)
# 创建gitlab用户失败 # 创建gitlab用户失败
flash[:error] = l(:error_create_gitlab_user) # 验证gitlab是否可由指定的账号、密码登录
render :action => 'newrepo', :layout =>'base_projects' if login_init_gitlab(params[:repository][:gitlab_email],params[:repository][:upassword],params[:repository][:gitlab_user].user.id)
return @gitlab_user = GitlabUser.find_by_user_id(user.id)
else
flash[:error] = l(:error_create_gitlab_user)
render :action => 'newrepo', :layout =>'base_projects'
return
end
end end
gitlab_user = GitlabUser.find_by_user_id(user.id) @gitlab_user = GitlabUser.find_by_user_id(user.id)
end end
# 创建gitlab版本库 # 创建gitlab版本库
repos = params[:repository][:identifier] repos = params[:repository][:identifier]
temp = create_project_for_user(repos,@project.id,gitlab_user.gitlab_user_id) temp = create_project_for_user(repos,@project.id,@gitlab_user.gitlab_user_id)
if !temp[:result] if !temp[:result]
# 创建gitlab项目失败 # 创建gitlab项目失败
flash[:error] = l(:error_create_gitlab_project) flash[:error] = l(:error_create_gitlab_project)

@ -7,11 +7,11 @@
module GitlabHelper module GitlabHelper
# gitlab版本库数据本地保存的根目录 # gitlab版本库数据本地保存的根目录
ROOT_PATH="/home/git/repositories/" ROOT_PATH="/home/git/repositories"
PROJECT_PATH_CUT = 40 PROJECT_PATH_CUT = 40
# gitlab版本库所在服务器 # gitlab版本库所在服务器
# 注意REPO_IP_ADDRESS必须以http://开头暂时只支持HTTP协议未支持SSH # 注意REPO_IP_ADDRESS必须以http://开头暂时只支持HTTP协议未支持SSH
#REPO_IP_ADDRESS = "http://" + Setting.repository_domain #REPO_IP_ADDRESS = "http://" + Setting.gitlab_server
REPO_IP_ADDRESS = "http://192.168.137.100" REPO_IP_ADDRESS = "http://192.168.137.100"
GITLAB_API = "/api/v3" GITLAB_API = "/api/v3"
# gitlab的默认登录用户需管理员 # gitlab的默认登录用户需管理员
@ -65,6 +65,41 @@ module GitlabHelper
end end
end end
# 登录并初始化gitlab用户
# 登录成功且本地未保存数据时保存
# email:登录gitlab用户邮件loginname登录gitlab用户账号, password 登录gitlab用户密码
# user_id:用户在trustie中的编号
# add by nwb
def login_init_gitlab(email,password,loginname,user_id)
url = REPO_IP_ADDRESS + GITLAB_API + "/session"
uri = URI.parse(url)
data = {email:email, password:password}
begin
res = Net::HTTP.post_form(uri, data)
# 判断返回数据
if res.code == '201'
temp = ActiveSupport::JSON.decode(res.body)
if temp['username'] == loginname
# 保存数据
gituser = GitlabUser.new
gituser.user_id = user_id
gituser.gitlab_user_id = temp['id']
gituser.email = email
gituser.password = password
gituser.login =loginname
gituser.save
return true
end
end
return false
rescue =>err
logger.error "gitlab: error: #{err.message}"
return false
end
end
# 创建项目 # 创建项目
# add by nwb # add by nwb
def create_project(project_name,project_id) def create_project(project_name,project_id)
@ -77,14 +112,14 @@ module GitlabHelper
if res.code == '201' if res.code == '201'
temp = ActiveSupport::JSON.decode(res.body) temp = ActiveSupport::JSON.decode(res.body)
#新创建项目的版本库地址 #新创建项目的版本库地址
repository_url = temp['http_url_to_repo'] repository_url = String.new(temp['http_url_to_repo'])
repository_url['http://localhost'] = REPO_IP_ADDRESS repository_url['http://localhost'] = REPO_IP_ADDRESS
# 新创建项目的web地址 # 新创建项目的web地址
web_url = temp['web_url'] web_url = String.new(temp['web_url'])
web_url['http://localhost'] = REPO_IP_ADDRESS web_url['http://localhost'] = REPO_IP_ADDRESS
# 新创建项目的本地文件路径 # 新创建项目的本地文件路径
localfile = temp['web_url'] localfile = String.new(temp['web_url'])
localfile['http://localhost'] = ROOT_PATH localfile[REPO_IP_ADDRESS] = ROOT_PATH
# 保存数据 # 保存数据
gitproject =GitlabProject.new gitproject =GitlabProject.new
@ -119,13 +154,13 @@ module GitlabHelper
if res.code == '201' if res.code == '201'
temp = ActiveSupport::JSON.decode(res.body) temp = ActiveSupport::JSON.decode(res.body)
#新创建项目的版本库地址 #新创建项目的版本库地址
repository_url = temp['http_url_to_repo'] repository_url = String.new(temp['http_url_to_repo'])
repository_url['http://localhost'] = REPO_IP_ADDRESS repository_url['http://localhost'] = REPO_IP_ADDRESS
# 新创建项目的web地址 # 新创建项目的web地址
web_url = temp['web_url'] web_url = String.new(temp['web_url'])
web_url['http://localhost'] = REPO_IP_ADDRESS web_url['http://localhost'] = REPO_IP_ADDRESS
# 新创建项目的本地文件路径 # 新创建项目的本地文件路径
localfile = temp['web_url'] localfile = String.new(temp['web_url'])
localfile['http://localhost'] = ROOT_PATH localfile['http://localhost'] = ROOT_PATH
# 保存数据 # 保存数据

@ -141,7 +141,7 @@ class Repository < ActiveRecord::Base
gitlabproject = GitlabProject.find_by_localfile_url(self.url) gitlabproject = GitlabProject.find_by_localfile_url(self.url)
if gitlabproject.nil? if gitlabproject.nil?
# 兼容老git模式 # 兼容老git模式
ip = RepositoriesHelper::REPO_IP_ADDRESS ip = Setting.gitlab_server
if ip.nil? if ip.nil?
self.url self.url
else else
@ -149,13 +149,23 @@ class Repository < ActiveRecord::Base
"http://" + self.login.to_s + self.identifier.to_s + ip + self.url.slice(project_path_cut, self.url.length) "http://" + self.login.to_s + self.identifier.to_s + ip + self.url.slice(project_path_cut, self.url.length)
end end
else else
self.url gitlabproject.repository_url
end end
else else
self.url self.url
end end
end end
# 版本库的web管理地址
def web_url
gitlabproject = GitlabProject.find_by_localfile_url(self.url)
if gitlabproject
gitlabproject.web_url
else
''
end
end
def <=>(repository) def <=>(repository)
if is_default? if is_default?
-1 -1

@ -7,8 +7,8 @@
<th><%= l(:field_identifier) %></th> <th><%= l(:field_identifier) %></th>
<th><%= l(:field_repository_is_default) %></th> <th><%= l(:field_repository_is_default) %></th>
<th><%= l(:label_scm) %></th> <th><%= l(:label_scm) %></th>
<th><%= l(:label_repository_path) %></th> <th style="width:250px"><%= l(:label_repository_path) %></th>
<th></th> <th style="width:250px"><%= l(:label_web_path) %></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -21,20 +21,21 @@
<td align="center"><%= checked_image repository.is_default? %></td> <td align="center"><%= checked_image repository.is_default? %></td>
<td><%=h repository.scm_name %></td> <td><%=h repository.scm_name %></td>
<td><%=h repository.repos_url %></td> <td><%=h repository.repos_url %></td>
<td><%=h repository.web_url %></td>
<td class="buttons" style="text-align:left"> </tr>
<tr>
<td></td> <td></td><td></td><td></td>
<td class="buttons" style="width:250px">
<% if repository.scm_name=="Subversion"%> <% if repository.scm_name=="Subversion"%>
<%if User.current.allowed_to?(:manage_repository, @project) %> <%if User.current.allowed_to?(:manage_repository, @project) %>
<%= link_to(l(:label_user_plural), committers_repository_path(repository), <%= link_to(l(:label_user_plural), committers_repository_path(repository),
:class => 'icon icon-user') %> :class => 'icon icon-user') %>
<%= link_to(l(:button_edit), edit_repository_path(repository), <%= link_to(l(:button_edit), edit_repository_path(repository),
:class => 'icon icon-edit') %> :class => 'icon icon-edit') %>
<%= delete_link repository_path(repository) %> <%= delete_link repository_path(repository) %>
<% end %> <% end %>
<% elsif repository.scm_name=="Git"%> <% elsif repository.scm_name=="Git"%>
<%if User.current.allowed_to?(:manage_repository, @project) %> <%if User.current.allowed_to?(:manage_repository, @project) %>
<!-- <%= link_to(l(:button_edit), edit_repository_path(repository),
:class => 'icon icon-edit') %> -->
<%= link_to(l(:label_user_plural), committers_repository_path(repository),:class => 'icon icon-user') %> <%= link_to(l(:label_user_plural), committers_repository_path(repository),:class => 'icon icon-user') %>
<%= link_to(l(:button_edit), edit_repository_path(repository),:class => 'icon icon-edit') %> <%= link_to(l(:button_edit), edit_repository_path(repository),:class => 'icon icon-edit') %>
<%# if repository.login.to_s==User.current.login.to_s %> <%# if repository.login.to_s==User.current.login.to_s %>
@ -45,6 +46,7 @@
<% end %> <% end %>
</td> </td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>

@ -205,6 +205,7 @@ default:
# that overrides the default ones # that overrides the default ones
production: production:
# specific configuration options for development environment # specific configuration options for development environment
# that overrides the default ones # that overrides the default ones
development: development:

@ -1348,6 +1348,7 @@ en:
label_join: join Trustie label_join: join Trustie
label_repository_new: link to existing SVN repository label_repository_new: link to existing SVN repository
label_repository_path: path of repository label_repository_path: path of repository
label_web_path: web management path
label_repository_new_repos: create a new repository label_repository_new_repos: create a new repository
label_repository_no: have no repository? label_repository_no: have no repository?
label_welcome_page_to: Participate %{project_count} projects label_welcome_page_to: Participate %{project_count} projects

@ -1549,6 +1549,7 @@ zh:
label_create_course_description: 课程小社区,创建新课程,让我们共同分享多到想不到的公共资源! label_create_course_description: 课程小社区,创建新课程,让我们共同分享多到想不到的公共资源!
label_welcome_page_to: 参与了 %{project_count} 个项目! label_welcome_page_to: 参与了 %{project_count} 个项目!
label_repository_path: 版本库路径 label_repository_path: 版本库路径
label_web_path: Web管理路径
label_repository_path_not_null: 库路径 不能为空字符 label_repository_path_not_null: 库路径 不能为空字符
label_password_not_null: 密码不能设置为空。 label_password_not_null: 密码不能设置为空。
label_exist_repository_path: 定义已有版本库URL路径定义格式file:///, http://, https://, svn:// label_exist_repository_path: 定义已有版本库URL路径定义格式file:///, http://, https://, svn://

@ -117,6 +117,13 @@ commit_logtime_enabled:
commit_logtime_activity_id: commit_logtime_activity_id:
format: int format: int
default: 0 default: 0
gitlab_server:
default: repository.trustie.net
gitlab_login_user:
default: admin@local.host
gitlab_login_password:
default: 123456
# autologin duration in days # autologin duration in days
# 0 means autologin is disabled # 0 means autologin is disabled
autologin: autologin:
@ -251,10 +258,6 @@ course_domain:
default: course.trustie.net default: course.trustie.net
repository_domain: repository_domain:
default: repository.trustie.net default: repository.trustie.net
gitlab_login_user:
default: admin@local.host
gitlab_login_password:
default: 123456
plugin_redmine_ckeditor: plugin_redmine_ckeditor:
serialized: true serialized: true
default: --- !ruby/hash:ActiveSupport::HashWithIndifferentAccess default: --- !ruby/hash:ActiveSupport::HashWithIndifferentAccess

Loading…
Cancel
Save