新增:触发webhook的job
This commit is contained in:
parent
17279c56f4
commit
b4672929dc
|
@ -0,0 +1,28 @@
|
|||
class TouchWebhookJob < ApplicationJob
|
||||
queue_as :webhook
|
||||
|
||||
def perform(source, *args)
|
||||
case source
|
||||
when 'IssueCreate'
|
||||
issue_id, sender_id = args[0], args[1]
|
||||
issue = Issue.find_by_id issue_id
|
||||
sender = User.find_by_id sender_id
|
||||
return if issue.nil? || sender.nil?
|
||||
issue.project.webhooks.each do |webhook|
|
||||
next unless webhook.events["events"]["issues"]
|
||||
@webhook_task = Webhook::IssueCreateClient.new(webhook, issue, sender).do_request
|
||||
Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}"
|
||||
end
|
||||
when 'IssueUpdate'
|
||||
issue_id, sender_id, changes = args[0], args[1], args[2]
|
||||
issue = Issue.find_by_id issue_id
|
||||
sender = User.find_by_id sender_id
|
||||
return if issue.nil? || sender.nil? || !changes.is_a?(Hash)
|
||||
issue.project.webhooks.each do |webhook|
|
||||
next unless webhook.events["events"]["issues"]
|
||||
@webhook_task = Webhook::IssueUpdateClient.new(webhook, issue, sender, changes.stringify_keys).do_request
|
||||
Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -67,6 +67,9 @@ class Api::V1::Issues::CreateService < ApplicationService
|
|||
SendTemplateMessageJob.perform_later('IssueAssigned', current_user.id, @created_issue&.id, assigner_ids) unless assigner_ids.blank?
|
||||
SendTemplateMessageJob.perform_later('ProjectIssue', current_user.id, @created_issue&.id)
|
||||
end
|
||||
|
||||
# 触发webhook
|
||||
TouchWebhookJob.perform_later('IssueCreate', @created_issue&.id, current_user.id)
|
||||
|
||||
unlock("Api::V1::Issues::CreateService:#{project.id}") # 结束写数据,解锁
|
||||
end
|
||||
|
|
|
@ -77,6 +77,9 @@ class Api::V1::Issues::UpdateService < ApplicationService
|
|||
SendTemplateMessageJob.perform_later('IssueAssigned', current_user.id, @issue&.id, add_assigner_ids) unless add_assigner_ids.blank?
|
||||
end
|
||||
|
||||
# 触发webhook
|
||||
TouchWebhookJob.perform_later('IssueCreate', @updated_issue&.id, current_user.id, previous_issue_changes)
|
||||
|
||||
unlock("Api::V1::Issues::UpdateService:#{project.id}:#{issue.id}")
|
||||
|
||||
return @updated_issue
|
||||
|
|
|
@ -38,6 +38,8 @@ class Webhook::IssueCreateClient
|
|||
request_content: request_content,
|
||||
response_content: response_content
|
||||
})
|
||||
|
||||
@webhook_task
|
||||
end
|
||||
|
||||
def payload_content
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
class Webhook::IssueUpdateClient
|
||||
|
||||
include Webhook::Client
|
||||
|
||||
attr_accessor :webhook, :issue, :sender, :changes
|
||||
attr_accessor :webhook_task
|
||||
|
||||
def initialize(webhook, issue, sender, changes={})
|
||||
@webhook = webhook
|
||||
@issue = issue
|
||||
@sender = sender
|
||||
@changes = changes
|
||||
# 创建webhook task
|
||||
@webhook_task = Gitea::WebhookTask.create(
|
||||
hook_id: @webhook.id,
|
||||
uuid: SecureRandom.uuid,
|
||||
payload_content: payload_content,
|
||||
event_type: "issues",
|
||||
is_delivered: true
|
||||
)
|
||||
|
||||
# 构建client参数
|
||||
super({
|
||||
uuid: @webhook_task.uuid,
|
||||
event: "issues",
|
||||
http_method: @webhook.http_method,
|
||||
content_type: @webhook.content_type,
|
||||
url: @webhook.url,
|
||||
secret: @webhook.secret,
|
||||
payload_content: @webhook_task.read_attribute_before_type_cast("payload_content")
|
||||
})
|
||||
end
|
||||
|
||||
def do_request
|
||||
request_content, response_content = super
|
||||
@webhook_task.update_attributes({
|
||||
delivered: Time.now.to_i * 1000000000,
|
||||
is_succeed: response_content["status"] < 300,
|
||||
request_content: request_content,
|
||||
response_content: response_content
|
||||
})
|
||||
|
||||
@webhook_task
|
||||
end
|
||||
|
||||
def payload_content
|
||||
{
|
||||
"action": "edited",
|
||||
"number": @issue.project_issues_index,
|
||||
"changes": @changes,
|
||||
"issue": JSON.parse(@issue.to_builder.target!),
|
||||
"project": JSON.parse(@issue.project.to_builder.target!),
|
||||
"sender": JSON.parse(@sender.to_builder.target!)
|
||||
}
|
||||
end
|
||||
|
||||
end
|
|
@ -9,3 +9,4 @@
|
|||
- [mailers, 101]
|
||||
- [cache, 10]
|
||||
- [message, 20]
|
||||
- [webhook, 20]
|
||||
|
|
Loading…
Reference in New Issue