gitlink-forgeplus/app/services/webhook/issue_update_client.rb

57 lines
1.5 KiB
Ruby

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