ADD edit protected branch api

This commit is contained in:
Jasder 2020-12-04 16:18:27 +08:00
parent b3649f03e7
commit 3f60dd3f83
8 changed files with 163 additions and 15 deletions

View File

@ -2734,6 +2734,75 @@ http://localhost:3000/api/trustie/truesite/protected_branches.json | jq
```
---
### 编辑保护分支参数
```
GET /api/:owner/:repo/protected_branches/:branch_name/edit
```
*示例*
```
curl -X GET \
http://localhost:3000/api/trustie/truesite/protected_branches/master/edit.json | jq
```
*请求参数说明:*
|参数名|必选|类型|说明|
|-|-|-|-|
|owner |是|string |项目拥有者登录名 |
|repo |否|boolean |仓库名称 |
|branch_name |是|string |保护分支名称 |
*返回参数说明:*
|参数名|类型|说明|
|-|-|-|
|protected |boolean |是否为保护分支, true: 是; false: 不是 |
|branch_name |string |保护分支名称 |
|enable_push |boolean |是否启用推送, true: 启用; false: 不启用, 默认为false |
|enable_push_whitelist |boolean |是否启用白名单推送, true: 启用; false: 不启用, 默认为false, 该参数与enable_push参数为单选项只能选择|
|push_whitelist_usernames |array |推送白名单用户(即具有写操作的项目成员名称的数组), 该参数与enable_push_whitelist参数配合使用 |
|enable_merge_whitelist |boolean |是否启用合并白名单, true: 启用, false: 不启用, 默认为false |
|merge_whitelist_usernames |array |合并白名单用户(即具有写操作的项目成员名称的数组), 该参数与enable_merge_whitelist配合使用 |
|enable_status_check |boolean |是否启用状态检查, true: 启用; false: 不启用, 默认为false |
|required_approvals |int |所需的批准数, 默认为0 |
|enable_approvals_whitelist |boolean |是否启用批准仅限列入白名单的用户或团队, true: 启用, false: 不启用, 默认为false |
|approvals_whitelist_username |array |审查者白名单(即具有写操作的项目成员名称的数组), 该参数与enable_approvals_whitelist配合使用 |
|block_on_rejected_reviews |boolean |是否启用拒绝审核阻止合并功能, true: 启用, false: 不启用, 默认为false |
|dismiss_stale_approvals |boolean |是否启用取消过时的批准, true: 启用, false: 不启用, 默认为false |
|require_signed_commits |boolean |是否需要签名提交, true: 是, false: 否, 默认为false |
|block_on_outdated_branch |boolean |如果拉取请求已经过时,是否阻止合并, true: 是, false: 否, 默认为false |
|created_at |string|创建时间|
|updated_at |string|更新时间|
返回值
```
{
"branch_name": "master",
"protected": true,
"protected_branch": {
"branch_name": "master",
"enable_push": false,
"required_approvals": 0,
"enable_status_check": true,
"enable_push_whitelist": false,
"enable_merge_whitelist": true,
"enable_approvals_whitelist": false,
"dismiss_stale_approvals": false,
"block_on_rejected_reviews": false,
"block_on_outdated_branch": false,
"require_signed_commits": false,
"merge_whitelist_usernames": [
"jasder"
],
"push_whitelist_usernames": [],
"approvals_whitelist_usernames": [],
"created_at": "2020-12-03 12:00",
"updated_at": "2020-12-04 10:50"
}
}
```
---
### 修改保护分支参数
```
PATCH /api/:owner/:repo/protected_branches/:branch_name

View File

@ -31,6 +31,10 @@ class ProtectedBranchesController < ApplicationController
@protected_branch = ProtectedBranches::GetService.call(@repository, @owner, params)
end
def edit
@branch, @protected_branch = ProtectedBranches::EditService.call(@repository, @owner, params[:branch_name])
end
private
def render_protected_branch_json
@protected_branch.persisted? ? @protected_branch : render_error('创建失败!')

View File

@ -168,19 +168,35 @@ class Gitea::ClientService < ApplicationService
status = response.status
body = response&.body
body, message =
if body.present?
body = JSON.parse(body)
fix_body(body)
else
nil
end
puts "[gitea] status: #{status}"
puts "[gitea] message: #{message}"
puts "[gitea] body: #{body}"
log_error(status, body)
body, message = get_body_by_status(status, body)
[status, message, body]
end
def get_body_by_status(status, body)
body, message =
case status
when 404 then [nil, "404"]
when 403 then [nil, "403"]
else
if body.present?
body = JSON.parse(body)
fix_body(body)
else
nil
end
end
[body, message]
end
def log_error(status, body)
puts "[gitea] status: #{status}"
puts "[gitea] body: #{body}"
end
def fix_body(body)
return [body, nil] if body.is_a? Array
@ -241,7 +257,7 @@ class Gitea::ClientService < ApplicationService
success_statuses = [200, 201, 202, 204]
status, message, body = render_response(response)
error(message, status) unless success_statuses.include? status
return error(message, status) unless success_statuses.include? status
render_body(body)
end

View File

@ -1,5 +1,6 @@
module ProtectedBranches
class BaseService < ApplicationService
Error = Class.new(StandardError)
attr_accessor :repository, :owner, :params
def initialize(repository, user = nil, params = {})
@ -250,7 +251,7 @@ module ProtectedBranches
}
end
def validate!
protected_branch_exists = repository.protected_branches.exists?(params[:branch_name])
raise Error, "Protected branch '#{branch_name}' already exists" if protected_branch_exists
@ -263,4 +264,13 @@ module ProtectedBranches
end
end
def error(errors, award: nil, status: nil)
errors = Array.wrap(errors)
super(errors.to_sentence.presence, status).merge({
award: award,
errors: errors
})
end
end

View File

@ -3,9 +3,10 @@ module ProtectedBranches
def call
validate!
save_gitea_protected_branch!
save_protected_branch!
ProtectedBranch.transaction do
save_gitea_protected_branch!
save_protected_branch!
end
protected_branch
end

View File

@ -0,0 +1,35 @@
module ProtectedBranches
class EditService < ProtectedBranches::BaseService
def call
validate_branch_name!
protected_branch
rescue => ex
Rails.logger.info ex
raise Error, ex
end
private
def protected_branch
branch = get_common_branch
protected_branch ||= @repository.protected_branches.find_by(branch_name: branch_name)
[branch, protected_branch]
end
def get_common_branch
result = Gitea::Repository::Branches::GetService.call(@owner.login,
@repository.identifier, branch_name, @owner.gitea_token)
raise Error, '404' if result[:status] == :error
result
end
def validate_branch_name!
raise Error, '分支名称不能为空' if branch_name.blank?
end
def branch_name
params
end
end
end

View File

@ -0,0 +1,9 @@
json.branch_name @branch['name']
json.protected @branch['protected']
json.protected_branch do
if @protected_branch
json.partial! @protected_branch, as: :protected_branch
else
json.nil!
end
end

View File

@ -333,6 +333,10 @@ Rails.application.routes.draw do
'/protected_branches/:branch_name',
to: 'protected_branches#show'
)
get(
'/protected_branches/:branch_name/edit',
to: 'protected_branches#edit'
)
delete(
'/protected_branches/:branch_name',
to: 'protected_branches#destroy'