| @@ -10,8 +10,9 @@ class ProjectsController < ApplicationController | |||||
| def index | def index | ||||
| scope = Projects::ListQuery.call(params) | 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 | end | ||||
| def create | def create | ||||
| @@ -13,7 +13,7 @@ class RepositoriesController < ApplicationController | |||||
| def show | def show | ||||
| @user = current_user | @user = current_user | ||||
| @repo = @project.repository | @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) | @project_fork_id = @project.try(:forked_from_project_id) | ||||
| if @project_fork_id.present? | if @project_fork_id.present? | ||||
| @fork_project = Project.find_by(id: @project_fork_id) | @fork_project = Project.find_by(id: @project_fork_id) | ||||
| @@ -27,26 +27,40 @@ class RepositoriesController < ApplicationController | |||||
| def entries | def entries | ||||
| @project.increment!(:visits) | @project.increment!(:visits) | ||||
| @project_owner = @project.owner | @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 | end | ||||
| def top_counts | 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 | 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: @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 | 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 | ||||
| end | end | ||||
| @@ -135,8 +149,8 @@ class RepositoriesController < ApplicationController | |||||
| end | end | ||||
| def get_statistics | 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 | end | ||||
| def get_ref | def get_ref | ||||
| @@ -144,9 +158,9 @@ class RepositoriesController < ApplicationController | |||||
| end | end | ||||
| def get_latest_commit | 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 | end | ||||
| def content_params | def content_params | ||||
| @@ -29,7 +29,9 @@ module ProjectsHelper | |||||
| end | end | ||||
| def json_response(project, user) | def json_response(project, user) | ||||
| repo = project.repository | |||||
| # repo = project.repository | |||||
| repo = Repository.select(:id).find_by(project: project) | |||||
| tmp_json = {} | tmp_json = {} | ||||
| unless project.common? | unless project.common? | ||||
| tmp_json = tmp_json.merge({ | tmp_json = tmp_json.merge({ | ||||
| @@ -55,8 +57,9 @@ module ProjectsHelper | |||||
| end | end | ||||
| tmp_json = tmp_json.merge({ | tmp_json = tmp_json.merge({ | ||||
| identifier: project.identifier, | |||||
| identifier: render_identifier(project), | |||||
| name: project.name, | name: project.name, | ||||
| platform: project.platform, | |||||
| id: project.id, | id: project.id, | ||||
| repo_id: repo.id, | repo_id: repo.id, | ||||
| open_devops: (user.blank? || user.is_a?(AnonymousUser)) ? false : project.open_devops?, | open_devops: (user.blank? || user.is_a?(AnonymousUser)) ? false : project.open_devops?, | ||||
| @@ -67,7 +70,19 @@ module ProjectsHelper | |||||
| render json: tmp_json | render json: tmp_json | ||||
| end | end | ||||
| def render_identifier(project) | |||||
| project.educoder? ? project.project_educoder&.repo_name&.split('/')[1] : project.identifier | |||||
| end | |||||
| def render_author(project) | 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 | ||||
| end | end | ||||
| @@ -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_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_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 :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 | ||||
| end | end | ||||
| @@ -10,10 +10,9 @@ class Projects::ListQuery < ApplicationQuery | |||||
| end | end | ||||
| def call | 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_type(params[:project_type]) | ||||
| .with_project_category(params[:category_id]) | .with_project_category(params[:category_id]) | ||||
| .with_project_language(params[:language_id]) | .with_project_language(params[:language_id]) | ||||
| @@ -21,7 +20,9 @@ class Projects::ListQuery < ApplicationQuery | |||||
| sort = params[:sort_by] || "updated_on" | sort = params[:sort_by] || "updated_on" | ||||
| sort_direction = params[:sort_direction] || "desc" | 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 | ||||
| end | end | ||||
| @@ -1,6 +1,6 @@ | |||||
| json.id project.id | json.id project.id | ||||
| json.repo_id project&.repository&.id | json.repo_id project&.repository&.id | ||||
| json.identifier project.identifier | |||||
| json.identifier render_identifier(project) | |||||
| json.name project.name | json.name project.name | ||||
| json.description Nokogiri::HTML(project.description).text | json.description Nokogiri::HTML(project.description).text | ||||
| json.visits project.visits | json.visits project.visits | ||||
| @@ -17,13 +17,13 @@ json.author do | |||||
| if project.educoder? | if project.educoder? | ||||
| project_educoder = project.project_educoder | project_educoder = project.project_educoder | ||||
| json.name project_educoder&.owner | 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 | else | ||||
| user = project.owner | user = project.owner | ||||
| json.name user.try(:show_real_name) | json.name user.try(:show_real_name) | ||||
| json.login user.login | json.login user.login | ||||
| json.image_url url_to_avatar(project.owner) | |||||
| json.image_url render_avatar_url(user) | |||||
| end | end | ||||
| end | end | ||||
| @@ -1,5 +1,11 @@ | |||||
| json.author do | 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 | end | ||||
| @@ -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 | 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 | end | ||||
| @@ -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 | end | ||||
| @@ -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 | |||||
| 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 | ||||
| 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'] | |||||
| 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 | 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 | end | ||||
| end | end | ||||
| @@ -1,10 +1,10 @@ | |||||
| json.identifier @project.identifier | |||||
| json.identifier render_identifier(@project) | |||||
| json.name @project.name | json.name @project.name | ||||
| json.project_id @project.id | json.project_id @project.id | ||||
| json.repo_id @repo.id | json.repo_id @repo.id | ||||
| json.issues_count @project.issues_count.to_i - @project.pull_requests_count.to_i | json.issues_count @project.issues_count.to_i - @project.pull_requests_count.to_i | ||||
| json.pull_requests_count @project.pull_requests_count | 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.praises_count @project.praises_count.to_i | ||||
| json.forked_count @project.forked_count.to_i | json.forked_count @project.forked_count.to_i | ||||
| json.watchers_count @project.watchers_count.to_i | json.watchers_count @project.watchers_count.to_i | ||||
| @@ -47,4 +47,4 @@ if @result | |||||
| json.private @result['private'] | json.private @result['private'] | ||||
| end | end | ||||
| json.partial! 'author', locals: { user: @project.owner } | |||||
| json.partial! 'author' | |||||
| @@ -1,16 +1,32 @@ | |||||
| json.last_commit do | |||||
| if @latest_commit | |||||
| json.partial! 'commit', commit: @latest_commit, project: @project | |||||
| else | |||||
| json.nil! | |||||
| 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 | ||||
| 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 | ||||
| end | end | ||||