From acc0bfb7aab3fd73c69cd236694ddf417a17a455 Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Wed, 3 Jun 2020 11:41:14 +0800 Subject: [PATCH] Update reposotiry's sub entries api --- app/controllers/repositories_controller.rb | 22 ++++++++++++------- .../repositories/sub_entries.json.jbuilder | 14 ++++++++++-- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 99029d566..ac8201c93 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -6,6 +6,7 @@ class RepositoriesController < ApplicationController before_action :authorizate!, except: [:sync_mirror, :tags, :commit] 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 :get_latest_commit, only: %i[entries sub_entries] def show @branches_count = Gitea::Repository::Branches::ListService.new(@project.owner, @project.identifier).call&.size @@ -25,20 +26,14 @@ class RepositoriesController < ApplicationController def entries @project.increment!(:visits) - @ref = params[:ref] || "master" - # TODO 获取最新commit信息 - @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 = Gitea::Repository::Entries::ListService.new(@project.owner, @project.identifier, ref: get_ref).call @entries = @entries.sort_by{ |hash| hash['type'] } end def sub_entries 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? @sub_entries = interactor.result @sub_entries = [] << @sub_entries unless @sub_entries.is_a? Array @@ -134,6 +129,17 @@ class RepositoriesController < ApplicationController 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 { filepath: params[:filepath], diff --git a/app/views/repositories/sub_entries.json.jbuilder b/app/views/repositories/sub_entries.json.jbuilder index 1f8de1eb7..4ac253465 100644 --- a/app/views/repositories/sub_entries.json.jbuilder +++ b/app/views/repositories/sub_entries.json.jbuilder @@ -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