diff --git a/app/jobs/touch_webhook_job.rb b/app/jobs/touch_webhook_job.rb new file mode 100644 index 000000000..9959d2ebc --- /dev/null +++ b/app/jobs/touch_webhook_job.rb @@ -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 \ No newline at end of file diff --git a/app/services/api/v1/issues/create_service.rb b/app/services/api/v1/issues/create_service.rb index 30f4a11ef..7080c3bda 100644 --- a/app/services/api/v1/issues/create_service.rb +++ b/app/services/api/v1/issues/create_service.rb @@ -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 diff --git a/app/services/api/v1/issues/update_service.rb b/app/services/api/v1/issues/update_service.rb index 9aa9d6bb1..21242edc2 100644 --- a/app/services/api/v1/issues/update_service.rb +++ b/app/services/api/v1/issues/update_service.rb @@ -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 diff --git a/app/services/webhook/issue_create_client.rb b/app/services/webhook/issue_create_client.rb index 9e8b94bc3..f222291fb 100644 --- a/app/services/webhook/issue_create_client.rb +++ b/app/services/webhook/issue_create_client.rb @@ -38,6 +38,8 @@ class Webhook::IssueCreateClient request_content: request_content, response_content: response_content }) + + @webhook_task end def payload_content diff --git a/app/services/webhook/issue_update_client.rb b/app/services/webhook/issue_update_client.rb new file mode 100644 index 000000000..ff9926006 --- /dev/null +++ b/app/services/webhook/issue_update_client.rb @@ -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 \ No newline at end of file diff --git a/config/sidekiq.yml b/config/sidekiq.yml index f8981a8b1..0b38a0b8f 100644 --- a/config/sidekiq.yml +++ b/config/sidekiq.yml @@ -9,3 +9,4 @@ - [mailers, 101] - [cache, 10] - [message, 20] + - [webhook, 20]