mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-07-14 22:24:36 +08:00
FIX 完善devops流程
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
class DevOps::Drone::API < DevOps::Drone::Request
|
||||
attr_reader :drone_token, :drone_url, :owner, :repo, :options
|
||||
attr_reader :drone_token, :endpoint, :owner, :repo, :options
|
||||
|
||||
# drone_token:
|
||||
# owner: repo's owner name or login
|
||||
# repo: repo's identifier
|
||||
def initialize(drone_token, drone_url, owner, repo, options={})
|
||||
def initialize(drone_token, endpoint, owner, repo, options={})
|
||||
@drone_token = drone_token
|
||||
@drone_url = drone_url
|
||||
@endpoint = endpoint
|
||||
@owner = owner
|
||||
@repo = repo
|
||||
@options = options
|
||||
@@ -15,40 +15,59 @@ class DevOps::Drone::API < DevOps::Drone::Request
|
||||
# Build List
|
||||
# GET api/repos/{owner}/{name}/builds
|
||||
# eq:
|
||||
# DevOps::Drone::API
|
||||
# DevOps::Drone::API.new(cloud_account.drone_token, cloud_account.drone_url, @repo.user.login, @repo.identifier)
|
||||
def builds
|
||||
request(:get, drone_url, "api/repos/#{owner}/#{repo}/builds", drone_token: drone_token)
|
||||
get(endpoint, "api/repos/#{owner}/#{repo}/builds", drone_token: drone_token)
|
||||
end
|
||||
|
||||
# Build Info
|
||||
# GET api/repos/{owner}/{name}/builds/{number}
|
||||
# eq:
|
||||
# DevOps::Drone::API.new(cloud_account.drone_token, cloud_account.endpoint, project.owner.login, project.identifier, number: number).build
|
||||
def build
|
||||
request(:get, "api/repos/#{owner}/#{repo}/builds/#{number}", drone_token: drone_token)
|
||||
get(endpoint, "api/repos/#{owner}/#{repo}/builds/#{options[:number]}", drone_token: drone_token)
|
||||
end
|
||||
|
||||
# Update .trustie-pipeline.yml file
|
||||
# PATCH api/repos/{owner}/{name}
|
||||
# PATCH api/repos/{owner}/{name}\
|
||||
# eq:
|
||||
# DevOps::Drone::API.new(cloud_account.drone_token, cloud_account.endpoint, project.owner.login, project.identifier, config_path: config_path).config_yml
|
||||
def config_yml
|
||||
request(:patch, "/api/repos/#{owner}/#{repo}", drone_token: drone_token, config_path: config_path)
|
||||
patch(endpoint, "/api/repos/#{owner}/#{repo}", drone_token: drone_token, config_path: options[:config_path])
|
||||
end
|
||||
|
||||
# Activate user's project with Drone CI
|
||||
# POST api/repos/{owner}/{name}
|
||||
# eq:
|
||||
# DevOps::Drone::API.new(cloud_account.drone_token, cloud_account.endpoint, project.owner.login, project.identifier).avtivate
|
||||
def activate
|
||||
request(:post, "/api/repos/#{owner}/#{repo}", drone_token: drone_token)
|
||||
post(endpoint, "/api/repos/#{owner}/#{repo}", drone_token: drone_token)
|
||||
end
|
||||
|
||||
# Build Restart
|
||||
# POST api/repos/{owner}/{name}/builds/{number}
|
||||
# Restart the specified build. Please note this api requires read and write access to the repository and the request parameter {build} is not the build id but the build number.
|
||||
# eq:
|
||||
# DevOps::Drone::API.new(cloud_account.drone_token, cloud_account.drone_url, @repo.user.login, @repo.identifier, number: number).restart
|
||||
def restart
|
||||
request(:post, "/api/repos/#{owner}/#{repo}/builds/#{number}", drone_token: drone_token)
|
||||
post(endpoint, "/api/repos/#{owner}/#{repo}/builds/#{options[:number]}", drone_token: drone_token)
|
||||
end
|
||||
|
||||
# Build Stop
|
||||
# DELETE api/repos/{owner}/{name}/builds/{number}
|
||||
# Stop the specified build. Please note this api requires administrative privileges and the request parameter {build} is not the build id but the build number.
|
||||
# eq:
|
||||
# DevOps::Drone::API.new(cloud_account.drone_token, cloud_account.drone_url, @repo.user.login, @repo.identifier, number: number).stop
|
||||
def stop
|
||||
request(:delete, "/api/repos/#{owner}/#{repo}/builds/#{number}", drone_token: drone_token)
|
||||
delete(endpoint, "/api/repos/#{owner}/#{repo}/builds/#{options[:number]}", drone_token: drone_token)
|
||||
end
|
||||
|
||||
# Build Logs
|
||||
# GET /api/repos/{owner}/{repo}/builds/{build}/logs/{stage}/{step}
|
||||
# Please note this api requires read access to the repository.
|
||||
# eq:
|
||||
# DevOps::Drone::API.new(cloud_account.drone_token, cloud_account.drone_url, @repo.user.login, @repo.identifier, build: build, stage: stage, step: step).logs
|
||||
def logs
|
||||
get(endpoint, "/api/repos/#{owner}/#{repo}/builds/#{options[:build]}/logs/#{options[:stage]}/#{options[:step]}", drone_token: drone_token)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,7 +5,7 @@ class DevOps::Drone::Ci
|
||||
# username: drone server's account
|
||||
# password: drone server's password
|
||||
# eq:
|
||||
# DevOps::Drone::Ci.new(@cloud_account.drone_ip, @cloud_account.account, @cloud_account.visible_secret).get_token
|
||||
# DevOps::Drone::Ci.new(@cloud_account.drone_ip, @cloud_account.account, @cloud_account.visible_secret, current_user.login).get_token
|
||||
def initialize(host, username, password, gitea_username)
|
||||
@host = host
|
||||
@username = username
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
# @private
|
||||
class DevOps::Drone::Request
|
||||
# format :json
|
||||
# headers 'Accept' => 'application/json'
|
||||
# parser Proc.new { |body, _| parse(body) }
|
||||
|
||||
|
||||
# Converts the response body to an ObjectifiedHash.
|
||||
def self.parse(body)
|
||||
body = decode(body)
|
||||
@@ -29,113 +23,70 @@
|
||||
end
|
||||
end
|
||||
|
||||
def get(path, options={})
|
||||
set_httparty_config(options)
|
||||
set_private_token_header(options)
|
||||
validate self.class.get(path, options)
|
||||
def get(endpoint, path, options={})
|
||||
set_request_defaults(endpoint)
|
||||
request(:get, endpoint, path, options)
|
||||
end
|
||||
|
||||
def post(path, options={})
|
||||
set_httparty_config(options)
|
||||
set_private_token_header(options, path)
|
||||
validate self.class.post(path, options)
|
||||
def post(endpoint, path, options={})
|
||||
set_request_defaults(endpoint)
|
||||
request(:post, endpoint, path, options)
|
||||
end
|
||||
|
||||
def put(path, options={})
|
||||
set_httparty_config(options)
|
||||
set_private_token_header(options)
|
||||
validate self.class.put(path, options)
|
||||
def put(endpoint, path, options={})
|
||||
set_request_defaults(endpoint)
|
||||
request(:put, endpoint, path, options)
|
||||
end
|
||||
|
||||
def delete(path, options={})
|
||||
set_httparty_config(options)
|
||||
set_private_token_header(options)
|
||||
validate self.class.delete(path, options)
|
||||
end
|
||||
|
||||
# Checks the response code for common errors.
|
||||
# Returns parsed response for successful requests.
|
||||
def validate(response)
|
||||
# case response.code
|
||||
# when 400; raise Error::BadRequest.new error_message(response)
|
||||
# when 401; raise Error::Unauthorized.new error_message(response)
|
||||
# when 403; raise Error::Forbidden.new error_message(response)
|
||||
# when 404; raise Error::NotFound.new error_message(response)
|
||||
# when 405; raise Error::MethodNotAllowed.new error_message(response)
|
||||
# when 406; raise Error::DataNotAccepted.new error_message(response)
|
||||
# when 409; raise Error::Conflict.new error_message(response)
|
||||
# when 500; raise Error::InternalServerError.new error_message(response)
|
||||
# when 502; raise Error::BadGateway.new error_message(response)
|
||||
# when 503; raise Error::ServiceUnavailable.new error_message(response)
|
||||
# end
|
||||
|
||||
response.parsed_response
|
||||
end
|
||||
|
||||
# Sets a base_uri and default_params for requests.
|
||||
# @raise [Error::MissingCredentials] if endpoint not set.
|
||||
def set_request_defaults(endpoint, private_token, sudo=nil)
|
||||
raise Error::MissingCredentials.new("Please set an endpoint to API") unless endpoint
|
||||
@private_token = private_token
|
||||
|
||||
self.class.base_uri endpoint
|
||||
self.class.default_params :sudo => sudo
|
||||
self.class.default_params.delete(:sudo) if sudo.nil?
|
||||
end
|
||||
|
||||
def request(method, domain, url, **params)
|
||||
Rails.logger.info("[drone] request: #{method} #{url} #{params.except(:secret).inspect}")
|
||||
|
||||
client = Faraday.new(url: domain)
|
||||
response = client.public_send(method, url, params)
|
||||
result = JSON.parse(response.body)
|
||||
|
||||
Rails.logger.info("[drone] response:#{response.status} #{result.inspect}")
|
||||
|
||||
if response.status != 200
|
||||
raise DevOps::Drone::Error.parse(result)
|
||||
end
|
||||
|
||||
if result['errcode'].present? && result['errcode'].to_i.nonzero?
|
||||
raise DevOps::Drone::Error.parse(result)
|
||||
end
|
||||
|
||||
result
|
||||
def delete(endpoint, path, options={})
|
||||
set_request_defaults(endpoint)
|
||||
request(:delete, endpoint, path, options)
|
||||
end
|
||||
|
||||
private
|
||||
def conn(auth={})
|
||||
token = auth[:token]
|
||||
puts "[gitea] token: #{token}"
|
||||
def request(method, endpoint, path, **params)
|
||||
Rails.logger.info("[drone] request: #{method} #{path} #{params.except(:secret).inspect}")
|
||||
|
||||
@client ||= begin
|
||||
Faraday.new(url: domain) do |req|
|
||||
req.request :url_encoded
|
||||
req.headers['Content-Type'] = 'application/json'
|
||||
req.response :logger # 显示日志
|
||||
req.adapter Faraday.default_adapter
|
||||
req.authorization :Bearer, token
|
||||
req.headers['Authorization']
|
||||
end
|
||||
client = Faraday.new(path: domain)
|
||||
response = client.public_send(method, path, params)
|
||||
result = JSON.parse(response.body)
|
||||
|
||||
Rails.logger.info("[drone] response:#{response.status} #{result.inspect}")
|
||||
|
||||
if response.status != 200
|
||||
raise DevOps::Drone::Error.parse(result)
|
||||
end
|
||||
@client
|
||||
|
||||
if result['errcode'].present? && result['errcode'].to_i.nonzero?
|
||||
raise DevOps::Drone::Error.parse(result)
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
# Sets a PRIVATE-TOKEN header for requests.
|
||||
# @raise [Error::MissingCredentials] if private_token not set.
|
||||
def set_private_token_header(options, path=nil)
|
||||
unless path == '/session'
|
||||
raise Error::MissingCredentials.new("Please set a private_token for user") unless @private_token
|
||||
options[:headers] = {'PRIVATE-TOKEN' => @private_token}
|
||||
end
|
||||
# Sets a base_uri and default_params for requests.
|
||||
# @raise [Error::MissingCredentials] if endpoint not set.
|
||||
def set_request_defaults(endpoint, private_token, sudo=nil)
|
||||
raise "Please set an endpoint to API" unless endpoint
|
||||
end
|
||||
|
||||
# Set HTTParty configuration
|
||||
# @see https://github.com/jnunemaker/httparty
|
||||
def set_httparty_config(options)
|
||||
if self.httparty
|
||||
options.merge!(self.httparty)
|
||||
end
|
||||
# Checks the response code for common errors.
|
||||
# Returns parsed response for successful requests.
|
||||
def validate(response)
|
||||
# case response.code
|
||||
# when 400; raise Error::BadRequest.new error_message(response)
|
||||
# when 401; raise Error::Unauthorized.new error_message(response)
|
||||
# when 403; raise Error::Forbidden.new error_message(response)
|
||||
# when 404; raise Error::NotFound.new error_message(response)
|
||||
# when 405; raise Error::MethodNotAllowed.new error_message(response)
|
||||
# when 406; raise Error::DataNotAccepted.new error_message(response)
|
||||
# when 409; raise Error::Conflict.new error_message(response)
|
||||
# when 500; raise Error::InternalServerError.new error_message(response)
|
||||
# when 502; raise Error::BadGateway.new error_message(response)
|
||||
# when 503; raise Error::ServiceUnavailable.new error_message(response)
|
||||
# end
|
||||
|
||||
response.parsed_response
|
||||
end
|
||||
|
||||
def error_message(response)
|
||||
|
||||
Reference in New Issue
Block a user