From ebc4305ca88c543f15ccb8d0124ec11a31960b8c Mon Sep 17 00:00:00 2001 From: yystopf Date: Mon, 26 Jul 2021 17:25:06 +0800 Subject: [PATCH] add: repository webhooks --- .../projects/webhooks_controller.rb | 24 ++++ .../slate/source/includes/_repositories.md | 85 +++++++++++ app/models/gitea/public_key.rb | 2 +- app/models/gitea/webhook.rb | 10 ++ app/models/project.rb | 1 + .../projects/webhooks/_detail.json.jbuilder | 4 + .../projects/webhooks/index.json.jbuilder | 4 + config/routes.rb | 2 + public/docs/api.html | 132 ++++++++++++++++++ 9 files changed, 263 insertions(+), 1 deletion(-) create mode 100644 app/controllers/projects/webhooks_controller.rb create mode 100644 app/models/gitea/webhook.rb create mode 100644 app/views/projects/webhooks/_detail.json.jbuilder create mode 100644 app/views/projects/webhooks/index.json.jbuilder diff --git a/app/controllers/projects/webhooks_controller.rb b/app/controllers/projects/webhooks_controller.rb new file mode 100644 index 000000000..b450e4a20 --- /dev/null +++ b/app/controllers/projects/webhooks_controller.rb @@ -0,0 +1,24 @@ +class Projects::WebhooksController < Projects::BaseController + + def index + @webhooks = @project.webhooks + @webhooks = kaminari_paginate(@webhooks) + end + + def create + end + + def edit + end + + def update + end + + def destroy + end + + private + def find_webhook + @webhook = Gitea::Webhook.find_by_id(params[:id]) + end +end \ No newline at end of file diff --git a/app/docs/slate/source/includes/_repositories.md b/app/docs/slate/source/includes/_repositories.md index 40d3ba2ff..350eb64fc 100644 --- a/app/docs/slate/source/includes/_repositories.md +++ b/app/docs/slate/source/includes/_repositories.md @@ -867,3 +867,88 @@ await octokit.request('GET /api/jasder/jasder_test/sub_entries.json') + +## 获取仓库webhooks列表 +获取仓库webhooks列表 + +> 示例: + +```shell +curl -X GET \ +http://localhost:3000/api/yystopf/ceshi/webhooks.json +``` + +```javascript +await octokit.request('GET /api/yystopf/ceshi/webhooks.json') +``` + +### HTTP 请求 +`GET /api/:owner/:repo/webhooks.json` + +### 请求参数: +参数 | 必选 | 默认 | 类型 | 字段说明 +--------- | ------- | ------- | -------- | ---------- +|owner |是| |string |用户登录名 | +|repo |是| |string |项目标识identifier | + + +### 返回字段说明: +参数 | 类型 | 字段说明 +--------- | ----------- | ----------- +|id |int |id | +|url |string|地址| +|http_method |string|请求方式| +|is_active |bool |是否激活| +|type |string|类型| +|last_status |string|最后一次推送的状态| +|create_time |string|创建时间| + + +> 返回的JSON示例: + +```json +{ + "total_count": 4, + "webhooks": [ + { + "id": 2, + "url": "https://oapi.dingtalk.com/robot/send?access_token=7e1e19d0eddb6a5e33c5c2c4e66f4c88f9437184b9ed2c2653194c6374c7d513", + "http_method": "", + "is_active": true, + "type": "dingtalk", + "last_status": "succeed", + "create_time": "2021-07-12 10:50:07" + }, + { + "id": 3, + "url": "http://localhost:3000", + "http_method": "GET", + "is_active": true, + "type": "gitea", + "last_status": "succeed", + "create_time": "2021-07-26 10:03:45" + }, + { + "id": 4, + "url": "http://localhost:10081", + "http_method": "POST", + "is_active": true, + "type": "gitea", + "last_status": "waiting", + "create_time": "2021-07-26 16:56:53" + }, + { + "id": 5, + "url": "http://localhost:3001", + "http_method": "POST", + "is_active": true, + "type": "gitea", + "last_status": "fail", + "create_time": "2021-07-26 16:58:23" + } + ] +} +``` + diff --git a/app/models/gitea/public_key.rb b/app/models/gitea/public_key.rb index bc37c3bc7..bb2192358 100644 --- a/app/models/gitea/public_key.rb +++ b/app/models/gitea/public_key.rb @@ -4,6 +4,6 @@ class Gitea::PublicKey < Gitea::Base self.table_name = "public_key" - belongs_to :user, class_name: '::User', foreign_key: :gitea_uid, primary_key: :owner_id, optional: true + belongs_to :user, class_name: '::User', primary_key: :gitea_uid, foreign_key: :owner_id, optional: true end diff --git a/app/models/gitea/webhook.rb b/app/models/gitea/webhook.rb new file mode 100644 index 000000000..77b6f2e0f --- /dev/null +++ b/app/models/gitea/webhook.rb @@ -0,0 +1,10 @@ +class Gitea::Webhook < Gitea::Base + self.inheritance_column = nil + + self.table_name = 'webhook' + + belongs_to :project, class_name: "::Project", primary_key: :gpid, foreign_key: :repo_id, optional: true + + enum hook_task_type: {gogs: 1, slack: 2, gitea: 3, discord: 4, dingtalk: 5, telegram: 6, msteams: 7, feishu: 8, matrix: 9} + enum last_status: {waiting: 0, succeed: 1, fail: 2} +end \ No newline at end of file diff --git a/app/models/project.rb b/app/models/project.rb index 6d4922a73..ff285f179 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -125,6 +125,7 @@ class Project < ApplicationRecord has_one :applied_transfer_project,-> { order created_at: :desc }, dependent: :destroy has_many :pinned_projects, dependent: :destroy has_many :has_pinned_users, through: :pinned_projects, source: :user + has_many :webhooks, class_name: "Gitea::Webhook", primary_key: :gpid, foreign_key: :repo_id after_save :check_project_members, :reset_cache_data before_save :set_invite_code diff --git a/app/views/projects/webhooks/_detail.json.jbuilder b/app/views/projects/webhooks/_detail.json.jbuilder new file mode 100644 index 000000000..2497e5c64 --- /dev/null +++ b/app/views/projects/webhooks/_detail.json.jbuilder @@ -0,0 +1,4 @@ +json.(webhook, :id, :url, :http_method, :is_active) +json.type webhook.hook_task_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/projects/webhooks/index.json.jbuilder b/app/views/projects/webhooks/index.json.jbuilder new file mode 100644 index 000000000..62722acbe --- /dev/null +++ b/app/views/projects/webhooks/index.json.jbuilder @@ -0,0 +1,4 @@ +json.total_count @webhooks.total_count +json.webhooks @webhooks.each do |webhook| + json.partial! 'detail', webhook: webhook +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 0cc16406c..88f9aceb5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -348,6 +348,7 @@ Rails.application.routes.draw do get '/auth/qq/callback', to: 'oauth/qq#create' get '/auth/wechat/callback', to: 'oauth/wechat#create' + get '/auth/educoder/callback', to: 'oauth/educoder#create' resource :bind_user, only: [:create] resources :hot_keywords, only: [:index] @@ -571,6 +572,7 @@ Rails.application.routes.draw do post :cancel end end + resources :webhooks, except: [:show, :new] scope do get( '/blob/*id/diff', diff --git a/public/docs/api.html b/public/docs/api.html index 47e51427b..b4fe40935 100644 --- a/public/docs/api.html +++ b/public/docs/api.html @@ -487,6 +487,9 @@
  • 获取仓库代码子目录或者文件
  • +
  • + 获取仓库webhooks列表 +
  • @@ -6752,6 +6755,135 @@ http://localhost:3000//api/jasder/jasder_test/sub_entries.json +

    获取仓库webhooks列表

    +

    获取仓库webhooks列表

    + +
    +

    示例:

    +
    +
    curl -X GET \
    +http://localhost:3000/api/yystopf/ceshi/webhooks.json
    +
    await octokit.request('GET /api/yystopf/ceshi/webhooks.json')
    +

    HTTP 请求

    +

    GET /api/:owner/:repo/webhooks.json

    +

    请求参数:

    + + + + + + + + + + + + + + + + + + + + + + + +
    参数必选默认类型字段说明
    ownerstring用户登录名
    repostring项目标识identifier
    +

    返回字段说明:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数类型字段说明
    idintid
    urlstring地址
    http_methodstring请求方式
    is_activebool是否激活
    typestring类型
    last_statusstring最后一次推送的状态
    create_timestring创建时间
    + +
    +

    返回的JSON示例:

    +
    +
    {
    +    "total_count": 4,
    +    "webhooks": [
    +        {
    +            "id": 2,
    +            "url": "https://oapi.dingtalk.com/robot/send?access_token=7e1e19d0eddb6a5e33c5c2c4e66f4c88f9437184b9ed2c2653194c6374c7d513",
    +            "http_method": "",
    +            "is_active": true,
    +            "type": "dingtalk",
    +            "last_status": "succeed",
    +            "create_time": "2021-07-12 10:50:07"
    +        },
    +        {
    +            "id": 3,
    +            "url": "http://localhost:3000",
    +            "http_method": "GET",
    +            "is_active": true,
    +            "type": "gitea",
    +            "last_status": "succeed",
    +            "create_time": "2021-07-26 10:03:45"
    +        },
    +        {
    +            "id": 4,
    +            "url": "http://localhost:10081",
    +            "http_method": "POST",
    +            "is_active": true,
    +            "type": "gitea",
    +            "last_status": "waiting",
    +            "create_time": "2021-07-26 16:56:53"
    +        },
    +        {
    +            "id": 5,
    +            "url": "http://localhost:3001",
    +            "http_method": "POST",
    +            "is_active": true,
    +            "type": "gitea",
    +            "last_status": "fail",
    +            "create_time": "2021-07-26 16:58:23"
    +        }
    +    ]
    +}
    +
    +

    Pulls

    Issues

    Organizations

    Teams

    Errors