From 163e9465f77f1b8899785c570648dc44c7e83dbf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Fri, 17 Dec 2021 10:54:53 +0100
Subject: [PATCH] gh file unpublish

---
 app/models/communication/website/github_file.rb |  5 +++++
 app/models/concerns/with_github_files.rb        | 13 +++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/app/models/communication/website/github_file.rb b/app/models/communication/website/github_file.rb
index e596d5839..0fd31d159 100644
--- a/app/models/communication/website/github_file.rb
+++ b/app/models/communication/website/github_file.rb
@@ -35,6 +35,11 @@ class Communication::Website::GithubFile < ApplicationRecord
   end
   handle_asynchronously :publish, queue: 'default'
 
+  def unpublish
+    remove_from_github
+  end
+  handle_asynchronously :unpublish, queue: 'default'
+
   def add_to_batch(github)
     return unless valid_for_publication?
     github.add_to_batch github_params
diff --git a/app/models/concerns/with_github_files.rb b/app/models/concerns/with_github_files.rb
index 750f34cdd..ddc2b863e 100644
--- a/app/models/concerns/with_github_files.rb
+++ b/app/models/concerns/with_github_files.rb
@@ -6,6 +6,7 @@ module WithGithubFiles
 
     after_save :create_github_files
     after_save_commit :publish_github_files
+    after_save_commit :unpublish_github_files, if: :should_unpublish_github_files?
   end
 
   def force_publish!
@@ -54,6 +55,18 @@ module WithGithubFiles
     end
   end
 
+  def unpublish_github_files
+    list_of_websites.each do |website|
+      github_manifest.each do |manifest_item|
+        github_files.find_by(website: website, manifest_identifier: manifest_item[:identifier])&.unpublish
+      end
+    end
+  end
+
+  def should_unpublish_github_files?
+    respond_to?(:published?) && saved_change_to_published? && !published?
+  end
+
   def list_of_websites
     respond_to?(:websites) ? websites : [website]
   end
-- 
GitLab