From a3a28756dc8533821b72e92fed3b8f6a69a3cb48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com> Date: Wed, 24 Nov 2021 15:03:40 +0100 Subject: [PATCH] #15: remove from github --- app/models/communication/website.rb | 2 +- app/models/communication/website/post.rb | 6 ++--- .../communication/website/with_media.rb | 5 ++++ .../website/with_publishable_objects.rb | 18 +++++++++++++ app/models/concerns/with_github.rb | 14 +++++++++-- .../concerns/with_publication_to_websites.rb | 9 +++++-- app/services/github.rb | 25 +++++++------------ 7 files changed, 55 insertions(+), 24 deletions(-) diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb index 91c21dd2c..31b134eff 100644 --- a/app/models/communication/website.rb +++ b/app/models/communication/website.rb @@ -106,6 +106,6 @@ class Communication::Website < ApplicationRecord end def github - @github ||= Github.with_site self + @github ||= Github.with_website self end end diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb index c16e0a74f..72053a06e 100644 --- a/app/models/communication/website/post.rb +++ b/app/models/communication/website/post.rb @@ -53,12 +53,12 @@ class Communication::Website::Post < ApplicationRecord foreign_key: 'communication_website_post_id', association_foreign_key: 'communication_website_category_id' - scope :ordered, -> { order(published_at: :desc, created_at: :desc) } - scope :recent, -> { order(published_at: :desc).limit(5) } - validates :title, presence: true validates :slug, uniqueness: { scope: :communication_website_id } + scope :ordered, -> { order(published_at: :desc, created_at: :desc) } + scope :recent, -> { order(published_at: :desc).limit(5) } + def github_path_generated "_posts/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html" end diff --git a/app/models/communication/website/with_media.rb b/app/models/communication/website/with_media.rb index 808ec780a..3b354cdfc 100644 --- a/app/models/communication/website/with_media.rb +++ b/app/models/communication/website/with_media.rb @@ -3,6 +3,7 @@ module Communication::Website::WithMedia included do after_save_commit :publish_media_to_github + after_destroy :remove_media_from_github end def active_storage_blobs @@ -15,4 +16,8 @@ module Communication::Website::WithMedia def publish_media_to_github active_storage_blobs.each { |blob| website.publish_blob(blob) } end + + def remove_media_from_github + active_storage_blobs.each { |blob| website.remove_blob(blob) } + end end diff --git a/app/models/communication/website/with_publishable_objects.rb b/app/models/communication/website/with_publishable_objects.rb index 52347ce3a..98bb2b20e 100644 --- a/app/models/communication/website/with_publishable_objects.rb +++ b/app/models/communication/website/with_publishable_objects.rb @@ -38,5 +38,23 @@ module Communication::Website::WithPublishableObjects end handle_asynchronously :publish_blob, queue: 'default' + def remove_object(object) + return unless github.valid? + if object.respond_to?(:github_path) + github_path = object.github_path + else + root_folder = "_#{object_model_name.pluralize.underscore}" + github_path = "#{root_folder}/#{object.id}.md" + end + github_commit_message = "[#{object_model_name}] Remove #{object.to_s}" + github.remove(github_path, github_commit_message) + end + + def remove_blob(blob) + return unless github.valid? + github_path = "_data/media/#{blob.id[0..1]}/#{blob.id}.yml" + github_commit_message = "[Medium] Remove ##{blob.id}" + github.remove(github_path, github_commit_message) + end end end diff --git a/app/models/concerns/with_github.rb b/app/models/concerns/with_github.rb index cc67a3b6d..ac068db7e 100644 --- a/app/models/concerns/with_github.rb +++ b/app/models/concerns/with_github.rb @@ -4,6 +4,7 @@ module WithGithub included do after_save_commit :publish_to_github after_touch :publish_to_github + after_destroy :remove_from_github end def force_publish! @@ -26,11 +27,15 @@ module WithGithub protected def github - @github ||= Github.with_site(website) + @github ||= Github.with_website(website) end def github_commit_message - "[#{self.class.name.demodulize}] Save #{ to_s }" + "[#{self.class.name.demodulize}] Save #{to_s}" + end + + def github_remove_commit_message + "[#{self.class.name.demodulize}] Remove #{to_s}" end def publish_to_github @@ -43,4 +48,9 @@ module WithGithub end end handle_asynchronously :publish_to_github, queue: 'default' + + def remove_from_github + return unless github.valid? + github.remove(github_path, github_remove_commit_message) + end end diff --git a/app/models/concerns/with_publication_to_websites.rb b/app/models/concerns/with_publication_to_websites.rb index f58d2d652..337e6bb0d 100644 --- a/app/models/concerns/with_publication_to_websites.rb +++ b/app/models/concerns/with_publication_to_websites.rb @@ -2,15 +2,20 @@ module WithPublicationToWebsites extend ActiveSupport::Concern included do - after_save_commit :publish_to_every_websites + after_save_commit :publish_to_every_website + after_destroy :remove_from_every_website end protected - def publish_to_every_websites + def publish_to_every_website websites.each { |website| website.publish_object(self) } end + def remove_from_every_website + websites.each { |website| website.remove_object(self) } + end + # You can define a `github_path` method to re-define where to save the object markdown. # Check Communication::Website#publish_object for the default value. diff --git a/app/services/github.rb b/app/services/github.rb index c9729ee03..84bb565b4 100644 --- a/app/services/github.rb +++ b/app/services/github.rb @@ -1,8 +1,8 @@ class Github attr_reader :access_token, :repository - def self.with_site(site) - new site&.access_token, site&.repository + def self.with_website(website) + new website&.access_token, website&.repository end def initialize(access_token, repository) @@ -35,20 +35,6 @@ class Github false end - def send_file(attachment, path) - return if repository.blank? - commit_message = "[file] Save #{ path }" - path_without_slash = path[1..-1] - client.create_contents repository, - path_without_slash, - commit_message, - attachment.download, - sha: file_sha(path) - true - rescue - false - end - def add_to_batch( path: nil, previous_path: nil, data:) @@ -84,6 +70,13 @@ class Github @tree = nil end + def remove(path, commit_message) + client.delete_contents repository, path, commit_message, file_sha(path) + true + rescue + false + end + def read_file_at(path) data = client.content repository, path: path Base64.decode64 data.content -- GitLab