From 08676e1b6fd1480755dfa93e8bdeae1b03e38c03 Mon Sep 17 00:00:00 2001 From: nwb Date: Fri, 25 Jul 2014 11:24:51 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=94=AF=E6=8C=81=E5=B7=B2=E6=9C=89gitlab?= =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E5=9C=A8=E6=96=B0trustie=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E8=BF=9E=E6=8E=A5gitlab=202.gitlab=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=BA=93=E7=AE=A1=E7=90=86=E3=80=81=E7=95=8C=E9=9D=A2=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/projects_controller.rb | 1 + app/controllers/repositories_controller.rb | 23 ++++---- app/helpers/gitlab_helper.rb | 53 +++++++++++++++---- app/models/repository.rb | 14 ++++- .../projects/settings/_repositories.html.erb | 16 +++--- config/configuration.yml | 1 + config/locales/en.yml | 1 + config/locales/zh.yml | 1 + config/settings.yml | 11 ++-- 9 files changed, 90 insertions(+), 31 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 136703c68..5bc1bb601 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -77,6 +77,7 @@ class ProjectsController < ApplicationController helper :words ### added by william include ActsAsTaggableOn::TagsHelper + include GitlabHelper def find_project_repository unless @project.repositories.nil? diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index f01dafa32..d10862925 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -41,7 +41,7 @@ class RepositoriesController < ApplicationController helper :repositories include GitlabHelper #@root_path = RepositoriesHelper::ROOT_PATH - + rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed def new @@ -100,6 +100,8 @@ class RepositoriesController < ApplicationController if params[:repository_scm].to_s == 'Git' # add by nwb # 增加对gitlab版本库的支持 + user = User.current + @gitlab_user = GitlabUser.find_by_user_id(user.id) attrs = pickup_extra_info @repository = Repository.factory('Git') @@ -129,20 +131,23 @@ class RepositoriesController < ApplicationController end # 判断gitlab用户是否存在,不存在则创建 - user = User.current - gitlab_user = GitlabUser.find_by_user_id(user.id) - if gitlab_user.nil? + if @gitlab_user.nil? if !create_user(params[:repository][:gitlab_user],user.show_name,params[:repository][:upassword],params[:repository][:gitlab_email],user.id) # 创建gitlab用户失败 - flash[:error] = l(:error_create_gitlab_user) - render :action => 'newrepo', :layout =>'base_projects' - return + # 验证gitlab是否可由指定的账号、密码登录 + if login_init_gitlab(params[:repository][:gitlab_email],params[:repository][:upassword],params[:repository][:gitlab_user].user.id) + @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 - gitlab_user = GitlabUser.find_by_user_id(user.id) + @gitlab_user = GitlabUser.find_by_user_id(user.id) end # 创建gitlab版本库 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] # 创建gitlab项目失败 flash[:error] = l(:error_create_gitlab_project) diff --git a/app/helpers/gitlab_helper.rb b/app/helpers/gitlab_helper.rb index a7a5b3cfc..27d221561 100644 --- a/app/helpers/gitlab_helper.rb +++ b/app/helpers/gitlab_helper.rb @@ -7,11 +7,11 @@ module GitlabHelper # gitlab版本库数据本地保存的根目录 - ROOT_PATH="/home/git/repositories/" + ROOT_PATH="/home/git/repositories" PROJECT_PATH_CUT = 40 # gitlab版本库所在服务器 # 注意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" GITLAB_API = "/api/v3" # gitlab的默认登录用户(需管理员) @@ -65,6 +65,41 @@ module GitlabHelper 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 def create_project(project_name,project_id) @@ -77,14 +112,14 @@ module GitlabHelper if res.code == '201' 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 # 新创建项目的web地址 - web_url = temp['web_url'] + web_url = String.new(temp['web_url']) web_url['http://localhost'] = REPO_IP_ADDRESS # 新创建项目的本地文件路径 - localfile = temp['web_url'] - localfile['http://localhost'] = ROOT_PATH + localfile = String.new(temp['web_url']) + localfile[REPO_IP_ADDRESS] = ROOT_PATH # 保存数据 gitproject =GitlabProject.new @@ -119,13 +154,13 @@ module GitlabHelper if res.code == '201' 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 # 新创建项目的web地址 - web_url = temp['web_url'] + web_url = String.new(temp['web_url']) web_url['http://localhost'] = REPO_IP_ADDRESS # 新创建项目的本地文件路径 - localfile = temp['web_url'] + localfile = String.new(temp['web_url']) localfile['http://localhost'] = ROOT_PATH # 保存数据 diff --git a/app/models/repository.rb b/app/models/repository.rb index 0fce323ab..bd0ee67fa 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -141,7 +141,7 @@ class Repository < ActiveRecord::Base gitlabproject = GitlabProject.find_by_localfile_url(self.url) if gitlabproject.nil? # 兼容老git模式 - ip = RepositoriesHelper::REPO_IP_ADDRESS + ip = Setting.gitlab_server if ip.nil? self.url 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) end else - self.url + gitlabproject.repository_url end else self.url end end + # 版本库的web管理地址 + def web_url + gitlabproject = GitlabProject.find_by_localfile_url(self.url) + if gitlabproject + gitlabproject.web_url + else + '' + end + end + def <=>(repository) if is_default? -1 diff --git a/app/views/projects/settings/_repositories.html.erb b/app/views/projects/settings/_repositories.html.erb index 4366057a1..b76eeb334 100644 --- a/app/views/projects/settings/_repositories.html.erb +++ b/app/views/projects/settings/_repositories.html.erb @@ -7,8 +7,8 @@ <%= l(:field_identifier) %> <%= l(:field_repository_is_default) %> <%= l(:label_scm) %> - <%= l(:label_repository_path) %> - + <%= l(:label_repository_path) %> + <%= l(:label_web_path) %> @@ -21,20 +21,21 @@ <%= checked_image repository.is_default? %> <%=h repository.scm_name %> <%=h repository.repos_url %> - - + <%=h repository.web_url %> + + + + <% if repository.scm_name=="Subversion"%> <%if User.current.allowed_to?(:manage_repository, @project) %> <%= 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), :class => 'icon icon-edit') %> <%= delete_link repository_path(repository) %> <% end %> <% elsif repository.scm_name=="Git"%> <%if User.current.allowed_to?(:manage_repository, @project) %> - <%= 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') %> <%# if repository.login.to_s==User.current.login.to_s %> @@ -45,6 +46,7 @@ <% end %> + <% end %> diff --git a/config/configuration.yml b/config/configuration.yml index 4bdd279aa..253578048 100644 --- a/config/configuration.yml +++ b/config/configuration.yml @@ -205,6 +205,7 @@ default: # that overrides the default ones production: + # specific configuration options for development environment # that overrides the default ones development: diff --git a/config/locales/en.yml b/config/locales/en.yml index 90690e07c..accb88591 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1348,6 +1348,7 @@ en: label_join: join Trustie! label_repository_new: link to existing SVN repository label_repository_path: path of repository + label_web_path: web management path label_repository_new_repos: create a new repository label_repository_no: have no repository? label_welcome_page_to: Participate %{project_count} projects! diff --git a/config/locales/zh.yml b/config/locales/zh.yml index e434ac854..258895b1b 100644 --- a/config/locales/zh.yml +++ b/config/locales/zh.yml @@ -1549,6 +1549,7 @@ zh: label_create_course_description: 课程小社区,创建新课程,让我们共同分享多到想不到的公共资源! label_welcome_page_to: 参与了 %{project_count} 个项目! label_repository_path: 版本库路径 + label_web_path: Web管理路径 label_repository_path_not_null: 库路径 不能为空字符 label_password_not_null: 密码不能设置为空。 label_exist_repository_path: 定义已有版本库URL路径,定义格式file:///, http://, https://, svn:// diff --git a/config/settings.yml b/config/settings.yml index 07a7efde7..e154f22cc 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -117,6 +117,13 @@ commit_logtime_enabled: commit_logtime_activity_id: format: int default: 0 +gitlab_server: + default: repository.trustie.net +gitlab_login_user: + default: admin@local.host +gitlab_login_password: + default: 123456 + # autologin duration in days # 0 means autologin is disabled autologin: @@ -251,10 +258,6 @@ 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