From afa1cdf8429df2eace5cf3dfcc1e9cbab236428d Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 11 Apr 2023 11:52:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E4=B8=B0=E5=AF=8C=E6=9B=B4=E6=96=B0=E5=88=A0=E9=99=A4=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=EF=BC=8Cissue=E4=BA=8B=E4=BB=B6=E7=B2=92=E5=BA=A6?= =?UTF-8?q?=E6=9B=B4=E7=BB=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/v1/issues/journals_controller.rb | 1 + app/controllers/journals_controller.rb | 3 ++- app/jobs/touch_webhook_job.rb | 16 ++++++++-------- app/models/journal.rb | 6 ++++++ app/services/api/v1/issues/create_service.rb | 4 ++-- .../api/v1/issues/journals/create_service.rb | 2 +- .../api/v1/issues/journals/update_service.rb | 1 + app/services/api/v1/issues/update_service.rb | 6 +++--- app/services/webhook/issue_comment_client.rb | 18 +++++++++++++----- app/services/webhook/pull_comment_client.rb | 19 +++++++++++++------ 10 files changed, 50 insertions(+), 26 deletions(-) diff --git a/app/controllers/api/v1/issues/journals_controller.rb b/app/controllers/api/v1/issues/journals_controller.rb index cee2b81e8..af23aa686 100644 --- a/app/controllers/api/v1/issues/journals_controller.rb +++ b/app/controllers/api/v1/issues/journals_controller.rb @@ -27,6 +27,7 @@ class Api::V1::Issues::JournalsController < Api::V1::BaseController end def destroy + TouchWebhookJob.perform_later('IssueComment', @issue&.id, current_user.id, @journal.id, 'deleted', JSON.parse(@journal.to_builder.target!)) if @journal.destroy! render_ok else diff --git a/app/controllers/journals_controller.rb b/app/controllers/journals_controller.rb index e539ec2cd..7bade2797 100644 --- a/app/controllers/journals_controller.rb +++ b/app/controllers/journals_controller.rb @@ -46,7 +46,7 @@ class JournalsController < ApplicationController end Rails.logger.info "[ATME] maybe to at such users: #{@atme_receivers.pluck(:login)}" AtmeService.call(current_user, @atme_receivers, journal) if @atme_receivers.size > 0 - TouchWebhookJob.perform_later('PullRequestComment', @issue&.id, current_user.id, journal.id) + TouchWebhookJob.perform_later('PullRequestComment', @issue&.id, current_user.id, journal.id, 'created', {}) # @issue.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "journal") render :json => { status: 0, message: "评论成功", id: journal.id} # normal_status(0, "评论成功") @@ -62,6 +62,7 @@ class JournalsController < ApplicationController def destroy if @journal.destroy #如果有子评论,子评论删除吗? + TouchWebhookJob.perform_later('PullRequestComment', @issue&.id, current_user.id, @journal.id, 'deleted', JSON.parse(@journal.to_builder.target!)) Journal.children_journals(@journal.id).destroy_all normal_status(0, "评论删除成功") else diff --git a/app/jobs/touch_webhook_job.rb b/app/jobs/touch_webhook_job.rb index d0d9ad9e3..061d5ece7 100644 --- a/app/jobs/touch_webhook_job.rb +++ b/app/jobs/touch_webhook_job.rb @@ -10,14 +10,14 @@ class TouchWebhookJob < ApplicationJob return if issue.nil? || sender.nil? issue.project.webhooks.each do |webhook| next unless webhook.events["events"]["issues"] - _, _, @webhook_task = Webhook::IssueClient.new(webhook, issue, sender,"issues",).do_request + _, _, @webhook_task = Webhook::IssueClient.new(webhook, issue, sender,"issues").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) + return if issue.nil? || sender.nil? || !changes.is_a?(Hash) || changes.blank? issue.project.webhooks.each do |webhook| next unless webhook.events["events"]["issues"] _, _, @webhook_task = Webhook::IssueClient.new(webhook, issue, sender, "issues", changes.stringify_keys).do_request @@ -47,29 +47,29 @@ class TouchWebhookJob < ApplicationJob end when 'IssueComment' - issue_id, sender_id, comment_id = args[0], args[1], args[2] + issue_id, sender_id, comment_id, action_type, comment_json = args[0], args[1], args[2], args[3], args[4] issue = Issue.find_by_id issue_id comment = issue.comment_journals.find_by_id comment_id sender = User.find_by_id sender_id - return if issue.nil? || sender.nil? || comment.nil? + return if issue.nil? || sender.nil? issue.project.webhooks.each do |webhook| next unless webhook.events["events"]["issue_comment"] - _, _, @webhook_task = Webhook::IssueCommentClient.new(webhook, issue, comment, sender, "issue_comment").do_request + _, _, @webhook_task = Webhook::IssueCommentClient.new(webhook, issue, comment, sender, "issue_comment", action_type, comment_json).do_request Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" end when 'PullRequestComment' - issue_id, sender_id, comment_id = args[0], args[1], args[2] + issue_id, sender_id, comment_id, action_type, comment_json = args[0], args[1], args[2], args[3], args[4] issue = Issue.find_by_id(issue_id) comment = issue.comment_journals.find_by_id comment_id sender = User.find_by_id sender_id pull = issue.try(:pull_request) - return if pull.nil? || sender.nil? || comment.nil? + return if pull.nil? || sender.nil? pull.project.webhooks.each do |webhook| next unless webhook.events["events"]["pull_request_comment"] - _, _, @webhook_task = Webhook::PullCommentClient.new(webhook, pull, comment, sender, "pull_request_comment").do_request + _, _, @webhook_task = Webhook::PullCommentClient.new(webhook, pull, comment, sender, "pull_request_comment", action_type, comment_json).do_request Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" end end diff --git a/app/models/journal.rb b/app/models/journal.rb index 703a21e82..ce69c1f3a 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -275,6 +275,12 @@ class Journal < ApplicationRecord def to_builder Jbuilder.new do |journal| journal.(self, :id, :notes, :comments_count) + if self.parent_journal.present? + journal.parent_journal self.parent_journal.to_builder + end + if self.reply_journal.present? + journal.reply_journal self.reply_journal.to_builder + end end end end diff --git a/app/services/api/v1/issues/create_service.rb b/app/services/api/v1/issues/create_service.rb index 5304c5026..ce24c8b49 100644 --- a/app/services/api/v1/issues/create_service.rb +++ b/app/services/api/v1/issues/create_service.rb @@ -70,8 +70,8 @@ class Api::V1::Issues::CreateService < ApplicationService # 触发webhook TouchWebhookJob.perform_later('IssueCreate', @created_issue&.id, current_user.id) - TouchWebhookJob.perform_later('IssueLabel', @created_issue&.id, current_user.id, issue_tag_ids) - TouchWebhookJob.perform_later('IssueAssign', @created_issue&.id, current_user.id, assigner_ids) + TouchWebhookJob.perform_later('IssueLabel', @created_issue&.id, current_user.id, issue_tag_ids) unless issue_tag_ids.blank? + TouchWebhookJob.perform_later('IssueAssign', @created_issue&.id, current_user.id, assigner_ids) unless assigner_ids.blank? unlock("Api::V1::Issues::CreateService:#{project.id}") # 结束写数据,解锁 end diff --git a/app/services/api/v1/issues/journals/create_service.rb b/app/services/api/v1/issues/journals/create_service.rb index 568deecdd..3121a635d 100644 --- a/app/services/api/v1/issues/journals/create_service.rb +++ b/app/services/api/v1/issues/journals/create_service.rb @@ -41,7 +41,7 @@ class Api::V1::Issues::Journals::CreateService < ApplicationService # @信息发送 AtmeService.call(current_user, @atme_receivers, @created_journal) unless receivers_login.blank? - TouchWebhookJob.perform_later('IssueComment', @issue&.id, @current_user.id, @created_journal.id) + TouchWebhookJob.perform_later('IssueComment', @issue&.id, @current_user.id, @created_journal.id, 'created', {}) unlock("Api::V1::Issues::Journals::CreateService:#{@issue.id}") @created_journal diff --git a/app/services/api/v1/issues/journals/update_service.rb b/app/services/api/v1/issues/journals/update_service.rb index e5031aafe..0b56dd303 100644 --- a/app/services/api/v1/issues/journals/update_service.rb +++ b/app/services/api/v1/issues/journals/update_service.rb @@ -38,6 +38,7 @@ class Api::V1::Issues::Journals::UpdateService < ApplicationService # @信息发送 AtmeService.call(current_user, @atme_receivers, @created_journal) unless receivers_login.blank? + TouchWebhookJob.perform_later('IssueComment', @issue&.id, @current_user.id, @updated_journal.id, 'edited', @updated_journal.previous_changes.stringify_keys) unlock("Api::V1::Issues::Journals::UpdateService:#{@issue.id}:#{@journal.id}") diff --git a/app/services/api/v1/issues/update_service.rb b/app/services/api/v1/issues/update_service.rb index 758dc9e43..9dfaaa447 100644 --- a/app/services/api/v1/issues/update_service.rb +++ b/app/services/api/v1/issues/update_service.rb @@ -78,9 +78,9 @@ class Api::V1::Issues::UpdateService < ApplicationService end # 触发webhook - TouchWebhookJob.perform_later('IssueCreate', @updated_issue&.id, current_user.id, previous_issue_changes) - TouchWebhookJob.perform_later('IssueLabel', @updated_issue&.id, current_user.id, issue_tag_ids) - TouchWebhookJob.perform_later('IssueAssign', @updated_issue&.id, current_user.id, assigner_ids) + TouchWebhookJob.perform_later('IssueUpdate', @updated_issue&.id, current_user.id, previous_issue_changes.except(:issue_tags_value, :assigned_to_id)) + TouchWebhookJob.perform_later('IssueLabel', @updated_issue&.id, current_user.id, issue_tag_ids) unless issue_tag_ids.nil? + TouchWebhookJob.perform_later('IssueAssign', @updated_issue&.id, current_user.id, assigner_ids) unless assigner_ids.nil? unlock("Api::V1::Issues::UpdateService:#{project.id}:#{issue.id}") return @updated_issue diff --git a/app/services/webhook/issue_comment_client.rb b/app/services/webhook/issue_comment_client.rb index a00953a1d..1881217ec 100644 --- a/app/services/webhook/issue_comment_client.rb +++ b/app/services/webhook/issue_comment_client.rb @@ -2,14 +2,16 @@ class Webhook::IssueCommentClient include Webhook::Client - attr_accessor :webhook, :issue, :journal, :sender, :event + attr_accessor :webhook, :issue, :journal, :sender, :event, :action_type, :comment_json - def initialize(webhook, issue, journal, sender, event) + def initialize(webhook, issue, journal, sender, event, action_type, comment_json={}) @webhook = webhook @issue = issue @journal = journal @sender = sender @event = event + @action_type = action_type + @comment_json = comment_json # 构建client参数 super({ @@ -25,13 +27,19 @@ class Webhook::IssueCommentClient private def payload_content - { - "action": "created", + payload_content = { + "action": @action_type, "issue": JSON.parse(@issue.to_builder.target!), - "journal": JSON.parse(@journal.to_builder.target!), + "journal": @journal.present? ? JSON.parse(@journal.to_builder.target!) : @comment_json, "project": JSON.parse(@issue.project.to_builder.target!), "sender": JSON.parse(@sender.to_builder.target!), "is_pull": false } + + payload_content.merge!({ + "changes": @comment_json, + }) if @action_type == "edited" + + payload_content end end \ No newline at end of file diff --git a/app/services/webhook/pull_comment_client.rb b/app/services/webhook/pull_comment_client.rb index cb6c144f1..dde9d983f 100644 --- a/app/services/webhook/pull_comment_client.rb +++ b/app/services/webhook/pull_comment_client.rb @@ -1,15 +1,16 @@ class Webhook::PullCommentClient include Webhook::Client - attr_accessor :webhook, :pull, :journal, :sender, :event + attr_accessor :webhook, :pull, :journal, :sender, :event, :action_type, :comment_json - def initialize(webhook, pull, journal, sender, event) + def initialize(webhook, pull, journal, sender, event, action_type='created', comment_json={}) @webhook = webhook @pull = pull @journal = journal @sender = sender @event = event - + @action_type = action_type + @comment_json = comment_json # 构建client参数 super({ uuid: SecureRandom.uuid, @@ -22,15 +23,21 @@ class Webhook::PullCommentClient private def payload_content - { - "action": "created", + payload_content = { + "action": @action_type, "number": @pull.gitea_number, "pull_request": JSON.parse(@pull.to_builder.target!), - "comment": JSON.parse(@journal.to_builder.target!), + "comment": @journal.present? ? JSON.parse(@journal.to_builder.target!) : @comment_json, "project": JSON.parse(@pull.project.to_builder.target!), "sender": JSON.parse(@sender.to_builder.target!), "is_pull": true } + + payload_content.merge!({ + "changes": @comment_json + }) if @action_type == "edited" + + payload_content end end \ No newline at end of file