diff --git a/api_document.md b/api_document.md index 9c927760c..27c71b901 100644 --- a/api_document.md +++ b/api_document.md @@ -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 diff --git a/app/controllers/concerns/repository/languages_percentagable.rb b/app/controllers/concerns/repository/languages_percentagable.rb new file mode 100644 index 000000000..620d93bf9 --- /dev/null +++ b/app/controllers/concerns/repository/languages_percentagable.rb @@ -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 diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 82f9e6c71..7e708f5e1 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -1,6 +1,7 @@ class RepositoriesController < ApplicationController include ApplicationHelper include OperateProjectAbilityAble + include Repository::LanguagesPercentagable before_action :require_login, only: %i[edit update create_file update_file delete_file sync_mirror] before_action :load_repository @@ -151,6 +152,10 @@ class RepositoriesController < ApplicationController render json: @readme end + def languages + render json: languages_precentagable + end + private def find_project diff --git a/app/services/gitea/repository/languages/list_service.rb b/app/services/gitea/repository/languages/list_service.rb new file mode 100644 index 000000000..88d5dcbed --- /dev/null +++ b/app/services/gitea/repository/languages/list_service.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 76a37dab0..8af453cf8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -354,6 +354,7 @@ Rails.application.routes.draw do get :top_counts get 'commits/:sha', to: 'repositories#commit', as: 'commit' get 'readme' + get 'languages' end end