ADD repository languages percentage api

This commit is contained in:
Jasder
2021-01-19 16:21:31 +08:00
parent 5fd5bf32d8
commit efa99e0f06
5 changed files with 93 additions and 0 deletions

View File

@@ -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