From 17279c56f4f0be362d44fb84ec00159310587eb2 Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 29 Mar 2023 18:49:20 +0800 Subject: [PATCH 01/20] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9Awebhook=20serv?= =?UTF-8?q?ice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/attachment.rb | 88 +++++++++++--------- app/models/gitea/webhook_task.rb | 6 +- app/models/issue.rb | 26 ++++++ app/models/issue_priority.rb | 6 ++ app/models/issue_status.rb | 6 ++ app/models/issue_tag.rb | 7 ++ app/models/project.rb | 55 ++++++++++++ app/models/user.rb | 10 +++ app/models/version.rb | 6 ++ app/services/webhook/client.rb | 92 +++++++++++++++++++++ app/services/webhook/issue_create_client.rb | 53 ++++++++++++ 11 files changed, 311 insertions(+), 44 deletions(-) create mode 100644 app/services/webhook/client.rb create mode 100644 app/services/webhook/issue_create_client.rb diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 3451246af..0cbf6fb0f 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -1,42 +1,42 @@ -# == Schema Information -# -# Table name: attachments -# -# id :integer not null, primary key -# container_id :integer -# container_type :string(30) -# filename :string(255) default(""), not null -# disk_filename :string(255) default(""), not null -# filesize :integer default("0"), not null -# content_type :string(255) default("") -# digest :string(60) default(""), not null -# downloads :integer default("0"), not null -# author_id :integer default("0"), not null -# created_on :datetime -# description :text(65535) -# disk_directory :string(255) -# attachtype :integer default("1") -# is_public :integer default("1") -# copy_from :string(255) -# quotes :integer default("0") -# is_publish :integer default("1") -# publish_time :datetime -# resource_bank_id :integer -# unified_setting :boolean default("1") -# cloud_url :string(255) default("") -# course_second_category_id :integer default("0") -# delay_publish :boolean default("0") -# link :string(255) -# clone_id :integer -# -# Indexes -# -# index_attachments_on_author_id (author_id) -# index_attachments_on_clone_id (clone_id) -# index_attachments_on_container_id_and_container_type (container_id,container_type) -# index_attachments_on_created_on (created_on) -# - +# == Schema Information +# +# Table name: attachments +# +# id :integer not null, primary key +# container_id :integer +# container_type :string(30) +# filename :string(255) default(""), not null +# disk_filename :string(255) default(""), not null +# filesize :integer default("0"), not null +# content_type :string(255) default("") +# digest :string(60) default(""), not null +# downloads :integer default("0"), not null +# author_id :integer default("0"), not null +# created_on :datetime +# description :text(65535) +# disk_directory :string(255) +# attachtype :integer default("1") +# is_public :integer default("1") +# copy_from :string(255) +# quotes :integer default("0") +# is_publish :integer default("1") +# publish_time :datetime +# resource_bank_id :integer +# unified_setting :boolean default("1") +# cloud_url :string(255) default("") +# course_second_category_id :integer default("0") +# delay_publish :boolean default("0") +# link :string(255) +# clone_id :integer +# +# Indexes +# +# index_attachments_on_author_id (author_id) +# index_attachments_on_clone_id (clone_id) +# index_attachments_on_container_id_and_container_type (container_id,container_type) +# index_attachments_on_created_on (created_on) +# + class Attachment < ApplicationRecord @@ -184,4 +184,14 @@ class Attachment < ApplicationRecord is_pdf end + def to_builder + Jbuilder.new do |attachment| + attachment.id self.id + attachment.title self.title + attachment.filesize self.filesize + attachment.is_pdf self.is_pdf? + attachment.created_on self.created_on.strftime("%Y-%m-%d %H:%M") + attachment.content_type self.content_type + end + end end diff --git a/app/models/gitea/webhook_task.rb b/app/models/gitea/webhook_task.rb index 325352c69..7e9bc68a7 100644 --- a/app/models/gitea/webhook_task.rb +++ b/app/models/gitea/webhook_task.rb @@ -1,6 +1,7 @@ class Gitea::WebhookTask < Gitea::Base serialize :payload_content, JSON serialize :request_content, JSON + serialize :response_content, JSON self.inheritance_column = nil @@ -10,9 +11,4 @@ class Gitea::WebhookTask < Gitea::Base enum type: {gogs: 1, slack: 2, gitea: 3, discord: 4, dingtalk: 5, telegram: 6, msteams: 7, feishu: 8, matrix: 9} - def response_content_json - JSON.parse(response_content) - rescue - {} - end end \ No newline at end of file diff --git a/app/models/issue.rb b/app/models/issue.rb index 0d38f7a2a..4ec77025e 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -217,4 +217,30 @@ class Issue < ApplicationRecord SendTemplateMessageJob.perform_later('IssueExpire', self.id) if Site.has_notice_menu? && self.due_date == Date.today + 1.days end + def to_builder + Jbuilder.new do |issue| + issue.(self, :id, :project_issues_index, :subject, :description) + issue.created_at self.created_on.strftime("%Y-%m-%d %H:%M") + issue.updated_at self.updated_on.strftime("%Y-%m-%d %H:%M") + issue.tags self.show_issue_tags.map{|t| t.to_builder} + issue.status self.issue_status.to_builder + if self.priority.present? + issue.priority self.priority.to_builder + else + issue.priority nil + end + if self.version.present? + issue.milestone self.version.to_builder + else + issue.milestone nil + end + issue.author self.user.to_builder + issue.assigners self.show_assigners.map{|t| t.to_builder} + issue.participants self.participants.distinct.map{|t| t.to_builder} + issue.comment_journals_count self.comment_journals.size + issue.operate_journals_count self.operate_journals.size + issue.attachments self.attachments.map{|t| t.to_builder} + end + end + end diff --git a/app/models/issue_priority.rb b/app/models/issue_priority.rb index c8ef73299..5bf70da05 100644 --- a/app/models/issue_priority.rb +++ b/app/models/issue_priority.rb @@ -32,4 +32,10 @@ class IssuePriority < ApplicationRecord end end end + + def to_builder + Jbuilder.new do |priority| + priority.(self, :id, :name) + end + end end diff --git a/app/models/issue_status.rb b/app/models/issue_status.rb index fcce29c32..fde871182 100644 --- a/app/models/issue_status.rb +++ b/app/models/issue_status.rb @@ -44,4 +44,10 @@ class IssueStatus < ApplicationRecord end end end + + def to_builder + Jbuilder.new do |status| + status.(self, :id, :name) + end + end end diff --git a/app/models/issue_tag.rb b/app/models/issue_tag.rb index ad6a82763..0da4ca730 100644 --- a/app/models/issue_tag.rb +++ b/app/models/issue_tag.rb @@ -53,4 +53,11 @@ class IssueTag < ApplicationRecord self.update_column(:pull_requests_count, pull_request_issues.size) end + + def to_builder + Jbuilder.new do |tag| + tag.(self, :id, :name, :description) + end + end + end diff --git a/app/models/project.rb b/app/models/project.rb index f08daae9e..02844c3bc 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -446,4 +446,59 @@ class Project < ApplicationRecord def del_project_issue_cache_delete_count $redis_cache.hdel("issue_cache_delete_count", self.id) end + + def to_builder + Jbuilder.new do |project| + project.id self.id + project.identifier self.identifier + project.name self.name + project.description Nokogiri::HTML(self.description).text + project.visits self.visits + project.praises_count self.praises_count.to_i + project.watchers_count self.watchers_count.to_i + project.issues_count self.issues_count.to_i + project.pull_requests_count self.pull_requests_count.to_i + project.forked_count self.forked_count.to_i + project.is_public self.is_public + project.mirror_url self.repository&.mirror_url + project.type self&.project_type + project.created_at self.created_on.strftime("%Y-%m-%d %H:%M") + project.updated_at self.updated_on.strftime("%Y-%m-%d %H:%M") + project.forked_from_project_id self.forked_from_project_id + project.platform self.platform + project.author do + if self.educoder? + project_educoder = self.project_educoder + project.name project_educoder&.owner + project.type 'Educoder' + project.login project_educoder&.repo_name.split('/')[0] + project.image_url render_educoder_avatar_url(self.project_educoder) + else + user = self.owner + project.name user.try(:show_real_name) + project.type user&.type + project.login user.login + project.image_url user.get_letter_avatar_url + end + end + + project.category do + if self.project_category.blank? + project.nil! + else + project.id self.project_category.id + project.name self.project_category.name + end + end + project.language do + if self.project_language.blank? + project.nil! + else + project.id self.project_language.id + project.name self.project_language.name + end + end + + end + end end diff --git a/app/models/user.rb b/app/models/user.rb index c3c62f9eb..70de1fc64 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -859,6 +859,16 @@ class User < Owner end end + def to_builder + Jbuilder.new do |user| + user.(self, :id, :login) + user.name self.real_name + user.email self.mail + user.image_url self.get_letter_avatar_url + end + end + + protected def validate_password_length # 管理员的初始密码是5位 diff --git a/app/models/version.rb b/app/models/version.rb index 1e14a135c..82474f55e 100644 --- a/app/models/version.rb +++ b/app/models/version.rb @@ -55,6 +55,12 @@ class Version < ApplicationRecord User.select(:login, :lastname,:firstname, :nickname)&.find_by_id(self.user_id) end + def to_builder + Jbuilder.new do |version| + version.(self, :id, :name, :description, :effective_date) + end + end + private def send_create_message_to_notice_system SendTemplateMessageJob.perform_later('ProjectMilestone', self.id, self.user_id) if Site.has_notice_menu? diff --git a/app/services/webhook/client.rb b/app/services/webhook/client.rb new file mode 100644 index 000000000..05e687f13 --- /dev/null +++ b/app/services/webhook/client.rb @@ -0,0 +1,92 @@ +module Webhook::Client + + # uuid SecureRandom.uuid + # hmac = OpenSSL::HMAC.new(secret, OpenSSL::Digest::SHA1.new) + # message = Gitea::WebhookTask.last.read_attribute_before_type_cast("payload_content") + # hmac.update(message) + # sha1 = hmac.digest.unpack('H*').first + + attr_reader :uuid, :event, :http_method, :content_type, :url, :secret, :payload_content + attr_accessor :request_content, :response_content + + def initialize(opts) + @uuid = opts[:uuid] + @event = opts[:event] + @http_method = opts[:http_method] + @content_type = opts[:content_type] + @url = opts[:url] + @secret = opts[:secret] + @payload_content = opts[:payload_content] + @request_content = {} + @response_content = {} + end + + def do_request + headers = {} + headers['Content-Type'] = trans_content_type + headers["X-Gitea-Delivery"] = @uuid + headers["X-Gitea-Event"] = @event + headers["X-Gitea-Event-Type"] = @event + headers["X-Gitea-Signature"] = signatureSHA256 + headers["X-Gogs-Delivery"] = @uuid + headers["X-Gogs-Event"] = @event + headers["X-Gogs-Event-Type"] = @event + headers["X-Gogs-Signature"] = signatureSHA256 + headers["X-Hub-Signature"] = "sha1=" + signatureSHA1 + headers["X-Hub-Signature-256"] = "sha256=" + signatureSHA256 + headers["X-GitHub-Delivery"] = @uuid + headers["X-GitHub-Event"] = @event + headers["X-GitHub-Event-Type"] = @event + @request_content["url"] = @url + @request_content["http_method"] = @http_method + @request_content["headers"] = headers + + response = RestClient::Request.execute(method: trans_http_method, url: @url, headers: headers, payload: payload_content) {|response, request, result| response } + + @response_content["status"] = response.code + @response_content["headers"] = response.headers + @response_content["body"] = response.body.to_json + + return @request_content, @response_content + end + + def request_content + @request_content + end + + def response_content + @response_content + end + + private + def signatureSHA1 + hmac = OpenSSL::HMAC.new(@secret, OpenSSL::Digest::SHA1.new) + message = @payload_content + + hmac.digest.unpack('H*').first + end + + def signatureSHA256 + hmac = OpenSSL::HMAC.new(@secret, OpenSSL::Digest::SHA256.new) + message = @payload_content + + hmac.digest.unpack('H*').first + end + + def trans_content_type + if @content_type == "form" + return "application/x-www-form-urlencoded" + else + return "application/json" + end + end + + def trans_http_method + if @http_method == "GET" + return :get + else + return :post + end + end + +end \ No newline at end of file diff --git a/app/services/webhook/issue_create_client.rb b/app/services/webhook/issue_create_client.rb new file mode 100644 index 000000000..9e8b94bc3 --- /dev/null +++ b/app/services/webhook/issue_create_client.rb @@ -0,0 +1,53 @@ +class Webhook::IssueCreateClient + + include Webhook::Client + + attr_accessor :webhook, :issue, :sender + attr_accessor :webhook_task + + def initialize(webhook, issue, sender) + @webhook = webhook + @issue = issue + @sender = sender + # 创建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 + }) + end + + def payload_content + { + "action": "opened", + "number": @issue.project_issues_index, + "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 From b4672929dc7cc550df041712a7023103a2d942d9 Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 30 Mar 2023 10:19:50 +0800 Subject: [PATCH 02/20] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E8=A7=A6?= =?UTF-8?q?=E5=8F=91webhook=E7=9A=84job?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/touch_webhook_job.rb | 28 ++++++++++ app/services/api/v1/issues/create_service.rb | 3 ++ app/services/api/v1/issues/update_service.rb | 3 ++ app/services/webhook/issue_create_client.rb | 2 + app/services/webhook/issue_update_client.rb | 57 ++++++++++++++++++++ config/sidekiq.yml | 1 + 6 files changed, 94 insertions(+) create mode 100644 app/jobs/touch_webhook_job.rb create mode 100644 app/services/webhook/issue_update_client.rb 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] From 91dfc94a7bec3cbfd8483419bd77dcf3ac5b9e93 Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 6 Apr 2023 17:13:47 +0800 Subject: [PATCH 03/20] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E6=AD=A3=E5=B8=B8=E6=98=BE=E7=A4=BAresponse=5Fcontent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/api/v1/projects/webhooks/hooktasks.json.jbuilder | 2 +- app/views/projects/webhooks/tasks.json.jbuilder | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/views/api/v1/projects/webhooks/hooktasks.json.jbuilder b/app/views/api/v1/projects/webhooks/hooktasks.json.jbuilder index 618a05515..22070cc68 100644 --- a/app/views/api/v1/projects/webhooks/hooktasks.json.jbuilder +++ b/app/views/api/v1/projects/webhooks/hooktasks.json.jbuilder @@ -1,6 +1,6 @@ json.total_count @hooktasks.total_count json.hooktasks @hooktasks.each do |task| json.(task, :id, :event_type, :type, :uuid, :is_succeed, :is_delivered, :payload_content, :request_content) - json.response_content task.response_content_json + json.response_content task.response_content json.delivered_time Time.at(task.delivered*10**-9).strftime("%Y-%m-%d %H:%M:%S") end \ No newline at end of file diff --git a/app/views/projects/webhooks/tasks.json.jbuilder b/app/views/projects/webhooks/tasks.json.jbuilder index 82b2eae4a..8e1809011 100644 --- a/app/views/projects/webhooks/tasks.json.jbuilder +++ b/app/views/projects/webhooks/tasks.json.jbuilder @@ -1,6 +1,6 @@ json.total_count @tasks.total_count json.tasks @tasks.each do |task| json.(task, :id, :event_type, :type, :uuid, :is_succeed, :is_delivered, :payload_content, :request_content) - json.response_content task.response_content_json - json.delivered_time Time.at(task.delivered*10**-9).strftime("%Y-%m-%d %H:%M:%S") + json.response_content task.response_content + json.delivered_time task.delivered.present? ? Time.at(task.delivered*10**-9).strftime("%Y-%m-%d %H:%M:%S") : nil end \ No newline at end of file From 769c8a019490b34561b624e0819a3d46b56cb953 Mon Sep 17 00:00:00 2001 From: chenjing <28122123@qq.com> Date: Thu, 6 Apr 2023 17:00:43 +0800 Subject: [PATCH 04/20] webhook for issue --- app/controllers/journals_controller.rb | 1 + app/jobs/touch_webhook_job.rb | 67 ++++++++- app/services/api/v1/issues/create_service.rb | 3 +- .../api/v1/issues/journals/create_service.rb | 2 +- app/services/api/v1/issues/update_service.rb | 3 +- app/services/webhook/event_client .rb | 130 ++++++++++++++++++ 6 files changed, 201 insertions(+), 5 deletions(-) create mode 100644 app/services/webhook/event_client .rb diff --git a/app/controllers/journals_controller.rb b/app/controllers/journals_controller.rb index 6dc1e29c9..e539ec2cd 100644 --- a/app/controllers/journals_controller.rb +++ b/app/controllers/journals_controller.rb @@ -46,6 +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) # @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, "评论成功") diff --git a/app/jobs/touch_webhook_job.rb b/app/jobs/touch_webhook_job.rb index 9959d2ebc..91155398e 100644 --- a/app/jobs/touch_webhook_job.rb +++ b/app/jobs/touch_webhook_job.rb @@ -10,7 +10,7 @@ class TouchWebhookJob < ApplicationJob 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 + @webhook_task = Webhook::EventClient.new(webhook, issue, sender,"issues","issues").do_request Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" end when 'IssueUpdate' @@ -20,7 +20,70 @@ class TouchWebhookJob < ApplicationJob 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 + @webhook_task = Webhook::EventClient.new(webhook, issue, sender, "issues", "issues",changes.stringify_keys).do_request + Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" + end + + when 'IssueAssign' + issue_id, sender_id, assigner_ids = 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::EventClient.new(webhook, issue, sender, "issues", "issue_assign",{assigner_ids: assigner_ids}).do_request + Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" + end + + when 'IssueLable' + issue_id, sender_id, issue_tag_ids = 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::EventClient.new(webhook, issue, sender, "issues", "issue_label",{issue_tag_ids: issue_tag_ids}).do_request + Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" + end + + when 'IssueComment' + issue_id, sender_id, comment_id = args[0], args[1], args[2] + 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? + + changes = { + id: comment.id, + user: comment.user.to_builder.target!, + body: comment.notes, + created_on: comment.created_on, + updated_on: comment.updated_on, + } + + issue.project.webhooks.each do |webhook| + next unless webhook.events["events"]["issue_comment"] + @webhook_task = Webhook::EventClient.new(webhook, issue, sender, "issue_comment", "issue_comment",changes.stringify_keys).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 = 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? + + changes = { + id: comment.id, + user: comment.user.to_builder.target!, + body: comment.notes, + created_on: comment.created_on, + updated_on: comment.updated_on, + } + issue.project.webhooks.each do |webhook| + next unless webhook.events["events"]["pull_request_comment"] + @webhook_task = Webhook::EventClient.new(webhook, issue, sender, "issue_comment", "pull_request_comment",changes.stringify_keys).do_request Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" end end diff --git a/app/services/api/v1/issues/create_service.rb b/app/services/api/v1/issues/create_service.rb index 7080c3bda..b0b1b05b0 100644 --- a/app/services/api/v1/issues/create_service.rb +++ b/app/services/api/v1/issues/create_service.rb @@ -70,7 +70,8 @@ class Api::V1::Issues::CreateService < ApplicationService # 触发webhook TouchWebhookJob.perform_later('IssueCreate', @created_issue&.id, current_user.id) - + TouchWebhookJob.perform_later('IssueLable', @created_issue&.id, issue_tag_ids) + TouchWebhookJob.perform_later('IssueAssign', @created_issue&.id, assigner_ids) 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 dce00349b..568deecdd 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) unlock("Api::V1::Issues::Journals::CreateService:#{@issue.id}") @created_journal diff --git a/app/services/api/v1/issues/update_service.rb b/app/services/api/v1/issues/update_service.rb index 21242edc2..153ea32e0 100644 --- a/app/services/api/v1/issues/update_service.rb +++ b/app/services/api/v1/issues/update_service.rb @@ -79,7 +79,8 @@ class Api::V1::Issues::UpdateService < ApplicationService # 触发webhook TouchWebhookJob.perform_later('IssueCreate', @updated_issue&.id, current_user.id, previous_issue_changes) - + TouchWebhookJob.perform_later('IssueLable', @updated_issue&.id, issue_tag_ids) + TouchWebhookJob.perform_later('IssueAssign', @updated_issue&.id, assigner_ids) unlock("Api::V1::Issues::UpdateService:#{project.id}:#{issue.id}") return @updated_issue diff --git a/app/services/webhook/event_client .rb b/app/services/webhook/event_client .rb new file mode 100644 index 000000000..28e473be5 --- /dev/null +++ b/app/services/webhook/event_client .rb @@ -0,0 +1,130 @@ +class Webhook::EventClient + + include Webhook::Client + + attr_accessor :webhook, :issue, :sender + attr_accessor :webhook_task + + def initialize(webhook, issue, sender, event, event_type, changes={}) + @webhook = webhook + @issue = issue + @sender = sender + @event = event + @event_type = event_type + @changes = changes + # 创建webhook task + @webhook_task = Gitea::WebhookTask.create( + hook_id: @webhook.id, + uuid: SecureRandom.uuid, + payload_content: payload_content, + event_type: @event_type, + is_delivered: true + ) + + # 构建client参数 + super({ + uuid: @webhook_task.uuid, + event: @event, + 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 + case @event_type + when "issues" + issue_payload_content + when "issue_assign" + issue_assign_payload_content + when "issue_label" + issue_label_payload_content + when "issue_comment" + issue_comment_payload_content + when "pull_request_comment" + pull_request_comment_payload_content + end + end + + def issue_payload_content + if @changes.blank? + { + "action": "opened", + "number": @issue.project_issues_index, + "issue": JSON.parse(@issue.to_builder.target!), + "project": JSON.parse(@issue.project.to_builder.target!), + "sender": JSON.parse(@sender.to_builder.target!) + } + else + { + "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 + + def issue_assign_payload_content + { + "action": @changes["assigner_ids"].blank? ? "unassigned" : "assigned", + "number": @issue.project_issues_index, + "issue": JSON.parse(@issue.to_builder.target!), + "assignees": @issue.assignees.map(&:to_builder.target!), + "project": JSON.parse(@issue.project.to_builder.target!), + "sender": JSON.parse(@sender.to_builder.target!) + } + end + + def issue_label_payload_content + { + "action": @changes["issue_tag_ids"].blank? ? "label_cleared" : "label_updated", + "number": @issue.project_issues_index, + "issue": JSON.parse(@issue.to_builder.target!), + "project": JSON.parse(@issue.project.to_builder.target!), + "sender": JSON.parse(@sender.to_builder.target!) + } + end + + def issue_comment_payload_content + { + "action": "created", + "number": @issue.project_issues_index, + "issue": JSON.parse(@issue.to_builder.target!), + "comment": @changes, + "project": JSON.parse(@issue.project.to_builder.target!), + "sender": JSON.parse(@sender.to_builder.target!) + } + end + + def pull_request_comment_payload_content + { + "action": "created", + "number": @issue.project_issues_index, + "issue": JSON.parse(@issue.to_builder.target!), + "comment": @changes, + "project": JSON.parse(@issue.project.to_builder.target!), + "sender": JSON.parse(@sender.to_builder.target!) + } + end + + + + +end \ No newline at end of file From cd489d3b9767b931c1b6437cb131ed7eec0020a8 Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 6 Apr 2023 19:19:54 +0800 Subject: [PATCH 05/20] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/touch_webhook_job.rb | 41 ++---- app/models/journal.rb | 6 +- app/models/pull_request.rb | 14 ++ app/services/webhook/client.rb | 36 +++-- app/services/webhook/event_client .rb | 130 ------------------- app/services/webhook/issue_client.rb | 75 +++++++++++ app/services/webhook/issue_comment_client.rb | 37 ++++++ app/services/webhook/issue_create_client.rb | 55 -------- app/services/webhook/issue_update_client.rb | 57 -------- app/services/webhook/pull_comment_client.rb | 36 +++++ 10 files changed, 208 insertions(+), 279 deletions(-) delete mode 100644 app/services/webhook/event_client .rb create mode 100644 app/services/webhook/issue_client.rb create mode 100644 app/services/webhook/issue_comment_client.rb delete mode 100644 app/services/webhook/issue_create_client.rb delete mode 100644 app/services/webhook/issue_update_client.rb create mode 100644 app/services/webhook/pull_comment_client.rb diff --git a/app/jobs/touch_webhook_job.rb b/app/jobs/touch_webhook_job.rb index 91155398e..77475098b 100644 --- a/app/jobs/touch_webhook_job.rb +++ b/app/jobs/touch_webhook_job.rb @@ -10,7 +10,7 @@ class TouchWebhookJob < ApplicationJob return if issue.nil? || sender.nil? issue.project.webhooks.each do |webhook| next unless webhook.events["events"]["issues"] - @webhook_task = Webhook::EventClient.new(webhook, issue, sender,"issues","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' @@ -20,7 +20,7 @@ class TouchWebhookJob < ApplicationJob 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::EventClient.new(webhook, issue, sender, "issues", "issues",changes.stringify_keys).do_request + _, _, @webhook_task = Webhook::IssueClient.new(webhook, issue, sender, "issues", changes.stringify_keys).do_request Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" end @@ -31,18 +31,18 @@ class TouchWebhookJob < ApplicationJob return if issue.nil? || sender.nil? issue.project.webhooks.each do |webhook| next unless webhook.events["events"]["issue_assign"] - @webhook_task = Webhook::EventClient.new(webhook, issue, sender, "issues", "issue_assign",{assigner_ids: assigner_ids}).do_request + _, _, @webhook_task = Webhook::IssueClient.new(webhook, issue, sender, "issue_assign",{assigner_ids: assigner_ids}.stringify_keys).do_request Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" end - when 'IssueLable' + when 'IssueLabel' issue_id, sender_id, issue_tag_ids = 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::EventClient.new(webhook, issue, sender, "issues", "issue_label",{issue_tag_ids: issue_tag_ids}).do_request + _, _, @webhook_task = Webhook::IssueClient.new(webhook, issue, sender, "issue_label",{issue_tag_ids: issue_tag_ids}.stringify_keys).do_request Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}" end @@ -53,37 +53,22 @@ class TouchWebhookJob < ApplicationJob sender = User.find_by_id sender_id return if issue.nil? || sender.nil? || comment.nil? - changes = { - id: comment.id, - user: comment.user.to_builder.target!, - body: comment.notes, - created_on: comment.created_on, - updated_on: comment.updated_on, - } - issue.project.webhooks.each do |webhook| next unless webhook.events["events"]["issue_comment"] - @webhook_task = Webhook::EventClient.new(webhook, issue, sender, "issue_comment", "issue_comment",changes.stringify_keys).do_request + _, _, @webhook_task = Webhook::IssueCommentClient.new(webhook, issue, comment, sender, "issue_comment").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 = Issue.find_by_id issue_id - comment = issue.comment_journals.find_by_id comment_id + pull_id, sender_id, comment_id = args[0], args[1], args[2] + pull = PullRequest.find_by_id pull_id + comment = pull.issue.comment_journals.find_by_id comment_id sender = User.find_by_id sender_id - return if issue.nil? || sender.nil? || comment.nil? - - changes = { - id: comment.id, - user: comment.user.to_builder.target!, - body: comment.notes, - created_on: comment.created_on, - updated_on: comment.updated_on, - } - issue.project.webhooks.each do |webhook| + return if pull.nil? || sender.nil? || comment.nil? + + pull.project.webhooks.each do |webhook| next unless webhook.events["events"]["pull_request_comment"] - @webhook_task = Webhook::EventClient.new(webhook, issue, sender, "issue_comment", "pull_request_comment",changes.stringify_keys).do_request + _, _, @webhook_task = Webhook::PullCommentClient.new(webhook, pull, comment, sender, "pull_request_comment").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 5f56b6f78..703a21e82 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -272,5 +272,9 @@ class Journal < ApplicationRecord send_details end - + def to_builder + Jbuilder.new do |journal| + journal.(self, :id, :notes, :comments_count) + end + end end diff --git a/app/models/pull_request.rb b/app/models/pull_request.rb index 270e7dc76..78debfe78 100644 --- a/app/models/pull_request.rb +++ b/app/models/pull_request.rb @@ -117,4 +117,18 @@ class PullRequest < ApplicationRecord JSON.parse file_names end + + def to_builder + Jbuilder.new do |pull| + pull.(self, :id, :gitea_number, :title, :body, :base, :head, :is_original, :comments_count) + pull.created_at self.created_at.strftime("%Y-%m-%d %H:%M") + pull.updated_at self.updated_at.strftime("%Y-%m-%d %H:%M") + pull.user self.user.to_builder + if self.fork_project.present? + pull.fork_project self.fork_project.to_builder + else + pull.fork_project nil + end + end + end end diff --git a/app/services/webhook/client.rb b/app/services/webhook/client.rb index 05e687f13..a64753d05 100644 --- a/app/services/webhook/client.rb +++ b/app/services/webhook/client.rb @@ -6,19 +6,27 @@ module Webhook::Client # hmac.update(message) # sha1 = hmac.digest.unpack('H*').first - attr_reader :uuid, :event, :http_method, :content_type, :url, :secret, :payload_content - attr_accessor :request_content, :response_content + attr_reader :uuid, :event, :http_method, :content_type, :url, :secret, :payload_content, :webhook + attr_accessor :request_content, :response_content, :webhook_task def initialize(opts) @uuid = opts[:uuid] @event = opts[:event] - @http_method = opts[:http_method] - @content_type = opts[:content_type] - @url = opts[:url] - @secret = opts[:secret] + @webhook = opts[:webhook] + @http_method = @webhook.http_method + @content_type = @webhook.content_type + @url = @webhook.url + @secret = @webhook.secret @payload_content = opts[:payload_content] @request_content = {} @response_content = {} + @webhook_task = Gitea::WebhookTask.create( + hook_id: @webhook.id, + uuid: @uuid, + payload_content: @payload_content, + event_type: @event, + is_delivered: true + ) end def do_request @@ -41,13 +49,21 @@ module Webhook::Client @request_content["http_method"] = @http_method @request_content["headers"] = headers - response = RestClient::Request.execute(method: trans_http_method, url: @url, headers: headers, payload: payload_content) {|response, request, result| response } + response = RestClient::Request.execute(method: trans_http_method, url: @url, headers: headers, payload: @webhook_task.read_attribute_before_type_cast("payload_content")) {|response, request, result| response } @response_content["status"] = response.code @response_content["headers"] = response.headers @response_content["body"] = response.body.to_json - return @request_content, @response_content + @webhook_task.update_attributes({ + delivered: Time.now.to_i * 1000000000, + is_succeed: @response_content["status"] < 300, + request_content: @request_content, + response_content: @response_content + }) + + + return @request_content, @response_content, @webhook_task end def request_content @@ -58,6 +74,10 @@ module Webhook::Client @response_content end + def webhook_task + @webhook_task + end + private def signatureSHA1 hmac = OpenSSL::HMAC.new(@secret, OpenSSL::Digest::SHA1.new) diff --git a/app/services/webhook/event_client .rb b/app/services/webhook/event_client .rb deleted file mode 100644 index 28e473be5..000000000 --- a/app/services/webhook/event_client .rb +++ /dev/null @@ -1,130 +0,0 @@ -class Webhook::EventClient - - include Webhook::Client - - attr_accessor :webhook, :issue, :sender - attr_accessor :webhook_task - - def initialize(webhook, issue, sender, event, event_type, changes={}) - @webhook = webhook - @issue = issue - @sender = sender - @event = event - @event_type = event_type - @changes = changes - # 创建webhook task - @webhook_task = Gitea::WebhookTask.create( - hook_id: @webhook.id, - uuid: SecureRandom.uuid, - payload_content: payload_content, - event_type: @event_type, - is_delivered: true - ) - - # 构建client参数 - super({ - uuid: @webhook_task.uuid, - event: @event, - 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 - case @event_type - when "issues" - issue_payload_content - when "issue_assign" - issue_assign_payload_content - when "issue_label" - issue_label_payload_content - when "issue_comment" - issue_comment_payload_content - when "pull_request_comment" - pull_request_comment_payload_content - end - end - - def issue_payload_content - if @changes.blank? - { - "action": "opened", - "number": @issue.project_issues_index, - "issue": JSON.parse(@issue.to_builder.target!), - "project": JSON.parse(@issue.project.to_builder.target!), - "sender": JSON.parse(@sender.to_builder.target!) - } - else - { - "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 - - def issue_assign_payload_content - { - "action": @changes["assigner_ids"].blank? ? "unassigned" : "assigned", - "number": @issue.project_issues_index, - "issue": JSON.parse(@issue.to_builder.target!), - "assignees": @issue.assignees.map(&:to_builder.target!), - "project": JSON.parse(@issue.project.to_builder.target!), - "sender": JSON.parse(@sender.to_builder.target!) - } - end - - def issue_label_payload_content - { - "action": @changes["issue_tag_ids"].blank? ? "label_cleared" : "label_updated", - "number": @issue.project_issues_index, - "issue": JSON.parse(@issue.to_builder.target!), - "project": JSON.parse(@issue.project.to_builder.target!), - "sender": JSON.parse(@sender.to_builder.target!) - } - end - - def issue_comment_payload_content - { - "action": "created", - "number": @issue.project_issues_index, - "issue": JSON.parse(@issue.to_builder.target!), - "comment": @changes, - "project": JSON.parse(@issue.project.to_builder.target!), - "sender": JSON.parse(@sender.to_builder.target!) - } - end - - def pull_request_comment_payload_content - { - "action": "created", - "number": @issue.project_issues_index, - "issue": JSON.parse(@issue.to_builder.target!), - "comment": @changes, - "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/app/services/webhook/issue_client.rb b/app/services/webhook/issue_client.rb new file mode 100644 index 000000000..92c85d32e --- /dev/null +++ b/app/services/webhook/issue_client.rb @@ -0,0 +1,75 @@ +class Webhook::IssueClient + + include Webhook::Client + + attr_accessor :webhook, :issue, :sender, :event, :changes + attr_accessor :webhook_task + + def initialize(webhook, issue, sender, event, changes={}) + @webhook = webhook + @issue = issue + @sender = sender + @event = event + @changes = changes + + # 构建client参数 + super({ + uuid: SecureRandom.uuid, + event: @event, + webhook: @webhook, + payload_content: payload_content + }) + end + + def payload_content + case @event + when "issues" + issue_payload_content + when "issue_assign" + issue_assign_payload_content + when "issue_label" + issue_label_payload_content + end + end + + def issue_payload_content + if @changes.blank? + { + "action": "opened", + "number": @issue.project_issues_index, + "issue": JSON.parse(@issue.to_builder.target!), + "project": JSON.parse(@issue.project.to_builder.target!), + "sender": JSON.parse(@sender.to_builder.target!) + } + else + { + "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 + + def issue_assign_payload_content + { + "action": @changes["assigner_ids"].blank? ? "unassigned" : "assigned", + "number": @issue.project_issues_index, + "issue": JSON.parse(@issue.to_builder.target!), + "project": JSON.parse(@issue.project.to_builder.target!), + "sender": JSON.parse(@sender.to_builder.target!) + } + end + + def issue_label_payload_content + { + "action": @changes["issue_tag_ids"].blank? ? "label_cleared" : "label_updated", + "number": @issue.project_issues_index, + "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/app/services/webhook/issue_comment_client.rb b/app/services/webhook/issue_comment_client.rb new file mode 100644 index 000000000..a00953a1d --- /dev/null +++ b/app/services/webhook/issue_comment_client.rb @@ -0,0 +1,37 @@ +class Webhook::IssueCommentClient + + include Webhook::Client + + attr_accessor :webhook, :issue, :journal, :sender, :event + + def initialize(webhook, issue, journal, sender, event) + @webhook = webhook + @issue = issue + @journal = journal + @sender = sender + @event = event + + # 构建client参数 + super({ + uuid: SecureRandom.uuid, + event: @event, + webhook: @webhook, + payload_content: payload_content + }) + end + + + + private + + def payload_content + { + "action": "created", + "issue": JSON.parse(@issue.to_builder.target!), + "journal": JSON.parse(@journal.to_builder.target!), + "project": JSON.parse(@issue.project.to_builder.target!), + "sender": JSON.parse(@sender.to_builder.target!), + "is_pull": false + } + end +end \ No newline at end of file diff --git a/app/services/webhook/issue_create_client.rb b/app/services/webhook/issue_create_client.rb deleted file mode 100644 index f222291fb..000000000 --- a/app/services/webhook/issue_create_client.rb +++ /dev/null @@ -1,55 +0,0 @@ -class Webhook::IssueCreateClient - - include Webhook::Client - - attr_accessor :webhook, :issue, :sender - attr_accessor :webhook_task - - def initialize(webhook, issue, sender) - @webhook = webhook - @issue = issue - @sender = sender - # 创建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": "opened", - "number": @issue.project_issues_index, - "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/app/services/webhook/issue_update_client.rb b/app/services/webhook/issue_update_client.rb deleted file mode 100644 index ff9926006..000000000 --- a/app/services/webhook/issue_update_client.rb +++ /dev/null @@ -1,57 +0,0 @@ -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/app/services/webhook/pull_comment_client.rb b/app/services/webhook/pull_comment_client.rb new file mode 100644 index 000000000..cb6c144f1 --- /dev/null +++ b/app/services/webhook/pull_comment_client.rb @@ -0,0 +1,36 @@ +class Webhook::PullCommentClient + include Webhook::Client + + attr_accessor :webhook, :pull, :journal, :sender, :event + + def initialize(webhook, pull, journal, sender, event) + @webhook = webhook + @pull = pull + @journal = journal + @sender = sender + @event = event + + # 构建client参数 + super({ + uuid: SecureRandom.uuid, + event: @event, + webhook: @webhook, + payload_content: payload_content + }) + end + + private + + def payload_content + { + "action": "created", + "number": @pull.gitea_number, + "pull_request": JSON.parse(@pull.to_builder.target!), + "comment": JSON.parse(@journal.to_builder.target!), + "project": JSON.parse(@pull.project.to_builder.target!), + "sender": JSON.parse(@sender.to_builder.target!), + "is_pull": true + } + end + +end \ No newline at end of file From 39c2166c12ede068700b3dff33e11196832e6b28 Mon Sep 17 00:00:00 2001 From: chenjing <28122123@qq.com> Date: Fri, 7 Apr 2023 09:50:35 +0800 Subject: [PATCH 06/20] update pr model and webhook --- app/jobs/touch_webhook_job.rb | 4 ++-- app/models/pull_request.rb | 25 ++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/app/jobs/touch_webhook_job.rb b/app/jobs/touch_webhook_job.rb index 77475098b..cff8e3aa5 100644 --- a/app/jobs/touch_webhook_job.rb +++ b/app/jobs/touch_webhook_job.rb @@ -60,8 +60,8 @@ class TouchWebhookJob < ApplicationJob end when 'PullRequestComment' - pull_id, sender_id, comment_id = args[0], args[1], args[2] - pull = PullRequest.find_by_id pull_id + issue_id, sender_id, comment_id = args[0], args[1], args[2] + pull = Issue.find_by_id(issue_id).try(:pull_request) comment = pull.issue.comment_journals.find_by_id comment_id sender = User.find_by_id sender_id return if pull.nil? || sender.nil? || comment.nil? diff --git a/app/models/pull_request.rb b/app/models/pull_request.rb index 78debfe78..f033844cd 100644 --- a/app/models/pull_request.rb +++ b/app/models/pull_request.rb @@ -119,16 +119,35 @@ class PullRequest < ApplicationRecord end def to_builder - Jbuilder.new do |pull| + Jbuilder.new do |pull| pull.(self, :id, :gitea_number, :title, :body, :base, :head, :is_original, :comments_count) - pull.created_at self.created_at.strftime("%Y-%m-%d %H:%M") - pull.updated_at self.updated_at.strftime("%Y-%m-%d %H:%M") pull.user self.user.to_builder if self.fork_project.present? pull.fork_project self.fork_project.to_builder else pull.fork_project nil end + pull.created_at self.created_at.strftime("%Y-%m-%d %H:%M") + pull.updated_at self.updated_at.strftime("%Y-%m-%d %H:%M") + pull.status self.pr_status + pull.url self.pr_url + end + end + + def pr_url + return nil if self.project.nil? + "#{Rails.application.config_for(:configuration)['platform_url']}/#{self.project.owner.login}/#{self.project.name}/pulls/#{self.id}" + end + + def pr_status + case status + when 0 + "OPEN" + when 1 + "MERGED" + when 2 + "CLOSED" end end + end From 661d1c2c06b2e22d457df1e44f70b4e3b9dfe629 Mon Sep 17 00:00:00 2001 From: chenjing <28122123@qq.com> Date: Fri, 7 Apr 2023 09:56:33 +0800 Subject: [PATCH 07/20] update touch webhook --- app/jobs/touch_webhook_job.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/jobs/touch_webhook_job.rb b/app/jobs/touch_webhook_job.rb index cff8e3aa5..d0d9ad9e3 100644 --- a/app/jobs/touch_webhook_job.rb +++ b/app/jobs/touch_webhook_job.rb @@ -61,9 +61,10 @@ class TouchWebhookJob < ApplicationJob when 'PullRequestComment' issue_id, sender_id, comment_id = args[0], args[1], args[2] - pull = Issue.find_by_id(issue_id).try(:pull_request) - comment = pull.issue.comment_journals.find_by_id comment_id + 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? pull.project.webhooks.each do |webhook| From ee2082463943a79cdcbd9fb108c279a0397b177b Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 7 Apr 2023 11:44:30 +0800 Subject: [PATCH 08/20] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E6=9C=AA?= =?UTF-8?q?=E4=BC=A0=E5=BD=93=E5=89=8D=E7=94=A8=E6=88=B7id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/api/v1/issues/create_service.rb | 4 ++-- app/services/api/v1/issues/update_service.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/services/api/v1/issues/create_service.rb b/app/services/api/v1/issues/create_service.rb index b0b1b05b0..5304c5026 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('IssueLable', @created_issue&.id, issue_tag_ids) - TouchWebhookJob.perform_later('IssueAssign', @created_issue&.id, assigner_ids) + 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) 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 153ea32e0..758dc9e43 100644 --- a/app/services/api/v1/issues/update_service.rb +++ b/app/services/api/v1/issues/update_service.rb @@ -79,8 +79,8 @@ class Api::V1::Issues::UpdateService < ApplicationService # 触发webhook TouchWebhookJob.perform_later('IssueCreate', @updated_issue&.id, current_user.id, previous_issue_changes) - TouchWebhookJob.perform_later('IssueLable', @updated_issue&.id, issue_tag_ids) - TouchWebhookJob.perform_later('IssueAssign', @updated_issue&.id, assigner_ids) + 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) unlock("Api::V1::Issues::UpdateService:#{project.id}:#{issue.id}") return @updated_issue From 1a3a7967fd619b654cc5e75bd98f9f18857719bd Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 7 Apr 2023 14:28:21 +0800 Subject: [PATCH 09/20] =?UTF-8?q?=E6=9B=B4=E6=94=B9=EF=BC=9Apayload?= =?UTF-8?q?=E4=B8=AD=E8=BF=87=E6=BB=A4emoji?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/project.rb | 2 +- app/models/user.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/project.rb b/app/models/project.rb index 02844c3bc..1ac3744bd 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -451,7 +451,7 @@ class Project < ApplicationRecord Jbuilder.new do |project| project.id self.id project.identifier self.identifier - project.name self.name + project.name self.name.to_s.each_char.select { |c| c.bytes.first < 240 }.join('') project.description Nokogiri::HTML(self.description).text project.visits self.visits project.praises_count self.praises_count.to_i diff --git a/app/models/user.rb b/app/models/user.rb index 70de1fc64..874da712a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -862,7 +862,7 @@ class User < Owner def to_builder Jbuilder.new do |user| user.(self, :id, :login) - user.name self.real_name + user.name self.real_name.to_s.each_char.select { |c| c.bytes.first < 240 }.join('') user.email self.mail user.image_url self.get_letter_avatar_url end From 7f0989f69d99b3ed50fbd262c2634f7adb4afd3b Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 7 Apr 2023 14:54:06 +0800 Subject: [PATCH 10/20] =?UTF-8?q?=E6=9B=B4=E6=94=B9=EF=BC=9Apayload?= =?UTF-8?q?=E4=B8=AD=E8=BF=87=E6=BB=A4emoji?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/project.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/project.rb b/app/models/project.rb index 1ac3744bd..52b729a85 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -475,7 +475,7 @@ class Project < ApplicationRecord project.image_url render_educoder_avatar_url(self.project_educoder) else user = self.owner - project.name user.try(:show_real_name) + project.name user.try(:show_real_name).to_s.each_char.select { |c| c.bytes.first < 240 }.join('') project.type user&.type project.login user.login project.image_url user.get_letter_avatar_url From afa1cdf8429df2eace5cf3dfcc1e9cbab236428d Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 11 Apr 2023 11:52:03 +0800 Subject: [PATCH 11/20] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E4=B8=B0=E5=AF=8C=E6=9B=B4=E6=96=B0=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=EF=BC=8Cissue=E4=BA=8B=E4=BB=B6=E7=B2=92?= =?UTF-8?q?=E5=BA=A6=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 From bea2831149b7f5f80894fd7c4c1147af0689050b Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 11 Apr 2023 13:55:25 +0800 Subject: [PATCH 12/20] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E6=9B=B4?= =?UTF-8?q?=E6=94=B9issue=E8=AF=84=E8=AE=BA=E4=BA=8B=E4=BB=B6=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/touch_webhook_job.rb | 2 ++ app/services/api/v1/issues/journals/update_service.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/jobs/touch_webhook_job.rb b/app/jobs/touch_webhook_job.rb index 061d5ece7..8319a9d66 100644 --- a/app/jobs/touch_webhook_job.rb +++ b/app/jobs/touch_webhook_job.rb @@ -52,6 +52,7 @@ class TouchWebhookJob < ApplicationJob comment = issue.comment_journals.find_by_id comment_id sender = User.find_by_id sender_id return if issue.nil? || sender.nil? + return if action_type == 'edited' && comment_json.blank? issue.project.webhooks.each do |webhook| next unless webhook.events["events"]["issue_comment"] @@ -66,6 +67,7 @@ class TouchWebhookJob < ApplicationJob sender = User.find_by_id sender_id pull = issue.try(:pull_request) return if pull.nil? || sender.nil? + return if action_type == 'edited' && comment_json.blank? pull.project.webhooks.each do |webhook| next unless webhook.events["events"]["pull_request_comment"] diff --git a/app/services/api/v1/issues/journals/update_service.rb b/app/services/api/v1/issues/journals/update_service.rb index 0b56dd303..2e27d53ea 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.stringify_keys) + TouchWebhookJob.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}") From 8663efc73bf9bc38b21813641e9b03e5aa8d621b Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 11 Apr 2023 14:10:24 +0800 Subject: [PATCH 13/20] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E6=95=B0=E6=8D=AE=E5=BB=B6=E8=BF=9F5=E7=A7=92?= =?UTF-8?q?=E8=A7=A6=E5=8F=91webhook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/api/v1/issues/create_service.rb | 2 +- app/services/api/v1/issues/journals/create_service.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/services/api/v1/issues/create_service.rb b/app/services/api/v1/issues/create_service.rb index ce24c8b49..b930a91b1 100644 --- a/app/services/api/v1/issues/create_service.rb +++ b/app/services/api/v1/issues/create_service.rb @@ -69,7 +69,7 @@ class Api::V1::Issues::CreateService < ApplicationService end # 触发webhook - TouchWebhookJob.perform_later('IssueCreate', @created_issue&.id, current_user.id) + 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? unlock("Api::V1::Issues::CreateService:#{project.id}") # 结束写数据,解锁 diff --git a/app/services/api/v1/issues/journals/create_service.rb b/app/services/api/v1/issues/journals/create_service.rb index 3121a635d..e60acb8b3 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, 'created', {}) + TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueComment', @issue&.id, @current_user.id, @created_journal.id, 'created', {}) unlock("Api::V1::Issues::Journals::CreateService:#{@issue.id}") @created_journal From f64afb8f55d2ce9ef00a2c0011c237fedaa4b41b Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 11 Apr 2023 14:11:17 +0800 Subject: [PATCH 14/20] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E6=95=B0=E6=8D=AE=E5=BB=B6=E8=BF=9F5=E7=A7=92?= =?UTF-8?q?=E8=A7=A6=E5=8F=91webhook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/journals_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/journals_controller.rb b/app/controllers/journals_controller.rb index 7bade2797..ff214774a 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, 'created', {}) + TouchWebhookJob.set(wait: 5.seconds).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, "评论成功") From 4bd60d458e36c9b63bd314147a6a69357b59959b Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 12 Apr 2023 18:34:49 +0800 Subject: [PATCH 15/20] =?UTF-8?q?=E6=9B=B4=E6=94=B9=EF=BC=9Awebhook?= =?UTF-8?q?=E6=9B=B4=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!), From 1e3ad9e98881625d4ecdb24dc3c21573369d4a2d Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 13 Apr 2023 09:30:00 +0800 Subject: [PATCH 16/20] =?UTF-8?q?=E6=9B=B4=E6=94=B9=EF=BC=9A=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/webhook/client.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/services/webhook/client.rb b/app/services/webhook/client.rb index a64753d05..ac7cfcf35 100644 --- a/app/services/webhook/client.rb +++ b/app/services/webhook/client.rb @@ -49,11 +49,19 @@ module Webhook::Client @request_content["http_method"] = @http_method @request_content["headers"] = headers - response = RestClient::Request.execute(method: trans_http_method, url: @url, headers: headers, payload: @webhook_task.read_attribute_before_type_cast("payload_content")) {|response, request, result| response } + begin - @response_content["status"] = response.code - @response_content["headers"] = response.headers - @response_content["body"] = response.body.to_json + response = RestClient::Request.execute(method: trans_http_method, url: @url, headers: headers, payload: @webhook_task.read_attribute_before_type_cast("payload_content")) {|response, request, result| response } + + @response_content["status"] = response.code + @response_content["headers"] = response.headers + @response_content["body"] = response.body.to_json + + rescue => e + @response_content["status"] = 500 + @response_content["headers"] = {} + @response_content["body"] = e.message + end @webhook_task.update_attributes({ delivered: Time.now.to_i * 1000000000, From a69e4e50143b43e5c1aecacd25eea8b3ec32622c Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 13 Apr 2023 09:42:12 +0800 Subject: [PATCH 17/20] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9Awebhook=20clie?= =?UTF-8?q?nt=E9=87=8C=E6=95=B0=E6=8D=AEreload?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/webhook/issue_client.rb | 4 ++-- app/services/webhook/issue_comment_client.rb | 6 +++--- app/services/webhook/pull_comment_client.rb | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/services/webhook/issue_client.rb b/app/services/webhook/issue_client.rb index b829f9740..cb6cca843 100644 --- a/app/services/webhook/issue_client.rb +++ b/app/services/webhook/issue_client.rb @@ -7,8 +7,8 @@ class Webhook::IssueClient def initialize(webhook, issue, sender, event, changes={}) @webhook = webhook - @issue = issue - @sender = sender + @issue = issue.reload + @sender = sender.reload @event = event @changes = changes diff --git a/app/services/webhook/issue_comment_client.rb b/app/services/webhook/issue_comment_client.rb index 1881217ec..4d74aa7ca 100644 --- a/app/services/webhook/issue_comment_client.rb +++ b/app/services/webhook/issue_comment_client.rb @@ -6,9 +6,9 @@ class Webhook::IssueCommentClient def initialize(webhook, issue, journal, sender, event, action_type, comment_json={}) @webhook = webhook - @issue = issue - @journal = journal - @sender = sender + @issue = issue.reload + @journal = journal.reload + @sender = sender.reload @event = event @action_type = action_type @comment_json = comment_json diff --git a/app/services/webhook/pull_comment_client.rb b/app/services/webhook/pull_comment_client.rb index dde9d983f..d7be63e8b 100644 --- a/app/services/webhook/pull_comment_client.rb +++ b/app/services/webhook/pull_comment_client.rb @@ -5,9 +5,9 @@ class Webhook::PullCommentClient def initialize(webhook, pull, journal, sender, event, action_type='created', comment_json={}) @webhook = webhook - @pull = pull - @journal = journal - @sender = sender + @pull = pull.reload + @journal = journal.reload + @sender = sender.reload @event = event @action_type = action_type @comment_json = comment_json From 1e7cffd716284b2560e4603a14f5d3253ecb6b51 Mon Sep 17 00:00:00 2001 From: chenjing <28122123@qq.com> Date: Thu, 13 Apr 2023 17:21:09 +0800 Subject: [PATCH 18/20] fix issue webhook error --- Gemfile | 3 ++ Gemfile.lock | 52 ++++++++++++++++++- app/models/issue.rb | 8 +-- app/services/api/v1/issues/update_service.rb | 4 +- app/services/webhook/issue_client.rb | 2 +- .../projects/webhooks/tasks.json.jbuilder | 2 +- 6 files changed, 62 insertions(+), 9 deletions(-) diff --git a/Gemfile b/Gemfile index abe202581..947543c5f 100644 --- a/Gemfile +++ b/Gemfile @@ -70,6 +70,9 @@ group :development do gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' gem 'spring' + gem 'pry-rails' + gem 'pry-remote' + gem 'byebug', platforms: [:mri,:mingw,:x64_mingw] gem 'spring-watcher-listen', '~> 2.0.0' gem "annotate", "~> 2.6.0" end diff --git a/Gemfile.lock b/Gemfile.lock index e27c504aa..7773ecad8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,5 +1,5 @@ GEM - remote: https://gems.ruby-china.com/ + remote: https://mirrors.cloud.tencent.com/rubygems/ specs: aasm (5.0.6) concurrent-ruby (~> 1.0) @@ -84,6 +84,7 @@ GEM builder (3.2.4) bulk_insert (1.7.0) activerecord (>= 3.2.0) + byebug (11.1.3) capybara (3.15.1) addressable mini_mime (>= 0.1.3) @@ -99,6 +100,7 @@ GEM archive-zip (~> 0.10) nokogiri (~> 1.8) chunky_png (1.3.11) + coderay (1.1.3) concurrent-ruby (1.1.6) connection_pool (2.2.2) crass (1.0.6) @@ -106,6 +108,8 @@ GEM activerecord (>= 3.1.0, < 7) diff-lcs (1.3) diffy (3.3.0) + domain_name (0.5.20190701) + unf (>= 0.0.5, < 1.0.0) doorkeeper (5.5.1) railties (>= 5) doorkeeper-jwt (0.4.1) @@ -133,6 +137,8 @@ GEM fugit (1.4.1) et-orbi (~> 1.1, >= 1.1.8) raabro (~> 1.4) + gitea-client (1.4.1) + rest-client (~> 2.1.0) globalid (0.4.2) activesupport (>= 4.2.0) grape-entity (0.7.1) @@ -143,6 +149,9 @@ GEM harmonious_dictionary (0.0.1) hashie (3.6.0) htmlentities (4.3.4) + http-accept (1.7.0) + http-cookie (1.0.5) + domain_name (~> 0.5) i18n (1.8.2) concurrent-ruby (~> 1.0) io-like (0.3.1) @@ -180,6 +189,9 @@ GEM mimemagic (~> 0.3.2) maruku (0.7.3) method_source (0.9.2) + mime-types (3.4.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2023.0218.1) mimemagic (0.3.10) nokogiri (~> 1) rake @@ -193,6 +205,7 @@ GEM mustermann (1.1.1) ruby2_keywords (~> 0.0.1) mysql2 (0.5.3) + netrc (0.11.0) nio4r (2.5.2) nokogiri (1.10.8) mini_portile2 (~> 2.4.0) @@ -209,9 +222,21 @@ GEM addressable (~> 2.3) nokogiri (~> 1.5) omniauth (~> 1.2) + omniauth-gitee (1.0.0) + omniauth (>= 1.5, < 3.0) + omniauth-oauth2 (>= 1.4.0, < 2.0) + omniauth-github (1.4.0) + omniauth (~> 1.5) + omniauth-oauth2 (>= 1.4.0, < 2.0) omniauth-oauth2 (1.6.0) oauth2 (~> 1.1) omniauth (~> 1.9) + omniauth-rails_csrf_protection (0.1.2) + actionpack (>= 4.2) + omniauth (>= 1.3.1) + omniauth-wechat-oauth2 (0.2.2) + omniauth (>= 1.3.2) + omniauth-oauth2 (>= 1.1.1) parallel (1.19.1) parser (2.7.1.1) ast (~> 2.4.0) @@ -221,6 +246,14 @@ GEM popper_js (1.16.0) powerpack (0.1.2) prettier (0.18.2) + pry (0.12.2) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + pry-rails (0.3.9) + pry (>= 0.10.4) + pry-remote (0.1.8) + pry (~> 0.9) + slop (~> 3.0) public_suffix (4.0.3) puma (3.12.2) raabro (1.4.0) @@ -292,6 +325,11 @@ GEM regexp_parser (1.7.0) request_store (1.5.0) rack (>= 1.4) + rest-client (2.1.0) + http-accept (>= 1.7.0, < 2.0) + http-cookie (>= 1.0.2, < 2.0) + mime-types (>= 1.16, < 4.0) + netrc (~> 0.8) reverse_markdown (1.4.0) nokogiri roo (2.8.3) @@ -380,6 +418,7 @@ GEM rack (~> 2.0) rack-protection (= 2.0.8.1) tilt (~> 2.0) + slop (3.6.0) solargraph (0.38.6) backport (~> 1.1) benchmark @@ -418,6 +457,9 @@ GEM thread_safe (~> 0.1) uglifier (4.2.0) execjs (>= 0.3.0, < 3) + unf (0.1.4) + unf_ext + unf_ext (0.0.8.2) unicode-display_width (1.6.1) web-console (3.7.0) actionview (>= 5.0) @@ -448,6 +490,7 @@ DEPENDENCIES bootsnap (>= 1.1.0) bootstrap (~> 4.3.1) bulk_insert + byebug capybara (>= 2.15, < 4.0) chartkick chinese_pinyin @@ -459,6 +502,7 @@ DEPENDENCIES enumerize faraday (~> 0.15.4) font-awesome-sass (= 4.7.0) + gitea-client (~> 1.4.1) grape-entity (~> 0.7.1) groupdate (~> 4.1.0) harmonious_dictionary (~> 0.0.1) @@ -472,10 +516,16 @@ DEPENDENCIES oauth2 omniauth (~> 1.9.0) omniauth-cas + omniauth-gitee (~> 1.0.0) + omniauth-github omniauth-oauth2 (~> 1.6.0) + omniauth-rails_csrf_protection + omniauth-wechat-oauth2 parallel (~> 1.19, >= 1.19.1) pdfkit prettier + pry-rails + pry-remote puma (~> 3.11) rack-cors rack-mini-profiler diff --git a/app/models/issue.rb b/app/models/issue.rb index 4ec77025e..555332852 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -222,7 +222,7 @@ class Issue < ApplicationRecord issue.(self, :id, :project_issues_index, :subject, :description) issue.created_at self.created_on.strftime("%Y-%m-%d %H:%M") issue.updated_at self.updated_on.strftime("%Y-%m-%d %H:%M") - issue.tags self.show_issue_tags.map{|t| t.to_builder} + issue.tags self.show_issue_tags.map{|t| JSON.parse(t.to_builder.target!)} issue.status self.issue_status.to_builder if self.priority.present? issue.priority self.priority.to_builder @@ -235,11 +235,11 @@ class Issue < ApplicationRecord issue.milestone nil end issue.author self.user.to_builder - issue.assigners self.show_assigners.map{|t| t.to_builder} - issue.participants self.participants.distinct.map{|t| t.to_builder} + issue.assigners self.show_assigners.map{|t| JSON.parse(t.to_builder.target!)} + issue.participants self.participants.distinct.map{|t| JSON.parse(t.to_builder.target!)} issue.comment_journals_count self.comment_journals.size issue.operate_journals_count self.operate_journals.size - issue.attachments self.attachments.map{|t| t.to_builder} + issue.attachments self.attachments.map{|t| JSON.parse(t.to_builder.target!)} end end diff --git a/app/services/api/v1/issues/update_service.rb b/app/services/api/v1/issues/update_service.rb index fc7eabea6..d72a1693b 100644 --- a/app/services/api/v1/issues/update_service.rb +++ b/app/services/api/v1/issues/update_service.rb @@ -79,13 +79,13 @@ class Api::V1::Issues::UpdateService < ApplicationService SendTemplateMessageJob.perform_later('IssueAssigned', current_user.id, @issue&.id, add_assigner_ids) unless add_assigner_ids.blank? end + unlock("Api::V1::Issues::UpdateService:#{project.id}:#{issue.id}") # 触发webhook + Rails.logger.info "################### 触发webhook" 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 end end diff --git a/app/services/webhook/issue_client.rb b/app/services/webhook/issue_client.rb index cb6cca843..abd84d030 100644 --- a/app/services/webhook/issue_client.rb +++ b/app/services/webhook/issue_client.rb @@ -82,7 +82,7 @@ class Webhook::IssueClient 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(before_assigners.blank? ? [] : before_assigners.map{|a|JSON.parse(a.to_builder.target!)}) @changes["assigners"].append(after_assigners.blank? ? [] : after_assigners.map{|a|JSON.parse(a.to_builder.target!)}) @changes.delete("assigner_ids") end diff --git a/app/views/projects/webhooks/tasks.json.jbuilder b/app/views/projects/webhooks/tasks.json.jbuilder index 8e1809011..115a59bcc 100644 --- a/app/views/projects/webhooks/tasks.json.jbuilder +++ b/app/views/projects/webhooks/tasks.json.jbuilder @@ -1,6 +1,6 @@ json.total_count @tasks.total_count json.tasks @tasks.each do |task| - json.(task, :id, :event_type, :type, :uuid, :is_succeed, :is_delivered, :payload_content, :request_content) + json.(task, :id, :event_type, :uuid, :is_succeed, :is_delivered, :payload_content, :request_content) json.response_content task.response_content json.delivered_time task.delivered.present? ? Time.at(task.delivered*10**-9).strftime("%Y-%m-%d %H:%M:%S") : nil end \ No newline at end of file From 16dbb87526af432a61a9e54ea910145a8738ecb3 Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 13 Apr 2023 19:56:43 +0800 Subject: [PATCH 19/20] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E9=83=A8?= =?UTF-8?q?=E5=88=86issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/issue.rb | 2 +- app/services/api/v1/issues/update_service.rb | 2 +- app/services/webhook/client.rb | 2 +- app/services/webhook/issue_client.rb | 1 + app/services/webhook/issue_comment_client.rb | 2 +- app/services/webhook/pull_comment_client.rb | 2 +- 6 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/models/issue.rb b/app/models/issue.rb index 555332852..9076551c4 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -219,7 +219,7 @@ class Issue < ApplicationRecord def to_builder Jbuilder.new do |issue| - issue.(self, :id, :project_issues_index, :subject, :description) + issue.(self, :id, :project_issues_index, :subject, :description, :branch_name, :start_date, :due_date) issue.created_at self.created_on.strftime("%Y-%m-%d %H:%M") issue.updated_at self.updated_on.strftime("%Y-%m-%d %H:%M") issue.tags self.show_issue_tags.map{|t| JSON.parse(t.to_builder.target!)} diff --git a/app/services/api/v1/issues/update_service.rb b/app/services/api/v1/issues/update_service.rb index d72a1693b..9c1b3ebfc 100644 --- a/app/services/api/v1/issues/update_service.rb +++ b/app/services/api/v1/issues/update_service.rb @@ -128,7 +128,7 @@ class Api::V1::Issues::UpdateService < ApplicationService end def build_previous_issue_changes - @previous_issue_changes.merge!(@updated_issue.previous_changes.slice("status_id", "priority_id", "fixed_version_id", "issue_tags_value", "branch_name").symbolize_keys) + @previous_issue_changes.merge!(@updated_issue.previous_changes.slice("status_id", "priority_id", "fixed_version_id", "issue_tags_value", "branch_name", "subject", "description").symbolize_keys) if @updated_issue.previous_changes[:start_date].present? @previous_issue_changes.merge!(start_date: [@updated_issue.previous_changes[:start_date][0].to_s, @updated_issue.previous_changes[:start_date][1].to_s]) end diff --git a/app/services/webhook/client.rb b/app/services/webhook/client.rb index ac7cfcf35..cb1cac61c 100644 --- a/app/services/webhook/client.rb +++ b/app/services/webhook/client.rb @@ -55,7 +55,7 @@ module Webhook::Client @response_content["status"] = response.code @response_content["headers"] = response.headers - @response_content["body"] = response.body.to_json + @response_content["body"] = response.body rescue => e @response_content["status"] = 500 diff --git a/app/services/webhook/issue_client.rb b/app/services/webhook/issue_client.rb index abd84d030..535330896 100644 --- a/app/services/webhook/issue_client.rb +++ b/app/services/webhook/issue_client.rb @@ -89,6 +89,7 @@ class Webhook::IssueClient { "action": @changes["assigners"].blank? ? "unassigned" : "assigned", "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!) diff --git a/app/services/webhook/issue_comment_client.rb b/app/services/webhook/issue_comment_client.rb index 4d74aa7ca..322041493 100644 --- a/app/services/webhook/issue_comment_client.rb +++ b/app/services/webhook/issue_comment_client.rb @@ -7,7 +7,7 @@ class Webhook::IssueCommentClient def initialize(webhook, issue, journal, sender, event, action_type, comment_json={}) @webhook = webhook @issue = issue.reload - @journal = journal.reload + @journal = journal.present? ? journal.reload : nil @sender = sender.reload @event = event @action_type = action_type diff --git a/app/services/webhook/pull_comment_client.rb b/app/services/webhook/pull_comment_client.rb index d7be63e8b..a4d6cab4b 100644 --- a/app/services/webhook/pull_comment_client.rb +++ b/app/services/webhook/pull_comment_client.rb @@ -6,7 +6,7 @@ class Webhook::PullCommentClient def initialize(webhook, pull, journal, sender, event, action_type='created', comment_json={}) @webhook = webhook @pull = pull.reload - @journal = journal.reload + @journal = journal.present? ? journal.reload : nil @sender = sender.reload @event = event @action_type = action_type From 8ac7b6a7e7cb94e028c3b49ddb2e3dd75546a3cd Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 13 Apr 2023 20:17:47 +0800 Subject: [PATCH 20/20] =?UTF-8?q?=E6=9B=B4=E6=94=B9=EF=BC=9Awebhook?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E8=AE=B0=E5=BD=95=E9=83=A8=E5=88=86=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E7=BC=96=E7=A0=81=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/project.rb | 4 ++-- app/models/user.rb | 2 +- .../20230413121129_change_webhook_task_field_character.rb | 7 +++++++ 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20230413121129_change_webhook_task_field_character.rb diff --git a/app/models/project.rb b/app/models/project.rb index 52b729a85..02844c3bc 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -451,7 +451,7 @@ class Project < ApplicationRecord Jbuilder.new do |project| project.id self.id project.identifier self.identifier - project.name self.name.to_s.each_char.select { |c| c.bytes.first < 240 }.join('') + project.name self.name project.description Nokogiri::HTML(self.description).text project.visits self.visits project.praises_count self.praises_count.to_i @@ -475,7 +475,7 @@ class Project < ApplicationRecord project.image_url render_educoder_avatar_url(self.project_educoder) else user = self.owner - project.name user.try(:show_real_name).to_s.each_char.select { |c| c.bytes.first < 240 }.join('') + project.name user.try(:show_real_name) project.type user&.type project.login user.login project.image_url user.get_letter_avatar_url diff --git a/app/models/user.rb b/app/models/user.rb index 874da712a..70de1fc64 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -862,7 +862,7 @@ class User < Owner def to_builder Jbuilder.new do |user| user.(self, :id, :login) - user.name self.real_name.to_s.each_char.select { |c| c.bytes.first < 240 }.join('') + user.name self.real_name user.email self.mail user.image_url self.get_letter_avatar_url end diff --git a/db/migrate/20230413121129_change_webhook_task_field_character.rb b/db/migrate/20230413121129_change_webhook_task_field_character.rb new file mode 100644 index 000000000..04c86716d --- /dev/null +++ b/db/migrate/20230413121129_change_webhook_task_field_character.rb @@ -0,0 +1,7 @@ +class ChangeWebhookTaskFieldCharacter < ActiveRecord::Migration[5.2] + def change + Gitea::Base.connection.execute("ALTER TABLE `hook_task` MODIFY `payload_content` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") + Gitea::Base.connection.execute("ALTER TABLE `hook_task` MODIFY `request_content` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") + Gitea::Base.connection.execute("ALTER TABLE `hook_task` MODIFY `response_content` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") + end +end