You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/services/git_service.rb

43 lines
1.2 KiB

6 years ago
#coding=utf-8
#
# 文档在 https://www.showdoc.cc/127895880302646?page_id=1077512172693249
#
class GitService
class << self
['add_repository', 'fork_repository', 'delete_repository', 'file_tree', 'update_file', 'file_content', 'commits'].each do |method|
define_method method do |params|
post(method, params)
end
end
private
def root_url
new_git_address = EduSetting.find_by_name('git_address_domain').try(:value)
raise 'error: new_git_address not configuration' unless new_git_address.present?
new_git_address
end
def logger
Rails.logger
end
def post(action, params)
uri = URI.parse("#{root_url}/api/#{action}")
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = root_url.start_with?('https')
req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' => 'text/plain;charset=utf-8'})
req.body = params.to_json
res = https.request(req)
body = res.body
logger.info("--uri_exec: .....res is #{body}")
content = JSON.parse(body)
#raise content["msg"] if content["code"] != 0
content["data"]
end
end
end