2014-07-16 15:05:16 +08:00
|
|
|
|
# Gitlab5.3 API操作接口
|
|
|
|
|
# Add by nwb
|
2014-07-16 15:52:41 +08:00
|
|
|
|
# 暂时没支持SSH
|
2014-07-16 15:05:16 +08:00
|
|
|
|
|
|
|
|
|
module GitlabHelper
|
|
|
|
|
|
|
|
|
|
# gitlab版本库数据本地保存的根目录
|
|
|
|
|
ROOT_PATH="/home/git/repositories/"
|
|
|
|
|
PROJECT_PATH_CUT = 40
|
|
|
|
|
# gitlab版本库所在服务器
|
2014-07-16 15:52:41 +08:00
|
|
|
|
#REPO_IP_ADDRESS = "http://" + Setting.repository_domain
|
2014-07-16 15:05:16 +08:00
|
|
|
|
REPO_IP_ADDRESS = "http://192.168.137.100"
|
|
|
|
|
GITLAB_API = "/api/v3"
|
|
|
|
|
|
|
|
|
|
def self.gitlab_token=(token)
|
|
|
|
|
Thread.current[:gitlab_token] = token
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# gitlab的登录验证信息
|
|
|
|
|
# add by nwb
|
|
|
|
|
def self.gitlab_token
|
|
|
|
|
Thread.current[:gitlab_token] ||= nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 登录gitlab
|
|
|
|
|
# add by nwb
|
|
|
|
|
def login_gitlab(email,password)
|
|
|
|
|
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)
|
2014-07-16 15:52:41 +08:00
|
|
|
|
GitlabHelper.gitlab_token= temp['private_token']
|
2014-07-16 15:05:16 +08:00
|
|
|
|
return true
|
|
|
|
|
else
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
rescue =>err
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 创建项目
|
|
|
|
|
# add by nwb
|
2014-07-16 15:52:41 +08:00
|
|
|
|
def create_project(project_name)
|
|
|
|
|
url = REPO_IP_ADDRESS + GITLAB_API + "/projects"
|
|
|
|
|
uri = URI.parse(url)
|
|
|
|
|
data = {name:project_name, private_token:GitlabHelper.gitlab_token}
|
|
|
|
|
begin
|
|
|
|
|
res = Net::HTTP.post_form(uri, data)
|
|
|
|
|
if res.code == '201'
|
|
|
|
|
temp = ActiveSupport::JSON.decode(res.body)
|
|
|
|
|
#新创建项目的版本库地址
|
|
|
|
|
respo = temp['http_url_to_repo']
|
|
|
|
|
respo['http://localhost'] = REPO_IP_ADDRESS
|
|
|
|
|
# 新创建项目的web地址
|
|
|
|
|
webaddress = temp['web_url']
|
|
|
|
|
webaddress['http://localhost'] = REPO_IP_ADDRESS
|
|
|
|
|
return true
|
|
|
|
|
else
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
rescue =>err
|
|
|
|
|
return false
|
|
|
|
|
end
|
2014-07-16 15:05:16 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 为指定用户创建版本库
|
2014-07-16 15:52:41 +08:00
|
|
|
|
# project_name:版本库名称 user_id:Gitlab版本库中的用户编号
|
|
|
|
|
# add by nwb
|
|
|
|
|
def create_project_for_user(project_name,user_id)
|
|
|
|
|
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}
|
|
|
|
|
begin
|
|
|
|
|
res = Net::HTTP.post_form(uri, data)
|
|
|
|
|
if res.code == '201'
|
|
|
|
|
temp = ActiveSupport::JSON.decode(res.body)
|
|
|
|
|
#新创建项目的版本库地址
|
|
|
|
|
respo = temp['http_url_to_repo']
|
|
|
|
|
respo['http://localhost'] = REPO_IP_ADDRESS
|
|
|
|
|
# 新创建项目的web地址
|
|
|
|
|
webaddress = temp['web_url']
|
|
|
|
|
webaddress['http://localhost'] = REPO_IP_ADDRESS
|
|
|
|
|
return true
|
|
|
|
|
else
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
rescue =>err
|
|
|
|
|
return false
|
|
|
|
|
end
|
2014-07-16 15:05:16 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 创建用户
|
|
|
|
|
# loginname:登录名称 username:用户姓名
|
|
|
|
|
def create_user (loginname,username,password,email)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 删除用户
|
|
|
|
|
def delete_user(user_id)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 给用户添加一个可操作的项目
|
|
|
|
|
def add_project_to_user(project_name,user)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def post(url, params)
|
|
|
|
|
uri = URI.parse(url)
|
|
|
|
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
|
|
|
if uri.scheme == 'https'
|
|
|
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
|
|
|
http.use_ssl = true
|
|
|
|
|
end
|
|
|
|
|
begin
|
|
|
|
|
request = Net::HTTP::Post.new(uri.request_uri)
|
|
|
|
|
request['Content-Type'] = 'application/json;charset=utf-8'
|
|
|
|
|
request['User-Agent'] = 'Mozilla/5.0 (Windows NT 5.1; rv:29.0) Gecko/20100101 Firefox/29.0'
|
|
|
|
|
request['X-ACL-TOKEN'] = 'xxx_token'
|
|
|
|
|
#request.set_form_data(params)
|
|
|
|
|
request.body = params.to_json
|
|
|
|
|
response = http.start { |http| http.request(request) }
|
|
|
|
|
puts response.body.inspect
|
|
|
|
|
return JSON.parse response.body
|
|
|
|
|
rescue =>err
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get(url, params)
|
|
|
|
|
uri = URI.parse(url)
|
|
|
|
|
uri.query = URI.encode_www_form(params)
|
|
|
|
|
http = Net::HTTP.new uri.host, uri.port
|
|
|
|
|
if uri.scheme == 'https'
|
|
|
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
|
|
|
http.use_ssl = true
|
|
|
|
|
end
|
|
|
|
|
begin
|
|
|
|
|
request = Net::HTTP::Get.new uri.request_uri
|
|
|
|
|
request['Content-Type'] = 'application/json;charset=utf-8'
|
|
|
|
|
request['User-Agent'] = 'Mozilla/5.0 (Windows NT 5.1; rv:29.0) Gecko/20100101 Firefox/29.0'
|
|
|
|
|
request['X-ACL-TOKEN'] = 'xxx_token'
|
|
|
|
|
response = http.start { |http| http.request request }
|
|
|
|
|
puts response.body.inspect
|
|
|
|
|
return JSON.parse response.body
|
|
|
|
|
rescue =>err
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|