From 3f78899c58d8b742e0f3af061bd763f7950608d4 Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 3 Feb 2023 10:53:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9Abranch=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 +- .../api/v1/projects/branches_controller.rb | 6 ++- .../api/v1/projects/branches/list_service.rb | 40 +++++++++++++++++++ .../_simple_gitea_detail.json.jbuilder | 2 +- .../v1/projects/branches/index.json.jbuilder | 4 ++ 5 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 app/services/api/v1/projects/branches/list_service.rb create mode 100644 app/views/api/v1/projects/branches/index.json.jbuilder diff --git a/Gemfile b/Gemfile index e37371108..2a431701b 100644 --- a/Gemfile +++ b/Gemfile @@ -139,4 +139,4 @@ gem 'doorkeeper' gem 'doorkeeper-jwt' -gem 'gitea-client', '~> 0.11.1' \ No newline at end of file +gem 'gitea-client', '~> 0.11.4' \ No newline at end of file diff --git a/app/controllers/api/v1/projects/branches_controller.rb b/app/controllers/api/v1/projects/branches_controller.rb index bc4919616..33346573c 100644 --- a/app/controllers/api/v1/projects/branches_controller.rb +++ b/app/controllers/api/v1/projects/branches_controller.rb @@ -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) diff --git a/app/services/api/v1/projects/branches/list_service.rb b/app/services/api/v1/projects/branches/list_service.rb new file mode 100644 index 000000000..e5c6fe442 --- /dev/null +++ b/app/services/api/v1/projects/branches/list_service.rb @@ -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 \ No newline at end of file diff --git a/app/views/api/v1/projects/branches/_simple_gitea_detail.json.jbuilder b/app/views/api/v1/projects/branches/_simple_gitea_detail.json.jbuilder index 70fac8238..6a35b782e 100644 --- a/app/views/api/v1/projects/branches/_simple_gitea_detail.json.jbuilder +++ b/app/views/api/v1/projects/branches/_simple_gitea_detail.json.jbuilder @@ -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) diff --git a/app/views/api/v1/projects/branches/index.json.jbuilder b/app/views/api/v1/projects/branches/index.json.jbuilder new file mode 100644 index 000000000..cfb9bb647 --- /dev/null +++ b/app/views/api/v1/projects/branches/index.json.jbuilder @@ -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 \ No newline at end of file