Class: CollavreGithub::MarkdownSync::InitialImportService

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre_github/markdown_sync/initial_import_service.rb

Constant Summary collapse

MAX_FILES =
200

Instance Method Summary collapse

Constructor Details

#initialize(repository_link:, user:) ⇒ InitialImportService

Returns a new instance of InitialImportService.



6
7
8
9
10
11
# File 'app/services/collavre_github/markdown_sync/initial_import_service.rb', line 6

def initialize(repository_link:, user:)
  @link = repository_link
  @user = user
  @client = CollavreGithub::Client.new(repository_link.)
  @repo = repository_link.repository_full_name
end

Instance Method Details

#callObject



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
# File 'app/services/collavre_github/markdown_sync/initial_import_service.rb', line 13

def call
  branch = resolve_branch
  tree_entries = @client.tree(@repo, branch)
  md_entries = tree_entries.select { |e| e.type == "blob" && e.path.end_with?(".md") }

  if md_entries.size > MAX_FILES
    Rails.logger.warn("[MarkdownSync] #{@repo}: #{md_entries.size} .md files found, limiting to #{MAX_FILES}")
    md_entries = md_entries.first(MAX_FILES)
  end

  # Archive existing root tree before reimport (pause/resume flow)
  root = @link.markdown_root_creative
  if root && root.archived_at.nil?
    root.archive!
  end

  parent_creative = @link.creative
  root_creative = create_root_creative(parent_creative)
  @link.update!(markdown_root_creative_id: root_creative.id, last_synced_at: Time.current)

  dir_map = { "" => root_creative }
  created = [ root_creative ]
  file_creatives = []

  if md_entries.empty?
    Collavre::Creative::RealtimeBroadcastable.broadcast_batch_created(created) if created.size > 1
    return created
  end

  md_entries.sort_by(&:path).each do |entry|
    parts = entry.path.split("/")
    filename = parts.pop

    parent = ensure_directories(parts, dir_map, root_creative, created)

    content = @client.file_content(@repo, entry.path, ref: branch)
    next if content.blank?

    creative = Collavre::Creative.new(
      description: filename,
      parent: parent,
      user: @user,
      data: {
        "source" => {
          "type" => "github_markdown",
          "repo" => @repo,
          "path" => entry.path,
          "sha" => entry.sha,
          "markdown" => content,
          "repository_link_id" => @link.id
        }
      }
    )
    creative.skip_github_validation = true
    creative.save!
    file_creatives << creative
    created << creative
  end

  # Second pass: resolve relative links and images now that all creatives exist
  path_map = build_path_map(created)
  processor = ContentProcessor.new(client: @client, repo: @repo, branch: branch, path_to_creative_map: path_map)

  file_creatives.each do |creative|
    raw_md = creative.data.dig("source", "markdown")
    next if raw_md.blank?

    processed, blobs = processor.process(raw_md, creative.data.dig("source", "path"))
    comment = create_content_comment(creative, processed)
    attach_blobs(comment, blobs) if blobs.any?
  end

  resequence_directories_first(created)
  Collavre::Creative::RealtimeBroadcastable.broadcast_batch_created(created) if created.size > 1
  created
end