2021-07-26 17:25:06 +08:00
|
|
|
|
class Projects::WebhooksController < Projects::BaseController
|
2021-07-27 11:11:23 +08:00
|
|
|
|
before_action :require_manager!
|
2021-07-28 14:27:11 +08:00
|
|
|
|
before_action :find_webhook, only:[:edit, :update, :destroy, :tasks, :test]
|
2021-07-26 17:25:06 +08:00
|
|
|
|
|
|
|
|
|
|
def index
|
|
|
|
|
|
@webhooks = @project.webhooks
|
|
|
|
|
|
@webhooks = kaminari_paginate(@webhooks)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def create
|
2021-07-27 11:11:23 +08:00
|
|
|
|
ActiveRecord::Base.transaction do
|
2022-09-08 10:40:52 +08:00
|
|
|
|
return render_error("webhooks数量已到上限!请删除暂不使用的webhooks以进行添加操作") if @project.webhooks.size > 49
|
2021-07-27 11:11:23 +08:00
|
|
|
|
return render_error("参数错误.") unless webhook_params.present?
|
|
|
|
|
|
form = Projects::Webhooks::CreateForm.new(webhook_params)
|
|
|
|
|
|
return render json: {status: -1, message: form.errors} unless form.validate!
|
2021-07-30 09:39:54 +08:00
|
|
|
|
response = Gitea::Repository::Webhooks::CreateService.new(operating_token, @project&.owner&.login, @project&.identifier, gitea_webhooks_params).call
|
2021-07-27 11:11:23 +08:00
|
|
|
|
if response[0] == 201
|
|
|
|
|
|
@webhook = response[2]
|
|
|
|
|
|
else
|
|
|
|
|
|
render_error("创建失败.")
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
rescue Exception => e
|
|
|
|
|
|
uid_logger_error(e.message)
|
|
|
|
|
|
tip_exception(e.message)
|
2021-07-26 17:25:06 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def edit
|
2021-07-27 16:44:58 +08:00
|
|
|
|
|
2021-07-26 17:25:06 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def update
|
2021-07-27 16:44:58 +08:00
|
|
|
|
return render_error("参数错误.") unless webhook_params.present?
|
|
|
|
|
|
form = Projects::Webhooks::CreateForm.new(webhook_params)
|
|
|
|
|
|
return render json: {status: -1, message: form.errors} unless form.validate!
|
2021-07-30 09:39:54 +08:00
|
|
|
|
response = Gitea::Repository::Webhooks::UpdateService.call(operating_token, @project&.owner&.login, @project&.identifier, @webhook.id, gitea_webhooks_params)
|
2021-07-27 16:44:58 +08:00
|
|
|
|
if response[0] == 200
|
|
|
|
|
|
@webhook = response[2]
|
|
|
|
|
|
render_ok
|
|
|
|
|
|
else
|
|
|
|
|
|
render_error("更新失败.")
|
|
|
|
|
|
end
|
|
|
|
|
|
rescue Exception => e
|
|
|
|
|
|
uid_logger_error(e.message)
|
|
|
|
|
|
tip_exception(e.message)
|
2021-07-26 17:25:06 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def destroy
|
2021-07-30 09:39:54 +08:00
|
|
|
|
response = Gitea::Repository::Webhooks::DeleteService.call(operating_token, @project&.owner&.login, @project&.identifier, @webhook.id)
|
2021-07-27 16:44:58 +08:00
|
|
|
|
if response[0] == 204
|
|
|
|
|
|
@webhook = response[2]
|
|
|
|
|
|
render_ok
|
|
|
|
|
|
else
|
|
|
|
|
|
render_error("删除失败.")
|
|
|
|
|
|
end
|
|
|
|
|
|
rescue Exception => e
|
|
|
|
|
|
uid_logger_error(e.message)
|
|
|
|
|
|
tip_exception(e.message)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2021-07-28 14:27:11 +08:00
|
|
|
|
def tasks
|
2021-07-30 16:24:34 +08:00
|
|
|
|
@tasks = @webhook.tasks.where(is_delivered: true).order("delivered desc")
|
2021-07-28 14:27:11 +08:00
|
|
|
|
@tasks = kaminari_paginate(@tasks)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-07-30 09:39:54 +08:00
|
|
|
|
response = Gitea::Repository::Webhooks::TestService.call(operating_token, @project&.owner&.login, @project&.identifier, @webhook.id)
|
2021-07-28 14:27:11 +08:00
|
|
|
|
if response[0] == 204
|
|
|
|
|
|
render_ok
|
|
|
|
|
|
else
|
|
|
|
|
|
render_error("测试推送失败.")
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
rescue Exception => e
|
|
|
|
|
|
uid_logger_error(e.message)
|
|
|
|
|
|
tip_exception(e.message)
|
2021-07-26 17:25:06 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
def find_webhook
|
2021-07-30 09:39:54 +08:00
|
|
|
|
@webhook = @project.webhooks.find_by_id(params[:id])
|
2021-07-29 16:17:30 +08:00
|
|
|
|
return render_not_found if @webhook.nil?
|
2021-07-26 17:25:06 +08:00
|
|
|
|
end
|
2021-07-27 11:11:23 +08:00
|
|
|
|
|
|
|
|
|
|
def webhook_params
|
|
|
|
|
|
params.require(:webhook).permit(:url, :type, :http_method, :content_type, :secret, :active, :branch_filter, events: [])
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def webhook_type
|
|
|
|
|
|
webhook_params.fetch(:type, "gitea")
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def webhook_branch_filter
|
|
|
|
|
|
webhook_params.fetch(:branch_filter, "*")
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def gitea_webhooks_params
|
|
|
|
|
|
{
|
|
|
|
|
|
active: webhook_params[:active],
|
|
|
|
|
|
branch_filter: webhook_branch_filter,
|
|
|
|
|
|
config: {
|
|
|
|
|
|
content_type: webhook_params[:content_type],
|
|
|
|
|
|
url: webhook_params[:url],
|
|
|
|
|
|
http_method: webhook_params[:http_method],
|
2021-07-28 17:30:32 +08:00
|
|
|
|
secret: webhook_params[:secret]
|
2021-07-27 11:11:23 +08:00
|
|
|
|
},
|
|
|
|
|
|
events: webhook_params[:events],
|
|
|
|
|
|
type: webhook_type,
|
|
|
|
|
|
}
|
|
|
|
|
|
end
|
2021-07-30 09:39:54 +08:00
|
|
|
|
|
|
|
|
|
|
def operating_token
|
|
|
|
|
|
@project.member?(current_user) ? current_user.gitea_token : @project&.owner&.gitea_token
|
|
|
|
|
|
end
|
2021-07-26 17:25:06 +08:00
|
|
|
|
end
|