From 2a3faa21d2c5af2cd0ca7d389bc086fc029f4662 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Fri, 31 May 2024 13:18:17 +0200
Subject: [PATCH] =?UTF-8?q?Synchronisation=20en=20multiple=20commits=20si?=
 =?UTF-8?q?=20n=C3=A9cessaire=20(#1967)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* schema

* sub batch sync
---
 app/models/communication/website.rb  |  1 +
 app/services/git/providers/github.rb | 18 ++++++++++++++++--
 db/schema.rb                         |  2 +-
 3 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index a63a74ab7..ee934d3ae 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -43,6 +43,7 @@
 #  updated_at              :datetime         not null
 #  about_id                :uuid             indexed => [about_type]
 #  default_language_id     :uuid             not null, indexed
+#  locked_by_job_id        :uuid
 #  university_id           :uuid             not null, indexed
 #
 # Indexes
diff --git a/app/services/git/providers/github.rb b/app/services/git/providers/github.rb
index 3a3ee2d9d..1f94ba556 100644
--- a/app/services/git/providers/github.rb
+++ b/app/services/git/providers/github.rb
@@ -1,5 +1,6 @@
 class Git::Providers::Github < Git::Providers::Abstract
   BASE_URL = "https://github.com".freeze
+  COMMIT_BATCH_SIZE = 250
 
   def url
     "#{BASE_URL}/#{repository}"
@@ -67,8 +68,7 @@ class Git::Providers::Github < Git::Providers::Abstract
 
   def push(commit_message)
     return if !valid? || batch.empty?
-    new_tree = client.create_tree repository, batch, base_tree: tree[:sha]
-    commit = client.create_commit repository, commit_message, new_tree[:sha], branch_sha
+    commit = create_commit_from_batch(batch, commit_message)
     client.update_branch repository, default_branch, commit[:sha]
     # The repo changed, invalidate the tree
     @tree = nil
@@ -77,6 +77,20 @@ class Git::Providers::Github < Git::Providers::Abstract
     true
   end
 
+  def create_commit_from_batch(batch, commit_message)
+    base_tree_sha = tree[:sha]
+    base_commit_sha = branch_sha
+    commit = nil
+    batch.each_slice(COMMIT_BATCH_SIZE) do |sub_batch|
+      puts "Creating commit with #{sub_batch.size} files."
+      new_tree = client.create_tree repository, sub_batch, base_tree: base_tree_sha
+      commit = client.create_commit repository, commit_message, new_tree[:sha], base_commit_sha
+      base_tree_sha = new_tree[:sha]
+      base_commit_sha = commit[:sha]
+    end
+    commit
+  end
+
   def computed_sha(string)
     # Git SHA-1 is calculated from the String "blob <length>\x00<contents>"
     # Source: https://alblue.bandlem.com/2011/08/git-tip-of-week-objects.html
diff --git a/db/schema.rb b/db/schema.rb
index c7cb71784..f0a3a045c 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -148,8 +148,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_05_31_065216) do
     t.datetime "updated_at", null: false
     t.string "title"
     t.boolean "published", default: true
-    t.uuid "heading_id"
     t.uuid "communication_website_id"
+    t.uuid "heading_id"
     t.string "migration_identifier"
     t.index ["about_type", "about_id"], name: "index_communication_website_blocks_on_about"
     t.index ["communication_website_id"], name: "index_communication_blocks_on_communication_website_id"
-- 
GitLab