FIX code review

This commit is contained in:
Jasder 2020-06-29 16:05:57 +08:00
parent 1133517127
commit e671071420
6 changed files with 41 additions and 23 deletions

View File

@ -8,14 +8,11 @@ class RepositoriesController < ApplicationController
before_action :find_repository_by_id, only: %i[commit sync_mirror tags] 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 :authorizate_user_can_edit_repo!, only: %i[sync_mirror]
before_action :get_ref, only: %i[entries sub_entries] 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 def show
@user = current_user @user = current_user
@branches_count = Gitea::Repository::Branches::ListService.new(@project.owner, @project.identifier).call&.size @repo = @project.repository
@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
@result = Gitea::Repository::GetService.new(@project.owner, @project.identifier).call @result = Gitea::Repository::GetService.new(@project.owner, @project.identifier).call
@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?
@ -96,11 +93,13 @@ class RepositoriesController < ApplicationController
end end
def repo_hook def repo_hook
end end
def sync_mirror 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) SyncMirroredRepositoryJob.perform_later(@repo.id, current_user.id)
render_ok render_ok
end end
@ -124,9 +123,17 @@ class RepositoriesController < ApplicationController
# TODO 获取最新commit信息 # TODO 获取最新commit信息
def get_latest_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 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 end
def get_ref def get_ref

View File

@ -1,7 +1,7 @@
class Mirror < ApplicationRecord class Mirror < ApplicationRecord
# 0 - succeeded, 1 - waiting, 2 - failed # 0 - succeeded, 1 - waiting, 2 - failed
# 0: 同步镜像成功1: 正在同步镜像2: 同步失败默认值为0 # 0: 同步镜像成功1: 正在同步镜像2: 同步失败; 默认值为0
enum status: { succeeded: 0, waiting: 1, failed: 2 } enum status: { succeeded: 0, waiting: 1, failed: 2 }
belongs_to :repository, foreign_key: :repo_id belongs_to :repository, foreign_key: :repo_id

View File

@ -10,7 +10,7 @@ class Repositories::MigrateService < ApplicationService
def call def call
@repository = Repository.new(repository_params) @repository = Repository.new(repository_params)
if @repository.save! if @repository.save!
@repository.set_mirror! if wrapper_mirror @repository.set_mirror!
MigrateRemoteRepositoryJob.perform_later(@repository.id, user.gitea_token, gitea_repository_params) MigrateRemoteRepositoryJob.perform_later(@repository.id, user.gitea_token, gitea_repository_params)
end end
@repository @repository

View File

@ -5,6 +5,9 @@ json.last_commit do
json.nil! json.nil!
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.zip_url render_zip_url(@project, @ref)
json.tar_url render_tar_url(@project, @ref) json.tar_url render_tar_url(@project, @ref)
json.entries do json.entries do

View File

@ -1,7 +1,7 @@
json.identifier @project.identifier json.identifier @project.identifier
json.name @project.name json.name @project.name
json.project_id @project.id 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.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 @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_releases_count @project.releases_size(@user.try(:id), "all")
json.version_releasesed_count @project.releases_size(@user.try(:id), "released") #已发行的版本 json.version_releasesed_count @project.releases_size(@user.try(:id), "released") #已发行的版本
json.contributor_users_count @project.contributor_users 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.permission @project.get_premission(@user)
json.mirror_url @project&.repository.mirror_url json.mirror_url @project&.repository.mirror_url
json.mirror @project&.repository.mirror_url.present? json.mirror @project&.repository.mirror_url.present?
json.type @project.numerical_for_project_type 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.watched @project.watched_by? @user
json.praised @project.praised_by? @user json.praised @project.praised_by? @user
json.status @project.status json.status @project.status
@ -32,12 +35,14 @@ json.fork_info do
end end
end end
json.size replace_bytes_to_b(number_to_human_size(@result['size'].to_i*1024)) if @result
json.ssh_url @result['ssh_url'] json.size replace_bytes_to_b(number_to_human_size(@result['size'].to_i*1024))
json.clone_url @result['clone_url'] json.ssh_url @result['ssh_url']
json.default_branch @result['default_branch'] json.clone_url @result['clone_url']
json.empty @result['empty'] json.default_branch @result['default_branch']
json.full_name @result['full_name'] 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 } json.partial! 'author', locals: { user: @project.owner }

View File

@ -6,6 +6,9 @@ json.last_commit do
json.nil! json.nil!
end end
end end
json.tags_count @tags_count
json.branches_count @branches_count
json.commits_count @commits_count
json.entries do json.entries do
json.array! @sub_entries do |entry| json.array! @sub_entries do |entry|
json.partial! 'repositories/simple_entry', locals: { entry: entry } json.partial! 'repositories/simple_entry', locals: { entry: entry }