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

130 lines
3.6 KiB
Ruby

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