diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index cc284b4cb..70ffd2289 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -8,14 +8,11 @@ class RepositoriesController < ApplicationController 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_ref, only: %i[entries sub_entries] - before_action :get_latest_commit, :get_ref, only: %i[entries sub_entries] + before_action :get_statistics, only: %i[entries sub_entries] def show @user = current_user - @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 + @repo = @project.repository @result = Gitea::Repository::GetService.new(@project.owner, @project.identifier).call @project_fork_id = @project.try(:forked_from_project_id) if @project_fork_id.present? @@ -96,11 +93,13 @@ class RepositoriesController < ApplicationController end def repo_hook - + end def sync_mirror - @repo&.mirror.set_status!(Mirror.statuses[:waiting]) + return render_error("正在镜像中..") if @repo.mirror.warning? + + @repo.sync_mirror! SyncMirroredRepositoryJob.perform_later(@repo.id, current_user.id) render_ok end @@ -124,9 +123,17 @@ class RepositoriesController < ApplicationController # TODO 获取最新commit信息 def get_latest_commit - @latest_commit = Gitea::Repository::Commits::ListService.new(@project.owner.login, @project.identifier, + 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_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 + + latest_commit = get_latest_commit + @latest_commit = latest_commit[:body][0] + @commits_count = latest_commit[:total_count] end def get_ref diff --git a/app/models/mirror.rb b/app/models/mirror.rb index 7ce4651ae..f5737b0f3 100644 --- a/app/models/mirror.rb +++ b/app/models/mirror.rb @@ -1,7 +1,7 @@ class Mirror < ApplicationRecord # 0 - succeeded, 1 - waiting, 2 - failed - # 0: 同步镜像成功;1: 正在同步镜像;2: 同步失败,默认值为0 + # 0: 同步镜像成功;1: 正在同步镜像;2: 同步失败; 默认值为0 enum status: { succeeded: 0, waiting: 1, failed: 2 } belongs_to :repository, foreign_key: :repo_id diff --git a/app/services/repositories/migrate_service.rb b/app/services/repositories/migrate_service.rb index dad7e3930..e2f520aeb 100644 --- a/app/services/repositories/migrate_service.rb +++ b/app/services/repositories/migrate_service.rb @@ -10,7 +10,7 @@ class Repositories::MigrateService < ApplicationService def call @repository = Repository.new(repository_params) if @repository.save! - @repository.set_mirror! if wrapper_mirror + @repository.set_mirror! MigrateRemoteRepositoryJob.perform_later(@repository.id, user.gitea_token, gitea_repository_params) end @repository diff --git a/app/views/repositories/entries.json.jbuilder b/app/views/repositories/entries.json.jbuilder index b246de32f..7c7b5ef70 100644 --- a/app/views/repositories/entries.json.jbuilder +++ b/app/views/repositories/entries.json.jbuilder @@ -5,6 +5,9 @@ json.last_commit do 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 diff --git a/app/views/repositories/show.json.jbuilder b/app/views/repositories/show.json.jbuilder index d01d842e7..eecd37524 100644 --- a/app/views/repositories/show.json.jbuilder +++ b/app/views/repositories/show.json.jbuilder @@ -1,7 +1,7 @@ json.identifier @project.identifier json.name @project.name json.project_id @project.id -json.repo_id @project.repository.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 @@ -12,14 +12,17 @@ json.versions_count @project.versions_count #里程碑数量 json.version_releases_count @project.releases_size(@user.try(:id), "all") json.version_releasesed_count @project.releases_size(@user.try(:id), "released") #已发行的版本 json.contributor_users_count @project.contributor_users -json.issue_tags_count @tags_count -json.branches_count @branches_count -json.commits_count @commits_count json.permission @project.get_premission(@user) json.mirror_url @project&.repository.mirror_url json.mirror @project&.repository.mirror_url.present? json.type @project.numerical_for_project_type -json.mirror_status @project.repository&.mirror&.numerical_for_status if @project.sync_mirror? + +unless @project.common? + json.mirror_status @repo.mirror_status + json.mirror_num @repo.mirror_num + json.first_sync @repo.first_sync? +end + json.watched @project.watched_by? @user json.praised @project.praised_by? @user json.status @project.status @@ -32,12 +35,14 @@ json.fork_info do end end -json.size replace_bytes_to_b(number_to_human_size(@result['size'].to_i*1024)) -json.ssh_url @result['ssh_url'] -json.clone_url @result['clone_url'] -json.default_branch @result['default_branch'] -json.empty @result['empty'] -json.full_name @result['full_name'] +if @result + json.size replace_bytes_to_b(number_to_human_size(@result['size'].to_i*1024)) + json.ssh_url @result['ssh_url'] + json.clone_url @result['clone_url'] + json.default_branch @result['default_branch'] + json.empty @result['empty'] + json.full_name @result['full_name'] + json.private @result['private'] +end -json.private @result['private'] json.partial! 'author', locals: { user: @project.owner } diff --git a/app/views/repositories/sub_entries.json.jbuilder b/app/views/repositories/sub_entries.json.jbuilder index 4ac253465..43dbf764e 100644 --- a/app/views/repositories/sub_entries.json.jbuilder +++ b/app/views/repositories/sub_entries.json.jbuilder @@ -6,6 +6,9 @@ json.last_commit do 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 }