|
|
|
@@ -0,0 +1,61 @@ |
|
|
|
class Api::V1::Projects::Webhooks::UpdateService < ApplicationService |
|
|
|
include ActiveModel::Model |
|
|
|
|
|
|
|
attr_reader :project, :id, :token, :owner, :repo, :active, :branch_filter, :content_type, :url, :http_method, :secret, :events |
|
|
|
attr_accessor :gitea_data |
|
|
|
|
|
|
|
validates :url, format: { with: URI::regexp(%w[http https]), message: "请输入正确的地址" } |
|
|
|
validates :active, inclusion: {in: [true, false]} |
|
|
|
validates :http_method, inclusion: { in: %w(POST GET), message: "请输入正确的请求方式"} |
|
|
|
validates :content_type, inclusion: { in: %w(json form), message: "请输入正确的Content Type"} |
|
|
|
|
|
|
|
def initialize(project, id, params, token=nil) |
|
|
|
@project = project |
|
|
|
@id = id |
|
|
|
@owner = project&.owner.login |
|
|
|
@repo = project&.identifier |
|
|
|
@active = params[:active] |
|
|
|
@branch_filter = params[:branch_filter] |
|
|
|
@content_type = params[:content_type] |
|
|
|
@url = params[:url] |
|
|
|
@http_method = params[:http_method] |
|
|
|
@secret = params[:secret] |
|
|
|
@events = params[:events] |
|
|
|
@token = token |
|
|
|
end |
|
|
|
|
|
|
|
def call |
|
|
|
raise Error, errors.full_messages.join(",") unless valid? |
|
|
|
begin |
|
|
|
$gitea_client.token = token unless token.blank? |
|
|
|
|
|
|
|
excute_data_to_gitea |
|
|
|
|
|
|
|
$gitea_client.token = nil unless token.blank? |
|
|
|
|
|
|
|
gitea_data |
|
|
|
rescue |
|
|
|
raise Error, "服务器错误,请联系系统管理员!" |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
private |
|
|
|
def request_body |
|
|
|
{ |
|
|
|
active: active, |
|
|
|
branch_filter: branch_filter, |
|
|
|
config: { |
|
|
|
content_type: content_type, |
|
|
|
url: url, |
|
|
|
http_method: http_method, |
|
|
|
secret: secret |
|
|
|
}, |
|
|
|
events: events || [], |
|
|
|
type: 'gitea', |
|
|
|
} |
|
|
|
end |
|
|
|
|
|
|
|
def excute_data_to_gitea |
|
|
|
@gitea_data = $gitea_client.patch_repos_hooks_by_owner_repo_id(owner, repo, id, {body: request_body.to_json}) |
|
|
|
end |
|
|
|
end |