add: project commit_slice

This commit is contained in:
yystopf 2021-09-08 09:45:15 +08:00
parent 91feb8cf89
commit af7488505d
4 changed files with 86 additions and 0 deletions

View File

@ -107,6 +107,11 @@ class RepositoriesController < ApplicationController
sha: params[:sha], page: params[:page], limit: params[:limit], token: current_user&.gitea_token).call
end
def commits_slice
@hash_commit = Gitea::Repository::Commits::ListSliceService.call(@owner.login, @project.identifier,
sha: params[:sha], page: params[:page], limit: params[:limit], token: current_user&.gitea_token)
end
def commit
@sha = params[:sha]
@commit = Gitea::Repository::Commits::GetService.call(@owner.login, @repository.identifier, @sha, current_user&.gitea_token)

View File

@ -0,0 +1,42 @@
# Get a list of all commits from a repository
class Gitea::Repository::Commits::ListSliceService < Gitea::ClientService
attr_reader :owner, :repo_name, :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, **args)
@owner = owner
@repo_name = repo_name
@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}/commits_slice".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

View File

@ -0,0 +1,38 @@
if @hash_commit.blank? #如果有状态值,则表示报错了
json.total_count 0
json.commits []
else
json.total_count @hash_commit[:total_count]
json.commit_dates do
json.array! @hash_commit[:body] do |commit_date|
json.commit_date commit_date["commit_date"]
json.commits do
json.array! commit_date["Commits"] do |commit|
commiter = commit['committer']
forge_user =
if commiter.present?
User.simple_select.find_by(gitea_uid: commiter['id'])
end
json.sha commit['sha']
json.message commit['commit']['message']
json.timestamp render_unix_time(commit['commit']['author']['date'])
json.time_from_now time_from_now(commit['commit']['author']['date'])
if forge_user
json.partial! 'author', user: forge_user
else
json.author do
json.id nil
json.login commit['commit']['author']['name']
json.type nil
json.name commit['commit']['author']['name']
json.image_url User::Avatar.get_letter_avatar_url(commit['commit']['author']['name'])
end
end
end
end
end
end
end

View File

@ -432,6 +432,7 @@ Rails.application.routes.draw do
get :entries
match :sub_entries, :via => [:get, :put]
get :commits
get :commits_slice
get :tags
get :contributors
post :create_file