diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index e56b4dcd..ae7e44a9 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -258,7 +258,7 @@ class IssuesController < ApplicationController end end - def clean + def clean issue_ids = params[:ids] if issue_ids.present? if Issue.where(id: issue_ids).destroy_all @@ -266,13 +266,13 @@ class IssuesController < ApplicationController else normal_status(-1, "删除失败") end - else + else normal_status(-1, "请选择任务") end end - def series_update - + def series_update + update_hash = {} update_hash.merge!(assigned_to_id: params[:assigned_to_id]) if params[:assigned_to_id].present? update_hash.merge!(fixed_version_id: params[:fixed_version_id]) if params[:fixed_version_id].present? @@ -287,7 +287,7 @@ class IssuesController < ApplicationController else normal_status(-1, "批量更新失败") end - else + else normal_status(-1, "请选择任务") end end @@ -428,7 +428,7 @@ class IssuesController < ApplicationController def get_branches all_branches = [] - get_all_branches = Gitea::Repository::BranchesService.new(@user, @project&.repository.try(:identifier)).call + get_all_branches = Gitea::Repository::Branches::ListService.new(@user, @project&.repository.try(:identifier)).call if get_all_branches && get_all_branches.size > 0 get_all_branches.each do |b| all_branches.push(b["name"]) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index aeadbd93..a70a380d 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -35,7 +35,7 @@ class ProjectsController < ApplicationController end def branches - @branches = Gitea::Repository::BranchesService.new(@project.owner, @project.identifier).call + @branches = Gitea::Repository::Branches::ListService.new(@project.owner, @project.identifier).call end def group_type_list @@ -94,8 +94,8 @@ class ProjectsController < ApplicationController @praises = paginate(praises) end - def fork_users - fork_users = @project.fork_users.includes(:user, :project).order("fork_users.created_at asc").distinct + def fork_users + fork_users = @project.fork_users.includes(:user, :project).order("fork_users.created_at asc").distinct @forks_count = fork_users.size @fork_users = paginate(fork_users) end @@ -111,7 +111,7 @@ class ProjectsController < ApplicationController :auth_password, :project_category_id, :project_language_id, :clone_addr, :private) end - def project_public? + def project_public? unless @project.is_public || current_user&admin? tip_exception(403, "..") end diff --git a/app/controllers/pull_requests_controller.rb b/app/controllers/pull_requests_controller.rb index ff212c97..c214503b 100644 --- a/app/controllers/pull_requests_controller.rb +++ b/app/controllers/pull_requests_controller.rb @@ -25,7 +25,7 @@ class PullRequestsController < ApplicationController def new @all_branches = [] - get_all_branches = Gitea::Repository::BranchesService.new(@user, @repository.try(:identifier)).call + get_all_branches = Gitea::Repository::Branches::ListService.new(@user, @repository.try(:identifier)).call if get_all_branches && get_all_branches.size > 0 get_all_branches.each do |b| @all_branches.push(b["name"]) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 8bc182db..6d0867a0 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -6,9 +6,11 @@ class RepositoriesController < ApplicationController before_action :authorizate!, except: [:sync_mirror, :tags, :commit] 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] 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 @@ -26,14 +28,13 @@ class RepositoriesController < ApplicationController def entries @project.increment!(:visits) - @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 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: params[:ref]) + interactor = Repositories::EntriesInteractor.call(@project.owner, @project.identifier, file_path_uri, ref: @ref) if interactor.success? @sub_entries = interactor.result @sub_entries = [] << @sub_entries unless @sub_entries.is_a? Array @@ -112,7 +113,7 @@ class RepositoriesController < ApplicationController def sync_mirror @repo&.mirror.set_status!(Mirror.statuses[:waiting]) - SyncMirroredRepositoryJob.perform_later(@repo, current_user) + SyncMirroredRepositoryJob.perform_later(@repo.id, current_user.id) render_ok end @@ -129,6 +130,17 @@ class RepositoriesController < ApplicationController end end + # TODO 获取最新commit信息 + def get_latest_commit + @latest_commit = 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_ref + @ref = params[:ref] || "master" + end + def content_params { filepath: params[:filepath], diff --git a/app/controllers/version_releases_controller.rb b/app/controllers/version_releases_controller.rb index 8a944475..6717bf06 100644 --- a/app/controllers/version_releases_controller.rb +++ b/app/controllers/version_releases_controller.rb @@ -14,7 +14,7 @@ class VersionReleasesController < ApplicationController def new #获取所有的分支 @all_branches = [] - get_all_branches = Gitea::Repository::BranchesService.new(@user, @repository.try(:identifier)).call + get_all_branches = Gitea::Repository::Branches::ListService.new(@user, @repository.try(:identifier)).call if get_all_branches && get_all_branches.size > 0 get_all_branches.each do |b| @all_branches.push(b["name"]) diff --git a/app/jobs/migrate_remote_repository_job.rb b/app/jobs/migrate_remote_repository_job.rb index bc17718a..75cf0613 100644 --- a/app/jobs/migrate_remote_repository_job.rb +++ b/app/jobs/migrate_remote_repository_job.rb @@ -1,18 +1,15 @@ class MigrateRemoteRepositoryJob < ApplicationJob queue_as :default - def perform(repo, token, params) + def perform(repo_id, token, params) + puts "############ perform: repo_id: #{repo_id}, token: #{token}, params: #{params}}" + repo = Repository.find_by(id: repo_id) + return if repo.blank? + gitea_repository = Gitea::Repository::MigrateService.new(token, params).call - sync_project(repo, gitea_repository) - sync_repository(repo, gitea_repository) - end - - private - def sync_project(repo, gitea_repository) - repo&.project.update_columns(gpid: gitea_repository["id"], identifier: gitea_repository["name"]) if gitea_repository - end - - def sync_repository(repository, gitea_repository) - repository.mirror.update_columns(statuses: Mirror.statuses[:succeeded]) if gitea_repository + if gitea_repository + repo&.project&.update_columns(gpid: gitea_repository["id"], identifier: gitea_repository["name"]) + repo&.mirror&.update_columns(status: Mirror.statuses[:succeeded]) + end end end diff --git a/app/jobs/sync_mirrored_repository_job.rb b/app/jobs/sync_mirrored_repository_job.rb index 7230e119..8184ddee 100644 --- a/app/jobs/sync_mirrored_repository_job.rb +++ b/app/jobs/sync_mirrored_repository_job.rb @@ -1,7 +1,10 @@ class SyncMirroredRepositoryJob < ApplicationJob queue_as :default - def perform(repo, current_user) + def perform(repo_id, user_id) + repo = Repository.find_by(id: repo_id) + current_user = User.find_by(id: user_id) + return if repo.blank? || current_user.blank? result = Gitea::Repository::SyncMirroredService.new(repo.user.login, repo.identifier, token: current_user.gitea_token).call repo&.mirror.set_status! if result[:status] === 200 end diff --git a/app/models/mirror.rb b/app/models/mirror.rb index 8b7e61b0..bc6ff6f3 100644 --- a/app/models/mirror.rb +++ b/app/models/mirror.rb @@ -8,6 +8,10 @@ class Mirror < ApplicationRecord def set_status!(status=Mirror.statuses[:succeeded]) - update_column(status: status) + update_column(:status, status) + end + + def numerical_for_status + self.class.name.constantize.statuses["#{self.status}"] end end diff --git a/app/models/project.rb b/app/models/project.rb index 12021231..742f8958 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -139,4 +139,8 @@ class Project < ApplicationRecord issues_count - closed_issues_count end + def numerical_for_project_type + self.class.name.constantize.project_types["#{self.project_type}"] + end + end diff --git a/app/services/gitea/repository/branches/get_service.rb b/app/services/gitea/repository/branches/get_service.rb new file mode 100644 index 00000000..d833b86b --- /dev/null +++ b/app/services/gitea/repository/branches/get_service.rb @@ -0,0 +1,27 @@ +# Retrieve a specific branch from a repository, including its effective branch protection +class Gitea::Repository::Branches::GetService < Gitea::ClientService + attr_reader :owner, :repo, :branch, :token + + # ex: + # Gitea::Repository::Branches::GetService.new(@project.owner, @project.identifier, 'master', current_user.gitea_token).call + def initialize(owner, repo, branch, token) + @owner = owner + @repo = repo + @branch = branch + @token = token + end + + def call + response = get(url, params) + render_data(response) + end + + private + def params + Hash.new.merge(token: token) + end + + def url + "/repos/#{owner}/#{repo}/branches/#{branch}".freeze + end +end diff --git a/app/services/gitea/repository/branches_service.rb b/app/services/gitea/repository/branches/list_service.rb similarity index 82% rename from app/services/gitea/repository/branches_service.rb rename to app/services/gitea/repository/branches/list_service.rb index 4e4c6752..17702259 100644 --- a/app/services/gitea/repository/branches_service.rb +++ b/app/services/gitea/repository/branches/list_service.rb @@ -1,4 +1,4 @@ -class Gitea::Repository::BranchesService < Gitea::ClientService +class Gitea::Repository::Branches::ListService < Gitea::ClientService attr_reader :user, :repo def initialize(user, repo) diff --git a/app/services/repositories/migrate_service.rb b/app/services/repositories/migrate_service.rb index 0e773d54..090d6473 100644 --- a/app/services/repositories/migrate_service.rb +++ b/app/services/repositories/migrate_service.rb @@ -12,7 +12,7 @@ class Repositories::MigrateService < ApplicationService ActiveRecord::Base.transaction do if @repository.save! @repository.set_mirror! if wrapper_mirror - MigrateRemoteRepositoryJob.perform_later(@repository, user.gitea_token, gitea_repository_params) + MigrateRemoteRepositoryJob.perform_later(@repository.id, user.gitea_token, gitea_repository_params) end @repository end diff --git a/app/views/projects/branches.json.jbuilder b/app/views/projects/branches.json.jbuilder index 58f7af98..d6f45137 100644 --- a/app/views/projects/branches.json.jbuilder +++ b/app/views/projects/branches.json.jbuilder @@ -7,19 +7,11 @@ json.array! @branches do |branch| json.zip_url render_zip_url(@project, branch['name']) json.tar_url render_tar_url(@project, branch['name']) json.last_commit do - json.id branch['commit']['id'] + json.sha branch['commit']['id'] json.message branch['commit']['message'] json.timestamp render_unix_time(branch['commit']['timestamp']) json.time_from_now time_from_now(branch['commit']['timestamp']) - end - - user = find_user_by_login_or_mail(branch['commit']['author']['name']) - json.author do - if user - json.login user.login - json.image_url url_to_avatar(user) - else - json.nil! - end + json.author branch['commit']['author'] + json.committer branch['commit']['committer'] end end diff --git a/app/views/repositories/_commit.json.jbuilder b/app/views/repositories/_commit.json.jbuilder new file mode 100644 index 00000000..5ad8a1bc --- /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 45ea7121..6017ab22 100644 --- a/app/views/repositories/entries.json.jbuilder +++ b/app/views/repositories/entries.json.jbuilder @@ -1,23 +1,14 @@ -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.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'] @@ -25,9 +16,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 diff --git a/app/views/repositories/show.json.jbuilder b/app/views/repositories/show.json.jbuilder index 30ec73e1..de647add 100644 --- a/app/views/repositories/show.json.jbuilder +++ b/app/views/repositories/show.json.jbuilder @@ -18,6 +18,8 @@ json.commits_count @commits_count json.permission render_edit_project_permission(current_user, @project) if current_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? json.watched current_user&.watched?(@project) json.praised current_user&.liked?(@project) json.status @project.status diff --git a/app/views/repositories/sub_entries.json.jbuilder b/app/views/repositories/sub_entries.json.jbuilder index 1f8de1eb..4ac25346 100644 --- a/app/views/repositories/sub_entries.json.jbuilder +++ b/app/views/repositories/sub_entries.json.jbuilder @@ -1,3 +1,13 @@ -json.array! @sub_entries do |entry| - json.partial! 'repositories/simple_entry', locals: { entry: entry } + +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! @sub_entries do |entry| + json.partial! 'repositories/simple_entry', locals: { entry: entry } + end end