LoginSignup
19
11

More than 5 years have passed since last update.

esaからconfluenceへの移行

Posted at

ドキュメント管理ツールのesaをチームでトライアルで使ってみたのですが、「やはりconfluenceを使いたい」という声が挙がったため、移行のスクリプトを書いてみました。
シンプルなスクリプトですが、confluenceのドキュメントを読むのに多少時間がかかったので、共有します。
個人的にはesaだったのですが残念……………。

前準備

script

以下を実行します。コメントやタグなどの情報については移行していません。
名前空間をちゃんと移行したい場合は CATEGORY_TO_ANCESTOR_ID に手動で値を入力してください。

require 'faraday'
require 'json'
require 'redcarpet'

class Confluence
  def initialize
    @host = 'https://katsumanarisawa.atlassian.net'
    @conn = Faraday.new(url: @host) do |faraday|
      faraday.request :url_encoded # encode post params
      faraday.adapter :net_http # Net::HTTP
      faraday.basic_auth('katsuma.narisawa@gmail.com', 'your_api_key')
    end
  end

  def create_page(title, body, ancestor_id = nil)
    response = @conn.post do |req|
      req.url '/wiki/rest/api/content/'
      req.headers['Content-Type'] = 'application/json'
      req.body = {
        "type": 'page',
        "title": title,
        "space": { "key": 'ESA' },
        "body": {
          "storage": {
            "value": body,
            "representation": 'storage',
          },
        },
        "ancestors": [
          { "id": ancestor_id&.to_s }.compact,
        ],
      }.compact.to_json
    end
    print "#{@host}/wiki#{JSON.parse(response.body)['_links']['webui']}\n" if response.status == 200
  end

  def get_pages
    response = @conn.get do |req|
      req.url '/wiki/rest/api/content/'
      req.headers['Content-Type'] = 'application/json'
    end
    print response.body
  end

  def get_page(content_id)
    response = @conn.get do |req|
      req.url "/wiki/rest/api/content/#{content_id}"
      req.headers['Content-Type'] = 'application/json'
    end
    print response.body
  end
end

class Esa
  def initialize(path)
    File.open(path, 'r') do |file|
      @content = file.read
      metadata_raw, @body = @content.split("\n---\n")
      @metadata = metadata_raw.split("\n").reject { |x| x == '---' }.map { |x| result = x.split(': '); result.push('') if result.count == 1; result }.to_h
    end

    @markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, tables: true)
  end

  def title
    @metadata['title'].strip.tr('"', '')
  end

  def body_html
    @markdown.render(@body)
  end

  def category
    @metadata['category'].split('/')[0]
  end
end

CATEGORY_TO_ANCESTOR_ID = {
  "Category1": 225_837_057,
  "Category2": 225_771_557,
}.freeze

confl = Confluence.new

# initialize space
# CATEGORY_TO_ANCESTOR_ID.keys.each do |key|
#   confl.create_page(key, '')
# end

# create page
Dir.glob('./esa/**/*').each do |path|
  next unless path.match?('md$')
  p path
  esa = Esa.new(path)
  raise unless CATEGORY_TO_ANCESTOR_ID.has_key?(esa.category.to_sym)
  confl.create_page(esa.title, esa.body_html, CATEGORY_TO_ANCESTOR_ID[esa.category.to_sym])
end

# get page(sample)
# confl.get_page(225_804_363)

19
11
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
11