mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-03 03:40:49 +08:00
新增:贡献者代码行接口
This commit is contained in:
11
app/controllers/api/v1/projects/contributors_controller.rb
Normal file
11
app/controllers/api/v1/projects/contributors_controller.rb
Normal file
@@ -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
|
||||
38
app/services/api/v1/projects/contributors/stat_service.rb
Normal file
38
app/services/api/v1/projects/contributors/stat_service.rb
Normal file
@@ -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
|
||||
26
app/views/api/v1/projects/contributors/stat.json.jbuilder
Normal file
26
app/views/api/v1/projects/contributors/stat.json.jbuilder
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user