From b550e98603303739ca85a8bf4def548611dc1ffd Mon Sep 17 00:00:00 2001
From: nwb
Date: Wed, 23 Jul 2014 15:31:45 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E7=89=88=E6=9C=AC=E5=BA=93?=
=?UTF-8?q?=E9=80=BB=E8=BE=91=E6=9B=B4=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
app/controllers/repositories_controller.rb | 51 ++++++++++++++------
app/helpers/gitlab_helper.rb | 16 ++++++
app/views/repositories/_form_create.html.erb | 6 ++-
config/locales/en.yml | 6 +++
config/locales/zh.yml | 6 +++
config/settings.yml | 4 ++
6 files changed, 74 insertions(+), 15 deletions(-)
diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb
index dc05ce500..91582d8e2 100644
--- a/app/controllers/repositories_controller.rb
+++ b/app/controllers/repositories_controller.rb
@@ -97,32 +97,55 @@ class RepositoriesController < ApplicationController
if params[:repository_scm].to_s == 'Git'
# add by nwb
# 增加对gitlab版本库的支持
- # 判断gitlab用户是否存在,不存在则创建
+
+ # 数据检测
+ if params[:repository][:identifier] == ""
+ flash[:error] = l(:project_module_repository) + l(:blank)
+ render :action => 'newrepo', :layout => 'base_projects'
+ return
+ elsif params[:repository][:gitlab_user] == ""
+ flash[:error] = l(:label_user) + l(:blank)
+ render :action => 'newrepo', :layout => 'base_projects'
+ return
+ elsif params[:repository][:gitlab_email] == ""
+ flash[:error] = l(:field_mail) + l(:blank)
+ render :action => 'newrepo', :layout => 'base_projects'
+ return
+ elsif params[:repository][:upassword] == ""
+ flash[:error] = l(:label_password) + l(:blank)
+ render :action => 'newrepo', :layout => 'base_projects'
+ return
+ end
+
+ attrs = pickup_extra_info
+ @repository = Repository.factory('Git')
+ @repository.safe_attributes = params[:repository]
+ if attrs[:attrs_extra].keys.any?
+ @repository.merge_extra_info(attrs[:attrs_extra])
+ end
+ @repository.project = @project
+
+ # 判断gitlab用户是否存在,不存在则创建
user = User.current
gitlab_user = GitlabUser.find_by_user_id(user.id)
if gitlab_user.nil?
- if create_user(user.login,user.show_name,user.password,user.mail,user.id)
- else
+ 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)
+ render :action => 'newrepo', :layout =>'base_projects'
+ return
end
end
# 创建gitlab版本库
repos = params[:repository][:identifier]
- if create_project_for_user(repos,@project.id,gitlab_user.gitlab_user_id)
- # 创建成功
- else
+ if !create_project_for_user(repos,@project.id,gitlab_user.gitlab_user_id)
# 创建gitlab项目失败
+ flash[:error] = l(:error_create_gitlab_project)
+ render :action => 'newrepo', :layout =>'base_projects'
+ return
end
# 连接创建成功后的gitlab版本库
- attrs = pickup_extra_info
- @repository = Repository.factory('Git')
- @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
redirect_to settings_project_path(@project, :tab => 'repositories')
else
diff --git a/app/helpers/gitlab_helper.rb b/app/helpers/gitlab_helper.rb
index 4b3f753b7..a52ceea6b 100644
--- a/app/helpers/gitlab_helper.rb
+++ b/app/helpers/gitlab_helper.rb
@@ -14,6 +14,10 @@ module GitlabHelper
#REPO_IP_ADDRESS = "http://" + Setting.repository_domain
REPO_IP_ADDRESS = "http://192.168.137.100"
GITLAB_API = "/api/v3"
+ # gitlab的默认登录用户(需管理员)
+ LOGIN_USER = Setting.gitlab_login_user
+ # gitlab的默认登录密码
+ LOGIN_PASSWORD = Setting.gitlab_login_password
# 用户在项目中的权限级别
GUEST = 10
@@ -33,6 +37,12 @@ module GitlabHelper
Thread.current[:gitlab_token] ||= nil
end
+ def login_check
+ if GitlabHelper.gitlab_token.nil?
+ login_gitlab(GitlabHelper.LOGIN_USER,GitlabHelper.LOGIN_PASSWORD)
+ end
+ end
+
# 登录gitlab
# add by nwb
def login_gitlab(email,password)
@@ -57,6 +67,7 @@ module GitlabHelper
# 创建项目
# add by nwb
def create_project(project_name,project_id)
+ login_check
url = REPO_IP_ADDRESS + GITLAB_API + "/projects"
uri = URI.parse(url)
data = {name:project_name, private_token:GitlabHelper.gitlab_token}
@@ -92,6 +103,7 @@ module GitlabHelper
# project_id: 项目在trustie中的编号
# add by nwb
def create_project_for_user(project_name,project_id,user_id)
+ login_check
url = REPO_IP_ADDRESS + GITLAB_API + "/projects/user/" + user_id
uri = URI.parse(url)
data = {user_id:user_id, name:project_name,private_token:GitlabHelper.gitlab_token}
@@ -126,6 +138,7 @@ module GitlabHelper
# loginname:登录名称(版本库路径包含) name:用户姓名
# add by nwb
def create_user (loginname,name,password,email,userid)
+ login_check
url = REPO_IP_ADDRESS + GITLAB_API + "/users"
uri = URI.parse(url)
data = {email:email,password:password,username:loginname, name:name, private_token:GitlabHelper.gitlab_token}
@@ -156,6 +169,7 @@ module GitlabHelper
# user_id:用户在gitlab中的id
# add by nwb
def delete_user(user_id)
+ login_check
url = REPO_IP_ADDRESS + GITLAB_API + "/users/" + user_id
uri = URI.parse(url)
data = {id:user_id,private_token:GitlabHelper.gitlab_token}
@@ -188,6 +202,7 @@ module GitlabHelper
#MASTER = 40
# add by nwb
def add_user_to_project(project_id,user_id,access_level)
+ login_check
url = REPO_IP_ADDRESS + GITLAB_API + "/projects/" + project_id +"/members"
uri = URI.parse(url)
data = {id:project_id,user_id:user_id,access_level:access_level, private_token:GitlabHelper.gitlab_token}
@@ -208,6 +223,7 @@ module GitlabHelper
# project_id:项目在gitlab中的ids user_id:用户在gitlab中的id
# add by nwb
def delete_user_from_project(project_id,user_id)
+ login_check
url = REPO_IP_ADDRESS + GITLAB_API + "/projects/" + project_id +"/members/" + user_id
uri = URI.parse(url)
data = {id:project_id,user_id:user_id,private_token:GitlabHelper.gitlab_token}
diff --git a/app/views/repositories/_form_create.html.erb b/app/views/repositories/_form_create.html.erb
index 89ae6ffd0..fc327f69a 100644
--- a/app/views/repositories/_form_create.html.erb
+++ b/app/views/repositories/_form_create.html.erb
@@ -28,8 +28,12 @@ border:none
<%= l(:text_repository_identifier_info).html_safe %>
<% end %>
+ <%= f.text_field :gitlab_user, :required =>true, :label=> :field_account,:value=>User.current.login %>
+ <%= l(:label_gitlab_account)%>
+ <%= f.text_field :gitlab_email, :required => true, :label => :field_account, :value => User.current.mail %>
+ <%= l(:label_gitlab_email) %>
<%= f.password_field :upassword, :required =>true, :label=> :field_password %>
- <%= l(:label_upassword_info)%>
+ <%= l(:label_gitlab_password)%>
<%= submit_tag(@repository.new_record? ? l(:button_create) : l(:button_save)) %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 1e975decc..f4c1c7612 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -186,6 +186,9 @@ en:
notice_account_deleted: "Your account has been permanently deleted."
notice_user_successful_create: "User %{id} created."
+ error_create_gitlab_user: "Create Gitlab user failed."
+ error_create_gitlab_project: "Create Gitlab project failed."
+
error_class_period_only_num: "class period can only digital"
error_can_t_load_default_data: "Default configuration could not be loaded: %{value}"
error_scm_not_found: "The entry or revision was not found in the repository."
@@ -1363,6 +1366,9 @@ en:
label_all_revisions: All revisions:
label_repository_name: Repository name
label_upassword_info: The password can be shared in the group
+ label_gitlab_email: The email user to login gitlab
+ label_gitlab_account: The account user to login gitlab
+ label_gitlab_password: The password user to login gitlab
label_how_commit_code: How to commit code:
label_how_commit_code_chinese: Chinese
label_welcome_leave_message: Hi!The platform is currently in beta version.If you have any comments and suggestions, please
diff --git a/config/locales/zh.yml b/config/locales/zh.yml
index cd5dd630a..b11bfbd67 100644
--- a/config/locales/zh.yml
+++ b/config/locales/zh.yml
@@ -196,6 +196,9 @@ zh:
error_complete_occupation: "请您填写工作单位,否则本系统的部分功能将无法正常使用。"
+ error_create_gitlab_user: "创建Gitlab用户失败!可能已存在指定的用户名或邮件名。"
+ error_create_gitlab_project: "创建Gitlab项目失败!指定用户可能已拥有指定的Gitlab项目。"
+
error_class_period_only_num: "课程学时只能为数字"
error_can_t_load_default_data: "无法载入默认设置:%{value}"
error_scm_not_found: "版本库中不存在该条目和(或)其修订版本。"
@@ -1563,6 +1566,9 @@ zh:
label_have_message : 》有新的留言
label_all_revisions: 所有版本:
label_upassword_info: 该密码在项目组内可共享
+ label_gitlab_account: 用户登录Gitlab的账号,推荐使用当前用户
+ label_gitlab_email: 用户登录Gitlab的邮件,推荐使用当前用户邮件
+ label_gitlab_password: 用户登录Gitlab的密码,推荐使用当前用户密码
label_how_commit_code: 查看如何提交代码:
label_how_commit_code_chinese: 中文
# modified by bai
diff --git a/config/settings.yml b/config/settings.yml
index cffbaa5fa..07a7efde7 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -251,6 +251,10 @@ course_domain:
default: course.trustie.net
repository_domain:
default: repository.trustie.net
+gitlab_login_user:
+ default: admin@local.host
+gitlab_login_password:
+ default: 123456
plugin_redmine_ckeditor:
serialized: true
default: --- !ruby/hash:ActiveSupport::HashWithIndifferentAccess