webhook for issue

This commit is contained in:
chenjing
2023-04-06 17:00:43 +08:00
committed by yystopf
parent 91dfc94a7b
commit 769c8a0194
6 changed files with 201 additions and 5 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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