1.添加给gitlab项目新增用户及赋权限接口

2.添加给gitlab项目删除用户接口
This commit is contained in:
nwb 2014-07-17 09:55:40 +08:00
parent 63e855725f
commit b60a0ddf59
2 changed files with 61 additions and 3 deletions

View File

@ -364,11 +364,13 @@ class UsersController < ApplicationController
def show def show
# 测试代码 # 测试代码
#login_gitlab("admin@local.host","19840410") login_gitlab("admin@local.host","19840410")
#create_project("testproject1") #create_project("testproject1")
#create_project_for_user("testproject1","2") #create_project_for_user("testproject1","2")
#create_user("test","just test","123456","test@sina.com") #create_user("test","just test","123456","test@sina.com")
#delete_user("3") #delete_user("3")
add_user_to_project("1","2",GitlabHelper::GUEST)
delete_user_from_project("1","2")
# 测试结束 # 测试结束
pre_count = 10 #limit pre_count = 10 #limit

View File

@ -12,6 +12,12 @@ module GitlabHelper
REPO_IP_ADDRESS = "http://192.168.137.100" REPO_IP_ADDRESS = "http://192.168.137.100"
GITLAB_API = "/api/v3" GITLAB_API = "/api/v3"
# 用户在项目中的权限级别
GUEST = 10
REPORTER = 20
DEVELOPER = 30
MASTER = 40
def self.gitlab_token=(token) def self.gitlab_token=(token)
Thread.current[:gitlab_token] = token Thread.current[:gitlab_token] = token
end end
@ -116,6 +122,8 @@ module GitlabHelper
end end
# 删除用户 # 删除用户
# user_id:用户在gitlab中的id
# add by nwb
def delete_user(user_id) def delete_user(user_id)
url = REPO_IP_ADDRESS + GITLAB_API + "/users/" + user_id url = REPO_IP_ADDRESS + GITLAB_API + "/users/" + user_id
uri = URI.parse(url) uri = URI.parse(url)
@ -138,9 +146,56 @@ module GitlabHelper
end 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 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) def post(url, params)
uri = URI.parse(url) uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port) http = Net::HTTP.new(uri.host, uri.port)
@ -163,7 +218,8 @@ module GitlabHelper
end end
end end
# 发送get消息
# add by nwb
def get(url, params) def get(url, params)
uri = URI.parse(url) uri = URI.parse(url)
uri.query = URI.encode_www_form(params) uri.query = URI.encode_www_form(params)