Skip to content

feat: マルチテナント対応 — テナント名ログイン & プロフィル連携#95

Merged
miya0001 merged 2 commits into
mainfrom
feat/multi-tenant-profile
Apr 3, 2026
Merged

feat: マルチテナント対応 — テナント名ログイン & プロフィル連携#95
miya0001 merged 2 commits into
mainfrom
feat/multi-tenant-profile

Conversation

@miya0001

@miya0001 miya0001 commented Apr 3, 2026

Copy link
Copy Markdown
Member

Summary

Closes #90 (Phase 1 残り + Phase 2)

  • -s <テナント名> でログイン対応: geonic auth login -s demo_smartcityavailableTenants から名前 or tenantId をマッチしてテナントスコープログイン。マッチしなければ利用可能なテナント一覧をエラー表示
  • tenantId / availableTenants を config に保存: GdbConfig に明示的フィールド追加。service (NGSILD-Tenant ヘッダー) との意味的分離
  • profile use 時のトークン自動リフレッシュ: プロフィル切り替え時にトークンが期限切れ/間近なら refreshToken で自動更新。テナント名を成功メッセージに表示
  • profile show でテナント一覧表示: availableTenants を整形表示、現在のテナントに ← current マーカー
  • テナント選択プロンプト改善: TenantChoicename フィールド追加、名前 (tenantId) [role] 形式で表示

Phase 3 (auth switch) は profile use でカバーできるため不要と判断し、スコープ外とした。

Test plan

  • npm run lint — パス
  • npm run typecheck — パス
  • npm test — 684/684 パス(新規テスト 11 件追加)
  • geonic auth login で複数テナント所属ユーザーのインタラクティブ選択を確認
  • geonic auth login -s <テナント名> でプロンプトなしにテナント指定ログインを確認
  • geonic profile showavailableTenants 一覧とカレントマーカーを確認
  • geonic profile use <name> で期限切れトークンの自動リフレッシュを確認

Summary by CodeRabbit

  • 新機能
    • マルチテナント対応のログインを追加。-s <テナント名>で名前指定またはID指定が可能になりました。
    • 選択されたテナント名/IDと利用可能テナント一覧が設定に保存されます。
    • プロファイル切替時に期限切れのアクセストークンを自動で更新する処理を追加。
    • プロファイル表示でテナント一覧と現在のテナントが見やすく表示されます。
  • テスト
    • マルチテナント挙動とプロファイル切替の自動更新を検証するテストを追加。

- auth login で --service (-s) によるテナント名指定に対応
  availableTenants から name/tenantId をマッチして解決
- GdbConfig に tenantId / availableTenants フィールドを追加
  service との意味的分離で config の可読性を向上
- profile use 時にトークンが期限切れなら refreshToken で自動リフレッシュ
- profile show で availableTenants を一覧表示(current マーカー付き)
- TenantChoice に name フィールド追加、選択プロンプトで名前表示に対応
@coderabbitai

coderabbitai Bot commented Apr 3, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bab2b4ca-b38e-41ab-abf6-8f82bb7bbec7

📥 Commits

Reviewing files that changed from the base of the PR and between 1bb94a3 and b0e1e9c.

📒 Files selected for processing (1)
  • CHANGELOG.md

📝 Walkthrough

Walkthrough

このプルリクエストはマルチテナント対応を追加します。ログインでテナント名指定(-s <テナント名>)またはID指定を扱い、選択されたtenantIdavailableTenantsを設定に保存し、profile useでトークンの自動更新を行います。

Changes

Cohort / File(s) Summary
型・設定
src/types.ts, src/config.ts
TenantInfoを追加。GdbConfigtenantId?availableTenants?を追加。v1→v2マイグレーションにtenantIdを含める。
プロンプト表示
src/prompt.ts
TenantChoicename?: stringを追加。テナント一覧表示で名前があれば併記し、ロール表記を[role]形式に変更。
認証フロー
src/commands/auth.ts
ログイン時に--tenant-id優先化、--serviceで名前/ID照合によるテナント解決を追加。解決済みテナントで再ログイン(skipTenantHeader: true)、config.tenantIdconfig.availableTenantsの保存/削除ロジックを更新。成功メッセージにテナントラベルを付加。
プロファイル管理
src/commands/profile.ts
profile useを非同期化し、切替時にgetTokenStatusで期限を判定して必要なら/auth/refreshでトークン更新して保存。profile showavailableTenantsを整形表示(現在のテナントにマーク)。
テスト
tests/auth.test.ts, tests/commands-profile.test.ts
マルチテナント関連のユニット追加・拡張:テナント解決、設定永続化、再ログイン挙動、profile useの自動リフレッシュ成功/失敗ケース、profile showの表示検証。
ドキュメント
CHANGELOG.md
Unreleased(2026-04-03)にマルチテナント対応の記載を追加。

Sequence Diagram(s)

sequenceDiagram
    participant User as ユーザー
    participant CLI as CLI (auth.ts)
    participant Server as Server (/auth/login)
    participant Config as Config
    User->>CLI: geonic auth login
    CLI->>Server: POST /auth/login (email,password)
    Server-->>CLI: tokens + availableTenants[]
    alt 複数テナント
        CLI->>User: テナント選択プロンプト
        User->>CLI: テナント選択
        CLI->>Server: POST /auth/login (email,password,tenantId)
        Server-->>CLI: 選択テナントのトークン
    else 単一テナントまたは解決済み
        CLI->>Server: (必要なら)POST /auth/login (tenantId)
        Server-->>CLI: トークン
    end
    CLI->>Config: tenantId, availableTenants を保存
    CLI-->>User: ✓ <tenant label> でログインしました
Loading
sequenceDiagram
    participant User as ユーザー
    participant CLI as CLI (profile.ts)
    participant Config as Config
    participant Server as Server (/auth/refresh)
    User->>CLI: geonic profile use <name>
    CLI->>Config: プロファイル読み込み
    Config-->>CLI: {token, refreshToken, tenantId, url,...}
    CLI->>CLI: getTokenStatus(token)
    alt トークン期限切れ/間近
        CLI->>Server: POST /auth/refresh (refreshToken)
        Server-->>CLI: 新 accessToken (+refreshToken?)
        CLI->>Config: 新トークンを保存 (saveConfig)
        CLI-->>User: ✓ Switched to <name>\n✓ Token refreshed
    else トークン有効
        CLI-->>User: ✓ Switched to <name>
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Poem

🐰 ウサギの小道しるべ
ログインで庭の名前が見えるよ
どの家へ行くか選べばいいよ
プロフィル乗り換えで鍵も新しくなる
ピョンと跳んで、また次のテナントへ 🌿

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed プルリクエストのタイトルは、マルチテナント対応、テナント名ログイン、プロフィル連携という主要な変更を正確に要約しており、変更内容と完全に関連しています。
Linked Issues check ✅ Passed 実装は #90 で指定された Phase 1(ログイン時のテナント選択)と Phase 2(プロフィルへのテナント情報紐付け)のコード要件をすべて満たしており、自動トークンリフレッシュ、テナント名による解決、availableTenants の保存と表示に対応しています。
Out of Scope Changes check ✅ Passed すべての変更は #90 の Phase 1/2 の要件の範囲内であり、Phase 3(auth switch コマンド)は意図的に範囲外とされており、無関係な変更はありません。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/multi-tenant-profile

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/types.ts (1)

3-7: TenantInfoTenantChoice の型重複について

src/prompt.tsTenantChoice インターフェースと構造が同一です(tenantId, name?, role)。現状は動作に問題ありませんが、将来的にどちらかの型が変更された場合、不整合が発生するリスクがあります。

共通の型を定義して両方で再利用することを検討してください。例えば TenantChoiceTenantInfo のエイリアスにするか、TenantInfosrc/prompt.ts でインポートして使用する方法があります。

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/types.ts` around lines 3 - 7, TenantInfo and TenantChoice duplicate the
same shape; pick one canonical type and reuse it to avoid drift: remove the
duplicate TenantChoice declaration and update the code that referenced
TenantChoice (e.g., in prompt.ts) to import and use TenantInfo, or conversely
make TenantChoice a type alias of TenantInfo (export type TenantChoice =
TenantInfo) and import that alias where needed; ensure all references to
TenantChoice/TenantInfo compile and update any exports so only the shared type
is maintained.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/types.ts`:
- Around line 3-7: TenantInfo and TenantChoice duplicate the same shape; pick
one canonical type and reuse it to avoid drift: remove the duplicate
TenantChoice declaration and update the code that referenced TenantChoice (e.g.,
in prompt.ts) to import and use TenantInfo, or conversely make TenantChoice a
type alias of TenantInfo (export type TenantChoice = TenantInfo) and import that
alias where needed; ensure all references to TenantChoice/TenantInfo compile and
update any exports so only the shared type is maintained.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b50d66e3-bb63-4871-ac57-bb2c719651f4

📥 Commits

Reviewing files that changed from the base of the PR and between a1071c9 and 1bb94a3.

📒 Files selected for processing (8)
  • CHANGELOG.md
  • src/commands/auth.ts
  • src/commands/profile.ts
  • src/config.ts
  • src/prompt.ts
  • src/types.ts
  • tests/auth.test.ts
  • tests/commands-profile.test.ts

@miya0001
miya0001 merged commit 53ab96f into main Apr 3, 2026
6 of 7 checks passed
@miya0001
miya0001 deleted the feat/multi-tenant-profile branch April 3, 2026 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

マルチテナント対応: ログイン時のテナント選択 & プロフィル連携

1 participant