From 0428e0867b274ae0f2546b57f9ee90cc2dd16db5 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Mon, 25 Oct 2021 08:57:08 +0200
Subject: [PATCH] batch posts

---
 app/models/communication/website/imported/post.rb    | 2 +-
 app/models/communication/website/imported/website.rb | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/app/models/communication/website/imported/post.rb b/app/models/communication/website/imported/post.rb
index b743fad87..37787a1e6 100644
--- a/app/models/communication/website/imported/post.rb
+++ b/app/models/communication/website/imported/post.rb
@@ -107,8 +107,8 @@ class Communication::Website::Imported::Post < ApplicationRecord
       http.use_ssl = true
       http.verify_mode = OpenSSL::SSL::VERIFY_NONE
       request = Net::HTTP::Get.new(uri.request_uri)
-      response = http.request(request)
       begin
+        response = http.request(request)
         post.featured_image.attach(io: StringIO.new(response.body), filename: filename, content_type: 'image/jpeg')
       rescue
       end
diff --git a/app/models/communication/website/imported/website.rb b/app/models/communication/website/imported/website.rb
index dff61d59b..6765db2e4 100644
--- a/app/models/communication/website/imported/website.rb
+++ b/app/models/communication/website/imported/website.rb
@@ -79,10 +79,18 @@ class Communication::Website::Imported::Website < ApplicationRecord
   end
 
   def sync_posts
+    Communication::Website::Post.skip_callback(:save, :after, :publish_to_github)
+    github = Github.with_site website
     wordpress.posts.each do |data|
       post = posts.where(university: university, identifier: data['id']).first_or_initialize
       post.data = data
       post.save
+      generated_post = post.post
+      github.add_to_batch path: generated_post.github_path_generated,
+                          previous_path: generated_post.github_path,
+                          data: generated_post.to_jekyll
     end
+    github.commit_batch '[Post] Batch update from import'
+    Communication::Website::Post.set_callback(:save, :after, :publish_to_github)
   end
 end
-- 
GitLab