add: repository create webhook

This commit is contained in:
yystopf 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

View File

@ -952,3 +952,102 @@ await octokit.request('GET /api/yystopf/ceshi/webhooks.json')
<aside class="success">
Success Data.
</aside>
## 添加仓库webhook
添加仓库webhook
> 示例:
```shell
curl -X POST \
http://localhost:3000/api/yystopf/ceshi/webhooks.json
```
```javascript
await octokit.request('POST /api/yystopf/ceshi/webhooks.json')
```
### HTTP 请求
`POST /api/:owner/:repo/webhooks.json`
### 请求参数:
参数 | 必选 | 默认 | 类型 | 字段说明
--------- | ------- | ------- | -------- | ----------
|owner |是| | string |用户登录名 |
|repo |是| | string |项目标识identifier |
|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_assign|易修被指派|
|issue_label|易修标签被更新或删除|
|issue_milestone|易修被收入里程碑|
|issue_comment|易修评论|
|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|合并请求被同步|
> 请求的JSON示例:
```json
{
"active": true,
"content_type": "json",
"http_method": "GET",
"secret": "123456",
"url": "http://localhost:10000",
"branch_filter": "*",
"events": ["push"]
}
```
### 返回字段说明:
参数 | 类型 | 字段说明
--------- | ----------- | -----------
|id |int |id |
|url |string|地址|
|content_type |string|POST Content Type|
|is_active |bool |是否激活|
|type |string|类型|
|events | array|触发事件 |
|create_time |string|创建时间|
> 返回的JSON示例:
```json
{
"id": 18,
"type": "gitea",
"content_type": "json",
"url": "http://localhost:10000",
"events": [
"push"
],
"active": true,
"create_time": "2021-07-26 18:53:43"
}
```
<aside class="success">
Success Data.
</aside>

View File

@ -0,0 +1,8 @@
class Projects::Webhooks::CreateForm < BaseForm
attr_accessor :type, :url, :http_method, :content_type, :secret, :events, :active, :branch_filter
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"}
end

View File

@ -0,0 +1,23 @@
class Gitea::Repository::Webhooks::CreateService < Gitea::ClientService
attr_reader :token, :owner, :repo, :params
def initialize(token, owner, repo, params)
@token = token
@owner = owner
@repo = repo
@params = params
end
def call
response = post(url, request_params)
render_response(response)
end
private
def request_params
Hash.new.merge({token: token, data: params})
end
def url
"/repos/#{owner}/#{repo}/hooks".freeze
end
end

View File

@ -0,0 +1,7 @@
json.id @webhook["id"]
json.type @webhook["type"]
json.content_type @webhook["config"]["content_type"]
json.url @webhook["config"]["url"]
json.events @webhook["events"]
json.active @webhook["active"]
json.create_time @webhook["created_at"].to_time.strftime("%Y-%m-%d %H:%M:%S")

View File

@ -490,6 +490,9 @@
<li>
<a href="#webhooks" class="toc-h2 toc-link" data-title="获取仓库webhooks列表">获取仓库webhooks列表</a>
</li>
<li>
<a href="#webhook" class="toc-h2 toc-link" data-title="添加仓库webhook">添加仓库webhook</a>
</li>
</ul>
</li>
<li>
@ -6884,6 +6887,248 @@ http://localhost:3000/api/yystopf/ceshi/webhooks.json
<aside class="success">
Success Data.
</aside>
<h2 id='webhook'>添加仓库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> POST <span class="se">\</span>
http://localhost:3000/api/yystopf/ceshi/webhooks.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">POST /api/yystopf/ceshi/webhooks.json</span><span class="dl">'</span><span class="p">)</span>
</code></pre></div><h3 id='http-15'>HTTP 请求</h3>
<p><code>POST /api/:owner/:repo/webhooks.json</code></p>
<h3 id='2eb6f47757-15'>请求参数:</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>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_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_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>
</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-15'>返回字段说明:</h3>
<table><thead>
<tr>
<th>参数</th>
<th>类型</th>
<th>字段说明</th>
</tr>
</thead><tbody>
<tr>
<td>id</td>
<td>int</td>
<td>id</td>
</tr>
<tr>
<td>url</td>
<td>string</td>
<td>地址</td>
</tr>
<tr>
<td>content_type</td>
<td>string</td>
<td>POST Content Type</td>
</tr>
<tr>
<td>is_active</td>
<td>bool</td>
<td>是否激活</td>
</tr>
<tr>
<td>type</td>
<td>string</td>
<td>类型</td>
</tr>
<tr>
<td>events</td>
<td>array</td>
<td>触发事件</td>
</tr>
<tr>
<td>create_time</td>
<td>string</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">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">18</span><span class="p">,</span><span class="w">
</span><span class="nl">"type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"gitea"</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">"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">"events"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
</span><span class="s2">"push"</span><span class="w">
</span><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">"create_time"</span><span class="p">:</span><span class="w"> </span><span class="s2">"2021-07-26 18:53:43"</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.