webhook for issue
This commit is contained in:
parent
91dfc94a7b
commit
769c8a0194
|
@ -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, "评论成功")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue