From d8638392ed5f71d493e329a2d90ee50c5c40cb7c Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Fri, 18 Feb 2022 18:41:56 +0100
Subject: [PATCH] gitlab proto

---
 app/models/communication/website.rb           |  2 +-
 app/models/communication/website/git_file.rb  | 34 +++++++++++++------
 app/services/git/providers/gitlab.rb          |  2 +-
 app/services/git/repository.rb                |  4 ++-
 ...ha256_to_communication_website_git_file.rb |  5 +++
 db/schema.rb                                  |  3 +-
 6 files changed, 35 insertions(+), 15 deletions(-)
 create mode 100644 db/migrate/20220218173530_add_previous_sha256_to_communication_website_git_file.rb

diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index f00c6def4..a5ff5c580 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -5,7 +5,7 @@
 #  id                                :uuid             not null, primary key
 #  about_type                        :string           indexed => [about_id]
 #  access_token                      :string
-#  git_provider                      :integer          default(0)
+#  git_provider                      :integer          default("github")
 #  name                              :string
 #  repository                        :string
 #  static_pathname_administrators    :string           default("administrators")
diff --git a/app/models/communication/website/git_file.rb b/app/models/communication/website/git_file.rb
index db09da8ad..155b8cb07 100644
--- a/app/models/communication/website/git_file.rb
+++ b/app/models/communication/website/git_file.rb
@@ -2,14 +2,15 @@
 #
 # Table name: communication_website_git_files
 #
-#  id            :uuid             not null, primary key
-#  about_type    :string           not null, indexed => [about_id]
-#  previous_path :string
-#  previous_sha  :string
-#  created_at    :datetime         not null
-#  updated_at    :datetime         not null
-#  about_id      :uuid             not null, indexed => [about_type]
-#  website_id    :uuid             not null, indexed
+#  id              :uuid             not null, primary key
+#  about_type      :string           not null, indexed => [about_id]
+#  previous_path   :string
+#  previous_sha    :string
+#  previous_sha256 :string
+#  created_at      :datetime         not null
+#  updated_at      :datetime         not null
+#  about_id        :uuid             not null, indexed => [about_type]
+#  website_id      :uuid             not null, indexed
 #
 # Indexes
 #
@@ -33,7 +34,7 @@ class Communication::Website::GitFile < ApplicationRecord
   end
 
   def synchronized_with_git?
-    git_sha == previous_sha
+    git_sha == previous_sha || git_sha == previous_sha256
   end
 
   def should_create?
@@ -48,8 +49,7 @@ class Communication::Website::GitFile < ApplicationRecord
   def should_update?
     !should_destroy? &&
     (
-      previous_path != path ||
-      previous_sha != sha
+      different_path || different_sha
     )
   end
 
@@ -61,12 +61,24 @@ class Communication::Website::GitFile < ApplicationRecord
     @path ||= about.git_path(website)&.gsub(/\/+/, '/')
   end
 
+  def different_path
+    previous_path != path
+  end
+
+  def different_sha
+    previous_sha != sha && previous_sha != sha256
+  end
+
   def sha
     # 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
     @sha ||= OpenSSL::Digest::SHA1.hexdigest "blob #{to_s.bytesize}\x00#{to_s}"
   end
 
+  def sha256
+    @sha256 ||= OpenSSL::Digest::SHA256.hexdigest to_s
+  end
+
   def to_s
     @to_s ||= ApplicationController.render(
       template: "admin/#{about.class.name.underscore.pluralize}/static",
diff --git a/app/services/git/providers/gitlab.rb b/app/services/git/providers/gitlab.rb
index efb7433d2..87c301233 100644
--- a/app/services/git/providers/gitlab.rb
+++ b/app/services/git/providers/gitlab.rb
@@ -8,7 +8,7 @@ class Git::Providers::Gitlab < Git::Providers::Abstract
   end
 
   def update_file(path, previous_path, content)
-    file = find_in_tree previous_path
+    file = find previous_path
     return if file.nil?
     if previous_path != path
       batch << {
diff --git a/app/services/git/repository.rb b/app/services/git/repository.rb
index 722c6e6ad..f5b1579b7 100644
--- a/app/services/git/repository.rb
+++ b/app/services/git/repository.rb
@@ -55,7 +55,9 @@ class Git::Repository
 
   def mark_as_synced
     git_files.each do |git_file|
-      git_file.update previous_path: git_file.path, previous_sha: git_file.sha
+      git_file.update previous_path: git_file.path,
+                      previous_sha: git_file.sha,
+                      previous_sha256: git_file.sha256
     end
   end
 end
diff --git a/db/migrate/20220218173530_add_previous_sha256_to_communication_website_git_file.rb b/db/migrate/20220218173530_add_previous_sha256_to_communication_website_git_file.rb
new file mode 100644
index 000000000..aff10cef6
--- /dev/null
+++ b/db/migrate/20220218173530_add_previous_sha256_to_communication_website_git_file.rb
@@ -0,0 +1,5 @@
+class AddPreviousSha256ToCommunicationWebsiteGitFile < ActiveRecord::Migration[6.1]
+  def change
+    add_column :communication_website_git_files, :previous_sha256, :string
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index f33a5da16..e07eaf67d 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2022_02_18_131148) do
+ActiveRecord::Schema.define(version: 2022_02_18_173530) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -129,6 +129,7 @@ ActiveRecord::Schema.define(version: 2022_02_18_131148) do
     t.datetime "created_at", precision: 6, null: false
     t.datetime "updated_at", precision: 6, null: false
     t.string "previous_sha"
+    t.string "previous_sha256"
     t.index ["about_type", "about_id"], name: "index_communication_website_github_files_on_about"
     t.index ["website_id"], name: "index_communication_website_git_files_on_website_id"
   end
-- 
GitLab