From 96ed75f6b0a144d25b6407a39dedad3641d02b64 Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Wed, 3 Jun 2020 11:21:43 +0800 Subject: [PATCH] Update get repo entries dir api --- app/controllers/repositories_controller.rb | 9 ++++-- app/views/repositories/_commit.json.jbuilder | 16 +++++++++ app/views/repositories/entries.json.jbuilder | 34 +++++++------------- 3 files changed, 34 insertions(+), 25 deletions(-) create mode 100644 app/views/repositories/_commit.json.jbuilder diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 63ae5cdc4..99029d566 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -8,7 +8,7 @@ class RepositoriesController < ApplicationController before_action :authorizate_user_can_edit_repo!, only: %i[sync_mirror] def show - @branches_count = Gitea::Repository::BranchesService.new(@project.owner, @project.identifier).call&.size + @branches_count = Gitea::Repository::Branches::ListService.new(@project.owner, @project.identifier).call&.size @commits_count = 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[:total_count] @tags_count = Gitea::Repository::Tags::ListService.new(current_user&.gitea_token, @project.owner.login, @project.identifier).call&.size @@ -25,8 +25,13 @@ 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] - @ref = params[:branch] || "master" @entries = Gitea::Repository::Entries::ListService.new(@project.owner, @project.identifier, ref: @ref).call @entries = @entries.sort_by{ |hash| hash['type'] } end diff --git a/app/views/repositories/_commit.json.jbuilder b/app/views/repositories/_commit.json.jbuilder new file mode 100644 index 000000000..5ad8a1bce --- /dev/null +++ b/app/views/repositories/_commit.json.jbuilder @@ -0,0 +1,16 @@ +json.commit do + json.sha commit['sha'] + json.url EduSetting.get('host_name') + commit_repository_path(project.repository, commit['sha']) + json.message commit['commit']['message'] + json.author commit['commit']['author'] + json.committer commit['commit']['committer'] + json.timestamp render_unix_time(commit['commit']['committer']['date']) + json.time_from_now time_from_now(commit['commit']['committer']['date']) +end + +json.author do + json.partial! 'commit_author', user: render_commit_author(commit['author']) +end +json.committer do + json.partial! 'commit_author', user: render_commit_author(commit['committer']) +end diff --git a/app/views/repositories/entries.json.jbuilder b/app/views/repositories/entries.json.jbuilder index 45ea71218..43275f3fa 100644 --- a/app/views/repositories/entries.json.jbuilder +++ b/app/views/repositories/entries.json.jbuilder @@ -1,23 +1,12 @@ -json.array! @entries do |entry| - # json.name entry['name'] - # json.path entry['path'] - # json.sha entry['sha'] - # json.type entry['type'] - # json.size entry['size'] - # json.content entry['content'] - # json.target entry['target'] - # json.commit entry['commit'] - - if entry['name'] == "README.md" - readme_md = Gitea::Repository::Entries::GetService.new(@project.owner, @project.identifier, entry['path'], ref:@ref).call - json.name readme_md['name'] - json.path readme_md['path'] - json.sha readme_md['sha'] - json.type readme_md['type'] - json.size readme_md['size'] - json.content readme_md['content'].present? ? render_decode64_content(readme_md['content']).force_encoding('UTF-8') : "" - json.target readme_md['target'] +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! @entries do |entry| json.name entry['name'] json.path entry['path'] json.sha entry['sha'] @@ -25,9 +14,8 @@ json.array! @entries do |entry| json.size entry['size'] json.content entry['content'] json.target entry['target'] - end - - if entry['latest_commit'] - json.partial! 'last_commit', entry: entry + if entry['latest_commit'] + json.partial! 'last_commit', entry: entry + end end end