fix: lastcommit

This commit is contained in:
yystopf 2021-10-08 14:43:46 +08:00
parent 76183344bc
commit 9d5503f09f
2 changed files with 51 additions and 2 deletions

View File

@ -254,8 +254,14 @@ class RepositoriesController < ApplicationController
# TODO 获取最新commit信息
def project_commits
Gitea::Repository::Commits::ListService.new(@project.owner.login, @project.identifier,
sha: get_ref, page: 1, limit: 1, token: current_user&.gitea_token).call
if params[:filepath].present?
file_path_uri = URI.parse(URI.encode(params[:filepath].to_s.strip))
Gitea::Repository::Commits::FileListService.new(@project.owner.login, @project.identifier, file_path_uri,
sha: get_ref, page: 1, limit: 1, token: current_user&.gitea_token).call
else
Gitea::Repository::Commits::ListService.new(@project.owner.login, @project.identifier,
sha: get_ref, page: 1, limit: 1, token: current_user&.gitea_token).call
end
end
def get_statistics

View File

@ -0,0 +1,43 @@
# Get a list of all commits from a repository
class Gitea::Repository::Commits::FileListService < Gitea::ClientService
attr_reader :owner, :repo_name, :filepath, :args
# sha: SHA or branch to start listing commits from (usually 'master')
# ex:
# Gitea::Repository::Commits::ListService.new(@project.owner.login, @project.identifier,
# sha: params[:sha], page: params[:page], limit: params[:limit], token: current_user&.gitea_token).call
def initialize(owner, repo_name, filepath, **args)
@owner = owner
@repo_name = repo_name
@filepath = filepath
@args = args
end
def call
response = get(url, params)
render_result(response)
end
private
def params
{sha: args[:sha] || 'master', page: args[:page] || PAGINATE_DEFAULT_PAGE, limit: args[:limit] || PAGINATE_DEFAULT_LIMIT, token: args[:token] || "" }
end
def url
"/repos/#{owner}/#{repo_name}/file_commits/#{filepath}".freeze
end
def render_result(response)
case response.status
when 200
result = {}
headers = response.headers.to_hash
body = JSON.parse(response.body)
total_count = headers["x-total"]
result.merge(total_count: total_count.to_i, body: body)
else
nil
# {status: -1, message: "#{body['message']}"}
end
end
end