From 4bd60d458e36c9b63bd314147a6a69357b59959b Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 12 Apr 2023 18:34:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=EF=BC=9Awebhook=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E8=BF=94=E5=9B=9E=E7=BB=93=E6=9E=84=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/v1/issues/journals_controller.rb | 2 +- app/controllers/journals_controller.rb | 2 +- app/jobs/touch_webhook_job.rb | 8 ++-- app/services/api/v1/issues/create_service.rb | 4 +- .../api/v1/issues/journals/update_service.rb | 2 +- app/services/api/v1/issues/update_service.rb | 11 +++-- app/services/webhook/issue_client.rb | 45 ++++++++++++++++++- 7 files changed, 59 insertions(+), 15 deletions(-) diff --git a/app/controllers/api/v1/issues/journals_controller.rb b/app/controllers/api/v1/issues/journals_controller.rb index af23aa686..4a88c1b0f 100644 --- a/app/controllers/api/v1/issues/journals_controller.rb +++ b/app/controllers/api/v1/issues/journals_controller.rb @@ -27,7 +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!)) + TouchWebhookJob.set(wait: 5.seconds).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 ff214774a..a6c6c8080 100644 --- a/app/controllers/journals_controller.rb +++ b/app/controllers/journals_controller.rb @@ -62,7 +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!)) + TouchWebhookJob.set(wait: 5.seconds).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 8319a9d66..0a8c03e9f 100644 --- a/app/jobs/touch_webhook_job.rb +++ b/app/jobs/touch_webhook_job.rb @@ -25,24 +25,24 @@ class TouchWebhookJob < ApplicationJob end when 'IssueAssign' - issue_id, sender_id, assigner_ids = args[0], args[1], args[2] + 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? issue.project.webhooks.each do |webhook| next unless webhook.events["events"]["issue_assign"] - _, _, @webhook_task = Webhook::IssueClient.new(webhook, issue, sender, "issue_assign",{assigner_ids: assigner_ids}.stringify_keys).do_request + _, _, @webhook_task = Webhook::IssueClient.new(webhook, issue, sender, "issue_assign", changes.stringify_keys).do_request Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" end when 'IssueLabel' - issue_id, sender_id, issue_tag_ids = args[0], args[1], args[2] + 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? issue.project.webhooks.each do |webhook| next unless webhook.events["events"]["issue_label"] - _, _, @webhook_task = Webhook::IssueClient.new(webhook, issue, sender, "issue_label",{issue_tag_ids: issue_tag_ids}.stringify_keys).do_request + _, _, @webhook_task = Webhook::IssueClient.new(webhook, issue, sender, "issue_label", changes.stringify_keys).do_request Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" end diff --git a/app/services/api/v1/issues/create_service.rb b/app/services/api/v1/issues/create_service.rb index b930a91b1..e81f65e19 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.set(wait: 5.seconds).perform_later('IssueCreate', @created_issue&.id, current_user.id) - 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? + TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueLabel', @created_issue&.id, current_user.id, {issue_tag_ids: [[], issue_tag_ids]}) unless issue_tag_ids.blank? + TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueAssign', @created_issue&.id, current_user.id, {assigner_ids: [[], assigner_ids]}) unless assigner_ids.blank? unlock("Api::V1::Issues::CreateService:#{project.id}") # 结束写数据,解锁 end diff --git a/app/services/api/v1/issues/journals/update_service.rb b/app/services/api/v1/issues/journals/update_service.rb index 2e27d53ea..c771d5c1a 100644 --- a/app/services/api/v1/issues/journals/update_service.rb +++ b/app/services/api/v1/issues/journals/update_service.rb @@ -38,7 +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.slice(:notes).stringify_keys) + TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueComment', @issue&.id, @current_user.id, @updated_journal.id, 'edited', @updated_journal.previous_changes.slice(:notes).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 9dfaaa447..fc7eabea6 100644 --- a/app/services/api/v1/issues/update_service.rb +++ b/app/services/api/v1/issues/update_service.rb @@ -5,7 +5,7 @@ class Api::V1::Issues::UpdateService < ApplicationService attr_reader :project, :issue, :current_user attr_reader :status_id, :priority_id, :milestone_id, :branch_name, :start_date, :due_date, :subject, :description - attr_reader :issue_tag_ids, :assigner_ids, :attachment_ids, :receivers_login + attr_reader :issue_tag_ids, :assigner_ids, :attachment_ids, :receivers_login, :before_issue_tag_ids, :before_assigner_ids attr_accessor :add_assigner_ids, :previous_issue_changes, :updated_issue, :atme_receivers validates :project, :issue, :current_user, presence: true @@ -24,6 +24,8 @@ class Api::V1::Issues::UpdateService < ApplicationService @description = params[:description] @issue_tag_ids = params[:issue_tag_ids] @assigner_ids = params[:assigner_ids] + @before_issue_tag_ids = issue.issue_tags.pluck(:id) + @before_assigner_ids = issue.assigners.pluck(:id) @attachment_ids = params[:attachment_ids] @receivers_login = params[:receivers_login] @add_assigner_ids = [] @@ -78,9 +80,10 @@ class Api::V1::Issues::UpdateService < ApplicationService end # 触发webhook - 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? + TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueUpdate', @updated_issue&.id, current_user.id, previous_issue_changes.except(:issue_tags_value, :assigned_to_id)) + TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueLabel', @issue&.id, current_user.id, {issue_tag_ids: [before_issue_tag_ids, issue_tag_ids]}) unless issue_tag_ids.nil? + TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueAssign', @issue&.id, current_user.id, {assigner_ids: [before_assigner_ids, 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_client.rb b/app/services/webhook/issue_client.rb index 92c85d32e..b829f9740 100644 --- a/app/services/webhook/issue_client.rb +++ b/app/services/webhook/issue_client.rb @@ -42,6 +42,30 @@ class Webhook::IssueClient "sender": JSON.parse(@sender.to_builder.target!) } else + if @changes.has_key?("status_id") + before_status = IssueStatus.find_by_id(@changes["status_id"][0]) + after_status = IssueStatus.find_by_id(@changes["status_id"][1]) + @changes["status"] = [] + @changes["status"].append(before_status.present? ? JSON.parse(before_status.to_builder.target!) : {}) + @changes["status"].append(after_status.present? ? JSON.parse(after_status.to_builder.target!) : {}) + @changes.delete("status_id") + end + if @changes.has_key?("fixed_version_id") + before_milestone = Version.find_by_id(@changes["fixed_version_id"][0]) + after_milestone = Version.find_by_id(@changes["fixed_version_id"][1]) + @changes["milestone"] = [] + @changes["milestone"].append(before_milestone.present? ? JSON.parse(before_milestone.to_builder.target!) : {}) + @changes["milestone"].append(after_milestone.present? ? JSON.parse(after_milestone.to_builder.target!) : {}) + @changes.delete("fixed_version_id") + end + if @changes.has_key?("priority_id") + before_priority = IssuePriority.find_by_id(@changes["priority_id"][0]) + after_priority = IssuePriority.find_by_id(@changes["priority_id"][1]) + @changes["priority"] = [] + @changes["priority"].append(before_priority.present? ? JSON.parse(before_priority.to_builder.target!) : {}) + @changes["priority"].append(after_priority.present? ? JSON.parse(after_priority.to_builder.target!) : {}) + @changes.delete("priority_id") + end { "action": "edited", "number": @issue.project_issues_index, @@ -54,8 +78,16 @@ class Webhook::IssueClient end def issue_assign_payload_content + if @changes.has_key?("assigner_ids") + before_assigners = User.where(id: @changes["assigner_ids"][0]) + after_assigners = User.where(id: @changes["assigner_ids"][1]) + @changes["assigners"] = [] + @changes["assigners"].append(before_assigners.blank? ? [] : before_assigners.map{|a|JSON.parse(a.to_buidler.target!)}) + @changes["assigners"].append(after_assigners.blank? ? [] : after_assigners.map{|a|JSON.parse(a.to_builder.target!)}) + @changes.delete("assigner_ids") + end { - "action": @changes["assigner_ids"].blank? ? "unassigned" : "assigned", + "action": @changes["assigners"].blank? ? "unassigned" : "assigned", "number": @issue.project_issues_index, "issue": JSON.parse(@issue.to_builder.target!), "project": JSON.parse(@issue.project.to_builder.target!), @@ -64,8 +96,17 @@ class Webhook::IssueClient end def issue_label_payload_content + if @changes.has_key?("issue_tag_ids") + before_issue_tags = IssueTag.where(id: @changes["issue_tag_ids"][0]) + after_issue_tags = IssueTag.where(id: @changes["issue_tag_ids"][1]) + @changes["issue_tags"] = [] + @changes["issue_tags"].append(before_issue_tags.blank? ? [] : before_issue_tags.map{|t|JSON.parse(t.to_builder.target!)}) + @changes["issue_tags"].append(after_issue_tags.blank? ? [] : after_issue_tags.map{|t|JSON.parse(t.to_builder.target!)}) + @changes.delete("issue_tag_ids") + end { - "action": @changes["issue_tag_ids"].blank? ? "label_cleared" : "label_updated", + "action": @changes["issue_tags"].blank? ? "label_cleared" : "label_updated", + "changes": @changes, "number": @issue.project_issues_index, "issue": JSON.parse(@issue.to_builder.target!), "project": JSON.parse(@issue.project.to_builder.target!),