タスク管理データベースのワークスペースお引越し(4) : hkob の雑記録 (142)

はじめに

hkob の雑記録の第142回目は、タスク管理データベースのお引越しの4回目です。今日は Notion API のインテグレーションをコネクトし、calendar_to_sprint.rb スクリプトを新しいデータベース用に作り替えます。

インテグレーションの作成

新しいワークスペースではまだインテグレーションは作成していませんでした。Connections の Develop or manage integrations から作成します。

Connections

ここをクリックするとインテグレーションの管理ページに飛びます。今はテンプレートの管理もここで行うようになっています。そのためにはプロフィールの作成が必要なのですが、このワークスペースは完全にプライベートで公開予定がないので、プロフィールなどは作成しない予定です。ここで新しいインテグレーションを作成します。

Integration

Manage tasks というインテグレーションを作成しました。

Create `Manage tasks' integration

このワークスペースは一人でしか使わないので、Webhook は使わない予定です。機能については、全ての項目を取得・変更できるようにしておきます。

Functions for the integration

これにより、Notion の Connections 設定画面から integration token を取得できるようになります。

Copy integration token

ここでコピーした API_KEY は環境変数 AMBASSADOR_NOTION_API_KEY に設定しておきました。

Tasks に Week id を追加

Sprint に Week id property を作成したのと同様に Tasks にも Week id プロパティを作成します。Tasks は時間情報を含むので注意が必要です。Notion API で処理をする場合、UTC で計算されるために Time zone オフセット分の 9時間のズレが生じます。具体的には、月曜日の午前9時前に開始される朝一の授業がうまく設定できませんでした。このため、dateAdd で 9 時間加算してから formatDate しています。

Week id property in Tasks

数式は以下のようになっています。このため、アプリ上の表示やデータベースオートメーションでは日曜日の 15:00 から 24:00 までの間は次の曜日になってしまいます。この時間だけ気をつければいいので、これで運用しています。

prop("Date").dateAdd(9, "hours").formatDate("GGGG-WW")

インテグレーションの接続

Launchpad に全てのテーブルが置かれているので、Launchpad に Manage tasks を接続しておきます。

Connect Manage tasks integration

calendar_to_sprint.rb の修正

Next sprint の設定とカレンダータスクのスプリント設定を行う Ruby スクリプトを修正します。修正点は以下の通りです。

  1. インテグレーションキーの環境変数名 NOTION_API_KEYAMBASSADOR_NOTION_API_KEY
  2. Sprints データベースの database_id
  3. Tasks データベースの database_id
  4. Week id のプロパティ名 (以前は week_id としていました)

修正したスクリプトは以下の通りです。

#! /usr/bin/env ruby

require "date"
require "notion_ruby_mapping"
include NotionRubyMapping
NotionRubyMapping.configure { |c| c.token = ENV["AMBASSADOR_NOTION_API_KEY"] }

# https://www.notion.so/1f80ce2a652080839676c2f8dea9768c?v=1f80ce2a6520801ab36e000ca09eec16&pvs=4
SPRINT_DATABASE_ID = "1f80ce2a652080839676c2f8dea9768c"
sprint_db = Database.find SPRINT_DATABASE_ID
sp = sprint_db.properties

today = Date.today
cwday = today.cwday
monday = today + (8 - cwday)
sunday = monday + 6

next_sprint = sprint_db.query_database(sp["Sprint status"].filter_equals("Next")).first
nsdp = next_sprint.properties["Dates"]

next_sprint_week_key = if nsdp.start_date.nil?
                         nsdp.start_date = monday
                         nsdp.end_date = sunday
                         next_sprint.save
                         next_sprint.properties["Week id"].formula["string"]
                       else
                         next_sprint.properties["Week id"].formula["string"]
                       end
next_sprint_title = next_sprint.title
if next_sprint_week_key != next_sprint_title
  next_sprint.properties["Sprint name"].text_objects.rich_text_objects = next_sprint_week_key
  next_sprint.save
end

# https://www.notion.so/1f70ce2a652080db9569e58b8eb84e98?v=1f70ce2a6520803898c6000cba4ffa66&pvs=4
TASK_DATABASE_ID = "1f70ce2a652080db9569e58b8eb84e98"
task_db = Database.find TASK_DATABASE_ID
tp = task_db.properties
query = tp["Date"].filter_on_or_before(sunday)
                        .and(tp["Sprint"].filter_is_empty)
                        .ascending(tp["Date"])
checked_sprint = {next_sprint_week_key => next_sprint}
unsettled_tasks = task_db.query_database(query)
unsettled_tasks.each do |task|
  week_id = task.properties["Week id"].formula["string"]
  sprint = checked_sprint[week_id]
  unless sprint
    sprint = sprint_db.query_database(sp["Sprint name"].filter_equals(week_id)).first
    checked_sprint[week_id] = sprint
  end
  task.properties["Sprint"].relation = [sprint.id]
  print "set #{task.title} to #{sprint.title}\n"
  task.save
end

手動実行

スクリプトを修正したので、このスクリプトを手動で実行してみます。ダミーで作成された Task 群にスプリントが設定されたことがわかります。

> calendar_to_sprint.rb
set システム障害の調査 to 2025-21
set プロジェクト計画書の作成 to 2025-21
set 顧客ミーティングの準備 to 2025-21

Sprints database を確認すると Next sprint にタイトルと日付が設定されており、Map への接続も完了しています。

Sprints database

Tasks database の各タスクにも Sprint が設定されていることがわかります。

Tasks database

自動実行の設定

明日の朝から修正されたスクリプトが実行されるので、cron に設定したインテグレーションキーの名称と内容を修正しました。これで明日の朝からは新しいデータベース側でスクリプトが動作するはずです。

13 7 * * * /bin/bash -c 'export PATH=/opt/homebrew/bin:$PATH; eval "$(rbenv init -)"; env AMBASSADOR_NOTION_API_KEY=ここにキーを記載 ruby $HOME/bin/calendar_to_sprint.rb' >> cron.log

おわりに

今回は、calendar_to_sprint.rb を新しいワークスペース側に変更し、自動実行処理も修正しました。明日はタスクに関するオートメーションの実装を進めていきます。

hkob.notion.site