Commit bae1045
authored
## Summary
Issue #283 の実装 stack **PR 2/7** — query / mutation / Octokit 解決層を複数 installation 対応にする。アプリケーションの動作はまだ変えない(fallback で互換維持)。
設計根拠: [`docs/rdd/issue-283-multiple-github-accounts.md`](./docs/rdd/issue-283-multiple-github-accounts.md)
作業計画: [`docs/rdd/issue-283-work-plan.md`](./docs/rdd/issue-283-work-plan.md)
依存: #288 (PR 1: schema)
## 変更内容
### query 層 (`app/services/github-integration-queries.server.ts`)
- `getGithubAppLinks(orgId)` 配列返却(ORDER BY createdAt ASC で決定的順序)
- `getGithubAppLinkByInstallationId(installationId)` 追加
- `assertInstallationBelongsToOrg(orgId, installationId)` 追加 — クライアント由来の `installationId` をサーバ側で検証する境界 guard
- `getGithubAppLink()` は最古の active link を返す互換 shim(`@deprecated`)
### mutation 層 (`app/services/github-app-mutations.server.ts`)
- `disconnectGithubAppLink(orgId, installationId)` 追加 — 単一 installation の soft-delete + 最後の active を失った時のみ `method='token'` に戻す + audit log 書き込み
- 1 transaction 内で完結(idempotent)
- `disconnectGithubApp()` は legacy UI 互換 wrapper として 1 transaction で全 link 一括 soft-delete に書き換え(`@deprecated`)
### audit log writer (`app/services/github-app-link-events.server.ts`) 新規追加
- `logGithubAppLinkEvent()` helper
- event_type / source / status の string union 型を export
- `Kysely<DB> | Transaction<DB>` を受け取り、呼び出し側のトランザクションに乗せられる
- PR 1 で追加した `github_app_link_events` table の **初回 writer**(disconnect 経由)
### Octokit 解決 (`app/services/github-octokit.server.ts`)
- `resolveOctokitForInstallation(installationId)` 追加
- `resolveOctokitForRepository({ integration, githubAppLinks, repository })` 追加 — repository ごとの解決
- `repository.githubInstallationId` がセットされている場合は厳密にそれを使う(suspended は弾く)
- `github_app` モードで未割当の repository に対する移行期間 fallback:
- active link 1 件 → そのまま使う
- 0 件 → エラー(**PAT 自動 fallback はしない**、RDD ルール)
- 2+ 件 → エラー(曖昧、明示的な assignment が必要)
- `token` モード: `privateToken` があれば PAT、無ければ未接続エラー
- `IntegrationForOctokit.method` を `'token' | 'github_app' | (string & {})` のユニオンに型を絞る
- `resolveOctokitFromOrg()` は legacy 互換 wrapper(`@deprecated`、PR 4 で削除予定)
### batch shape 更新 (`batch/db/queries.ts`)
- `getGithubAppLinkByOrgId` → `getGithubAppLinksByOrgId`(配列返却)
- `getAllGithubAppLinks` を `Map.groupBy` で書き換え
- `getOrganization()` / `listAllOrganizations()` が `githubAppLinks: []` を返すよう変更
### crawl / backfill ジョブ (`app/services/jobs/{crawl,backfill}.server.ts`)
- 単一 Octokit 共有から per-repository 解決に変更
- `load-organization` step 内で `github_app + active 0` / `token + privateToken null` の早期エラー検出
- repository ループ内で `resolveOctokitForRepository()` を呼び、解決失敗時は warn ログ + skip(crawl 全体は止めない)
### tsconfig
- `lib` を `ES2024` に bump(`Map.groupBy` を使うため)
### tests
- `app/services/github-octokit.server.test.ts` に `resolveOctokitForRepository` の 11 ケース追加
- 明示 installation id (一致 / 不一致 / suspended)
- 移行期間 fallback (active 1 / 0 / 2+ / suspended 除外)
- token モード (PAT あり / なし)
- integration null
## 満たす受入条件
- **#6**: `crawl.server.ts` / `backfill.server.ts` が repository ごとに対応 installation の Octokit を使う
## Stack 位置
```text
PR 1 (#288): schema
└ [PR 2: query/octokit] ← this PR
└ PR 3 (webhook/membership)
└ PR 4 (UI)
└ PR 5 (repo UI)
└ PR 6 (backfill)
└ PR 7 (strict)
```
## 後続 PR への影響
- PR 3: webhook handler 群がここで追加した `getGithubAppLinkByInstallationId` / `logGithubAppLinkEvent` / canonical reassignment helper(PR 3 で実装)を使う
- PR 4: UI loader / action から `getGithubAppLink()` を `getGithubAppLinks()` に移行 + `assertInstallationBelongsToOrg` を loader 境界で呼ぶ
- PR 7: 移行期間 fallback (`activeLinks.length === 1` 分岐) を削除し、`github_installation_id IS NULL` を strict エラーにする
## テスト
- [x] \`pnpm validate\` (lint / format / typecheck / build / test 全 331 tests)
- [x] \`resolveOctokitForRepository\` の主要 11 ケースをユニットテストで検証
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit
## リリースノート
* **新機能**
* GitHub Appのリンク接続・切断イベントに対する監査ログ機能を追加しました。
* **バグ修正**
* リポジトリごとのGitHub認証解決ロジックを改善し、複数のアクティブなリンク存在時のエラーハンドリングを強化しました。
* GitHub Appの削除状態をより厳密に追跡するようにしました。
* **改善**
* GitHub統合に関する検証とエラー処理を堅牢化しました。
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 386b835 commit bae1045
9 files changed
Lines changed: 539 additions & 67 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | | - | |
7 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
8 | 15 | | |
9 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
10 | 20 | | |
| 21 | + | |
11 | 22 | | |
12 | | - | |
| 23 | + | |
13 | 24 | | |
14 | 25 | | |
15 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
16 | 83 | | |
17 | 84 | | |
18 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
19 | 95 | | |
20 | 96 | | |
21 | 97 | | |
22 | 98 | | |
23 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
24 | 114 | | |
| 115 | + | |
25 | 116 | | |
26 | 117 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
4 | 15 | | |
5 | 16 | | |
6 | 17 | | |
| |||
9 | 20 | | |
10 | 21 | | |
11 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
12 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
13 | 51 | | |
14 | 52 | | |
15 | 53 | | |
16 | | - | |
17 | | - | |
| 54 | + | |
| 55 | + | |
18 | 56 | | |
19 | 57 | | |
20 | 58 | | |
21 | 59 | | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
0 commit comments