Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

esaからconfluenceへの移行

More than 1 year has passed since last update.

ドキュメント管理ツールの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)

nullnull
フリーランスのエンジニア、たまに言語処理屋、ときどきフォトグラファー。 RubyとVue.jsが好き。最近は関数型も好き。 https://twitter.com/KatsumaNarisawa
https://katsumanarisawa.me/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away