add: repository create webhook

This commit is contained in:
2021-07-27 11:11:23 +08:00
parent ebc4305ca8
commit cb1a4d8c8c
7 changed files with 427 additions and 0 deletions

View File

@@ -4,4 +4,7 @@ class Projects::BaseController < ApplicationController
before_action :load_project
before_action :load_repository
def require_manager!
return render_forbidden('你没有权限操作') unless current_user.admin? || @project.manager?(current_user)
end
end

View File

@@ -1,4 +1,5 @@
class Projects::WebhooksController < Projects::BaseController
before_action :require_manager!
def index
@webhooks = @project.webhooks
@@ -6,6 +7,21 @@ class Projects::WebhooksController < Projects::BaseController
end
def create
ActiveRecord::Base.transaction do
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!
response = Gitea::Repository::Webhooks::CreateService.new(current_user.gitea_token, @project&.owner&.login, @project&.identifier, gitea_webhooks_params).call
if response[0] == 201
@webhook = response[2]
puts @webhook
else
render_error("创建失败.")
end
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def edit
@@ -21,4 +37,30 @@ class Projects::WebhooksController < Projects::BaseController
def find_webhook
@webhook = Gitea::Webhook.find_by_id(params[:id])
end
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],
},
events: webhook_params[:events],
type: webhook_type,
}
end
end