Class: CollavreGithub::MarkdownSync::IncrementalSyncService
- Inherits:
-
Object
- Object
- CollavreGithub::MarkdownSync::IncrementalSyncService
- Defined in:
- app/services/collavre_github/markdown_sync/incremental_sync_service.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(repository_link:, push_payload:) ⇒ IncrementalSyncService
constructor
A new instance of IncrementalSyncService.
Constructor Details
#initialize(repository_link:, push_payload:) ⇒ IncrementalSyncService
Returns a new instance of IncrementalSyncService.
4 5 6 7 8 9 10 |
# File 'app/services/collavre_github/markdown_sync/incremental_sync_service.rb', line 4 def initialize(repository_link:, push_payload:) @link = repository_link @payload = push_payload @client = CollavreGithub::Client.new(repository_link.github_account) @repo = repository_link.repository_full_name @user = repository_link.creative.user end |
Instance Method Details
#call ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/services/collavre_github/markdown_sync/incremental_sync_service.rb', line 12 def call return unless @link.markdown_sync_enabled? return unless target_branch_push? root = @link.markdown_root_creative return unless root commits = @payload["commits"] || [] return if commits.empty? added_paths = [] modified_paths = [] removed_paths = [] commits.each do |commit| added_paths.concat(Array(commit["added"])) modified_paths.concat(Array(commit["modified"])) removed_paths.concat(Array(commit["removed"])) end # Only care about .md files added_paths = added_paths.uniq.select { |p| p.end_with?(".md") } modified_paths = modified_paths.uniq.select { |p| p.end_with?(".md") } removed_paths = removed_paths.uniq.select { |p| p.end_with?(".md") } # Remove from modified if also in added (new file) modified_paths -= added_paths return if added_paths.empty? && modified_paths.empty? && removed_paths.empty? branch = @link.markdown_sync_branch # Pre-fetch tree once for SHA lookups (avoid per-file API calls) @tree_cache = @client.tree(@repo, branch).each_with_object({}) do |entry, h| h[entry.path] = entry.sha end @synced_creatives = load_synced_creatives processor = ContentProcessor.new( client: @client, repo: @repo, branch: branch, path_to_creative_map: @synced_creatives ) created = [] removed_paths.each do |path| creative = @synced_creatives[path] next unless creative creative.archive! if creative.respond_to?(:archive!) @synced_creatives.delete(path) end modified_paths.each do |path| creative = @synced_creatives[path] next unless creative content = @client.file_content(@repo, path, ref: branch) next if content.blank? source = creative.data["source"].merge( "markdown" => content, "sha" => @tree_cache[path] ) source.delete("rendered_html") creative.skip_github_validation = true creative.update!(data: creative.data.merge("source" => source)) processed, blobs = processor.process(content, path) update_content_comment(creative, processed, blobs) end added_paths.each do |path| next if @synced_creatives[path] # idempotency: skip if already synced (redelivery/retry) parts = path.split("/") parts.pop parent, new_dirs = ensure_parent_directories(parts, root) created.concat(new_dirs) content = @client.file_content(@repo, path, ref: branch) next if content.blank? filename = path.split("/").last creative = Collavre::Creative.new( description: filename, parent: parent, user: @user, data: { "source" => { "type" => "github_markdown", "repo" => @repo, "path" => path, "sha" => @tree_cache[path], "markdown" => content, "repository_link_id" => @link.id } } ) creative.skip_github_validation = true creative.save! @synced_creatives[path] = creative processed, blobs = processor.process(content, path) comment = create_content_comment(creative, processed) attach_blobs(comment, blobs) if blobs.any? created << creative end resequence_affected_parents(created) @link.update!(last_synced_at: Time.current) Collavre::Creative::RealtimeBroadcastable.broadcast_batch_created(created) if created.any? end |