From d0c3a39f68efbbbff8069227ae293738a8ee808f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Mon, 3 Jul 2023 15:23:57 +0200
Subject: [PATCH] update theme version

---
 .../website/with_git_repository.rb              |  5 +++++
 app/services/git/providers/abstract.rb          |  4 ++++
 app/services/git/providers/github.rb            | 17 +++++++++++++++++
 app/services/git/repository.rb                  |  5 +++++
 4 files changed, 31 insertions(+)

diff --git a/app/models/communication/website/with_git_repository.rb b/app/models/communication/website/with_git_repository.rb
index 36b964990..e78f7db89 100644
--- a/app/models/communication/website/with_git_repository.rb
+++ b/app/models/communication/website/with_git_repository.rb
@@ -49,6 +49,11 @@ module Communication::Website::WithGitRepository
     (saved_change_to_about_id? && about_id_before_last_save.present?) || language_was_removed
   end
 
+  def update_theme_version
+    git_repository.update_theme_version!
+  end
+  handle_asynchronously :update_theme_version, queue: :default
+
   protected
 
   def add_direct_source_to_sync(direct_source)
diff --git a/app/services/git/providers/abstract.rb b/app/services/git/providers/abstract.rb
index cd53c2d0d..ceb8a3423 100644
--- a/app/services/git/providers/abstract.rb
+++ b/app/services/git/providers/abstract.rb
@@ -28,6 +28,10 @@ class Git::Providers::Abstract
     raise NotImplementedError
   end
 
+  def update_theme
+    raise NotImplementedError
+  end
+
   def push(commit_message)
     raise NotImplementedError
   end
diff --git a/app/services/git/providers/github.rb b/app/services/git/providers/github.rb
index 407dc39a7..6dc2f4f80 100644
--- a/app/services/git/providers/github.rb
+++ b/app/services/git/providers/github.rb
@@ -1,5 +1,8 @@
 class Git::Providers::Github < Git::Providers::Abstract
   BASE_URL = "https://github.com".freeze
+  THEME_REPOSITORY = "noesya/osuny-hugo-theme-aaa".freeze
+  THEME_BRANCH = "main".freeze
+  THEME_PATH = "themes/osuny-hugo-theme-aaa".freeze
 
   def url
     "#{BASE_URL}/#{repository}"
@@ -44,6 +47,16 @@ class Git::Providers::Github < Git::Providers::Abstract
     }
   end
 
+  def update_theme
+    previous_theme_sha = git_sha(THEME_PATH)
+    batch << {
+      path: THEME_PATH,
+      mode: '160000',
+      type: 'commit',
+      sha: current_theme_sha
+    } if previous_theme_sha != current_theme_sha
+  end
+
   def push(commit_message)
     return if !valid? || batch.empty?
     new_tree = client.create_tree repository, batch, base_tree: tree[:sha]
@@ -91,6 +104,10 @@ class Git::Providers::Github < Git::Providers::Abstract
     @branch_sha ||= client.branch(repository, default_branch)[:commit][:sha]
   end
 
+  def current_theme_sha
+    @current_theme_sha ||= client.branch(THEME_REPOSITORY, THEME_BRANCH)[:commit][:sha]
+  end
+
   def tree_item_at_path(path)
     tree_items_by_path[path] if tree_items_by_path.has_key? path
   end
diff --git a/app/services/git/repository.rb b/app/services/git/repository.rb
index 5ccb56b74..9ee604fc4 100644
--- a/app/services/git/repository.rb
+++ b/app/services/git/repository.rb
@@ -26,6 +26,11 @@ class Git::Repository
     mark_as_synced if provider.push(commit_message)
   end
 
+  def update_theme_version!
+    provider.update_theme
+    provider.push("Updated osuny-hugo-theme-aaa version")
+  end
+
   # Based on content, with the provider's algorithm (sha1 or sha256)
   def computed_sha(string)
     provider.computed_sha(string)
-- 
GitLab