Update reposotiry's sub entries api

This commit is contained in:
Jasder 2020-06-03 11:41:14 +08:00
parent 96ed75f6b0
commit acc0bfb7aa
2 changed files with 26 additions and 10 deletions

View File

@ -6,6 +6,7 @@ class RepositoriesController < ApplicationController
before_action :authorizate!, except: [:sync_mirror, :tags, :commit] before_action :authorizate!, except: [:sync_mirror, :tags, :commit]
before_action :find_repository_by_id, only: %i[commit sync_mirror tags] before_action :find_repository_by_id, only: %i[commit sync_mirror tags]
before_action :authorizate_user_can_edit_repo!, only: %i[sync_mirror] before_action :authorizate_user_can_edit_repo!, only: %i[sync_mirror]
before_action :get_latest_commit, only: %i[entries sub_entries]
def show def show
@branches_count = Gitea::Repository::Branches::ListService.new(@project.owner, @project.identifier).call&.size @branches_count = Gitea::Repository::Branches::ListService.new(@project.owner, @project.identifier).call&.size
@ -25,20 +26,14 @@ class RepositoriesController < ApplicationController
def entries def entries
@project.increment!(:visits) @project.increment!(:visits)
@ref = params[:ref] || "master"
# TODO 获取最新commit信息 @entries = Gitea::Repository::Entries::ListService.new(@project.owner, @project.identifier, ref: get_ref).call
@latest_commit = Gitea::Repository::Commits::ListService.new(@project.owner.login, @project.identifier,
sha: @ref, page: 1, limit: 1, token: current_user&.gitea_token).call
@latest_commit = @latest_commit.blank? ? nil : @latest_commit[:body][0]
@entries = Gitea::Repository::Entries::ListService.new(@project.owner, @project.identifier, ref: @ref).call
@entries = @entries.sort_by{ |hash| hash['type'] } @entries = @entries.sort_by{ |hash| hash['type'] }
end end
def sub_entries def sub_entries
file_path_uri = URI.parse(URI.encode(params[:filepath].to_s.strip)) file_path_uri = URI.parse(URI.encode(params[:filepath].to_s.strip))
interactor = Repositories::EntriesInteractor.call(@project.owner, @project.identifier, file_path_uri, ref: params[:ref]) interactor = Repositories::EntriesInteractor.call(@project.owner, @project.identifier, file_path_uri, ref: get_ref)
if interactor.success? if interactor.success?
@sub_entries = interactor.result @sub_entries = interactor.result
@sub_entries = [] << @sub_entries unless @sub_entries.is_a? Array @sub_entries = [] << @sub_entries unless @sub_entries.is_a? Array
@ -134,6 +129,17 @@ class RepositoriesController < ApplicationController
end end
end end
# TODO 获取最新commit信息
def get_latest_commit
@latest_commit = Gitea::Repository::Commits::ListService.new(@project.owner.login, @project.identifier,
sha: get_ref, page: 1, limit: 1, token: current_user&.gitea_token).call
@latest_commit = @latest_commit.blank? ? nil : @latest_commit[:body][0]
end
def get_ref
params[:ref] || "master"
end
def content_params def content_params
{ {
filepath: params[:filepath], filepath: params[:filepath],

View File

@ -1,3 +1,13 @@
json.array! @sub_entries do |entry|
json.partial! 'repositories/simple_entry', locals: { entry: entry } json.last_commit do
if @latest_commit
json.partial! 'commit', commit: @latest_commit, project: @project
else
json.nil!
end
end
json.entries do
json.array! @sub_entries do |entry|
json.partial! 'repositories/simple_entry', locals: { entry: entry }
end
end end