LoginSignup
1
1

More than 1 year has passed since last update.

Confluenceで定例ページを毎週自動でコピーする

Last updated at Posted at 2023-03-15

概要

毎週の定例ページをコピーして作るのめんどいので自動化する

image.png

処理の流れ

  • Amazon Event Bridge
    • 日時を決めてLambdaを実行する
  • Lambda
    • コピー元のページの特定(毎回同じテンプレページをコピーするなら不要)
      • 親ページのIDを指定して子ページ一覧取得APIを実行
      • タイトルで昇順にして一番最後のページが先週のページ
        • 定例ページの命名規則による
      • 先週のページのIDを特定する
    • コピーして新ページ作成
      • 先週のページのIDとどこのページの配下に作るかを指定してコピーする

コード

# frozen_string_literal: true

require 'faraday'
require 'json'


DOMAIN = 'https://buysell-tech.atlassian.net'
PARENT_PAGE_ID = '12345'
# https://developer.atlassian.com/cloud/confluence/rest/v1/api-group-content---children-and-descendants/#api-wiki-rest-api-content-id-descendant-get
DESCENDANT_INDEX_PATH = "/wiki/rest/api/content/#{PARENT_PAGE_ID}/descendant/page"

def lambda_handler(event:, context:)
  descendant_response = faraday(DOMAIN).get(DESCENDANT_INDEX_PATH) do |request|
    request.headers['Content-type'] = 'application/json'
    request.headers['Accept'] = 'application/json;charset=UTF-8'
  end

  descendant_results = JSON.parse(descendant_response.body)['results']
  last_week_page_id = descendant_results.sort_by { |page| page['title'] }.last['id']

  # https://developer.atlassian.com/cloud/confluence/rest/v1/api-group-content---children-and-descendants/#api-wiki-rest-api-content-id-copy-post
  copy_url_path = "/wiki/rest/api/content/#{last_week_page_id}/copy"

  response = faraday(DOMAIN).post(copy_url_path) do |request|
    request.body = {
      copyAttachments: true,
      copyPermissions: true,
      copyProperties: true,
      copyLabels: true,
      copyCustomContents: true,
      destination: {
        type: 'parent_page',
        value: PARENT_PAGE_ID
      },
      pageTitle: "#{(Date.today + 7).to_s}定例"
    }.to_json

    request.headers['Content-type'] = 'application/json'
    request.headers['Accept'] = 'application/json;charset=UTF-8'
  end

  log = Logger.new(STDOUT)
  log.info("response | status_code: #{response.status}, body: #{response.body}")
end

def faraday(service_url)
  Faraday.new(url: service_url) do |builder|
    builder.request :json
    builder.request :authorization, :basic, 'test@example.com', '<API TOKEN>'

    builder.options[:open_timeout] = 30
    builder.options[:timeout] = 120
  end
end
1
1
0

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
1
1