| @@ -27,6 +27,7 @@ class Api::V1::Issues::JournalsController < Api::V1::BaseController | |||||
| end | end | ||||
| def destroy | def destroy | ||||
| TouchWebhookJob.perform_later('IssueComment', @issue&.id, current_user.id, @journal.id, 'deleted', JSON.parse(@journal.to_builder.target!)) | |||||
| if @journal.destroy! | if @journal.destroy! | ||||
| render_ok | render_ok | ||||
| else | else | ||||
| @@ -46,7 +46,7 @@ class JournalsController < ApplicationController | |||||
| end | end | ||||
| Rails.logger.info "[ATME] maybe to at such users: #{@atme_receivers.pluck(:login)}" | 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 | 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") | # @issue.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "journal") | ||||
| render :json => { status: 0, message: "评论成功", id: journal.id} | render :json => { status: 0, message: "评论成功", id: journal.id} | ||||
| # normal_status(0, "评论成功") | # normal_status(0, "评论成功") | ||||
| @@ -62,6 +62,7 @@ class JournalsController < ApplicationController | |||||
| def destroy | def destroy | ||||
| if @journal.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 | Journal.children_journals(@journal.id).destroy_all | ||||
| normal_status(0, "评论删除成功") | normal_status(0, "评论删除成功") | ||||
| else | else | ||||
| @@ -10,14 +10,14 @@ class TouchWebhookJob < ApplicationJob | |||||
| return if issue.nil? || sender.nil? | return if issue.nil? || sender.nil? | ||||
| issue.project.webhooks.each do |webhook| | issue.project.webhooks.each do |webhook| | ||||
| next unless webhook.events["events"]["issues"] | 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}" | Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" | ||||
| end | end | ||||
| when 'IssueUpdate' | when 'IssueUpdate' | ||||
| issue_id, sender_id, changes = args[0], args[1], args[2] | issue_id, sender_id, changes = args[0], args[1], args[2] | ||||
| issue = Issue.find_by_id issue_id | issue = Issue.find_by_id issue_id | ||||
| sender = User.find_by_id sender_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| | issue.project.webhooks.each do |webhook| | ||||
| next unless webhook.events["events"]["issues"] | next unless webhook.events["events"]["issues"] | ||||
| _, _, @webhook_task = Webhook::IssueClient.new(webhook, issue, sender, "issues", changes.stringify_keys).do_request | _, _, @webhook_task = Webhook::IssueClient.new(webhook, issue, sender, "issues", changes.stringify_keys).do_request | ||||
| @@ -47,29 +47,29 @@ class TouchWebhookJob < ApplicationJob | |||||
| end | end | ||||
| when 'IssueComment' | 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 | issue = Issue.find_by_id issue_id | ||||
| comment = issue.comment_journals.find_by_id comment_id | comment = issue.comment_journals.find_by_id comment_id | ||||
| sender = User.find_by_id sender_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| | issue.project.webhooks.each do |webhook| | ||||
| next unless webhook.events["events"]["issue_comment"] | 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}" | Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" | ||||
| end | end | ||||
| when 'PullRequestComment' | 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) | issue = Issue.find_by_id(issue_id) | ||||
| comment = issue.comment_journals.find_by_id comment_id | comment = issue.comment_journals.find_by_id comment_id | ||||
| sender = User.find_by_id sender_id | sender = User.find_by_id sender_id | ||||
| pull = issue.try(:pull_request) | 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| | pull.project.webhooks.each do |webhook| | ||||
| next unless webhook.events["events"]["pull_request_comment"] | 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}" | Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" | ||||
| end | end | ||||
| end | end | ||||
| @@ -275,6 +275,12 @@ class Journal < ApplicationRecord | |||||
| def to_builder | def to_builder | ||||
| Jbuilder.new do |journal| | Jbuilder.new do |journal| | ||||
| journal.(self, :id, :notes, :comments_count) | 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 | end | ||||
| end | end | ||||
| @@ -70,8 +70,8 @@ class Api::V1::Issues::CreateService < ApplicationService | |||||
| # 触发webhook | # 触发webhook | ||||
| TouchWebhookJob.perform_later('IssueCreate', @created_issue&.id, current_user.id) | 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}") # 结束写数据,解锁 | unlock("Api::V1::Issues::CreateService:#{project.id}") # 结束写数据,解锁 | ||||
| end | end | ||||
| @@ -41,7 +41,7 @@ class Api::V1::Issues::Journals::CreateService < ApplicationService | |||||
| # @信息发送 | # @信息发送 | ||||
| AtmeService.call(current_user, @atme_receivers, @created_journal) unless receivers_login.blank? | 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}") | unlock("Api::V1::Issues::Journals::CreateService:#{@issue.id}") | ||||
| @created_journal | @created_journal | ||||
| @@ -38,6 +38,7 @@ class Api::V1::Issues::Journals::UpdateService < ApplicationService | |||||
| # @信息发送 | # @信息发送 | ||||
| AtmeService.call(current_user, @atme_receivers, @created_journal) unless receivers_login.blank? | 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}") | unlock("Api::V1::Issues::Journals::UpdateService:#{@issue.id}:#{@journal.id}") | ||||
| @@ -78,9 +78,9 @@ class Api::V1::Issues::UpdateService < ApplicationService | |||||
| end | end | ||||
| # 触发webhook | # 触发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}") | unlock("Api::V1::Issues::UpdateService:#{project.id}:#{issue.id}") | ||||
| return @updated_issue | return @updated_issue | ||||
| @@ -2,14 +2,16 @@ class Webhook::IssueCommentClient | |||||
| include Webhook::Client | 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 | @webhook = webhook | ||||
| @issue = issue | @issue = issue | ||||
| @journal = journal | @journal = journal | ||||
| @sender = sender | @sender = sender | ||||
| @event = event | @event = event | ||||
| @action_type = action_type | |||||
| @comment_json = comment_json | |||||
| # 构建client参数 | # 构建client参数 | ||||
| super({ | super({ | ||||
| @@ -25,13 +27,19 @@ class Webhook::IssueCommentClient | |||||
| private | private | ||||
| def payload_content | def payload_content | ||||
| { | |||||
| "action": "created", | |||||
| payload_content = { | |||||
| "action": @action_type, | |||||
| "issue": JSON.parse(@issue.to_builder.target!), | "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!), | "project": JSON.parse(@issue.project.to_builder.target!), | ||||
| "sender": JSON.parse(@sender.to_builder.target!), | "sender": JSON.parse(@sender.to_builder.target!), | ||||
| "is_pull": false | "is_pull": false | ||||
| } | } | ||||
| payload_content.merge!({ | |||||
| "changes": @comment_json, | |||||
| }) if @action_type == "edited" | |||||
| payload_content | |||||
| end | end | ||||
| end | end | ||||
| @@ -1,15 +1,16 @@ | |||||
| class Webhook::PullCommentClient | class Webhook::PullCommentClient | ||||
| include Webhook::Client | 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 | @webhook = webhook | ||||
| @pull = pull | @pull = pull | ||||
| @journal = journal | @journal = journal | ||||
| @sender = sender | @sender = sender | ||||
| @event = event | @event = event | ||||
| @action_type = action_type | |||||
| @comment_json = comment_json | |||||
| # 构建client参数 | # 构建client参数 | ||||
| super({ | super({ | ||||
| uuid: SecureRandom.uuid, | uuid: SecureRandom.uuid, | ||||
| @@ -22,15 +23,21 @@ class Webhook::PullCommentClient | |||||
| private | private | ||||
| def payload_content | def payload_content | ||||
| { | |||||
| "action": "created", | |||||
| payload_content = { | |||||
| "action": @action_type, | |||||
| "number": @pull.gitea_number, | "number": @pull.gitea_number, | ||||
| "pull_request": JSON.parse(@pull.to_builder.target!), | "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!), | "project": JSON.parse(@pull.project.to_builder.target!), | ||||
| "sender": JSON.parse(@sender.to_builder.target!), | "sender": JSON.parse(@sender.to_builder.target!), | ||||
| "is_pull": true | "is_pull": true | ||||
| } | } | ||||
| payload_content.merge!({ | |||||
| "changes": @comment_json | |||||
| }) if @action_type == "edited" | |||||
| payload_content | |||||
| end | end | ||||
| end | end | ||||