From fea11bdc2ec9b0a5c44d3863cc0832571187c6c7 Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 3 Aug 2022 14:20:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9Awebhook=20type?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/v1/projects/webhooks_controller.rb | 7 ++++++- app/docs/slate/source/includes/_pulls.md | 1 + app/docs/slate/source/includes/_repositories.md | 2 +- .../api/v1/projects/webhooks/create_service.rb | 7 ++++--- .../projects/webhooks/_simple_detail.json.jbuilder | 2 +- .../webhooks/_simple_gitea_detail.json.jbuilder | 1 + public/docs/api.html | 12 ++++++++++++ 7 files changed, 26 insertions(+), 6 deletions(-) diff --git a/app/controllers/api/v1/projects/webhooks_controller.rb b/app/controllers/api/v1/projects/webhooks_controller.rb index bb2c2172c..1dec26d21 100644 --- a/app/controllers/api/v1/projects/webhooks_controller.rb +++ b/app/controllers/api/v1/projects/webhooks_controller.rb @@ -5,11 +5,12 @@ class Api::V1::Projects::WebhooksController < Api::V1::BaseController def index # @result_object = Api::V1::Projects::Webhooks::ListService.call(@project, current_user&.gitea_token) @webhooks = @project.webhooks + @webhooks = @webhooks.where(type: params[:type]) if params[:type].present? @webhooks = kaminari_paginate(@webhooks) end def create - @result_object = Api::V1::Projects::Webhooks::CreateService.call(@project, webhook_params, current_user&.gitea_token) + @result_object = Api::V1::Projects::Webhooks::CreateService.call(@project, create_webhook_params, current_user&.gitea_token) end def show @@ -44,6 +45,10 @@ class Api::V1::Projects::WebhooksController < Api::V1::BaseController end private + def create_webhook_params + params.require(:webhook).permit(:active, :branch_filter, :http_method, :url, :content_type, :secret, :type, events: []) + end + def webhook_params params.require(:webhook).permit(:active, :branch_filter, :http_method, :url, :content_type, :secret, events: []) end diff --git a/app/docs/slate/source/includes/_pulls.md b/app/docs/slate/source/includes/_pulls.md index adc6e3cc4..671c7fbd5 100644 --- a/app/docs/slate/source/includes/_pulls.md +++ b/app/docs/slate/source/includes/_pulls.md @@ -903,6 +903,7 @@ await octokit.request('GET /api/v1/yystopf/ceshi_commit/pulls/3/versions/33/diff |is_renamed|bool|是否重命名| |is_ambiguous|bool|| |is_submodule|bool|是否为子模块| +|diff|string|git diff内容| |sections.file_name|string|文件名称| |sections.name|string|| |sections.lines.left_index|int|文件变动之前所在行数| diff --git a/app/docs/slate/source/includes/_repositories.md b/app/docs/slate/source/includes/_repositories.md index a9fa42e2c..b06325090 100644 --- a/app/docs/slate/source/includes/_repositories.md +++ b/app/docs/slate/source/includes/_repositories.md @@ -2283,7 +2283,7 @@ await octokit.request('POST /api/v1/yystopf/ceshi/webhooks.json') |webhook.active |是| | bool | 是否激活| |webhook.branch_filter|否| |string|分支过滤| |webhook.events |否| |array|触发事件| - +|webhook.type |否| gitea |string| hook类型,gitea slack discord dingtalk telegram msteams feishu matrix jianmu| 触发事件字段说明 参数| 含义| diff --git a/app/services/api/v1/projects/webhooks/create_service.rb b/app/services/api/v1/projects/webhooks/create_service.rb index bef0a3405..edc8b2263 100644 --- a/app/services/api/v1/projects/webhooks/create_service.rb +++ b/app/services/api/v1/projects/webhooks/create_service.rb @@ -1,14 +1,14 @@ class Api::V1::Projects::Webhooks::CreateService < ApplicationService include ActiveModel::Model - attr_reader :project, :token, :owner, :repo, :active, :branch_filter, :content_type, :url, :http_method, :secret, :events + attr_reader :project, :token, :owner, :repo, :active, :branch_filter, :content_type, :url, :http_method, :secret, :events, :type attr_accessor :gitea_data validates :url, format: { with: URI::regexp(%w[http https]), message: "请输入正确的地址" } validates :active, inclusion: {in: [true, false]} validates :http_method, inclusion: { in: %w(POST GET), message: "请输入正确的请求方式"} validates :content_type, inclusion: { in: %w(json form), message: "请输入正确的Content Type"} - + validates :type, inclusion: {in: %w(gitea slack discord dingtalk telegram msteams feishu matrix jianmu), message: "请输入正确的Webhook Type"} def initialize(project, params, token=nil) @project = project @owner = project&.owner.login @@ -20,6 +20,7 @@ class Api::V1::Projects::Webhooks::CreateService < ApplicationService @http_method = params[:http_method] @secret = params[:secret] @events = params[:events] + @type = params[:type] || "gitea" @token = token end @@ -52,7 +53,7 @@ class Api::V1::Projects::Webhooks::CreateService < ApplicationService secret: secret }, events: events || [], - type: 'gitea', + type: type, } end diff --git a/app/views/api/v1/projects/webhooks/_simple_detail.json.jbuilder b/app/views/api/v1/projects/webhooks/_simple_detail.json.jbuilder index 145b3e59b..6926cfd32 100644 --- a/app/views/api/v1/projects/webhooks/_simple_detail.json.jbuilder +++ b/app/views/api/v1/projects/webhooks/_simple_detail.json.jbuilder @@ -1,3 +1,3 @@ -json.(webhook, :id, :url, :http_method, :is_active) +json.(webhook, :id, :url, :http_method, :is_active, :type) json.last_status webhook.last_status json.create_time Time.at(webhook.created_unix).strftime("%Y-%m-%d %H:%M:%S") \ No newline at end of file diff --git a/app/views/api/v1/projects/webhooks/_simple_gitea_detail.json.jbuilder b/app/views/api/v1/projects/webhooks/_simple_gitea_detail.json.jbuilder index ca180d8b1..96c9eac12 100644 --- a/app/views/api/v1/projects/webhooks/_simple_gitea_detail.json.jbuilder +++ b/app/views/api/v1/projects/webhooks/_simple_gitea_detail.json.jbuilder @@ -1,4 +1,5 @@ json.id webhook["id"] +json.type webhook["type"] json.content_type webhook['config']['content_type'] json.http_method webhook['config']['http_method'] json.url webhook['config']['url'] diff --git a/public/docs/api.html b/public/docs/api.html index bcc4da5c9..a9909eb91 100644 --- a/public/docs/api.html +++ b/public/docs/api.html @@ -11364,6 +11364,13 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks.json array 触发事件 + +webhook.type +否 +gitea +string +hook类型,gitea slack discord dingtalk telegram msteams feishu matrix jianmu +

触发事件字段说明

@@ -14032,6 +14039,11 @@ http://localhost:3000/api/v1/yystopf/ceshi_commit/pulls/3/versions/33/diff.json 是否为子模块 +diff +string +git diff内容 + + sections.file_name string 文件名称