新增:branch列表接口

This commit is contained in:
yystopf 2023-02-03 10:53:24 +08:00
parent bfe14352c0
commit 3f78899c58
5 changed files with 51 additions and 3 deletions

View File

@ -139,4 +139,4 @@ gem 'doorkeeper'
gem 'doorkeeper-jwt'
gem 'gitea-client', '~> 0.11.1'
gem 'gitea-client', '~> 0.11.4'

View File

@ -1,5 +1,9 @@
class Api::V1::Projects::BranchesController < Api::V1::BaseController
before_action :require_public_and_member_above, only: [:all]
before_action :require_public_and_member_above, only: [:index, :all]
def index
@result_object = Api::V1::Projects::Branches::ListService.call(@project, {name: params[:name], page: page, limit: limit}, current_user&.gitea_token)
end
def all
@result_object = Api::V1::Projects::Branches::AllListService.call(@project, current_user&.gitea_token)

View File

@ -0,0 +1,40 @@
class Api::V1::Projects::Branches::ListService < ApplicationService
attr_accessor :project, :token, :owner, :repo, :name, :page, :limit
attr_accessor :gitea_data
def initialize(project, params, token=nil)
@project = project
@owner = project&.owner.login
@repo = project&.identifier
@token = token
@name = params[:name]
@page = params[:page]
@limit = params[:limit]
end
def call
load_gitea_data
gitea_data
end
private
def request_params
params = {
access_token: token,
page: page,
limit: limit
}
params.merge!({name: name}) if name.present?
params
end
def load_gitea_data
puts request_params
@gitea_data = $gitea_client.get_repos_branches_by_owner_repo(owner, repo, {query: request_params}) rescue nil
puts @gitea_data
raise Error, '获取分支列表失败!' unless @gitea_data.is_a?(Hash)
end
end

View File

@ -18,7 +18,7 @@ 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 time_from_now(branch['commit_time'].to_time)
json.commit_time_from_now time_from_now(branch['commit']['timestamp'].to_time)
json.commit_time branch['commit_time']
json.default_branch branch['default_branch']
json.http_url render_http_url(@project)

View File

@ -0,0 +1,4 @@
json.total_count @result_object[:total_data].to_i
json.branches @result_object[:data].each do |branch|
json.partial! "api/v1/projects/branches/simple_gitea_detail", branch: branch
end