| @@ -3106,6 +3106,35 @@ curl -X GET http://localhost:3000/api/trusite/trusite/readme | jq | |||||
| ``` | ``` | ||||
| --- | --- | ||||
| #### 获取仓库仓库的语言百分占比 | |||||
| ``` | |||||
| GET api/:owner/:repo/languages | |||||
| ``` | |||||
| *示例* | |||||
| ```bash | |||||
| curl -X GET http://localhost:3000/api/jasder/trusite/languages | jq | |||||
| ``` | |||||
| *请求参数说明:* | |||||
| |参数名|类型|说明| | |||||
| |-|-|-| | |||||
| |owner |是|string |项目拥有者登录名 | | |||||
| |repo |否|boolean |仓库名称 | | |||||
| 返回值 | |||||
| ```json | |||||
| { | |||||
| "JavaScript": "90.2%", | |||||
| "CSS": "6.1%", | |||||
| "Java": "2.9%", | |||||
| "HTML": "0.8%" | |||||
| } | |||||
| ``` | |||||
| --- | |||||
| ### DevOps相关api | ### DevOps相关api | ||||
| @@ -0,0 +1,19 @@ | |||||
| module Repository::LanguagesPercentagable | |||||
| extend ActiveSupport::Concern | |||||
| def languages_precentagable | |||||
| result = Gitea::Repository::Languages::ListService.call(@owner.login, | |||||
| @repository.identifier, current_user&.gitea_token) | |||||
| result[:status] === :success ? hash_transform_precentagable(result[:body]) : nil | |||||
| end | |||||
| # hash eq:{"JavaScript": 301681522,"Ruby": 1444004,"Roff": 578781} | |||||
| def hash_transform_precentagable(hash) | |||||
| total_byte_size = hash.values.sum | |||||
| hash.transform_values { |v| | |||||
| ActionController::Base.helpers | |||||
| .number_to_percentage((v * 100.0 / total_byte_size), precision: 1) | |||||
| } | |||||
| end | |||||
| end | |||||
| @@ -1,6 +1,7 @@ | |||||
| class RepositoriesController < ApplicationController | class RepositoriesController < ApplicationController | ||||
| include ApplicationHelper | include ApplicationHelper | ||||
| include OperateProjectAbilityAble | include OperateProjectAbilityAble | ||||
| include Repository::LanguagesPercentagable | |||||
| before_action :require_login, only: %i[edit update create_file update_file delete_file sync_mirror] | before_action :require_login, only: %i[edit update create_file update_file delete_file sync_mirror] | ||||
| before_action :load_repository | before_action :load_repository | ||||
| @@ -140,6 +141,10 @@ class RepositoriesController < ApplicationController | |||||
| render json: @readme | render json: @readme | ||||
| end | end | ||||
| def languages | |||||
| render json: languages_precentagable | |||||
| end | |||||
| private | private | ||||
| def find_project | def find_project | ||||
| @@ -0,0 +1,39 @@ | |||||
| # Get languages and number of bytes of code written | |||||
| class Gitea::Repository::Languages::ListService < Gitea::ClientService | |||||
| attr_reader :owner, :repo, :token | |||||
| # owner: owner of the repo | |||||
| # repo: the name of repository | |||||
| # token: token of gitea user | |||||
| # eq: Gitea::Repository::Languages::ListService.call(@owner.identifier, | |||||
| # @project.identifier, current_user&.gitea_token) | |||||
| def initialize(owner, repo, token) | |||||
| @owner = owner | |||||
| @repo = repo | |||||
| @args = token | |||||
| end | |||||
| def call | |||||
| response = get(url, params) | |||||
| status, message, body = render_response(response) | |||||
| json_format(status, message, body) | |||||
| end | |||||
| private | |||||
| def params | |||||
| {}.merge(token: token) | |||||
| end | |||||
| def url | |||||
| "/repos/#{owner}/#{repo}/languages".freeze | |||||
| end | |||||
| def json_format(status, message, body) | |||||
| case status | |||||
| when 200 then success(body) | |||||
| else | |||||
| error(message, status) | |||||
| end | |||||
| end | |||||
| end | |||||
| @@ -328,6 +328,7 @@ Rails.application.routes.draw do | |||||
| get :top_counts | get :top_counts | ||||
| get 'commits/:sha', to: 'repositories#commit', as: 'commit' | get 'commits/:sha', to: 'repositories#commit', as: 'commit' | ||||
| get 'readme' | get 'readme' | ||||
| get 'languages' | |||||
| end | end | ||||
| end | end | ||||