From 8b53aac8a697373b4ce34a0dccad0bcd0d91ae90 Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 27 Jul 2021 16:44:58 +0800 Subject: [PATCH] add: repository update destroy webhook --- .../projects/webhooks_controller.rb | 30 +- .../slate/source/includes/_repositories.md | 132 +++++++- app/models/gitea/webhook.rb | 1 + .../repository/webhooks/delete_service.rb | 24 ++ .../repository/webhooks/update_service.rb | 24 ++ .../projects/webhooks/edit.json.jbuilder | 11 + public/docs/api.html | 293 +++++++++++++++++- 7 files changed, 511 insertions(+), 4 deletions(-) create mode 100644 app/services/gitea/repository/webhooks/delete_service.rb create mode 100644 app/services/gitea/repository/webhooks/update_service.rb create mode 100644 app/views/projects/webhooks/edit.json.jbuilder diff --git a/app/controllers/projects/webhooks_controller.rb b/app/controllers/projects/webhooks_controller.rb index 144082f56..f10f0d224 100644 --- a/app/controllers/projects/webhooks_controller.rb +++ b/app/controllers/projects/webhooks_controller.rb @@ -1,5 +1,6 @@ class Projects::WebhooksController < Projects::BaseController before_action :require_manager! + before_action :find_webhook, only:[:edit, :update, :destroy] def index @webhooks = @project.webhooks @@ -8,13 +9,13 @@ class Projects::WebhooksController < Projects::BaseController def create ActiveRecord::Base.transaction do + return render_error("webhooks数量已到上限!请删除暂不使用的webhooks以进行添加操作") if @project.webhooks.size > 19 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 @@ -25,12 +26,39 @@ class Projects::WebhooksController < Projects::BaseController end def edit + end def update + 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::UpdateService.call(current_user.gitea_token, @project&.owner&.login, @project&.identifier, @webhook.id, gitea_webhooks_params) + 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) end def destroy + response = Gitea::Repository::Webhooks::DeleteService.call(current_user.gitea_token, @project&.owner&.login, @project&.identifier, @webhook.id) + 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 + + def webhook_tasks end private diff --git a/app/docs/slate/source/includes/_repositories.md b/app/docs/slate/source/includes/_repositories.md index 04b232522..08d48f385 100644 --- a/app/docs/slate/source/includes/_repositories.md +++ b/app/docs/slate/source/includes/_repositories.md @@ -988,14 +988,16 @@ await octokit.request('POST /api/yystopf/ceshi/webhooks.json') 参数| 含义| --------- | ------- | ------- | -|create|仓库创建| +|create|创建分支或标签| |delete|分支或标签删除| |fork|仓库被fork| |push|git仓库推送| +|issue|易修已打开、已关闭、已重新打开或编辑| |issue_assign|易修被指派| |issue_label|易修标签被更新或删除| |issue_milestone|易修被收入里程碑| |issue_comment|易修评论| +|pull_request|合并请求| |pull_request_assign|合并请求被指派| |pull_request_label|合并请求被贴上标签| |pull_request_milestone|合并请求被记录于里程碑中| @@ -1004,7 +1006,8 @@ await octokit.request('POST /api/yystopf/ceshi/webhooks.json') |pull_request_review_rejected|合并请求被拒绝| |pull_request_review_comment|合并请求被提出审查意见| |pull_request_sync|合并请求被同步| - +|repository|创建或删除仓库| +|release|版本发布| > 请求的JSON示例: @@ -1051,3 +1054,128 @@ await octokit.request('POST /api/yystopf/ceshi/webhooks.json') + +## 更新仓库webhook +更新仓库webhook + +> 示例: + +```shell +curl -X PATCH \ +http://localhost:3000/api/yystopf/ceshi/webhooks/7.json +``` + +```javascript +await octokit.request('PATCH /api/yystopf/ceshi/webhooks/7.json') +``` + +### HTTP 请求 +`PATCH /api/:owner/:repo/webhooks/:id.json` + +### 请求参数: +参数 | 必选 | 默认 | 类型 | 字段说明 +--------- | ------- | ------- | -------- | ---------- +|owner |是| | string |用户登录名 | +|repo |是| | string |项目标识identifier | +|id |是| | string |webhook id | +|webhook.url |是| | string |目标url | +|webhook.type |否| | string |类型| +|webhook.http_method |是| | string | http方法, POST和GET | +|webhook.content_type |是| | string | POST Content Type | +|webhook.secret |否| | string |密钥文本| +|webhook.active |是| | bool | 是否激活| +|webhook.branch_filter|否| |string|分支过滤| +|webhook.events |否| |array|触发事件| + +触发事件字段说明 + +参数| 含义| +--------- | ------- | ------- | +|create|创建分支或标签| +|delete|分支或标签删除| +|fork|仓库被fork| +|push|git仓库推送| +|issue|易修已打开、已关闭、已重新打开或编辑| +|issue_assign|易修被指派| +|issue_label|易修标签被更新或删除| +|issue_milestone|易修被收入里程碑| +|issue_comment|易修评论| +|pull_request|合并请求| +|pull_request_assign|合并请求被指派| +|pull_request_label|合并请求被贴上标签| +|pull_request_milestone|合并请求被记录于里程碑中| +|pull_request_comment|合并请求被评论| +|pull_request_review_approved|合并请求被批准| +|pull_request_review_rejected|合并请求被拒绝| +|pull_request_review_comment|合并请求被提出审查意见| +|pull_request_sync|合并请求被同步| +|repository|创建或删除仓库| +|release|版本发布| + + +> 请求的JSON示例: + +```json +{ + "active": true, + "content_type": "json", + "http_method": "GET", + "secret": "123456", + "url": "http://localhost:10000", + "branch_filter": "*", + "events": ["push"] +} +``` + +### 返回字段说明: + +> 返回的JSON示例: + +```json +{ + "status": 0, + "message": "success" +} +``` + + + +## 删除仓库webhook +删除仓库webhook + +> 示例: + +```shell +curl -X DELETE \ +http://localhost:3000/api/yystopf/ceshi/webhooks/7.json +``` + +```javascript +await octokit.request('DELETE /api/yystopf/ceshi/webhooks/7.json') +``` + +### HTTP 请求 +`DELETE /api/:owner/:repo/webhooks/:id.json` + +### 请求参数: +参数 | 必选 | 默认 | 类型 | 字段说明 +--------- | ------- | ------- | -------- | ---------- +|owner |是| | string |用户登录名 | +|repo |是| | string |项目标识identifier | +|id |是| | string |webhook id | + +### 返回字段说明: + +> 返回的JSON示例: + +```json +{ + "status": 0, + "message": "success" +} +``` + diff --git a/app/models/gitea/webhook.rb b/app/models/gitea/webhook.rb index 77b6f2e0f..597272cd2 100644 --- a/app/models/gitea/webhook.rb +++ b/app/models/gitea/webhook.rb @@ -7,4 +7,5 @@ class Gitea::Webhook < Gitea::Base 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} + enum content_type: {json: 1, form: 2} end \ No newline at end of file diff --git a/app/services/gitea/repository/webhooks/delete_service.rb b/app/services/gitea/repository/webhooks/delete_service.rb new file mode 100644 index 000000000..997e9006e --- /dev/null +++ b/app/services/gitea/repository/webhooks/delete_service.rb @@ -0,0 +1,24 @@ +class Gitea::Repository::Webhooks::DeleteService < Gitea::ClientService + attr_reader :token, :owner, :repo, :id + + def initialize(token, owner, repo, id) + @token = token + @owner = owner + @repo = repo + @id = id + end + + def call + response = delete(url, params) + render_response(response) + end + + private + def params + Hash.new.merge(token: token) + end + + def url + "/repos/#{owner}/#{repo}/hooks/#{id}".freeze + end +end \ No newline at end of file diff --git a/app/services/gitea/repository/webhooks/update_service.rb b/app/services/gitea/repository/webhooks/update_service.rb new file mode 100644 index 000000000..6094c6c51 --- /dev/null +++ b/app/services/gitea/repository/webhooks/update_service.rb @@ -0,0 +1,24 @@ +class Gitea::Repository::Webhooks::UpdateService < Gitea::ClientService + attr_reader :token, :owner, :repo, :id, :params + def initialize(token, owner, repo, id, params) + @token = token + @owner = owner + @repo = repo + @id = id + @params = params + end + + def call + response = patch(url, data_params) + render_response(response) + end + + private + def url + "/repos/#{owner}/#{repo}/hooks/#{id}" + end + + def data_params + Hash.new.merge(token: token, data: params).compact + end +end \ No newline at end of file diff --git a/app/views/projects/webhooks/edit.json.jbuilder b/app/views/projects/webhooks/edit.json.jbuilder new file mode 100644 index 000000000..97686b55f --- /dev/null +++ b/app/views/projects/webhooks/edit.json.jbuilder @@ -0,0 +1,11 @@ +json.id @webhook.id +json.(@webhook, :id, :http_method, :content_type, :url, :secret, :last_status, :is_active) +json.type @webhook.hook_task_type +json.create_time Time.at(@webhook.created_unix).strftime("%Y-%m-%d %H:%M:%S") +event = JSON.parse(@webhook.events) +json.branch_filter event["branch_filter"] +if event["send_everything"] + json.events event["events"].keys +else + json.events event["events"].select{|k, v| v}.keys +end diff --git a/public/docs/api.html b/public/docs/api.html index 59d7574c3..8033891a4 100644 --- a/public/docs/api.html +++ b/public/docs/api.html @@ -493,6 +493,12 @@
  • 添加仓库webhook
  • +
  • + 更新仓库webhook +
  • +
  • + 删除仓库webhook +
  • @@ -6990,7 +6996,7 @@ http://localhost:3000/api/yystopf/ceshi/webhooks.json create -仓库创建 +创建分支或标签 delete @@ -7005,6 +7011,10 @@ http://localhost:3000/api/yystopf/ceshi/webhooks.json git仓库推送 +issue +易修已打开、已关闭、已重新打开或编辑 + + issue_assign 易修被指派 @@ -7021,6 +7031,10 @@ http://localhost:3000/api/yystopf/ceshi/webhooks.json 易修评论 +pull_request +合并请求 + + pull_request_assign 合并请求被指派 @@ -7052,6 +7066,14 @@ http://localhost:3000/api/yystopf/ceshi/webhooks.json pull_request_sync 合并请求被同步 + +repository +创建或删除仓库 + + +release +版本发布 +
    @@ -7129,6 +7151,275 @@ http://localhost:3000/api/yystopf/ceshi/webhooks.json +

    更新仓库webhook

    +

    更新仓库webhook

    + +
    +

    示例:

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

    HTTP 请求

    +

    PATCH /api/:owner/:repo/webhooks/:id.json

    +

    请求参数:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数必选默认类型字段说明
    ownerstring用户登录名
    repostring项目标识identifier
    idstringwebhook id
    webhook.urlstring目标url
    webhook.typestring类型
    webhook.http_methodstringhttp方法, POST和GET
    webhook.content_typestringPOST Content Type
    webhook.secretstring密钥文本
    webhook.activebool是否激活
    webhook.branch_filterstring分支过滤
    webhook.eventsarray触发事件
    + +

    触发事件字段说明

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数含义
    create创建分支或标签
    delete分支或标签删除
    fork仓库被fork
    pushgit仓库推送
    issue易修已打开、已关闭、已重新打开或编辑
    issue_assign易修被指派
    issue_label易修标签被更新或删除
    issue_milestone易修被收入里程碑
    issue_comment易修评论
    pull_request合并请求
    pull_request_assign合并请求被指派
    pull_request_label合并请求被贴上标签
    pull_request_milestone合并请求被记录于里程碑中
    pull_request_comment合并请求被评论
    pull_request_review_approved合并请求被批准
    pull_request_review_rejected合并请求被拒绝
    pull_request_review_comment合并请求被提出审查意见
    pull_request_sync合并请求被同步
    repository创建或删除仓库
    release版本发布
    + +
    +

    请求的JSON示例:

    +
    +
    {
    +    "active": true, 
    +    "content_type": "json",
    +    "http_method": "GET",
    +    "secret": "123456",
    +    "url": "http://localhost:10000",
    +    "branch_filter": "*",
    +    "events": ["push"]
    +}
    +

    返回字段说明:

    +
    +

    返回的JSON示例:

    +
    +
    {
    +    "status": 0,
    +    "message": "success"
    +}
    +
    + +

    删除仓库webhook

    +

    删除仓库webhook

    + +
    +

    示例:

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

    HTTP 请求

    +

    DELETE /api/:owner/:repo/webhooks/:id.json

    +

    请求参数:

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

    返回字段说明:

    +
    +

    返回的JSON示例:

    +
    +
    {
    +    "status": 0,
    +    "message": "success"
    +}
    +
    +

    Pulls

    Issues

    Organizations

    Teams

    Errors