Browse Source

merge from develop

pull/313/head
yystopf 4 years ago
parent
commit
63cd6bb0a4
12 changed files with 165 additions and 2 deletions
  1. +1
    -1
      Gemfile
  2. +13
    -0
      app/controllers/api/v1/base_controller.rb
  3. +19
    -0
      app/controllers/api/v1/projects/branches_controller.rb
  4. +1
    -1
      app/controllers/concerns/api/project_helper.rb
  5. +27
    -0
      app/services/api/v1/projects/branches/all_list_service.rb
  6. +47
    -0
      app/services/api/v1/projects/branches/create_service.rb
  7. +4
    -0
      app/views/api/v1/projects/branches/_simple_detail.json.jbuilder
  8. +23
    -0
      app/views/api/v1/projects/branches/_simple_gitea_detail.json.jbuilder
  9. +3
    -0
      app/views/api/v1/projects/branches/all.json.jbuilder
  10. +1
    -0
      app/views/api/v1/projects/branches/create.json.jbuilder
  11. +21
    -0
      app/views/api/v1/users/_commit_user.json.jbuilder
  12. +5
    -0
      config/routes/api.rb

+ 1
- 1
Gemfile View File

@@ -135,4 +135,4 @@ gem 'doorkeeper'

gem 'doorkeeper-jwt'

gem 'gitea-client', '~> 0.8.2'
gem 'gitea-client', '~> 0.9.1'

+ 13
- 0
app/controllers/api/v1/base_controller.rb View File

@@ -20,8 +20,21 @@ class Api::V1::BaseController < ApplicationController
# end
# end

# 具有对仓库的管理权限
def require_manager_above
@project = load_project
return render_forbidden unless current_user.admin? && @project.manager?(current_user)
end

# 具有对仓库的操作权限
def require_operate_above
@project = load_project
return render_forbidden unless current_user.admin? && @project.operator?(current_user)
end

# 具有对仓库的访问权限
def require_public_and_member_above
@project = load_project
return render_forbidden unless @project.is_public || (current_user.admin? && @project.member?(current_user))
end
end

+ 19
- 0
app/controllers/api/v1/projects/branches_controller.rb View File

@@ -0,0 +1,19 @@
class Api::V1::Projects::BranchesController < Api::V1::BaseController
before_action :require_public_and_member_above, only: [:index]

def all
@result_object = Api::V1::Projects::Branches::AllListService.call(@project, current_user&.gitea_token)
end

before_action :require_operate_above, only: [:create]

def create
@result_object = Api::V1::Projects::Branches::CreateService.call(@project, branch_params, current_user&.gitea_token)
puts @result_object
end

private
def branch_params
params.require(:branch).permit(:new_branch_name, :old_branch_name)
end
end

+ 1
- 1
app/controllers/concerns/api/project_helper.rb View File

@@ -8,7 +8,7 @@ module Api::ProjectHelper
@project, @owner = Project.find_with_namespace(namespace, repo)

if @project
logger.info "###########:project not founded"
logger.info "###########:project founded"
@project
else
logger.info "###########:project not found"


+ 27
- 0
app/services/api/v1/projects/branches/all_list_service.rb View File

@@ -0,0 +1,27 @@
class Api::V1::Projects::Branches::AllListService < ApplicationService

attr_accessor :project, :token, :owner, :repo
attr_accessor :gitea_data

def initialize(project, token=nil)
@project = project
@owner = project&.owner.login
@repo = project&.identifier
@token = token
end

def call
$gitea_client.token = token unless token.blank?
load_gitea_data
$gitea_client.token = nil unless token.blank?
gitea_data
rescue
raise Error, "服务器错误,请联系系统管理员!"
end

private
def load_gitea_data
@gitea_data = $gitea_client.get_repos_branch_name_set_by_owner_repo(owner, repo)
end
end

+ 47
- 0
app/services/api/v1/projects/branches/create_service.rb View File

@@ -0,0 +1,47 @@
class Api::V1::Projects::Branches::CreateService < ApplicationService
include ActiveModel::Model

attr_accessor :project, :token, :owner, :repo, :old_branch_name, :new_branch_name
attr_accessor :gitea_data

validates :new_branch_name, :old_branch_name, presence: :true

def initialize(project, params, token=nil)
@project = project
@owner = project&.owner.login
@repo = project&.identifier
@new_branch_name = params[:new_branch_name]
@old_branch_name = params[:old_branch_name]
@token = token
end

def call
raise Error, errors.full_messages.join(",") unless valid?
$gitea_client.token = token unless token.blank?

check_new_branch_exist
excute_data_to_gitea
$gitea_client.token = nil unless token.blank?
gitea_data
end

private
def request_body
{
new_branch_name: new_branch_name,
old_branch_name: old_branch_name,
}
end

def excute_data_to_gitea
@gitea_data = $gitea_client.post_repos_branches_by_owner_repo(owner, repo, {body: request_body.to_json})
raise Error, '创建分支失败!' unless @gitea_data.is_a?(Hash)
end

def check_new_branch_exist
result = $gitea_client.get_repos_branch_name_set_by_owner_repo(owner, repo)
raise Error, '查询分支名称失败!' unless result.is_a?(Hash)
raise Error, '分支已存在!' if result['branch_name'].include?(@new_branch_name)
end
end

+ 4
- 0
app/views/api/v1/projects/branches/_simple_detail.json.jbuilder View File

@@ -0,0 +1,4 @@
json.name branch
json.http_url render_http_url(@project)
json.zip_url render_zip_url(@owner, @project.repository, branch)
json.tar_url render_tar_url(@owner, @project.repository, branch)

+ 23
- 0
app/views/api/v1/projects/branches/_simple_gitea_detail.json.jbuilder View File

@@ -0,0 +1,23 @@
json.name branch['name']
json.commit do
json.id branch['commit']['id']
json.message branch['commit']['message']
json.author do
json.partial! 'api/v1/users/commit_user', locals: { user: render_cache_commit_author(branch['commit']['author']), name: branch['commit']['author']['name'] }
end
json.committer do
json.partial! 'api/v1/users/commit_user', locals: { user: render_cache_commit_author(branch['commit']['committer']), name: branch['commit']['committer']['name'] }
end
json.time_ago time_from_now(branch['commit']['timestamp'].to_time)
json.timestamp branch['commit']['timestamp']
end

json.protected branch['protected']
json.user_can_push branch['user_can_push']
json.user_can_merge branch['user_can_merge']
json.commit_id branch['commit_id']
json.commit_time_from_now branch['commit_time']
json.commit_time branch['commit_time']
json.default_branch branch['default_branch']

+ 3
- 0
app/views/api/v1/projects/branches/all.json.jbuilder View File

@@ -0,0 +1,3 @@
json.array! @result_object["branch_name"] do |branch|
json.partial! "api/v1/projects/branches/simple_detail", branch: branch
end

+ 1
- 0
app/views/api/v1/projects/branches/create.json.jbuilder View File

@@ -0,0 +1 @@
json.partial! "api/v1/projects/branches/simple_gitea_detail", branch: @result_object

+ 21
- 0
app/views/api/v1/users/_commit_user.json.jbuilder View File

@@ -0,0 +1,21 @@
if user.present?
if user.is_a?(Hash)
json.id user["id"]
json.login user["login"]
json.name user["name"]
json.type user["type"]
json.image_url user["avatar_url"]
else
json.id user.id
json.login user.login
json.name user.real_name
json.type user&.type
json.image_url url_to_avatar(user)
end
else
json.id nil
json.login name
json.name name
json.type nil
json.image_url User::Avatar.get_letter_avatar_url(name)
end

+ 5
- 0
config/routes/api.rb View File

@@ -23,6 +23,11 @@ defaults format: :json do
get :hooktasks
end
end
resources :branches, only:[:index, :create] do
collection do
get :all
end
end
end
end



Loading…
Cancel
Save