Class: WikiPromoter::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/wiki_promoter/publisher.rb

Defined Under Namespace

Classes: PublishResult

Constant Summary collapse

SUPPORTED_EXTENSIONS =
%w[.png .jpg .jpeg .gif .svg .webp .ico .pdf .txt .csv .tsv .json .jsonld .xml .yml .yaml].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(docs_path:, wiki_repository: nil, wiki_deploy_token: nil, output_dir: File.join("tmp", "wiki-migration"), wiki_checkout: File.join("tmp", "wiki-checkout"), roadmap_path: "docs/2026-07-01-roadmap.md", update_home_page: true, source_repository: nil, wiki_clone_url: nil, wiki_branch: "master", force: false, branch: nil, entry_page_name: nil, github_host: "https://github.com", git_user_name: "github-actions[bot]", git_user_email: "github-actions[bot]@users.noreply.github.com", command_runner: nil, capture_runner: nil, input: $stdin, output: $stdout, interactive: nil) ⇒ Publisher

Returns a new instance of Publisher.



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
# File 'lib/wiki_promoter/publisher.rb', line 18

def initialize(
  docs_path:,
  wiki_repository: nil,
  wiki_deploy_token: nil,
  output_dir: File.join("tmp", "wiki-migration"),
  wiki_checkout: File.join("tmp", "wiki-checkout"),
  roadmap_path: "docs/2026-07-01-roadmap.md",
  update_home_page: true,
  source_repository: nil,
  wiki_clone_url: nil,
  wiki_branch: "master",
  force: false,
  branch: nil,
  entry_page_name: nil,
  github_host: "https://github.com",
  git_user_name: "github-actions[bot]",
  git_user_email: "github-actions[bot]@users.noreply.github.com",
  command_runner: nil,
  capture_runner: nil,
  input: $stdin,
  output: $stdout,
  interactive: nil
)
  @docs_path = docs_path
  @wiki_repository = wiki_repository ? normalize_wiki_repository(wiki_repository) : nil
  @wiki_deploy_token = wiki_deploy_token
  @output_dir = output_dir
  @wiki_checkout = wiki_checkout
  @roadmap_path = roadmap_path
  @update_home_page = update_home_page
  @source_repository = source_repository || (wiki_repository ? source_repository_from_wiki : nil)
  @wiki_clone_url = wiki_clone_url
  @wiki_branch = wiki_branch
  @force = force
  @branch = branch
  @github_host = (github_host.nil? || github_host.to_s.strip.empty?) ? "https://github.com" : github_host.to_s.sub(%r{/+\z}, "")
  @git_user_name = git_user_name
  @git_user_email = git_user_email
  @command_runner = command_runner
  @capture_runner = capture_runner
  @input = input
  @output = output
  @interactive = interactive
  @migrator = Migrator.new(docs_path, entry_page_name: entry_page_name)
  @entry_url = @source_repository ? build_entry_url : nil
end

Instance Attribute Details

#docs_pathObject (readonly)

Returns the value of attribute docs_path.



16
17
18
# File 'lib/wiki_promoter/publisher.rb', line 16

def docs_path
  @docs_path
end

#entry_urlObject (readonly)

Returns the value of attribute entry_url.



16
17
18
# File 'lib/wiki_promoter/publisher.rb', line 16

def entry_url
  @entry_url
end

#migratorObject (readonly)

Returns the value of attribute migrator.



16
17
18
# File 'lib/wiki_promoter/publisher.rb', line 16

def migrator
  @migrator
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



16
17
18
# File 'lib/wiki_promoter/publisher.rb', line 16

def output_dir
  @output_dir
end

#wiki_checkoutObject (readonly)

Returns the value of attribute wiki_checkout.



16
17
18
# File 'lib/wiki_promoter/publisher.rb', line 16

def wiki_checkout
  @wiki_checkout
end

Class Method Details

.blank?(value) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/wiki_promoter/publisher.rb', line 93

def self.blank?(value)
  value.nil? || value.to_s.strip.empty?
end

.from_env(docs_path) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/wiki_promoter/publisher.rb', line 65

def self.from_env(docs_path)
  new(
    docs_path: docs_path,
    wiki_repository: ENV["WIKI_REPOSITORY"] || ENV["WIKI_REPO"],
    wiki_deploy_token: ENV["WIKI_DEPLOY_TOKEN"],
    output_dir: ENV["WIKI_OUTPUT_DIR"] || File.join("tmp", "wiki-migration"),
    wiki_checkout: ENV["WIKI_CHECKOUT_DIR"] || File.join("tmp", "wiki-checkout"),
    roadmap_path: ENV["ROADMAP_PATH"] || "docs/2026-07-01-roadmap.md",
    update_home_page: truthy_env?("UPDATE_HOME_PAGE", default: true),
    source_repository: ENV["SOURCE_REPOSITORY"] || ENV["GITHUB_REPOSITORY"],
    wiki_clone_url: ENV["WIKI_CLONE_URL"],
    wiki_branch: ENV["WIKI_BRANCH"] || "master",
    force: ENV["WIKI_FORCE"] == "1" || ENV["WIKI_FORCE"] == "true",
    branch: ENV["GITHUB_REF_NAME"],
    entry_page_name: ENV["ENTRY_PAGE_NAME"].then { |value| blank?(value) ? nil : value },
    github_host: ENV["GITHUB_SERVER_URL"] || "https://github.com",
    git_user_name: ENV["GIT_USER_NAME"] || "github-actions[bot]",
    git_user_email: ENV["GIT_USER_EMAIL"] || "github-actions[bot]@users.noreply.github.com"
  )
end

.truthy_env?(name, default:) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
# File 'lib/wiki_promoter/publisher.rb', line 86

def self.truthy_env?(name, default:)
  value = ENV[name]
  return default if blank?(value)

  !%w[0 false no off].include?(value.downcase)
end

Instance Method Details

#migrateObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/wiki_promoter/publisher.rb', line 97

def migrate
  FileUtils.rm_rf(output_dir)
  FileUtils.mkdir_p(output_dir)

  validate_non_markdown_files!

  pages = migrator.pages
  pages.each do |filename, content|
    File.write(File.join(output_dir, filename), content)
  end

  non_markdown_files.each do |file|
    rel = relative_path(file)
    target = File.join(output_dir, rel)
    FileUtils.mkdir_p(File.dirname(target))
    FileUtils.cp(file, target)
  end

  pages
end

#publishObject

Raises:

  • (ArgumentError)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/wiki_promoter/publisher.rb', line 118

def publish
  raise ArgumentError, "wiki_repository required to publish" if blank?(@wiki_repository)
  raise ArgumentError, "wiki_deploy_token required to publish" if blank?(@wiki_deploy_token)

  # Prevent git from prompting for credentials on a local TTY and hanging.
  ENV["GIT_TERMINAL_PROMPT"] = "0"

  pages = migrate
  configure_git(".")
  clone_or_update_wiki
  configure_git(wiki_checkout)
  copy_all_files_to_wiki
  add_home_index_entry if @update_home_page
  # Prove we can push the source-branch cleanup before we make the
  # irreversible wiki push, so a permissions/connectivity problem aborts
  # while both remotes are still untouched rather than after the wiki is
  # updated but the branch cleanup can't land.
  verify_source_push_access if Dir.exist?(docs_path)
  commit_and_push_wiki
  cleanup_source_branch

  PublishResult.new(wiki_url: entry_url, pages: pages)
end