From b60a0ddf592aa03d19f9bb738211fbdba0fb74da Mon Sep 17 00:00:00 2001 From: nwb Date: Thu, 17 Jul 2014 09:55:40 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E7=BB=99gitlab=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E6=96=B0=E5=A2=9E=E7=94=A8=E6=88=B7=E5=8F=8A=E8=B5=8B?= =?UTF-8?q?=E6=9D=83=E9=99=90=E6=8E=A5=E5=8F=A3=202.=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=BB=99gitlab=E9=A1=B9=E7=9B=AE=E5=88=A0=E9=99=A4=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 4 +- app/helpers/gitlab_helper.rb | 60 ++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d272a7678..a72444f0a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -364,11 +364,13 @@ class UsersController < ApplicationController def show # 测试代码 - #login_gitlab("admin@local.host","19840410") + login_gitlab("admin@local.host","19840410") #create_project("testproject1") #create_project_for_user("testproject1","2") #create_user("test","just test","123456","test@sina.com") #delete_user("3") + add_user_to_project("1","2",GitlabHelper::GUEST) + delete_user_from_project("1","2") # 测试结束 pre_count = 10 #limit diff --git a/app/helpers/gitlab_helper.rb b/app/helpers/gitlab_helper.rb index 1b2906523..20faf551c 100644 --- a/app/helpers/gitlab_helper.rb +++ b/app/helpers/gitlab_helper.rb @@ -12,6 +12,12 @@ module GitlabHelper REPO_IP_ADDRESS = "http://192.168.137.100" GITLAB_API = "/api/v3" + # 用户在项目中的权限级别 + GUEST = 10 + REPORTER = 20 + DEVELOPER = 30 + MASTER = 40 + def self.gitlab_token=(token) Thread.current[:gitlab_token] = token end @@ -116,6 +122,8 @@ module GitlabHelper end # 删除用户 + # user_id:用户在gitlab中的id + # add by nwb def delete_user(user_id) url = REPO_IP_ADDRESS + GITLAB_API + "/users/" + user_id uri = URI.parse(url) @@ -138,9 +146,56 @@ module GitlabHelper end # 给用户添加一个可操作的项目 - def add_project_to_user(project_name,user) + # 若用户已在该项目中,权限级别取新权限与现有权限的最大值 + # project_id:项目在gitlab中的id user_id:用户在gitlab中的id + # access_level为用户在项目中的权限级别,对应如下: + #GUEST = 10 + #REPORTER = 20 + #DEVELOPER = 30 + #MASTER = 40 + # add by nwb + def add_user_to_project(project_id,user_id,access_level) + 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} + begin + res = Net::HTTP.post_form(uri, data) + if res.code == '201' + temp = ActiveSupport::JSON.decode(res.body) + return true + else + return false + end + rescue =>err + return false + end end + # 从项目中删除用户 + # project_id:项目在gitlab中的ids user_id:用户在gitlab中的id + # add by nwb + def delete_user_from_project(project_id,user_id) + 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} + http = Net::HTTP.new uri.host, uri.port + begin + req = Net::HTTP::Delete.new(uri.request_uri) + req.form_data = data + res= http.start { |http| http.request req } + if res.code == '200' + temp = ActiveSupport::JSON.decode(res.body) + return true + else + return false + end + rescue =>err + return false + end + end + + # 发送post消息 + # add by nwb def post(url, params) uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) @@ -163,7 +218,8 @@ module GitlabHelper end end - + # 发送get消息 + # add by nwb def get(url, params) uri = URI.parse(url) uri.query = URI.encode_www_form(params)