From ee50083a5744fcb5ba256090a9828e317944a118 Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 23 Mar 2023 09:33:52 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E7=89=B9?= =?UTF-8?q?=E6=AE=8A=E7=BB=9F=E8=AE=A1commit=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/repositories_controller.rb | 7 +++- lib/tasks/special_commit.rake | 40 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 lib/tasks/special_commit.rake diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index e6fef78f0..7ac3984db 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -322,7 +322,12 @@ class RepositoriesController < ApplicationController def get_latest_commit 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 + cache_total_commits = $redis_cache.get("ProjectSpecialCommit:#{project.id}") + if cache_total_commits.present? + @commits_count = cache_total_commits.to_i + else + @commits_count = latest_commit.present? ? latest_commit[:total_count] : 0 + end end def content_params diff --git a/lib/tasks/special_commit.rake b/lib/tasks/special_commit.rake new file mode 100644 index 000000000..4d5e04f7a --- /dev/null +++ b/lib/tasks/special_commit.rake @@ -0,0 +1,40 @@ +namespace :special_commit do + desc "batch_add_issues" + task :load, [:login, :identifier] => :environment do |t, args| + owner = Owner.find_by(login: args.login) + project = Project.find_by(user_id: owner&.id, identifier: args.identifier) + if owner.nil? || project.nil? + puts "====Project is not found.====" + else + puts "====Sync Project: #{owner.real_name}/#{project.name}====" + puts "====Sync Count Project Self Commit====" + self_commit_list_result = $gitea_client.get_repos_commits_by_owner_repo(owner.login, project.identifier) + total_commits = self_commit_list_result[:total_data].to_i + puts "====Sync Count Project Submodule Commit====" + entries = $gitea_client.get_repos_contents_by_owner_repo(owner.login, project.identifier) + entries.each do |entry| + next if entry["submodule_git_url"].nil? + submodule_git_url = entry["submodule_git_url"].gsub(Rails.application.config_for(:configuration)['platform_url'], '').gsub(".git", '') + real_relative_path = File.expand_path(submodule_git_url, "#{owner.login}/#{project.identifier}").gsub("#{Rails.root}/", '') + sub_project_owner_login = real_relative_path.split("/")[0] + sub_project_identifier = real_relative_path.split("/")[1] + sub_owner = Owner.find_by(login: sub_project_owner_login) + sub_project = sub_owner.projects.find_by(identifier: sub_project_identifier) + next if sub_owner.nil? || sub_project.nil? + sub_commit_list_result = $gitea_client.get_repos_commits_by_owner_repo(sub_project_owner_login, sub_project_identifier) + total_commits += sub_commit_list_result[:total_data].to_i + end + puts "====Sync Count Project forkproject Commit====" + project.forked_projects.each do |p| + forked_project_owner_login = p.owner&.login + forked_project_identifier = p.identifier + next if forked_project_owner_login.nil? || forked_project_owner_login.nil? + forked_commit_list_result = $gitea_client.get_repos_commits_by_owner_repo(forked_project_owner_login, forked_project_identifier) + total_commits += forked_commit_list_result[:total_data].to_i + end + puts "====Write total commits to cache====" + $redis_cache.set("ProjectSpecialCommit:#{project.id}", total_commits) + $redis_cache.expireat("ProjectSpecialCommit:#{project.id}", (Date.today+30.days).to_time.to_i) + end + end +end \ No newline at end of file From e0692bd92acfeb29f3ec82838e1b5ff73913f51c Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 23 Mar 2023 09:49:35 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=94=B9=EF=BC=9Afork=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E4=B8=BA=E5=AD=90=E9=A1=B9=E7=9B=AE=E4=B8=8B=E7=9A=84?= =?UTF-8?q?fork=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/special_commit.rake | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/tasks/special_commit.rake b/lib/tasks/special_commit.rake index 4d5e04f7a..922c51cfe 100644 --- a/lib/tasks/special_commit.rake +++ b/lib/tasks/special_commit.rake @@ -23,15 +23,16 @@ namespace :special_commit do next if sub_owner.nil? || sub_project.nil? sub_commit_list_result = $gitea_client.get_repos_commits_by_owner_repo(sub_project_owner_login, sub_project_identifier) total_commits += sub_commit_list_result[:total_data].to_i + puts "====Sync Count Project Submodule forkproject Commit====" + sub_project.forked_projects.each do |p| + forked_project_owner_login = p.owner&.login + forked_project_identifier = p.identifier + next if forked_project_owner_login.nil? || forked_project_owner_login.nil? + forked_commit_list_result = $gitea_client.get_repos_commits_by_owner_repo(forked_project_owner_login, forked_project_identifier) + total_commits += forked_commit_list_result[:total_data].to_i + end end - puts "====Sync Count Project forkproject Commit====" - project.forked_projects.each do |p| - forked_project_owner_login = p.owner&.login - forked_project_identifier = p.identifier - next if forked_project_owner_login.nil? || forked_project_owner_login.nil? - forked_commit_list_result = $gitea_client.get_repos_commits_by_owner_repo(forked_project_owner_login, forked_project_identifier) - total_commits += forked_commit_list_result[:total_data].to_i - end + puts "====Write total commits to cache====" $redis_cache.set("ProjectSpecialCommit:#{project.id}", total_commits) $redis_cache.expireat("ProjectSpecialCommit:#{project.id}", (Date.today+30.days).to_time.to_i) From 511cbf45655df614dd9c3c0e4328413347108266 Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 23 Mar 2023 10:00:14 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=89=A7?= =?UTF-8?q?=E8=A1=8Crake=E4=BB=BB=E5=8A=A1=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/special_commit.rake | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/tasks/special_commit.rake b/lib/tasks/special_commit.rake index 922c51cfe..ab6c63767 100644 --- a/lib/tasks/special_commit.rake +++ b/lib/tasks/special_commit.rake @@ -1,5 +1,8 @@ +# 执行示例 bundle exec rake "special_commit:load[yystopf, pig]" +# RAILS_ENV=production bundle exec rake "special_commit:load[yystopf, pig]" +# namespace :special_commit do - desc "batch_add_issues" + desc "Sync Special Commit to Cache" task :load, [:login, :identifier] => :environment do |t, args| owner = Owner.find_by(login: args.login) project = Project.find_by(user_id: owner&.id, identifier: args.identifier)