自動化 仕様書

トリガー→条件→アクションエンジン

ステータス: Draft / 作成日: 2026-05-27 PR #8 — 依存: コア, コメント・アクティビティ, 通知・ウォッチャー


1. 概要

「X が起きたとき、(条件を満たせば)Y をする」をノーコードで設定できるルールエンジン。アクティビティ記録・通知基盤が整った後に実装する。


2. データモデル

automations

pub struct Model {
    pub id: Uuid,
    pub project_id: Uuid,
    pub name: String,
    pub trigger: Value,     // トリガー定義 JSONB
    pub conditions: Value,  // 追加条件 JSONB(AND 結合)
    pub actions: Value,     // アクション定義 JSONB(順番に実行)
    pub is_active: bool,
    pub created_by: Uuid,
    pub created_at: DateTimeUtc,
}
カラム 制約
id UUID PK
project_id UUID NOT NULL, FK→projects CASCADE
name VARCHAR(255) NOT NULL
trigger JSONB NOT NULL
conditions JSONB NOT NULL DEFAULT '[]'
actions JSONB NOT NULL
is_active BOOLEAN NOT NULL DEFAULT true
created_by UUID NOT NULL, FK→users
created_at TIMESTAMPTZ NOT NULL DEFAULT now()

automation_run_logs(無限ループ防止用)

カラム 制約 説明
task_id UUID NOT NULL, FK→tasks CASCADE  
automation_id UUID NOT NULL  
ran_at TIMESTAMPTZ NOT NULL DEFAULT now()  
INDEX(task_id, automation_id, ran_at DESC) 5 秒以内の再発火チェックに使用

3. JSONB スキーマ

トリガー(trigger

jsonc
// ステータスが特定の値に変わったとき
{ "event": "status_changed", "to_status_id": "uuid" }

// 担当者が追加されたとき
{ "event": "assignee_added" }

// 締切の N 日前(バッチジョブが毎日評価)
{ "event": "deadline_approaching", "days_before": 1 }

// 全サブタスクが完了したとき
{ "event": "subtask_all_done" }

// GitHub PR がマージされたとき(PR #9 実装後に有効)
{ "event": "github_pr_merged" }

// コメントが投稿されたとき
{ "event": "comment_added" }

条件(conditions

jsonc
// 優先度が high 以上
{ "field": "priority", "op": "in", "value": ["high", "critical", "critical_fire"] }

// 担当者が特定ユーザー
{ "field": "assignee_ids", "op": "contains", "value": "uuid" }

// ラベルが付いている
{ "field": "label_ids", "op": "contains", "value": "uuid" }

アクション(actions)— 配列、順番に実行

jsonc
[
  // ステータスを変更
  { "type": "set_status", "status_id": "uuid" },

  // ラベルを追加 / 削除
  { "type": "add_label", "label_id": "uuid" },
  { "type": "remove_label", "label_id": "uuid" },

  // 担当者を追加
  { "type": "assign_user", "user_id": "uuid", "role": "secondary" },

  // 進捗率を設定
  { "type": "set_progress", "value": 100 },

  // 担当者全員に通知
  { "type": "notify_assignees" },

  // コメントを自動投稿
  { "type": "post_comment", "body": "全サブタスクが完了しました。自動クローズします。" }
]

4. 実行フロー

1. イベント発生(タスク更新・コメント投稿等)
2. そのプロジェクトの is_active=true な全 automations を取得
3. trigger.event が一致するものを絞り込む
4. 無限ループ防止チェック:
   - automation_run_logs に同タスク×同automation で 5秒以内のレコードがあればスキップ
5. conditions を評価(全条件が true の場合のみ実行)
6. actions を順番に実行
7. automation_run_logs に記録
8. task_activities に automation_triggered イベントを記録

5. マイグレーション

CREATE TABLE automations (
    id UUID PRIMARY KEY,
    project_id UUID NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    trigger JSONB NOT NULL,
    conditions JSONB NOT NULL DEFAULT '[]',
    actions JSONB NOT NULL,
    is_active BOOLEAN NOT NULL DEFAULT true,
    created_by UUID NOT NULL REFERENCES users(id),
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE automation_run_logs (
    task_id UUID NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
    automation_id UUID NOT NULL REFERENCES automations(id) ON DELETE CASCADE,
    ran_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_automation_run_logs
    ON automation_run_logs(task_id, automation_id, ran_at DESC);

6. API

メソッド パス 説明
GET /automations 一覧
POST /automations 作成(テナントオーナーのみ)
PUT /automations/{id} 更新
DELETE /automations/{id} 削除
POST /automations/{id}/toggle 有効 / 無効切替

POST /automations リクエスト例(全サブタスク完了で親タスクを Done にする):

{
  "name": "全サブタスク完了 → 親を Done に",
  "trigger": { "event": "subtask_all_done" },
  "conditions": [],
  "actions": [
    { "type": "set_status", "status_id": "uuid-of-done" },
    { "type": "set_progress", "value": 100 }
  ]
}

7. フロントエンド(Phase B)

ページ

/tenants/{tid}/projects/{pid}/automations

Automation エディタ UI

┌─────────────────────────────────────────────────┐
│ 自動化ルール: 全サブタスク完了 → Done          [✓ 有効] │
├─────────────────────────────────────────────────┤
│ トリガー                                        │
│   [全サブタスクが完了したとき ▼]                │
│                                                 │
│ 条件(任意)                                    │
│   [+ 条件を追加]                                │
│                                                 │
│ アクション                                      │
│   1. ステータスを [Done ▼] に変更               │
│   2. 進捗率を [100%] に設定                     │
│   [+ アクションを追加]                          │
└─────────────────────────────────────────────────┘

コンポーネント

コンポーネント ファイル
AutomationPage pages/automations/+Page.vue
AutomationEditor components/automations/AutomationEditor.vue
TriggerSelector components/automations/TriggerSelector.vue
ConditionEditor components/automations/ConditionEditor.vue
ActionEditor components/automations/ActionEditor.vue