add: repository update destroy webhook

This commit is contained in:
yystopf 2021-07-27 16:44:58 +08:00
parent cb1a4d8c8c
commit 8b53aac8a6
7 changed files with 511 additions and 4 deletions

View File

@ -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

View File

@ -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')
<aside class="success">
Success Data.
</aside>
## 更新仓库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"
}
```
<aside class="success">
Success Data.
</aside>
## 删除仓库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"
}
```
<aside class="success">
Success Data.
</aside>

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -493,6 +493,12 @@
<li>
<a href="#webhook" class="toc-h2 toc-link" data-title="添加仓库webhook">添加仓库webhook</a>
</li>
<li>
<a href="#webhook-2" class="toc-h2 toc-link" data-title="更新仓库webhook">更新仓库webhook</a>
</li>
<li>
<a href="#webhook-3" class="toc-h2 toc-link" data-title="删除仓库webhook">删除仓库webhook</a>
</li>
</ul>
</li>
<li>
@ -6990,7 +6996,7 @@ http://localhost:3000/api/yystopf/ceshi/webhooks.json
</thead><tbody>
<tr>
<td>create</td>
<td>仓库创建</td>
<td>创建分支或标签</td>
</tr>
<tr>
<td>delete</td>
@ -7005,6 +7011,10 @@ http://localhost:3000/api/yystopf/ceshi/webhooks.json
<td>git仓库推送</td>
</tr>
<tr>
<td>issue</td>
<td>易修已打开、已关闭、已重新打开或编辑</td>
</tr>
<tr>
<td>issue_assign</td>
<td>易修被指派</td>
</tr>
@ -7021,6 +7031,10 @@ http://localhost:3000/api/yystopf/ceshi/webhooks.json
<td>易修评论</td>
</tr>
<tr>
<td>pull_request</td>
<td>合并请求</td>
</tr>
<tr>
<td>pull_request_assign</td>
<td>合并请求被指派</td>
</tr>
@ -7052,6 +7066,14 @@ http://localhost:3000/api/yystopf/ceshi/webhooks.json
<td>pull_request_sync</td>
<td>合并请求被同步</td>
</tr>
<tr>
<td>repository</td>
<td>创建或删除仓库</td>
</tr>
<tr>
<td>release</td>
<td>版本发布</td>
</tr>
</tbody></table>
<blockquote>
@ -7129,6 +7151,275 @@ http://localhost:3000/api/yystopf/ceshi/webhooks.json
<aside class="success">
Success Data.
</aside>
<h2 id='webhook-2'>更新仓库webhook</h2>
<p>更新仓库webhook</p>
<blockquote>
<p>示例:</p>
</blockquote>
<div class="highlight"><pre class="highlight shell tab-shell"><code>curl <span class="nt">-X</span> PATCH <span class="se">\</span>
http://localhost:3000/api/yystopf/ceshi/webhooks/7.json
</code></pre></div><div class="highlight"><pre class="highlight javascript tab-javascript"><code><span class="k">await</span> <span class="nx">octokit</span><span class="p">.</span><span class="nx">request</span><span class="p">(</span><span class="dl">'</span><span class="s1">PATCH /api/yystopf/ceshi/webhooks/7.json</span><span class="dl">'</span><span class="p">)</span>
</code></pre></div><h3 id='http-16'>HTTP 请求</h3>
<p><code>PATCH /api/:owner/:repo/webhooks/:id.json</code></p>
<h3 id='2eb6f47757-16'>请求参数:</h3>
<table><thead>
<tr>
<th>参数</th>
<th>必选</th>
<th>默认</th>
<th>类型</th>
<th>字段说明</th>
</tr>
</thead><tbody>
<tr>
<td>owner</td>
<td></td>
<td></td>
<td>string</td>
<td>用户登录名</td>
</tr>
<tr>
<td>repo</td>
<td></td>
<td></td>
<td>string</td>
<td>项目标识identifier</td>
</tr>
<tr>
<td>id</td>
<td></td>
<td></td>
<td>string</td>
<td>webhook id</td>
</tr>
<tr>
<td>webhook.url</td>
<td></td>
<td></td>
<td>string</td>
<td>目标url</td>
</tr>
<tr>
<td>webhook.type</td>
<td></td>
<td></td>
<td>string</td>
<td>类型</td>
</tr>
<tr>
<td>webhook.http_method</td>
<td></td>
<td></td>
<td>string</td>
<td>http方法, POST和GET</td>
</tr>
<tr>
<td>webhook.content_type</td>
<td></td>
<td></td>
<td>string</td>
<td>POST Content Type</td>
</tr>
<tr>
<td>webhook.secret</td>
<td></td>
<td></td>
<td>string</td>
<td>密钥文本</td>
</tr>
<tr>
<td>webhook.active</td>
<td></td>
<td></td>
<td>bool</td>
<td>是否激活</td>
</tr>
<tr>
<td>webhook.branch_filter</td>
<td></td>
<td></td>
<td>string</td>
<td>分支过滤</td>
</tr>
<tr>
<td>webhook.events</td>
<td></td>
<td></td>
<td>array</td>
<td>触发事件</td>
</tr>
</tbody></table>
<p>触发事件字段说明 </p>
<table><thead>
<tr>
<th>参数</th>
<th>含义</th>
</tr>
</thead><tbody>
<tr>
<td>create</td>
<td>创建分支或标签</td>
</tr>
<tr>
<td>delete</td>
<td>分支或标签删除</td>
</tr>
<tr>
<td>fork</td>
<td>仓库被fork</td>
</tr>
<tr>
<td>push</td>
<td>git仓库推送</td>
</tr>
<tr>
<td>issue</td>
<td>易修已打开、已关闭、已重新打开或编辑</td>
</tr>
<tr>
<td>issue_assign</td>
<td>易修被指派</td>
</tr>
<tr>
<td>issue_label</td>
<td>易修标签被更新或删除</td>
</tr>
<tr>
<td>issue_milestone</td>
<td>易修被收入里程碑</td>
</tr>
<tr>
<td>issue_comment</td>
<td>易修评论</td>
</tr>
<tr>
<td>pull_request</td>
<td>合并请求</td>
</tr>
<tr>
<td>pull_request_assign</td>
<td>合并请求被指派</td>
</tr>
<tr>
<td>pull_request_label</td>
<td>合并请求被贴上标签</td>
</tr>
<tr>
<td>pull_request_milestone</td>
<td>合并请求被记录于里程碑中</td>
</tr>
<tr>
<td>pull_request_comment</td>
<td>合并请求被评论</td>
</tr>
<tr>
<td>pull_request_review_approved</td>
<td>合并请求被批准</td>
</tr>
<tr>
<td>pull_request_review_rejected</td>
<td>合并请求被拒绝</td>
</tr>
<tr>
<td>pull_request_review_comment</td>
<td>合并请求被提出审查意见</td>
</tr>
<tr>
<td>pull_request_sync</td>
<td>合并请求被同步</td>
</tr>
<tr>
<td>repository</td>
<td>创建或删除仓库</td>
</tr>
<tr>
<td>release</td>
<td>版本发布</td>
</tr>
</tbody></table>
<blockquote>
<p>请求的JSON示例:</p>
</blockquote>
<div class="highlight"><pre class="highlight json tab-json"><code><span class="p">{</span><span class="w">
</span><span class="nl">"active"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w">
</span><span class="nl">"content_type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"json"</span><span class="p">,</span><span class="w">
</span><span class="nl">"http_method"</span><span class="p">:</span><span class="w"> </span><span class="s2">"GET"</span><span class="p">,</span><span class="w">
</span><span class="nl">"secret"</span><span class="p">:</span><span class="w"> </span><span class="s2">"123456"</span><span class="p">,</span><span class="w">
</span><span class="nl">"url"</span><span class="p">:</span><span class="w"> </span><span class="s2">"http://localhost:10000"</span><span class="p">,</span><span class="w">
</span><span class="nl">"branch_filter"</span><span class="p">:</span><span class="w"> </span><span class="s2">"*"</span><span class="p">,</span><span class="w">
</span><span class="nl">"events"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">"push"</span><span class="p">]</span><span class="w">
</span><span class="p">}</span><span class="w">
</span></code></pre></div><h3 id='7447e4874e-16'>返回字段说明:</h3>
<blockquote>
<p>返回的JSON示例:</p>
</blockquote>
<div class="highlight"><pre class="highlight json tab-json"><code><span class="p">{</span><span class="w">
</span><span class="nl">"status"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
</span><span class="nl">"message"</span><span class="p">:</span><span class="w"> </span><span class="s2">"success"</span><span class="w">
</span><span class="p">}</span><span class="w">
</span></code></pre></div>
<aside class="success">
Success Data.
</aside>
<h2 id='webhook-3'>删除仓库webhook</h2>
<p>删除仓库webhook</p>
<blockquote>
<p>示例:</p>
</blockquote>
<div class="highlight"><pre class="highlight shell tab-shell"><code>curl <span class="nt">-X</span> DELETE <span class="se">\</span>
http://localhost:3000/api/yystopf/ceshi/webhooks/7.json
</code></pre></div><div class="highlight"><pre class="highlight javascript tab-javascript"><code><span class="k">await</span> <span class="nx">octokit</span><span class="p">.</span><span class="nx">request</span><span class="p">(</span><span class="dl">'</span><span class="s1">DELETE /api/yystopf/ceshi/webhooks/7.json</span><span class="dl">'</span><span class="p">)</span>
</code></pre></div><h3 id='http-17'>HTTP 请求</h3>
<p><code>DELETE /api/:owner/:repo/webhooks/:id.json</code></p>
<h3 id='2eb6f47757-17'>请求参数:</h3>
<table><thead>
<tr>
<th>参数</th>
<th>必选</th>
<th>默认</th>
<th>类型</th>
<th>字段说明</th>
</tr>
</thead><tbody>
<tr>
<td>owner</td>
<td></td>
<td></td>
<td>string</td>
<td>用户登录名</td>
</tr>
<tr>
<td>repo</td>
<td></td>
<td></td>
<td>string</td>
<td>项目标识identifier</td>
</tr>
<tr>
<td>id</td>
<td></td>
<td></td>
<td>string</td>
<td>webhook id</td>
</tr>
</tbody></table>
<h3 id='7447e4874e-17'>返回字段说明:</h3>
<blockquote>
<p>返回的JSON示例:</p>
</blockquote>
<div class="highlight"><pre class="highlight json tab-json"><code><span class="p">{</span><span class="w">
</span><span class="nl">"status"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
</span><span class="nl">"message"</span><span class="p">:</span><span class="w"> </span><span class="s2">"success"</span><span class="w">
</span><span class="p">}</span><span class="w">
</span></code></pre></div>
<aside class="success">
Success Data.
</aside>
<h1 id='pulls'>Pulls</h1><h1 id='issues'>Issues</h1><h1 id='organizations'>Organizations</h1><h1 id='teams'>Teams</h1><h1 id='errors'>Errors</h1>
<aside class="notice">
This error section is stored in a separate file in <code>includes/_errors.md</code>. Slate allows you to optionally separate out your docs into many files...just save them to the <code>includes</code> folder and add them to the top of your <code>index.md</code>'s frontmatter. Files are included in the order listed.