From 58f8d606461ba80908f217cfbe02dba0b6d0c459 Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Thu, 22 Oct 2020 18:00:31 +0800 Subject: [PATCH] =?UTF-8?q?FIX=20=E7=89=88=E6=9C=AC=E5=BA=93=E5=85=BC?= =?UTF-8?q?=E5=AE=B9euducoder=E5=B9=B3=E5=8F=B0=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/projects_controller.rb | 5 +- app/controllers/repositories_controller.rb | 48 +++++---- app/helpers/projects_helper.rb | 21 +++- app/models/concerns/matchable.rb | 1 + app/queries/projects/list_query.rb | 11 ++- .../projects/_project_detail.json.jbuilder | 8 +- app/views/repositories/_author.json.jbuilder | 12 ++- app/views/repositories/_commit.json.jbuilder | 44 ++++++--- .../repositories/_simple_entry.json.jbuilder | 56 +++++++---- app/views/repositories/entries.json.jbuilder | 97 +++++++++++++------ app/views/repositories/show.json.jbuilder | 6 +- .../repositories/sub_entries.json.jbuilder | 42 +++++--- 12 files changed, 240 insertions(+), 111 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 207dd8ee4..f2bb28ff1 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -10,8 +10,9 @@ class ProjectsController < ApplicationController def index scope = Projects::ListQuery.call(params) - @projects = kaminari_paginate(scope) - @total_count = @projects.total_count + # @projects = kaminari_paginate(scope) + @projects = paginate scope.includes(:project_category, :project_language, :repository, :project_educoder, owner: :user_extension) + @total_count = scope.size end def create diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 6a57ef299..2b35313dc 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -13,7 +13,7 @@ class RepositoriesController < ApplicationController def show @user = current_user @repo = @project.repository - @result = Gitea::Repository::GetService.new(@project.owner, @project.identifier).call + @result = @project.forge? ? Gitea::Repository::GetService.new(@project.owner, @project.identifier).call : nil @project_fork_id = @project.try(:forked_from_project_id) if @project_fork_id.present? @fork_project = Project.find_by(id: @project_fork_id) @@ -27,26 +27,40 @@ class RepositoriesController < ApplicationController def entries @project.increment!(:visits) @project_owner = @project.owner - @entries = Gitea::Repository::Entries::ListService.new(@project_owner, @project.identifier, ref: @ref).call - @entries = @entries.present? ? @entries.sort_by{ |hash| hash['type'] } : [] - @path = Gitea.gitea_config[:domain]+"/#{@project.owner.login}/#{@project.identifier}/raw/branch/#{@ref}/" + + if @project.educoder? + @entries = Educoder::Repository::Entries::ListService.call(@project&.project_educoder.repo_name) + else + @entries = Gitea::Repository::Entries::ListService.new(@project_owner, @project.identifier, ref: @ref).call + @entries = @entries.present? ? @entries.sort_by{ |hash| hash['type'] } : [] + @path = Gitea.gitea_config[:domain]+"/#{@project.owner.login}/#{@project.identifier}/raw/branch/#{@ref}/" + end end def top_counts - @result = Gitea::Repository::GetService.new(@project.owner, @project.identifier).call + @result = @project.educoder? ? nil : Gitea::Repository::GetService.new(@project.owner, @project.identifier).call 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: @ref) - if interactor.success? - result = interactor.result - return @sub_entries = [] if result.is_a?(Hash) && result[:status] == -1 - @sub_entries = result.is_a?(Array) ? result : [result] - @sub_entries = @sub_entries.sort_by{ |hash| hash['type'] } + if @project.educoder? + if params[:type] === 'file' + @sub_entries = Educoder::Repository::Entries::GetService.call(@project&.project_educoder&.repo_name, file_path_uri) + return render_error('该文件暂未开放,敬请期待.') if @sub_entries['status'].to_i === -1 + end + @sub_entries = Educoder::Repository::Entries::ListService.call(@project&.project_educoder&.repo_name, {path: file_path_uri}) else - render_error(interactor.error) + interactor = Repositories::EntriesInteractor.call(@project.owner, @project.identifier, file_path_uri, ref: @ref) + if interactor.success? + result = interactor.result + return @sub_entries = [] if result.is_a?(Hash) && result[:status] == -1 + + @sub_entries = result.is_a?(Array) ? result : [result] + @sub_entries = @sub_entries.sort_by{ |hash| hash['type'] } + else + render_error(interactor.error) + end end end @@ -135,8 +149,8 @@ class RepositoriesController < ApplicationController end def get_statistics - @branches_count = Gitea::Repository::Branches::ListService.new(@project.owner, @project.identifier).call&.size - @tags_count = Gitea::Repository::Tags::ListService.new(current_user&.gitea_token, @project.owner.login, @project.identifier).call&.size + @branches_count = @project.educoder? ? 0 : Gitea::Repository::Branches::ListService.new(@project.owner, @project.identifier).call&.size + @tags_count = @project.educoder? ? 0 : Gitea::Repository::Tags::ListService.new(current_user&.gitea_token, @project.owner.login, @project.identifier).call&.size end def get_ref @@ -144,9 +158,9 @@ class RepositoriesController < ApplicationController end def get_latest_commit - latest_commit = project_commits - @latest_commit = latest_commit[:body][0] if latest_commit.present? - @commits_count = latest_commit[:total_count] if latest_commit.present? + latest_commit = @project.educoder? ? nil : project_commits + @latest_commit = latest_commit.present? ? latest_commit[:body][0] : nil + @commits_count = latest_commit.present? ? latest_commit[:total_count] : 0 end def content_params diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index ad8447bd4..625bdfecd 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -29,7 +29,9 @@ module ProjectsHelper end def json_response(project, user) - repo = project.repository + # repo = project.repository + repo = Repository.select(:id).find_by(project: project) + tmp_json = {} unless project.common? tmp_json = tmp_json.merge({ @@ -55,8 +57,9 @@ module ProjectsHelper end tmp_json = tmp_json.merge({ - identifier: project.identifier, + identifier: render_identifier(project), name: project.name, + platform: project.platform, id: project.id, repo_id: repo.id, open_devops: (user.blank? || user.is_a?(AnonymousUser)) ? false : project.open_devops?, @@ -67,7 +70,19 @@ module ProjectsHelper render json: tmp_json end - def render_author(project) + def render_identifier(project) + project.educoder? ? project.project_educoder&.repo_name&.split('/')[1] : project.identifier + end + def render_author(project) + project.educoder? ? project.project_educoder&.repo_name&.split('/')[0] : project.owner.login + end + + def render_educoder_avatar_url(project_educoder) + [Rails.application.config_for(:configuration)['educoder']['main_site'], project_educoder&.image_url].join('/') + end + + def render_avatar_url(owner) + [Rails.application.config_for(:configuration)['platform_url'], 'images', url_to_avatar(owner)].join('/') end end diff --git a/app/models/concerns/matchable.rb b/app/models/concerns/matchable.rb index 943fb5ec4..5c013f951 100644 --- a/app/models/concerns/matchable.rb +++ b/app/models/concerns/matchable.rb @@ -5,6 +5,7 @@ module Matchable scope :with_project_category, ->(category_id) { where(project_category_id: category_id) unless category_id.blank? } scope :with_project_language, ->(language_id) { where(project_language_id: language_id) unless language_id.blank? } scope :with_project_type, ->(project_type) { where(project_type: project_type) if Project.project_types.include?(project_type) } + scope :by_name_or_identifier, ->(search) { where("name like :search or identifier LIKE :search", :search => "%#{search.split(" ").join('|')}%") unless search.blank? } end end diff --git a/app/queries/projects/list_query.rb b/app/queries/projects/list_query.rb index 91a2a7627..04f1d168b 100644 --- a/app/queries/projects/list_query.rb +++ b/app/queries/projects/list_query.rb @@ -10,10 +10,9 @@ class Projects::ListQuery < ApplicationQuery end def call - q = Project.visible.search_project(params[:search]) + q = Project.visible.by_name_or_identifier(params[:search]) - scope = q.result(distinct: true) - .includes(:project_category, :project_language, :repository, owner: :user_extension) + scope = q .with_project_type(params[:project_type]) .with_project_category(params[:category_id]) .with_project_language(params[:language_id]) @@ -21,7 +20,9 @@ class Projects::ListQuery < ApplicationQuery sort = params[:sort_by] || "updated_on" sort_direction = params[:sort_direction] || "desc" - scope = scope.no_anomory_projects.reorder("projects.#{sort} #{sort_direction}") - scope + custom_sort(scope, sort, sort_direction) + + # scope = scope.reorder("projects.#{sort} #{sort_direction}") + # scope end end diff --git a/app/views/projects/_project_detail.json.jbuilder b/app/views/projects/_project_detail.json.jbuilder index 0f655a48d..ed12602ae 100644 --- a/app/views/projects/_project_detail.json.jbuilder +++ b/app/views/projects/_project_detail.json.jbuilder @@ -1,6 +1,6 @@ json.id project.id json.repo_id project&.repository&.id -json.identifier project.identifier +json.identifier render_identifier(project) json.name project.name json.description Nokogiri::HTML(project.description).text json.visits project.visits @@ -17,13 +17,13 @@ json.author do if project.educoder? project_educoder = project.project_educoder json.name project_educoder&.owner - json.login project_educoder#.owner - json.image_url project_educoder.image_url + json.login project_educoder&.repo_name.split('/')[0] + json.image_url render_educoder_avatar_url(project.project_educoder) else user = project.owner json.name user.try(:show_real_name) json.login user.login - json.image_url url_to_avatar(project.owner) + json.image_url render_avatar_url(user) end end diff --git a/app/views/repositories/_author.json.jbuilder b/app/views/repositories/_author.json.jbuilder index 0c2a177c0..326920223 100644 --- a/app/views/repositories/_author.json.jbuilder +++ b/app/views/repositories/_author.json.jbuilder @@ -1,5 +1,11 @@ json.author do - json.login user.login - json.name user.real_name - json.image_url url_to_avatar(user) + if @project.forge? + json.login user.login + json.name user.real_name + json.image_url url_to_avatar(user) + else + json.login @project.project_educoder&.repo_name&.split('/')[0] + json.name @project.project_educoder&.owner + json.image_url @project.project_educoder&.image_url + end end diff --git a/app/views/repositories/_commit.json.jbuilder b/app/views/repositories/_commit.json.jbuilder index 888508839..079473b3d 100644 --- a/app/views/repositories/_commit.json.jbuilder +++ b/app/views/repositories/_commit.json.jbuilder @@ -1,16 +1,34 @@ -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']) +if @project.educoder? + json.commit do + json.sha commit[0]['id'] + json.message commit[0]['title'] + json.author {} + json.committer {} + json.timestamp 0 + json.time_from_now commit[0]['time'] + end + json.author do + {} + # json.partial! '/projects/author', user: render_commit_author(commit['author']) + end + json.committer {} 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']) +if @project.forge? + 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 end diff --git a/app/views/repositories/_simple_entry.json.jbuilder b/app/views/repositories/_simple_entry.json.jbuilder index be603d91b..50001d4df 100644 --- a/app/views/repositories/_simple_entry.json.jbuilder +++ b/app/views/repositories/_simple_entry.json.jbuilder @@ -1,18 +1,40 @@ -file_name = entry['name'] -file_type = file_name.to_s.split(".").last -direct_download = download_type(file_type) -image_type = image_type?(file_type) -json.name file_name -json.sha entry['sha'] -json.path entry['path'] -json.type entry['type'] -json.size entry['size'] -json.content entry['content'].present? && !direct_download ? render_decode64_content(entry['content']) : "" -json.target entry['target'] -json.download_url entry['download_url'] -json.direct_download direct_download -json.image_type image_type -json.is_readme_file is_readme_type?(file_name) -if entry['latest_commit'] - json.partial! 'last_commit', entry: entry +if @project.forge? + file_name = entry['name'] + file_type = file_name.to_s.split(".").last + direct_download = download_type(file_type) + image_type = image_type?(file_type) + json.name file_name + json.sha entry['sha'] + json.path entry['path'] + json.type entry['type'] + json.size entry['size'] + json.content entry['content'].present? && !direct_download ? render_decode64_content(entry['content']) : "" + json.target entry['target'] + json.download_url entry['download_url'] + json.direct_download direct_download + json.image_type image_type + json.is_readme_file is_readme_type?(file_name) + if entry['latest_commit'] + json.partial! 'last_commit', entry: entry + end +end + +if @project.educoder? + file_path = params[:filepath].present? ? [params[:filepath], entry['name']].join('/') : entry['name'] + + json.name entry['name'] + json.sha nil + json.path file_path + json.type entry['type'] === 'blob'? 'file' : 'dir' + json.size 0 + json.content nil + json.target nil + json.download_url nil + json.direct_download false + json.image_type false + json.is_readme_file false + if entry['latest_commit'] + # json.partial! 'last_commit', entry: entry + json.partial! 'repositories/simple_entry', locals: { entry: entry } + end end diff --git a/app/views/repositories/entries.json.jbuilder b/app/views/repositories/entries.json.jbuilder index 636845efd..2513294d3 100644 --- a/app/views/repositories/entries.json.jbuilder +++ b/app/views/repositories/entries.json.jbuilder @@ -1,36 +1,71 @@ -json.last_commit do - if @latest_commit - json.partial! 'commit', commit: @latest_commit, project: @project - else - json.nil! +if @project.educoder? + json.last_commit do + if @entries['commits'] + json.partial! 'commit', commit: @entries['commits'], project: @project + else + json.nil! + end end -end -#json.tags_count @tags_count -#json.branches_count @branches_count -#json.commits_count @commits_count -json.zip_url render_zip_url(@project, @ref) -json.tar_url render_tar_url(@project, @ref) -json.entries do - 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'] - content = - if is_readme_type?(entry['name']) - is_readme_file = true - content = Gitea::Repository::Entries::GetService.call(@project_owner, @project.identifier, entry['name'], ref: @ref)['content'] - readme_render_decode64_content(content, @path) - else - is_readme_file = false - entry['content'] + json.commits_count @entries['commit_count'] + json.zip_url @entries['git_url'] + json.tar_url '' + json.entries do + json.array! @entries['trees'] do |entry| + json.name entry['name'] + json.path entry['name'] + json.sha nil + json.type entry['type'] === 'blob'? 'file' : 'dir' + json.size 0 + json.is_readme_file false + json.content nil + json.target nil + json.commit do + json.message entry['title'] + json.time_from_now entry['time'] + json.sha nil + json.created_at_unix nil + json.created_at nil + end + end + end +end + + +if @project.forge? + json.last_commit do + if @latest_commit + json.partial! 'commit', commit: @latest_commit, project: @project + else + json.nil! + end + end + #json.tags_count @tags_count + #json.branches_count @branches_count + #json.commits_count @commits_count + json.zip_url render_zip_url(@project, @ref) + json.tar_url render_tar_url(@project, @ref) + json.entries do + 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'] + content = + if is_readme_type?(entry['name']) + is_readme_file = true + content = Gitea::Repository::Entries::GetService.call(@project_owner, @project.identifier, entry['name'], ref: @ref)['content'] + readme_render_decode64_content(content, @path) + else + is_readme_file = false + entry['content'] + end + json.is_readme_file is_readme_file + json.content content + json.target entry['target'] + if entry['latest_commit'] + json.partial! 'last_commit', entry: entry end - json.is_readme_file is_readme_file - json.content content - json.target entry['target'] - if entry['latest_commit'] - json.partial! 'last_commit', entry: entry end end end diff --git a/app/views/repositories/show.json.jbuilder b/app/views/repositories/show.json.jbuilder index fca2ad5bd..6993abad8 100644 --- a/app/views/repositories/show.json.jbuilder +++ b/app/views/repositories/show.json.jbuilder @@ -1,10 +1,10 @@ -json.identifier @project.identifier +json.identifier render_identifier(@project) json.name @project.name json.project_id @project.id json.repo_id @repo.id json.issues_count @project.issues_count.to_i - @project.pull_requests_count.to_i json.pull_requests_count @project.pull_requests_count -json.project_identifier @project.identifier +json.project_identifier render_identifier(@project) json.praises_count @project.praises_count.to_i json.forked_count @project.forked_count.to_i json.watchers_count @project.watchers_count.to_i @@ -47,4 +47,4 @@ if @result json.private @result['private'] end -json.partial! 'author', locals: { user: @project.owner } +json.partial! 'author' diff --git a/app/views/repositories/sub_entries.json.jbuilder b/app/views/repositories/sub_entries.json.jbuilder index 2032df857..5d94f5d60 100644 --- a/app/views/repositories/sub_entries.json.jbuilder +++ b/app/views/repositories/sub_entries.json.jbuilder @@ -1,16 +1,32 @@ +if @project.forge? + json.last_commit do + if @latest_commit + json.partial! 'commit', commit: @latest_commit, project: @project + else + json.nil! + end + end + #json.tags_count @tags_count + #json.branches_count @branches_count + #json.commits_count @commits_count + json.entries do + json.array! @sub_entries do |entry| + json.partial! 'repositories/simple_entry', locals: { entry: entry } + end + end +end -json.last_commit do - if @latest_commit - json.partial! 'commit', commit: @latest_commit, project: @project - else - json.nil! - end -end -#json.tags_count @tags_count -#json.branches_count @branches_count -#json.commits_count @commits_count -json.entries do - json.array! @sub_entries do |entry| - json.partial! 'repositories/simple_entry', locals: { entry: entry } +if @project.educoder? + json.last_commit do + if @sub_entries['commits'] + json.partial! 'commit', commit: @sub_entries['commits'], project: @project + else + json.nil! + end + end + json.entries do + json.array! @sub_entries['trees'] do |entry| + json.partial! 'repositories/simple_entry', locals: { entry: entry } + end end end