From 578acd7845afd15a97010f64d51898810bc4f866 Mon Sep 17 00:00:00 2001 From: yystopf Date: Mon, 17 Apr 2023 11:35:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E8=B4=A1=E7=8C=AE?= =?UTF-8?q?=E8=80=85=E4=BB=A3=E7=A0=81=E8=A1=8C=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 +- .../v1/projects/contributors_controller.rb | 11 ++++++ .../v1/projects/contributors/stat_service.rb | 38 +++++++++++++++++++ .../projects/contributors/stat.json.jbuilder | 26 +++++++++++++ config/routes/api.rb | 5 +++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 app/controllers/api/v1/projects/contributors_controller.rb create mode 100644 app/services/api/v1/projects/contributors/stat_service.rb create mode 100644 app/views/api/v1/projects/contributors/stat.json.jbuilder diff --git a/Gemfile b/Gemfile index 947543c5f..8051448e4 100644 --- a/Gemfile +++ b/Gemfile @@ -143,4 +143,4 @@ gem 'doorkeeper' gem 'doorkeeper-jwt' -gem 'gitea-client', '~> 1.4.1' +gem 'gitea-client', '~> 1.4.2' diff --git a/app/controllers/api/v1/projects/contributors_controller.rb b/app/controllers/api/v1/projects/contributors_controller.rb new file mode 100644 index 000000000..abb5827c5 --- /dev/null +++ b/app/controllers/api/v1/projects/contributors_controller.rb @@ -0,0 +1,11 @@ +class Api::V1::Projects::ContributorsController < Api::V1::BaseController + before_action :require_public_and_member_above, only: [:index, :stat] + + # todo + def index + end + + def stat + @result_object = Api::V1::Projects::Contributors::StatService.call(@project, {branch: params[:branch], pass_year: params[:pass_year], page: page, limit: limit}, current_user&.gitea_token) + end +end \ No newline at end of file diff --git a/app/services/api/v1/projects/contributors/stat_service.rb b/app/services/api/v1/projects/contributors/stat_service.rb new file mode 100644 index 000000000..d264a109b --- /dev/null +++ b/app/services/api/v1/projects/contributors/stat_service.rb @@ -0,0 +1,38 @@ +class Api::V1::Projects::Contributors::StatService < ApplicationService + + attr_reader :project, :branch, :pass_year, :owner, :repo, :token, :page, :limit + attr_accessor :gitea_data + + def initialize(project, params, token=nil) + @project = project + @branch = params[:branch] + @pass_year = params[:pass_year] + @page = params[:page] || 1 + @limit = params[:limit] || 15 + @owner = project&.owner.login + @repo = project&.identifier + @token = token + end + + def call + load_gitea_data + + gitea_data + end + + private + def request_params + param = { + access_token: token + } + param.merge!(branch: branch) if branch.present? + param.merge!(pass_year: pass_year) if pass_year.present? + + param + end + + def load_gitea_data + @gitea_data = $gitea_hat_client.get_repos_contributors_stat_by_owner_repo(owner, repo, {query: request_params}) rescue nil + raise Error, '获取贡献者(代码行)失败!' unless @gitea_data.is_a?(Hash) + end +end \ No newline at end of file diff --git a/app/views/api/v1/projects/contributors/stat.json.jbuilder b/app/views/api/v1/projects/contributors/stat.json.jbuilder new file mode 100644 index 000000000..a01ad81a5 --- /dev/null +++ b/app/views/api/v1/projects/contributors/stat.json.jbuilder @@ -0,0 +1,26 @@ +json.total_count @result_object[:total_data].to_i +json.contributors @result_object[:data].each do |contributor| + user = $redis_cache.hgetall("v2-owner-common:#{contributor["name"]}-#{contributor["email"]}") + if user.blank? + json.contributions contributor["commits"] + json.additions contributor["additions"] + json.deletions contributor["deletions"] + # json.id contributor["id"] + json.login contributor["name"] + json.email contributor["email"] + json.type nil + json.name contributor["name"] + json.image_url User::Avatar.get_letter_avatar_url(contributor["name"]) + else + json.contributions contributor["commits"] + json.additions contributor["additions"] + json.deletions contributor["deletions"] + json.id user["id"] + json.login user["login"] + json.email user["email"] + json.name user["name"] + json.type user["type"] + json.name user["name"] + json.image_url user["avatar_url"] + end +end \ No newline at end of file diff --git a/config/routes/api.rb b/config/routes/api.rb index 0a02065e2..62d49caf2 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -82,6 +82,11 @@ defaults format: :json do resources :commits, only: [:index] resources :code_stats, only: [:index] + resources :contributors, only: [:index] do + collection do + get :stat + end + end get '/commits/:sha/diff', to: 'commits#diff' get '/git/blobs/:sha', to: 'git#blobs' get '/git/trees/:sha', to: 'git#trees'