From de1e931c2f442c9d6cb2b02427ae7aa07e52aa84 Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 23 Feb 2023 11:38:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=EF=BC=9A=E8=B4=A1=E7=8C=AE?= =?UTF-8?q?=E8=80=85=E8=8E=B7=E5=8F=96=E8=A7=84=E5=88=99=E4=BB=A5=E5=8F=8A?= =?UTF-8?q?=E5=88=86=E6=94=AF=E5=88=97=E8=A1=A8=E5=90=8C=E6=AD=A5=E6=97=A7?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/repositories_controller.rb | 5 ++-- .../api/v1/projects/branches/list_service.rb | 13 ++++++---- .../repository/contributors/get_service.rb | 24 +++++++++++++++---- .../repositories/_contributor.json.jbuilder | 18 +++++++------- .../repositories/contributors.json.jbuilder | 2 +- 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index e32b31017..e6fef78f0 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -166,8 +166,9 @@ class RepositoriesController < ApplicationController if params[:filepath].present? || @project.educoder? @contributors = [] else - result = Gitea::Repository::Contributors::GetService.call(@owner, @repository.identifier) - @contributors = result.is_a?(Hash) && result.key?(:status) ? [] : result + result = Gitea::Repository::Contributors::GetService.call(@owner, @repository.identifier, {page: params[:page], limit: params[:limit]}) + @total_count = result[:total_count] + @contributors = result.is_a?(Hash) ? result[:body] : [] end rescue @contributors = [] diff --git a/app/services/api/v1/projects/branches/list_service.rb b/app/services/api/v1/projects/branches/list_service.rb index e5c6fe442..f53dc1f49 100644 --- a/app/services/api/v1/projects/branches/list_service.rb +++ b/app/services/api/v1/projects/branches/list_service.rb @@ -1,7 +1,7 @@ class Api::V1::Projects::Branches::ListService < ApplicationService attr_accessor :project, :token, :owner, :repo, :name, :page, :limit - attr_accessor :gitea_data + attr_accessor :gitea_data, :gitea_repo_data def initialize(project, params, token=nil) @project = project @@ -16,6 +16,8 @@ class Api::V1::Projects::Branches::ListService < ApplicationService def call load_gitea_data + gitea_data["default_branch"] = gitea_repo_data["default_branch"] + gitea_data end @@ -32,9 +34,12 @@ class Api::V1::Projects::Branches::ListService < ApplicationService 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 + @gitea_data = $gitea_hat_client.get_repos_branches_by_owner_repo(owner, repo, {query: request_params}) rescue nil raise Error, '获取分支列表失败!' unless @gitea_data.is_a?(Hash) end + + def load_default_branch + @gitea_repo_data = $gitea_client.get_repos_by_owner_repo('yystopf', 'pig') rescue nil + raise Error, '获取仓库信息失败!' unless @gitea_data.is_a?(Hash) + end end \ No newline at end of file diff --git a/app/services/gitea/repository/contributors/get_service.rb b/app/services/gitea/repository/contributors/get_service.rb index e5ad32e38..fe9ed2463 100644 --- a/app/services/gitea/repository/contributors/get_service.rb +++ b/app/services/gitea/repository/contributors/get_service.rb @@ -1,22 +1,38 @@ class Gitea::Repository::Contributors::GetService < Gitea::ClientService - attr_reader :owner, :repo_name + attr_reader :owner, :repo_name, :page, :limit - def initialize(owner, repo_name) + def initialize(owner, repo_name, params={}) @owner = owner @repo_name = repo_name + @page = params[:page] || 1 + @limit = params[:limit] || 20 end def call response = get(url, params, true) - render_status(response) + render_result(response) end private def params - Hash.new.merge(token: owner.gitea_token) + Hash.new.merge(token: owner.gitea_token, page: page, limit: limit) end def url "/repos/#{owner.login}/#{repo_name}/contributors" end + + def render_result(response) + case response.status + when 200 + result = {} + headers = response.headers.to_hash + body = JSON.parse(response.body) + total_count = headers["x-total"] + result.merge(total_count: total_count.to_i, body: body) + else + nil + # {status: -1, message: "#{body['message']}"} + end + end end \ No newline at end of file diff --git a/app/views/repositories/_contributor.json.jbuilder b/app/views/repositories/_contributor.json.jbuilder index 56fa9ce86..ed036aae3 100644 --- a/app/views/repositories/_contributor.json.jbuilder +++ b/app/views/repositories/_contributor.json.jbuilder @@ -1,15 +1,15 @@ -user = $redis_cache.hgetall("v2-owner-common:#{contributor["login"]}-#{contributor["email"]}") +user = $redis_cache.hgetall("v2-owner-common:#{contributor["name"]}-#{contributor["email"]}") if user.blank? - json.contributions contributor["contributions"] - # json.gid contributor["id"] - json.login contributor["login"] + json.contributions contributor["commits"] + # json.id contributor["id"] + json.name contributor["name"] json.type nil - json.name contributor["login"] - json.image_url User::Avatar.get_letter_avatar_url(contributor["login"]) + json.name contributor["name"] + json.image_url User::Avatar.get_letter_avatar_url(contributor["name"]) else - json.contributions contributor["contributions"] - # json.gid contributor["id"] - json.login user["login"] + json.contributions contributor["commits"] + json.id user["id"] + json.name user["name"] json.type user["type"] json.name user["name"] json.image_url user["avatar_url"] diff --git a/app/views/repositories/contributors.json.jbuilder b/app/views/repositories/contributors.json.jbuilder index 2fb6abae8..bec285523 100644 --- a/app/views/repositories/contributors.json.jbuilder +++ b/app/views/repositories/contributors.json.jbuilder @@ -1,4 +1,4 @@ -total_count = @contributors.size +total_count = @total_count json.list @contributors.each do |contributor| json.partial! 'contributor', locals: { contributor: contributor } end