新增: 创建分支接口
This commit is contained in:
parent
4ed3a9d478
commit
24d83133e7
2
Gemfile
2
Gemfile
|
@ -135,4 +135,4 @@ gem 'doorkeeper'
|
||||||
|
|
||||||
gem 'doorkeeper-jwt'
|
gem 'doorkeeper-jwt'
|
||||||
|
|
||||||
gem 'gitea-client', '~> 0.8.2'
|
gem 'gitea-client', '~> 0.9.1'
|
|
@ -20,8 +20,21 @@ class Api::V1::BaseController < ApplicationController
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
|
|
||||||
|
# 具有对仓库的管理权限
|
||||||
def require_manager_above
|
def require_manager_above
|
||||||
@project = load_project
|
@project = load_project
|
||||||
return render_forbidden unless current_user.admin? && @project.manager?(current_user)
|
return render_forbidden unless current_user.admin? && @project.manager?(current_user)
|
||||||
end
|
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
|
end
|
|
@ -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
|
|
@ -248,6 +248,18 @@ class ApplicationController < ActionController::Base
|
||||||
#return if params[:controller] == "main"
|
#return if params[:controller] == "main"
|
||||||
# Find the current user
|
# Find the current user
|
||||||
#Rails.logger.info("current_laboratory is #{current_laboratory} domain is #{request.subdomain}")
|
#Rails.logger.info("current_laboratory is #{current_laboratory} domain is #{request.subdomain}")
|
||||||
|
if request.headers["Authorization"].present? && request.headers["Authorization"].start_with?('Bearer')
|
||||||
|
tip_exception(401, "请登录后再操作!") unless valid_doorkeeper_token?
|
||||||
|
if @doorkeeper_token.present?
|
||||||
|
# client方法对接,需要一直带着用户标识uid
|
||||||
|
if @doorkeeper_token.resource_owner_id.blank?
|
||||||
|
tip_exception(-1, "缺少用户标识!") if params[:uid].nil?
|
||||||
|
User.current = User.find(params[:uid])
|
||||||
|
else
|
||||||
|
User.current = User.find_by(id: @doorkeeper_token.resource_owner_id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
User.current = find_current_user
|
User.current = find_current_user
|
||||||
uid_logger("user_setup: " + (User.current.logged? ? "#{User.current.try(:login)} (id=#{User.current.try(:id)})" : "anonymous"))
|
uid_logger("user_setup: " + (User.current.logged? ? "#{User.current.try(:login)} (id=#{User.current.try(:id)})" : "anonymous"))
|
||||||
|
|
||||||
|
@ -265,15 +277,15 @@ class ApplicationController < ActionController::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if !User.current.logged? && Rails.env.development?
|
# if !User.current.logged? && Rails.env.development?
|
||||||
user = User.find 1
|
# user = User.find 1
|
||||||
User.current = user
|
# User.current = user
|
||||||
start_user_session(user)
|
# start_user_session(user)
|
||||||
end
|
# end
|
||||||
|
|
||||||
|
|
||||||
# 测试版前端需求
|
# 测试版前端需求
|
||||||
logger.info("subdomain:#{request.subdomain}")
|
# logger.info("subdomain:#{request.subdomain}")
|
||||||
# if request.subdomain != "www"
|
# if request.subdomain != "www"
|
||||||
# if params[:debug] == 'teacher' #todo 为了测试,记得讲debug删除
|
# if params[:debug] == 'teacher' #todo 为了测试,记得讲debug删除
|
||||||
# User.current = User.find 81403
|
# User.current = User.find 81403
|
||||||
|
@ -286,6 +298,7 @@ class ApplicationController < ActionController::Base
|
||||||
# cookies.signed[:user_id] = user.id
|
# cookies.signed[:user_id] = user.id
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
|
end
|
||||||
# User.current = User.find 81403
|
# User.current = User.find 81403
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ module Api::ProjectHelper
|
||||||
@project, @owner = Project.find_with_namespace(namespace, repo)
|
@project, @owner = Project.find_with_namespace(namespace, repo)
|
||||||
|
|
||||||
if @project
|
if @project
|
||||||
logger.info "###########:project not founded"
|
logger.info "###########:project founded"
|
||||||
@project
|
@project
|
||||||
else
|
else
|
||||||
logger.info "###########:project not found"
|
logger.info "###########:project not found"
|
||||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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)
|
|
@ -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']
|
|
@ -0,0 +1,3 @@
|
||||||
|
json.array! @result_object["branch_name"] do |branch|
|
||||||
|
json.partial! "api/v1/projects/branches/simple_detail", branch: branch
|
||||||
|
end
|
|
@ -0,0 +1 @@
|
||||||
|
json.partial! "api/v1/projects/branches/simple_gitea_detail", branch: @result_object
|
|
@ -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
|
|
@ -23,6 +23,11 @@ defaults format: :json do
|
||||||
get :hooktasks
|
get :hooktasks
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
resources :branches, only:[:index, :create] do
|
||||||
|
collection do
|
||||||
|
get :all
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue