mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-03 03:40:49 +08:00
Merge pull request 'webhook自定义事件' (#43) from feature_custom_webhook into develop
This commit is contained in:
@@ -27,6 +27,7 @@ class Api::V1::Issues::JournalsController < Api::V1::BaseController
|
||||
end
|
||||
|
||||
def destroy
|
||||
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
|
||||
|
||||
@@ -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.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, "评论成功")
|
||||
@@ -61,6 +62,7 @@ class JournalsController < ApplicationController
|
||||
|
||||
def destroy
|
||||
if @journal.destroy #如果有子评论,子评论删除吗?
|
||||
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
|
||||
|
||||
79
app/jobs/touch_webhook_job.rb
Normal file
79
app/jobs/touch_webhook_job.rb
Normal file
@@ -0,0 +1,79 @@
|
||||
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::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) || 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
|
||||
Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}"
|
||||
end
|
||||
|
||||
when 'IssueAssign'
|
||||
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", changes.stringify_keys).do_request
|
||||
Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}"
|
||||
end
|
||||
|
||||
when 'IssueLabel'
|
||||
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", changes.stringify_keys).do_request
|
||||
Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}"
|
||||
end
|
||||
|
||||
when 'IssueComment'
|
||||
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?
|
||||
return if action_type == 'edited' && comment_json.blank?
|
||||
|
||||
issue.project.webhooks.each do |webhook|
|
||||
next unless webhook.events["events"]["issue_comment"]
|
||||
_, _, @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, 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?
|
||||
return if action_type == 'edited' && comment_json.blank?
|
||||
|
||||
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", action_type, comment_json).do_request
|
||||
Rails.logger.info "Touch Webhook Response result: #{@webhook_task.response_content}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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, :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!)}
|
||||
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| 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| JSON.parse(t.to_builder.target!)}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -32,4 +32,10 @@ class IssuePriority < ApplicationRecord
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def to_builder
|
||||
Jbuilder.new do |priority|
|
||||
priority.(self, :id, :name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -44,4 +44,10 @@ class IssueStatus < ApplicationRecord
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def to_builder
|
||||
Jbuilder.new do |status|
|
||||
status.(self, :id, :name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -272,5 +272,15 @@ class Journal < ApplicationRecord
|
||||
send_details
|
||||
end
|
||||
|
||||
|
||||
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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -117,4 +117,37 @@ 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.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
|
||||
|
||||
@@ -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位
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -67,7 +67,11 @@ 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.set(wait: 5.seconds).perform_later('IssueCreate', @created_issue&.id, current_user.id)
|
||||
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
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ class Api::V1::Issues::Journals::CreateService < ApplicationService
|
||||
|
||||
# @信息发送
|
||||
AtmeService.call(current_user, @atme_receivers, @created_journal) unless receivers_login.blank?
|
||||
|
||||
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
|
||||
|
||||
@@ -38,6 +38,7 @@ class Api::V1::Issues::Journals::UpdateService < ApplicationService
|
||||
|
||||
# @信息发送
|
||||
AtmeService.call(current_user, @atme_receivers, @created_journal) unless receivers_login.blank?
|
||||
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}")
|
||||
|
||||
|
||||
@@ -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,7 +80,12 @@ class Api::V1::Issues::UpdateService < ApplicationService
|
||||
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?
|
||||
|
||||
return @updated_issue
|
||||
end
|
||||
end
|
||||
@@ -121,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
|
||||
|
||||
120
app/services/webhook/client.rb
Normal file
120
app/services/webhook/client.rb
Normal file
@@ -0,0 +1,120 @@
|
||||
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, :webhook
|
||||
attr_accessor :request_content, :response_content, :webhook_task
|
||||
|
||||
def initialize(opts)
|
||||
@uuid = opts[:uuid]
|
||||
@event = opts[:event]
|
||||
@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
|
||||
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
|
||||
|
||||
begin
|
||||
|
||||
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
|
||||
|
||||
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,
|
||||
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
|
||||
@request_content
|
||||
end
|
||||
|
||||
def response_content
|
||||
@response_content
|
||||
end
|
||||
|
||||
def webhook_task
|
||||
@webhook_task
|
||||
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
|
||||
117
app/services/webhook/issue_client.rb
Normal file
117
app/services/webhook/issue_client.rb
Normal file
@@ -0,0 +1,117 @@
|
||||
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.reload
|
||||
@sender = sender.reload
|
||||
@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
|
||||
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,
|
||||
"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
|
||||
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_builder.target!)})
|
||||
@changes["assigners"].append(after_assigners.blank? ? [] : after_assigners.map{|a|JSON.parse(a.to_builder.target!)})
|
||||
@changes.delete("assigner_ids")
|
||||
end
|
||||
{
|
||||
"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!)
|
||||
}
|
||||
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_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!),
|
||||
"sender": JSON.parse(@sender.to_builder.target!)
|
||||
}
|
||||
end
|
||||
end
|
||||
45
app/services/webhook/issue_comment_client.rb
Normal file
45
app/services/webhook/issue_comment_client.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
class Webhook::IssueCommentClient
|
||||
|
||||
include Webhook::Client
|
||||
|
||||
attr_accessor :webhook, :issue, :journal, :sender, :event, :action_type, :comment_json
|
||||
|
||||
def initialize(webhook, issue, journal, sender, event, action_type, comment_json={})
|
||||
@webhook = webhook
|
||||
@issue = issue.reload
|
||||
@journal = journal.present? ? journal.reload : nil
|
||||
@sender = sender.reload
|
||||
@event = event
|
||||
@action_type = action_type
|
||||
@comment_json = comment_json
|
||||
|
||||
# 构建client参数
|
||||
super({
|
||||
uuid: SecureRandom.uuid,
|
||||
event: @event,
|
||||
webhook: @webhook,
|
||||
payload_content: payload_content
|
||||
})
|
||||
end
|
||||
|
||||
|
||||
|
||||
private
|
||||
|
||||
def payload_content
|
||||
payload_content = {
|
||||
"action": @action_type,
|
||||
"issue": JSON.parse(@issue.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
|
||||
43
app/services/webhook/pull_comment_client.rb
Normal file
43
app/services/webhook/pull_comment_client.rb
Normal file
@@ -0,0 +1,43 @@
|
||||
class Webhook::PullCommentClient
|
||||
include Webhook::Client
|
||||
|
||||
attr_accessor :webhook, :pull, :journal, :sender, :event, :action_type, :comment_json
|
||||
|
||||
def initialize(webhook, pull, journal, sender, event, action_type='created', comment_json={})
|
||||
@webhook = webhook
|
||||
@pull = pull.reload
|
||||
@journal = journal.present? ? journal.reload : nil
|
||||
@sender = sender.reload
|
||||
@event = event
|
||||
@action_type = action_type
|
||||
@comment_json = comment_json
|
||||
# 构建client参数
|
||||
super({
|
||||
uuid: SecureRandom.uuid,
|
||||
event: @event,
|
||||
webhook: @webhook,
|
||||
payload_content: payload_content
|
||||
})
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def payload_content
|
||||
payload_content = {
|
||||
"action": @action_type,
|
||||
"number": @pull.gitea_number,
|
||||
"pull_request": JSON.parse(@pull.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
|
||||
@@ -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
|
||||
@@ -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.(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
|
||||
Reference in New Issue
Block a user