diff --git a/claude.md b/claude.md index f0dabb0..65fa358 100644 --- a/claude.md +++ b/claude.md @@ -89,6 +89,11 @@ API Key(クエリパラメータ)と OAuth 2.0(Bearer トークン)の 2. `BACKLOG_SPACE` 環境変数 3. 設定ファイルの `defaultSpace` +プロジェクト解決の優先順位: +1. コマンドローカルの `--project` / `-p` フラグ +2. `BACKLOG_PROJECT` 環境変数 +3. インタラクティブプロンプト(`issue create` 等の一部コマンド) + ## 設計原則 ### 名前解決 @@ -168,8 +173,15 @@ const name = await promptRequired("Project name:", args.name); プロジェクトが必要だが指定されていない場合の解決順: 1. コマンドローカルの `--project` / `-p` フラグ -2. Git リモート URL からの推測(Backlog Git のリモートが設定されている場合) -3. インタラクティブ選択(TTY 接続時) +2. `BACKLOG_PROJECT` 環境変数 +3. インタラクティブ選択(`issue create` 等の一部コマンド、TTY 接続時) + +プロジェクト解決のユーティリティ: + +- `resolveProjectArg(argValue?)` — 必須プロジェクトの解決(引数 → 環境変数 → エラー終了) + - 37 の必須プロジェクトコマンドで使用 +- 任意プロジェクトのコマンド(`issue list`, `issue create`, `browse`)では + `args.project || process.env.BACKLOG_PROJECT` で個別にフォールバック ### エラーハンドリング diff --git a/docs/src/content/docs/commands/browse.md b/docs/src/content/docs/commands/browse.md index 53fb8b1..23960e4 100644 --- a/docs/src/content/docs/commands/browse.md +++ b/docs/src/content/docs/commands/browse.md @@ -19,7 +19,7 @@ Backlog のページをデフォルトブラウザで開きます。 | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--issues` | | boolean | 課題一覧を開く | | `--wiki` | | boolean | Wiki を開く | | `--git` | | boolean | Git リポジトリページを開く | diff --git a/docs/src/content/docs/commands/category/create.md b/docs/src/content/docs/commands/category/create.md index 939e0f2..cfd81b2 100644 --- a/docs/src/content/docs/commands/category/create.md +++ b/docs/src/content/docs/commands/category/create.md @@ -11,7 +11,7 @@ backlog category create [flags] | フラグ | 短縮 | 型 | 必須 | 説明 | |--------|------|------|------|------| -| `--project` | `-p` | string | Yes | プロジェクトキー | +| `--project` | `-p` | string | Yes | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--name` | `-n` | string | Yes | カテゴリ名 | ## 使用例 diff --git a/docs/src/content/docs/commands/category/delete.md b/docs/src/content/docs/commands/category/delete.md index b20855d..2aa0004 100644 --- a/docs/src/content/docs/commands/category/delete.md +++ b/docs/src/content/docs/commands/category/delete.md @@ -17,7 +17,7 @@ backlog category delete [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--confirm` | | boolean | 確認プロンプトをスキップ | ## 使用例 diff --git a/docs/src/content/docs/commands/category/edit.md b/docs/src/content/docs/commands/category/edit.md index 46fc928..75b163c 100644 --- a/docs/src/content/docs/commands/category/edit.md +++ b/docs/src/content/docs/commands/category/edit.md @@ -17,7 +17,7 @@ backlog category edit [flags] | フラグ | 短縮 | 型 | 必須 | 説明 | |--------|------|------|------|------| -| `--project` | `-p` | string | Yes | プロジェクトキー | +| `--project` | `-p` | string | Yes | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--name` | `-n` | string | Yes | カテゴリ名 | ## 使用例 diff --git a/docs/src/content/docs/commands/category/list.md b/docs/src/content/docs/commands/category/list.md index b5dbe09..53f9f2d 100644 --- a/docs/src/content/docs/commands/category/list.md +++ b/docs/src/content/docs/commands/category/list.md @@ -11,7 +11,7 @@ backlog category list [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | ## 使用例 diff --git a/docs/src/content/docs/commands/issue-type/create.md b/docs/src/content/docs/commands/issue-type/create.md index 0769360..6c09343 100644 --- a/docs/src/content/docs/commands/issue-type/create.md +++ b/docs/src/content/docs/commands/issue-type/create.md @@ -11,7 +11,7 @@ backlog issue-type create [flags] | フラグ | 短縮 | 型 | 必須 | 説明 | |--------|------|------|------|------| -| `--project` | `-p` | string | Yes | プロジェクトキー | +| `--project` | `-p` | string | Yes | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--name` | `-n` | string | Yes | 種別名 | | `--color` | | string | Yes | 表示色(`#hex` 形式) | diff --git a/docs/src/content/docs/commands/issue-type/delete.md b/docs/src/content/docs/commands/issue-type/delete.md index 9ca44a6..b8f30fa 100644 --- a/docs/src/content/docs/commands/issue-type/delete.md +++ b/docs/src/content/docs/commands/issue-type/delete.md @@ -19,7 +19,7 @@ backlog issue-type delete [flags] | フラグ | 短縮 | 型 | 必須 | 説明 | |--------|------|------|------|------| -| `--project` | `-p` | string | Yes | プロジェクトキー | +| `--project` | `-p` | string | Yes | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--substitute-issue-type-id` | | number | Yes | 代替種別 ID | | `--confirm` | | boolean | No | 確認プロンプトをスキップ | diff --git a/docs/src/content/docs/commands/issue-type/edit.md b/docs/src/content/docs/commands/issue-type/edit.md index cdcb2c0..d2c6f2a 100644 --- a/docs/src/content/docs/commands/issue-type/edit.md +++ b/docs/src/content/docs/commands/issue-type/edit.md @@ -17,7 +17,7 @@ backlog issue-type edit [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--name` | `-n` | string | 種別名 | | `--color` | | string | 表示色(`#hex` 形式) | diff --git a/docs/src/content/docs/commands/issue-type/list.md b/docs/src/content/docs/commands/issue-type/list.md index dc5ff5d..d28cf37 100644 --- a/docs/src/content/docs/commands/issue-type/list.md +++ b/docs/src/content/docs/commands/issue-type/list.md @@ -11,7 +11,7 @@ backlog issue-type list [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | ## 使用例 diff --git a/docs/src/content/docs/commands/issue/create.md b/docs/src/content/docs/commands/issue/create.md index 9dd6eee..a388463 100644 --- a/docs/src/content/docs/commands/issue/create.md +++ b/docs/src/content/docs/commands/issue/create.md @@ -13,7 +13,7 @@ backlog issue create [flags] | フラグ | 短縮 | 型 | 必須 | デフォルト | 説明 | |--------|------|------|------|------------|------| -| `--project` | `-p` | string | Yes* | — | プロジェクトキー | +| `--project` | `-p` | string | Yes* | — | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--title` | `-t` | string | Yes* | — | 課題の件名 | | `--type` | `-T` | string | Yes* | — | 課題種別名 | | `--priority` | `-P` | string | Yes* | — | 優先度名 | diff --git a/docs/src/content/docs/commands/issue/list.md b/docs/src/content/docs/commands/issue/list.md index 2b1a762..d924b01 100644 --- a/docs/src/content/docs/commands/issue/list.md +++ b/docs/src/content/docs/commands/issue/list.md @@ -13,7 +13,7 @@ backlog issue list [flags] | フラグ | 短縮 | 型 | デフォルト | 説明 | |--------|------|------|------------|------| -| `--project` | `-p` | string | — | プロジェクトキー(カンマ区切りで複数可) | +| `--project` | `-p` | string | — | プロジェクトキー(カンマ区切りで複数可)(env: `BACKLOG_PROJECT`) | | `--assignee` | `-a` | string | — | 担当者(ユーザー名 or `@me`) | | `--status` | `-S` | string | — | ステータス名(カンマ区切りで複数可) | | `--type` | `-T` | string | — | 課題種別名(カンマ区切りで複数可) | diff --git a/docs/src/content/docs/commands/milestone/create.md b/docs/src/content/docs/commands/milestone/create.md index 869d972..bbafbc9 100644 --- a/docs/src/content/docs/commands/milestone/create.md +++ b/docs/src/content/docs/commands/milestone/create.md @@ -11,7 +11,7 @@ backlog milestone create [flags] | フラグ | 短縮 | 型 | 必須 | 説明 | |--------|------|------|------|------| -| `--project` | `-p` | string | Yes | プロジェクトキー | +| `--project` | `-p` | string | Yes | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--name` | `-n` | string | Yes | マイルストーン名 | | `--description` | `-d` | string | No | 説明 | | `--start-date` | | string | No | 開始日(`yyyy-MM-dd`) | diff --git a/docs/src/content/docs/commands/milestone/delete.md b/docs/src/content/docs/commands/milestone/delete.md index 70c6882..21d89bd 100644 --- a/docs/src/content/docs/commands/milestone/delete.md +++ b/docs/src/content/docs/commands/milestone/delete.md @@ -17,7 +17,7 @@ backlog milestone delete [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--confirm` | | boolean | 確認プロンプトをスキップ | ## 使用例 diff --git a/docs/src/content/docs/commands/milestone/edit.md b/docs/src/content/docs/commands/milestone/edit.md index 4592cb8..d3f9cd0 100644 --- a/docs/src/content/docs/commands/milestone/edit.md +++ b/docs/src/content/docs/commands/milestone/edit.md @@ -17,7 +17,7 @@ backlog milestone edit [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--name` | `-n` | string | マイルストーン名 | | `--description` | `-d` | string | 説明 | | `--start-date` | | string | 開始日(`yyyy-MM-dd`) | diff --git a/docs/src/content/docs/commands/milestone/list.md b/docs/src/content/docs/commands/milestone/list.md index 31af067..c60ceec 100644 --- a/docs/src/content/docs/commands/milestone/list.md +++ b/docs/src/content/docs/commands/milestone/list.md @@ -11,7 +11,7 @@ backlog milestone list [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | ## 使用例 diff --git a/docs/src/content/docs/commands/pr/close.md b/docs/src/content/docs/commands/pr/close.md index 9d5e6e9..349e22a 100644 --- a/docs/src/content/docs/commands/pr/close.md +++ b/docs/src/content/docs/commands/pr/close.md @@ -19,7 +19,7 @@ backlog pr close [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--repo` | `-R` | string | リポジトリ名 | | `--comment` | `-c` | string | クローズコメント | diff --git a/docs/src/content/docs/commands/pr/comment.md b/docs/src/content/docs/commands/pr/comment.md index 1c330ce..44c3b2c 100644 --- a/docs/src/content/docs/commands/pr/comment.md +++ b/docs/src/content/docs/commands/pr/comment.md @@ -19,7 +19,7 @@ backlog pr comment [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--repo` | `-R` | string | リポジトリ名 | | `--body` | `-b` | string | コメント本文(`-` で標準入力) | diff --git a/docs/src/content/docs/commands/pr/comments.md b/docs/src/content/docs/commands/pr/comments.md index 522dca0..0e71bd1 100644 --- a/docs/src/content/docs/commands/pr/comments.md +++ b/docs/src/content/docs/commands/pr/comments.md @@ -19,7 +19,7 @@ backlog pr comments [flags] | フラグ | 短縮 | 型 | デフォルト | 説明 | |--------|------|------|------------|------| -| `--project` | `-p` | string | — | プロジェクトキー | +| `--project` | `-p` | string | — | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--repo` | `-R` | string | — | リポジトリ名 | | `--limit` | `-L` | number | `20` | 取得件数 | diff --git a/docs/src/content/docs/commands/pr/create.md b/docs/src/content/docs/commands/pr/create.md index cbf68d5..92f8034 100644 --- a/docs/src/content/docs/commands/pr/create.md +++ b/docs/src/content/docs/commands/pr/create.md @@ -13,7 +13,7 @@ backlog pr create [flags] | フラグ | 短縮 | 型 | 必須 | 説明 | |--------|------|------|------|------| -| `--project` | `-p` | string | Yes | プロジェクトキー | +| `--project` | `-p` | string | Yes | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--repo` | `-R` | string | Yes | リポジトリ名 | | `--title` | `-t` | string | Yes* | PR タイトル | | `--body` | `-b` | string | Yes* | PR 説明 | diff --git a/docs/src/content/docs/commands/pr/edit.md b/docs/src/content/docs/commands/pr/edit.md index c136b85..df19617 100644 --- a/docs/src/content/docs/commands/pr/edit.md +++ b/docs/src/content/docs/commands/pr/edit.md @@ -19,7 +19,7 @@ backlog pr edit [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--repo` | `-R` | string | リポジトリ名 | | `--title` | `-t` | string | タイトル | | `--body` | `-b` | string | 説明 | diff --git a/docs/src/content/docs/commands/pr/list.md b/docs/src/content/docs/commands/pr/list.md index 10ab2d0..bda0029 100644 --- a/docs/src/content/docs/commands/pr/list.md +++ b/docs/src/content/docs/commands/pr/list.md @@ -13,7 +13,7 @@ backlog pr list [flags] | フラグ | 短縮 | 型 | デフォルト | 説明 | |--------|------|------|------------|------| -| `--project` | `-p` | string | — | プロジェクトキー | +| `--project` | `-p` | string | — | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--repo` | `-R` | string | — | リポジトリ名 | | `--status` | `-S` | string | `open` | ステータス(`open` / `closed` / `merged`、カンマ区切り) | | `--assignee` | `-a` | string | — | 担当者(ユーザー名 or `@me`) | diff --git a/docs/src/content/docs/commands/pr/merge.md b/docs/src/content/docs/commands/pr/merge.md index a8b5fd9..ddbb056 100644 --- a/docs/src/content/docs/commands/pr/merge.md +++ b/docs/src/content/docs/commands/pr/merge.md @@ -19,7 +19,7 @@ backlog pr merge [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--repo` | `-R` | string | リポジトリ名 | | `--comment` | `-c` | string | マージコメント | diff --git a/docs/src/content/docs/commands/pr/reopen.md b/docs/src/content/docs/commands/pr/reopen.md index dcc106b..36fb7d0 100644 --- a/docs/src/content/docs/commands/pr/reopen.md +++ b/docs/src/content/docs/commands/pr/reopen.md @@ -19,7 +19,7 @@ backlog pr reopen [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--repo` | `-R` | string | リポジトリ名 | ## 使用例 diff --git a/docs/src/content/docs/commands/pr/status.md b/docs/src/content/docs/commands/pr/status.md index ee752e7..12acac0 100644 --- a/docs/src/content/docs/commands/pr/status.md +++ b/docs/src/content/docs/commands/pr/status.md @@ -13,7 +13,7 @@ backlog pr status [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--repo` | `-R` | string | リポジトリ名 | ## 使用例 diff --git a/docs/src/content/docs/commands/pr/view.md b/docs/src/content/docs/commands/pr/view.md index 17ceee4..e7a088e 100644 --- a/docs/src/content/docs/commands/pr/view.md +++ b/docs/src/content/docs/commands/pr/view.md @@ -19,7 +19,7 @@ backlog pr view [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--repo` | `-R` | string | リポジトリ名 | | `--comments` | | boolean | コメントも表示する | | `--web` | | boolean | ブラウザで開く | diff --git a/docs/src/content/docs/commands/repo/clone.md b/docs/src/content/docs/commands/repo/clone.md index 198ae13..f12da10 100644 --- a/docs/src/content/docs/commands/repo/clone.md +++ b/docs/src/content/docs/commands/repo/clone.md @@ -19,7 +19,7 @@ Backlog Git リポジトリをローカルにクローンします。 | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--directory` | `-d` | string | クローン先ディレクトリ | ## 使用例 diff --git a/docs/src/content/docs/commands/repo/view.md b/docs/src/content/docs/commands/repo/view.md index c6e6065..7f3397e 100644 --- a/docs/src/content/docs/commands/repo/view.md +++ b/docs/src/content/docs/commands/repo/view.md @@ -19,7 +19,7 @@ Git リポジトリの詳細情報を表示します。 | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--web` | | boolean | ブラウザで開く | ## 使用例 diff --git a/docs/src/content/docs/commands/status-type/create.md b/docs/src/content/docs/commands/status-type/create.md index 4430c37..6270836 100644 --- a/docs/src/content/docs/commands/status-type/create.md +++ b/docs/src/content/docs/commands/status-type/create.md @@ -11,7 +11,7 @@ backlog status-type create [flags] | フラグ | 短縮 | 型 | 必須 | 説明 | |--------|------|------|------|------| -| `--project` | `-p` | string | Yes | プロジェクトキー | +| `--project` | `-p` | string | Yes | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--name` | `-n` | string | Yes | ステータス名 | | `--color` | | string | Yes | 表示色(`#hex` 形式) | diff --git a/docs/src/content/docs/commands/status-type/delete.md b/docs/src/content/docs/commands/status-type/delete.md index 33ca1bf..46f1d7b 100644 --- a/docs/src/content/docs/commands/status-type/delete.md +++ b/docs/src/content/docs/commands/status-type/delete.md @@ -19,7 +19,7 @@ backlog status-type delete [flags] | フラグ | 短縮 | 型 | 必須 | 説明 | |--------|------|------|------|------| -| `--project` | `-p` | string | Yes | プロジェクトキー | +| `--project` | `-p` | string | Yes | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--substitute-status-id` | | number | Yes | 代替ステータス ID | | `--confirm` | | boolean | No | 確認プロンプトをスキップ | diff --git a/docs/src/content/docs/commands/status-type/edit.md b/docs/src/content/docs/commands/status-type/edit.md index d093c33..49f117c 100644 --- a/docs/src/content/docs/commands/status-type/edit.md +++ b/docs/src/content/docs/commands/status-type/edit.md @@ -17,7 +17,7 @@ backlog status-type edit [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--name` | `-n` | string | ステータス名 | | `--color` | | string | 表示色(`#hex` 形式) | diff --git a/docs/src/content/docs/commands/status-type/list.md b/docs/src/content/docs/commands/status-type/list.md index 29a3acd..89c7c34 100644 --- a/docs/src/content/docs/commands/status-type/list.md +++ b/docs/src/content/docs/commands/status-type/list.md @@ -11,7 +11,7 @@ backlog status-type list [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | ## 使用例 diff --git a/docs/src/content/docs/commands/webhook/create.md b/docs/src/content/docs/commands/webhook/create.md index 6bdafb1..b08a7cc 100644 --- a/docs/src/content/docs/commands/webhook/create.md +++ b/docs/src/content/docs/commands/webhook/create.md @@ -11,7 +11,7 @@ backlog webhook create [flags] | フラグ | 短縮 | 型 | 必須 | 説明 | |--------|------|------|------|------| -| `--project` | `-p` | string | Yes | プロジェクトキー | +| `--project` | `-p` | string | Yes | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--name` | `-n` | string | Yes | Webhook 名 | | `--hook-url` | | string | Yes | 通知先 URL | | `--description` | `-d` | string | No | 説明 | diff --git a/docs/src/content/docs/commands/webhook/delete.md b/docs/src/content/docs/commands/webhook/delete.md index 0653ac7..27d6f4b 100644 --- a/docs/src/content/docs/commands/webhook/delete.md +++ b/docs/src/content/docs/commands/webhook/delete.md @@ -17,7 +17,7 @@ backlog webhook delete [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--confirm` | | boolean | 確認プロンプトをスキップ | ## 使用例 diff --git a/docs/src/content/docs/commands/webhook/edit.md b/docs/src/content/docs/commands/webhook/edit.md index b76bdfb..2407ccf 100644 --- a/docs/src/content/docs/commands/webhook/edit.md +++ b/docs/src/content/docs/commands/webhook/edit.md @@ -17,7 +17,7 @@ backlog webhook edit [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--name` | `-n` | string | Webhook 名 | | `--hook-url` | | string | 通知先 URL | | `--description` | `-d` | string | 説明 | diff --git a/docs/src/content/docs/commands/webhook/list.md b/docs/src/content/docs/commands/webhook/list.md index 37a47b9..eef3930 100644 --- a/docs/src/content/docs/commands/webhook/list.md +++ b/docs/src/content/docs/commands/webhook/list.md @@ -11,7 +11,7 @@ backlog webhook list [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | ## 使用例 diff --git a/docs/src/content/docs/commands/webhook/view.md b/docs/src/content/docs/commands/webhook/view.md index 58e76e4..f3972a1 100644 --- a/docs/src/content/docs/commands/webhook/view.md +++ b/docs/src/content/docs/commands/webhook/view.md @@ -17,7 +17,7 @@ backlog webhook view [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | ## 使用例 diff --git a/docs/src/content/docs/commands/wiki/count.md b/docs/src/content/docs/commands/wiki/count.md index adc40ce..6832994 100644 --- a/docs/src/content/docs/commands/wiki/count.md +++ b/docs/src/content/docs/commands/wiki/count.md @@ -13,7 +13,7 @@ backlog wiki count [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | ## 使用例 diff --git a/docs/src/content/docs/commands/wiki/create.md b/docs/src/content/docs/commands/wiki/create.md index c9a78fd..dad0333 100644 --- a/docs/src/content/docs/commands/wiki/create.md +++ b/docs/src/content/docs/commands/wiki/create.md @@ -11,7 +11,7 @@ backlog wiki create [flags] | フラグ | 短縮 | 型 | 必須 | 説明 | |--------|------|------|------|------| -| `--project` | `-p` | string | Yes | プロジェクトキー | +| `--project` | `-p` | string | Yes | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--name` | `-n` | string | Yes* | ページ名 | | `--body` | `-b` | string | Yes* | 本文 | | `--notify` | | boolean | No | メール通知 | diff --git a/docs/src/content/docs/commands/wiki/list.md b/docs/src/content/docs/commands/wiki/list.md index dcd8028..1e40679 100644 --- a/docs/src/content/docs/commands/wiki/list.md +++ b/docs/src/content/docs/commands/wiki/list.md @@ -13,7 +13,7 @@ Wiki ページの一覧を取得します。 | フラグ | 短縮 | 型 | デフォルト | 説明 | |--------|------|------|------------|------| -| `--project` | `-p` | string | — | プロジェクトキー | +| `--project` | `-p` | string | — | プロジェクトキー(env: `BACKLOG_PROJECT`) | | `--keyword` | `-k` | string | — | キーワード検索 | | `--sort` | | string | `updated` | ソートキー | | `--order` | | string | `desc` | 並び順(`asc` / `desc`) | diff --git a/docs/src/content/docs/commands/wiki/tags.md b/docs/src/content/docs/commands/wiki/tags.md index f43f48c..85c1719 100644 --- a/docs/src/content/docs/commands/wiki/tags.md +++ b/docs/src/content/docs/commands/wiki/tags.md @@ -13,7 +13,7 @@ backlog wiki tags [flags] | フラグ | 短縮 | 型 | 説明 | |--------|------|------|------| -| `--project` | `-p` | string | プロジェクトキー | +| `--project` | `-p` | string | プロジェクトキー(env: `BACKLOG_PROJECT`) | ## 使用例 diff --git a/docs/src/content/docs/guides/configuration.md b/docs/src/content/docs/guides/configuration.md index 1ea8e0f..281dce1 100644 --- a/docs/src/content/docs/guides/configuration.md +++ b/docs/src/content/docs/guides/configuration.md @@ -51,3 +51,19 @@ backlog config list --hostname your-space.backlog.com | 環境変数 | 説明 | |----------|------| | `BACKLOG_SPACE` | 使用するスペースのホスト名(`--space` フラグと同等) | +| `BACKLOG_PROJECT` | デフォルトのプロジェクトキー(`--project` フラグと同等) | + +### `BACKLOG_PROJECT` + +`BACKLOG_PROJECT` 環境変数を設定すると、`--project` フラグを省略できます。 + +```bash +export BACKLOG_PROJECT=MY_PROJECT + +# --project を省略可能 +backlog issue list +backlog milestone list +backlog pr list --repo my-repo +``` + +`--project` フラグが明示的に指定された場合は、環境変数より優先されます。 diff --git a/packages/cli/README.md b/packages/cli/README.md index b811d9a..d0412d2 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -74,6 +74,21 @@ backlog status | `backlog api` | Backlog API の直接呼び出し | | `backlog completion` | シェル補完スクリプトの生成 | +## 環境変数 + +| 環境変数 | 説明 | +|----------|------| +| `BACKLOG_SPACE` | 使用するスペースのホスト名(`--space` フラグと同等) | +| `BACKLOG_PROJECT` | デフォルトのプロジェクトキー(`--project` フラグと同等) | + +```bash +export BACKLOG_PROJECT=YOUR_PROJECT + +# --project を省略可能 +backlog issue list +backlog milestone list +``` + ## 出力形式 デフォルトではテーブル形式で出力されます。JSON 形式で取得する場合は `--json` フラグを使用します。 diff --git a/packages/cli/src/commands/browse.ts b/packages/cli/src/commands/browse.ts index 8dad8c1..d037a48 100644 --- a/packages/cli/src/commands/browse.ts +++ b/packages/cli/src/commands/browse.ts @@ -21,7 +21,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", + description: "Project key (env: BACKLOG_PROJECT)", }, issues: { type: "boolean", @@ -42,6 +42,7 @@ export default defineCommand({ }, async run({ args }) { const { host } = await getClient(); + const projectKey = args.project || process.env.BACKLOG_PROJECT; let url: string; @@ -53,20 +54,20 @@ export default defineCommand({ // Treat as a path url = buildBacklogUrl(host, `/${args.target}`); } - } else if (args.project) { + } else if (projectKey) { if (args.issues) { - url = buildBacklogUrl(host, `/find/${args.project}`); + url = buildBacklogUrl(host, `/find/${projectKey}`); } else if (args.wiki) { - url = buildBacklogUrl(host, `/wiki/${args.project}`); + url = buildBacklogUrl(host, `/wiki/${projectKey}`); } else if (args.git) { - url = buildBacklogUrl(host, `/git/${args.project}`); + url = buildBacklogUrl(host, `/git/${projectKey}`); } else if (args.settings) { url = buildBacklogUrl( host, - `/EditProject.action?project.key=${args.project}`, + `/EditProject.action?project.key=${projectKey}`, ); } else { - url = projectUrl(host, args.project); + url = projectUrl(host, projectKey); } } else { url = dashboardUrl(host); diff --git a/packages/cli/src/commands/category/create.ts b/packages/cli/src/commands/category/create.ts index 77589cf..410fdf5 100644 --- a/packages/cli/src/commands/category/create.ts +++ b/packages/cli/src/commands/category/create.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { promptRequired } from "#utils/prompt.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -13,8 +14,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, name: { type: "string", @@ -23,12 +23,14 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const name = await promptRequired("Category name:", args.name); const category = await client( - `/projects/${args.project}/categories`, + `/projects/${project}/categories`, { method: "POST", body: { name }, diff --git a/packages/cli/src/commands/category/delete.ts b/packages/cli/src/commands/category/delete.ts index a47c249..e0c693f 100644 --- a/packages/cli/src/commands/category/delete.ts +++ b/packages/cli/src/commands/category/delete.ts @@ -2,6 +2,7 @@ import type { BacklogCategory } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -17,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, confirm: { type: "boolean", @@ -26,6 +26,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); if (!args.confirm) { @@ -40,7 +42,7 @@ export default defineCommand({ } const category = await client( - `/projects/${args.project}/categories/${args.id}`, + `/projects/${project}/categories/${args.id}`, { method: "DELETE", }, diff --git a/packages/cli/src/commands/category/edit.ts b/packages/cli/src/commands/category/edit.ts index 402696b..dda3d0e 100644 --- a/packages/cli/src/commands/category/edit.ts +++ b/packages/cli/src/commands/category/edit.ts @@ -2,6 +2,7 @@ import type { BacklogCategory } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -17,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, name: { type: "string", @@ -28,10 +28,12 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const category = await client( - `/projects/${args.project}/categories/${args.id}`, + `/projects/${project}/categories/${args.id}`, { method: "PATCH", body: { name: args.name }, diff --git a/packages/cli/src/commands/category/list.ts b/packages/cli/src/commands/category/list.ts index 004895b..e5738d9 100644 --- a/packages/cli/src/commands/category/list.ts +++ b/packages/cli/src/commands/category/list.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { padEnd } from "#utils/format.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -13,15 +14,16 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const categories = await client( - `/projects/${args.project}/categories`, + `/projects/${project}/categories`, ); if (categories.length === 0) { diff --git a/packages/cli/src/commands/issue-type/create.ts b/packages/cli/src/commands/issue-type/create.ts index f9e3aa5..e0b7a7c 100644 --- a/packages/cli/src/commands/issue-type/create.ts +++ b/packages/cli/src/commands/issue-type/create.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { promptRequired } from "#utils/prompt.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -13,8 +14,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, name: { type: "string", @@ -27,13 +27,15 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const name = await promptRequired("Issue type name:", args.name); const color = await promptRequired("Display color (#hex):", args.color); const issueType = await client( - `/projects/${args.project}/issueTypes`, + `/projects/${project}/issueTypes`, { method: "POST", body: { name, color }, diff --git a/packages/cli/src/commands/issue-type/delete.ts b/packages/cli/src/commands/issue-type/delete.ts index f0465d5..2c95dfa 100644 --- a/packages/cli/src/commands/issue-type/delete.ts +++ b/packages/cli/src/commands/issue-type/delete.ts @@ -2,6 +2,7 @@ import type { BacklogIssueType } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -17,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, "substitute-issue-type-id": { type: "string", @@ -31,6 +31,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); if (!args.confirm) { @@ -45,7 +47,7 @@ export default defineCommand({ } const issueType = await client( - `/projects/${args.project}/issueTypes/${args.id}`, + `/projects/${project}/issueTypes/${args.id}`, { method: "DELETE", body: { diff --git a/packages/cli/src/commands/issue-type/edit.ts b/packages/cli/src/commands/issue-type/edit.ts index 0d8b9f6..95fcdff 100644 --- a/packages/cli/src/commands/issue-type/edit.ts +++ b/packages/cli/src/commands/issue-type/edit.ts @@ -2,6 +2,7 @@ import type { BacklogIssueType } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -17,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, name: { type: "string", @@ -31,6 +31,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const body: Record = {}; @@ -43,7 +45,7 @@ export default defineCommand({ } const issueType = await client( - `/projects/${args.project}/issueTypes/${args.id}`, + `/projects/${project}/issueTypes/${args.id}`, { method: "PATCH", body, diff --git a/packages/cli/src/commands/issue-type/list.ts b/packages/cli/src/commands/issue-type/list.ts index 5004ab5..dfa7a44 100644 --- a/packages/cli/src/commands/issue-type/list.ts +++ b/packages/cli/src/commands/issue-type/list.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { padEnd } from "#utils/format.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -13,15 +14,16 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const issueTypes = await client( - `/projects/${args.project}/issueTypes`, + `/projects/${project}/issueTypes`, ); if (issueTypes.length === 0) { diff --git a/packages/cli/src/commands/issue/create.ts b/packages/cli/src/commands/issue/create.ts index 07b09cb..b7003e8 100644 --- a/packages/cli/src/commands/issue/create.ts +++ b/packages/cli/src/commands/issue/create.ts @@ -20,7 +20,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", + description: "Project key (env: BACKLOG_PROJECT)", }, title: { type: "string", @@ -64,7 +64,10 @@ export default defineCommand({ const { client, host } = await getClient(); // Resolve required fields — prompt interactively if missing - const projectKey = await promptRequired("Project key:", args.project); + const projectKey = await promptRequired( + "Project key:", + args.project || process.env.BACKLOG_PROJECT, + ); const title = await promptRequired("Issue title:", args.title); const typeName = await promptRequired("Issue type:", args.type); const priorityName = await promptRequired("Priority:", args.priority); diff --git a/packages/cli/src/commands/issue/list.ts b/packages/cli/src/commands/issue/list.ts index b7b3e7d..bb1cef8 100644 --- a/packages/cli/src/commands/issue/list.ts +++ b/packages/cli/src/commands/issue/list.ts @@ -18,7 +18,8 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key (multiple allowed, comma-separated)", + description: + "Project key (multiple allowed, comma-separated) (env: BACKLOG_PROJECT)", }, assignee: { type: "string", @@ -108,8 +109,9 @@ export default defineCommand({ } // Resolve project keys to IDs - if (args.project) { - const keys = args.project.split(",").map((k) => k.trim()); + const projectArg = args.project || process.env.BACKLOG_PROJECT; + if (projectArg) { + const keys = projectArg.split(",").map((k) => k.trim()); const ids = await Promise.all( keys.map((key) => resolveProjectId(client, key)), ); diff --git a/packages/cli/src/commands/milestone/create.ts b/packages/cli/src/commands/milestone/create.ts index faf73db..461101b 100644 --- a/packages/cli/src/commands/milestone/create.ts +++ b/packages/cli/src/commands/milestone/create.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { promptRequired } from "#utils/prompt.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -13,8 +14,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, name: { type: "string", @@ -36,6 +36,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const name = await promptRequired("Milestone name:", args.name); @@ -53,7 +55,7 @@ export default defineCommand({ } const milestone = await client( - `/projects/${args.project}/versions`, + `/projects/${project}/versions`, { method: "POST", body, diff --git a/packages/cli/src/commands/milestone/delete.ts b/packages/cli/src/commands/milestone/delete.ts index 680b822..0b35e9e 100644 --- a/packages/cli/src/commands/milestone/delete.ts +++ b/packages/cli/src/commands/milestone/delete.ts @@ -2,6 +2,7 @@ import type { BacklogMilestone } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -17,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, confirm: { type: "boolean", @@ -26,6 +26,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); if (!args.confirm) { @@ -40,7 +42,7 @@ export default defineCommand({ } const milestone = await client( - `/projects/${args.project}/versions/${args.id}`, + `/projects/${project}/versions/${args.id}`, { method: "DELETE", }, diff --git a/packages/cli/src/commands/milestone/edit.ts b/packages/cli/src/commands/milestone/edit.ts index 6b8f5a5..d8ca8bf 100644 --- a/packages/cli/src/commands/milestone/edit.ts +++ b/packages/cli/src/commands/milestone/edit.ts @@ -2,6 +2,7 @@ import type { BacklogMilestone } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -17,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, name: { type: "string", @@ -44,6 +44,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const body: Record = {}; @@ -65,7 +67,7 @@ export default defineCommand({ } const milestone = await client( - `/projects/${args.project}/versions/${args.id}`, + `/projects/${project}/versions/${args.id}`, { method: "PATCH", body, diff --git a/packages/cli/src/commands/milestone/list.ts b/packages/cli/src/commands/milestone/list.ts index 770cba8..34ec445 100644 --- a/packages/cli/src/commands/milestone/list.ts +++ b/packages/cli/src/commands/milestone/list.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { formatDate, padEnd } from "#utils/format.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -13,15 +14,16 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const milestones = await client( - `/projects/${args.project}/versions`, + `/projects/${project}/versions`, ); if (milestones.length === 0) { diff --git a/packages/cli/src/commands/pr/close.ts b/packages/cli/src/commands/pr/close.ts index 04f9d00..12c7429 100644 --- a/packages/cli/src/commands/pr/close.ts +++ b/packages/cli/src/commands/pr/close.ts @@ -2,6 +2,7 @@ import type { BacklogPullRequest } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -17,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, repo: { type: "string", @@ -33,6 +33,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const body: Record = { @@ -44,7 +46,7 @@ export default defineCommand({ } const pr = await client( - `/projects/${args.project}/git/repositories/${args.repo}/pullRequests/${args.number}`, + `/projects/${project}/git/repositories/${args.repo}/pullRequests/${args.number}`, { method: "PATCH", body, diff --git a/packages/cli/src/commands/pr/comment.ts b/packages/cli/src/commands/pr/comment.ts index 13b1a5b..1a53df8 100644 --- a/packages/cli/src/commands/pr/comment.ts +++ b/packages/cli/src/commands/pr/comment.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { promptRequired } from "#utils/prompt.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -18,8 +19,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, repo: { type: "string", @@ -34,6 +34,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); let content = args.body; @@ -53,7 +55,7 @@ export default defineCommand({ } const comment = await client( - `/projects/${args.project}/git/repositories/${args.repo}/pullRequests/${args.number}/comments`, + `/projects/${project}/git/repositories/${args.repo}/pullRequests/${args.number}/comments`, { method: "POST", body: { content }, diff --git a/packages/cli/src/commands/pr/comments.ts b/packages/cli/src/commands/pr/comments.ts index 5295168..aca1279 100644 --- a/packages/cli/src/commands/pr/comments.ts +++ b/packages/cli/src/commands/pr/comments.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { formatDate } from "#utils/format.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -18,8 +19,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, repo: { type: "string", @@ -35,6 +35,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const query: Record = { @@ -42,7 +44,7 @@ export default defineCommand({ }; const comments = await client( - `/projects/${args.project}/git/repositories/${args.repo}/pullRequests/${args.number}/comments`, + `/projects/${project}/git/repositories/${args.repo}/pullRequests/${args.number}/comments`, { query }, ); diff --git a/packages/cli/src/commands/pr/create.ts b/packages/cli/src/commands/pr/create.ts index d906a79..c256093 100644 --- a/packages/cli/src/commands/pr/create.ts +++ b/packages/cli/src/commands/pr/create.ts @@ -3,7 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { promptRequired } from "#utils/prompt.ts"; -import { resolveUserId } from "#utils/resolve.ts"; +import { resolveProjectArg, resolveUserId } from "#utils/resolve.ts"; import { pullRequestUrl } from "#utils/url.ts"; export default defineCommand({ @@ -15,8 +15,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, repo: { type: "string", @@ -58,6 +57,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client, host } = await getClient(); // Prompt for required fields if not provided @@ -82,7 +83,7 @@ export default defineCommand({ } const pr = await client( - `/projects/${args.project}/git/repositories/${args.repo}/pullRequests`, + `/projects/${project}/git/repositories/${args.repo}/pullRequests`, { method: "POST", body, @@ -92,7 +93,7 @@ export default defineCommand({ consola.success(`Created PR #${pr.number}: ${pr.summary}`); if (args.web) { - const url = pullRequestUrl(host, args.project, args.repo, pr.number); + const url = pullRequestUrl(host, project, args.repo, pr.number); consola.info(`Opening ${url}`); Bun.spawn(["open", url]); } diff --git a/packages/cli/src/commands/pr/edit.ts b/packages/cli/src/commands/pr/edit.ts index 07571b1..747f9bd 100644 --- a/packages/cli/src/commands/pr/edit.ts +++ b/packages/cli/src/commands/pr/edit.ts @@ -2,7 +2,7 @@ import type { BacklogPullRequest } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; -import { resolveUserId } from "#utils/resolve.ts"; +import { resolveProjectArg, resolveUserId } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -18,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, repo: { type: "string", @@ -48,6 +47,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const body: Record = {}; @@ -67,7 +68,7 @@ export default defineCommand({ } const pr = await client( - `/projects/${args.project}/git/repositories/${args.repo}/pullRequests/${args.number}`, + `/projects/${project}/git/repositories/${args.repo}/pullRequests/${args.number}`, { method: "PATCH", body, diff --git a/packages/cli/src/commands/pr/list.ts b/packages/cli/src/commands/pr/list.ts index 96600a3..83287c0 100644 --- a/packages/cli/src/commands/pr/list.ts +++ b/packages/cli/src/commands/pr/list.ts @@ -3,7 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { formatPullRequestLine, padEnd } from "#utils/format.ts"; -import { resolveUserId } from "#utils/resolve.ts"; +import { resolveProjectArg, resolveUserId } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -14,8 +14,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, repo: { type: "string", @@ -54,6 +53,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const query: Record = { @@ -98,7 +99,7 @@ export default defineCommand({ } const prs = await client( - `/projects/${args.project}/git/repositories/${args.repo}/pullRequests`, + `/projects/${project}/git/repositories/${args.repo}/pullRequests`, { query }, ); diff --git a/packages/cli/src/commands/pr/merge.ts b/packages/cli/src/commands/pr/merge.ts index f27b8e7..cc3212b 100644 --- a/packages/cli/src/commands/pr/merge.ts +++ b/packages/cli/src/commands/pr/merge.ts @@ -2,6 +2,7 @@ import type { BacklogPullRequest } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -17,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, repo: { type: "string", @@ -33,6 +33,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const body: Record = { @@ -44,7 +46,7 @@ export default defineCommand({ } const pr = await client( - `/projects/${args.project}/git/repositories/${args.repo}/pullRequests/${args.number}`, + `/projects/${project}/git/repositories/${args.repo}/pullRequests/${args.number}`, { method: "PATCH", body, diff --git a/packages/cli/src/commands/pr/reopen.ts b/packages/cli/src/commands/pr/reopen.ts index 3bdf345..663fc26 100644 --- a/packages/cli/src/commands/pr/reopen.ts +++ b/packages/cli/src/commands/pr/reopen.ts @@ -2,6 +2,7 @@ import type { BacklogPullRequest } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -17,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, repo: { type: "string", @@ -28,10 +28,12 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const pr = await client( - `/projects/${args.project}/git/repositories/${args.repo}/pullRequests/${args.number}`, + `/projects/${project}/git/repositories/${args.repo}/pullRequests/${args.number}`, { method: "PATCH", body: { diff --git a/packages/cli/src/commands/pr/status.ts b/packages/cli/src/commands/pr/status.ts index fe38df5..4a2e5b4 100644 --- a/packages/cli/src/commands/pr/status.ts +++ b/packages/cli/src/commands/pr/status.ts @@ -2,6 +2,7 @@ import type { BacklogPullRequest, BacklogUser } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -12,8 +13,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, repo: { type: "string", @@ -23,12 +23,14 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const me = await client("/users/myself"); const prs = await client( - `/projects/${args.project}/git/repositories/${args.repo}/pullRequests`, + `/projects/${project}/git/repositories/${args.repo}/pullRequests`, { query: { "assigneeId[]": [me.id], diff --git a/packages/cli/src/commands/pr/view.ts b/packages/cli/src/commands/pr/view.ts index 1edc604..2d3d73e 100644 --- a/packages/cli/src/commands/pr/view.ts +++ b/packages/cli/src/commands/pr/view.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { formatDate } from "#utils/format.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; import { pullRequestUrl } from "#utils/url.ts"; export default defineCommand({ @@ -19,8 +20,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, repo: { type: "string", @@ -38,13 +38,15 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client, host } = await getClient(); - const basePath = `/projects/${args.project}/git/repositories/${args.repo}/pullRequests`; + const basePath = `/projects/${project}/git/repositories/${args.repo}/pullRequests`; const pr = await client(`${basePath}/${args.number}`); if (args.web) { - const url = pullRequestUrl(host, args.project, args.repo, pr.number); + const url = pullRequestUrl(host, project, args.repo, pr.number); consola.info(`Opening ${url}`); Bun.spawn(["open", url]); return; diff --git a/packages/cli/src/commands/repo/clone.ts b/packages/cli/src/commands/repo/clone.ts index a41a6c5..4153d47 100644 --- a/packages/cli/src/commands/repo/clone.ts +++ b/packages/cli/src/commands/repo/clone.ts @@ -2,6 +2,7 @@ import type { BacklogRepository } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -17,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, directory: { type: "string", @@ -27,10 +27,12 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const repo = await client( - `/projects/${args.project}/git/repositories/${args.repoName}`, + `/projects/${project}/git/repositories/${args.repoName}`, ); const cloneArgs = ["git", "clone", repo.httpUrl]; diff --git a/packages/cli/src/commands/repo/view.ts b/packages/cli/src/commands/repo/view.ts index 770558c..3d13848 100644 --- a/packages/cli/src/commands/repo/view.ts +++ b/packages/cli/src/commands/repo/view.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { formatDate } from "#utils/format.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; import { repositoryUrl } from "#utils/url.ts"; export default defineCommand({ @@ -19,8 +20,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, web: { type: "boolean", @@ -28,14 +28,16 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client, host } = await getClient(); const repo = await client( - `/projects/${args.project}/git/repositories/${args.repoName}`, + `/projects/${project}/git/repositories/${args.repoName}`, ); if (args.web) { - const url = repositoryUrl(host, args.project, repo.name); + const url = repositoryUrl(host, project, repo.name); consola.info(`Opening ${url}`); Bun.spawn(["open", url]); return; diff --git a/packages/cli/src/commands/status-type/create.ts b/packages/cli/src/commands/status-type/create.ts index e069615..a48feaa 100644 --- a/packages/cli/src/commands/status-type/create.ts +++ b/packages/cli/src/commands/status-type/create.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { promptRequired } from "#utils/prompt.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -13,8 +14,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, name: { type: "string", @@ -27,13 +27,15 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const name = await promptRequired("Status name:", args.name); const color = await promptRequired("Display color (#hex):", args.color); const status = await client( - `/projects/${args.project}/statuses`, + `/projects/${project}/statuses`, { method: "POST", body: { name, color }, diff --git a/packages/cli/src/commands/status-type/delete.ts b/packages/cli/src/commands/status-type/delete.ts index 75e842c..da396a9 100644 --- a/packages/cli/src/commands/status-type/delete.ts +++ b/packages/cli/src/commands/status-type/delete.ts @@ -2,6 +2,7 @@ import type { BacklogStatus } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -17,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, "substitute-status-id": { type: "string", @@ -31,6 +31,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); if (!args.confirm) { @@ -45,7 +47,7 @@ export default defineCommand({ } const status = await client( - `/projects/${args.project}/statuses/${args.id}`, + `/projects/${project}/statuses/${args.id}`, { method: "DELETE", body: { diff --git a/packages/cli/src/commands/status-type/edit.ts b/packages/cli/src/commands/status-type/edit.ts index ad538d0..1e53104 100644 --- a/packages/cli/src/commands/status-type/edit.ts +++ b/packages/cli/src/commands/status-type/edit.ts @@ -2,6 +2,7 @@ import type { BacklogStatus } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -17,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, name: { type: "string", @@ -31,6 +31,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const body: Record = {}; @@ -43,7 +45,7 @@ export default defineCommand({ } const status = await client( - `/projects/${args.project}/statuses/${args.id}`, + `/projects/${project}/statuses/${args.id}`, { method: "PATCH", body, diff --git a/packages/cli/src/commands/status-type/list.ts b/packages/cli/src/commands/status-type/list.ts index 897ba5f..b113dfb 100644 --- a/packages/cli/src/commands/status-type/list.ts +++ b/packages/cli/src/commands/status-type/list.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { padEnd } from "#utils/format.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -13,15 +14,16 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const statuses = await client( - `/projects/${args.project}/statuses`, + `/projects/${project}/statuses`, ); if (statuses.length === 0) { diff --git a/packages/cli/src/commands/webhook/create.ts b/packages/cli/src/commands/webhook/create.ts index 40dd2ae..b29ee3b 100644 --- a/packages/cli/src/commands/webhook/create.ts +++ b/packages/cli/src/commands/webhook/create.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { promptRequired } from "#utils/prompt.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -13,8 +14,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, name: { type: "string", @@ -40,6 +40,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const name = await promptRequired("Webhook name:", args.name); @@ -62,7 +64,7 @@ export default defineCommand({ } const webhook = await client( - `/projects/${args.project}/webhooks`, + `/projects/${project}/webhooks`, { method: "POST", body, diff --git a/packages/cli/src/commands/webhook/delete.ts b/packages/cli/src/commands/webhook/delete.ts index b528967..28fc1d3 100644 --- a/packages/cli/src/commands/webhook/delete.ts +++ b/packages/cli/src/commands/webhook/delete.ts @@ -2,6 +2,7 @@ import type { BacklogWebhook } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -17,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, confirm: { type: "boolean", @@ -26,6 +26,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); if (!args.confirm) { @@ -40,7 +42,7 @@ export default defineCommand({ } const webhook = await client( - `/projects/${args.project}/webhooks/${args.id}`, + `/projects/${project}/webhooks/${args.id}`, { method: "DELETE", }, diff --git a/packages/cli/src/commands/webhook/edit.ts b/packages/cli/src/commands/webhook/edit.ts index 4a6af03..e8abf86 100644 --- a/packages/cli/src/commands/webhook/edit.ts +++ b/packages/cli/src/commands/webhook/edit.ts @@ -2,6 +2,7 @@ import type { BacklogWebhook } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -17,8 +18,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, name: { type: "string", @@ -44,6 +44,8 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const body: Record = {}; @@ -71,7 +73,7 @@ export default defineCommand({ } const webhook = await client( - `/projects/${args.project}/webhooks/${args.id}`, + `/projects/${project}/webhooks/${args.id}`, { method: "PATCH", body, diff --git a/packages/cli/src/commands/webhook/list.ts b/packages/cli/src/commands/webhook/list.ts index 4ab588e..9379c6e 100644 --- a/packages/cli/src/commands/webhook/list.ts +++ b/packages/cli/src/commands/webhook/list.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { formatDate, padEnd } from "#utils/format.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -13,15 +14,16 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const webhooks = await client( - `/projects/${args.project}/webhooks`, + `/projects/${project}/webhooks`, ); if (webhooks.length === 0) { diff --git a/packages/cli/src/commands/webhook/view.ts b/packages/cli/src/commands/webhook/view.ts index b80d83d..e61033e 100644 --- a/packages/cli/src/commands/webhook/view.ts +++ b/packages/cli/src/commands/webhook/view.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { formatDate } from "#utils/format.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -18,15 +19,16 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const webhook = await client( - `/projects/${args.project}/webhooks/${args.id}`, + `/projects/${project}/webhooks/${args.id}`, ); consola.log(""); diff --git a/packages/cli/src/commands/wiki/count.ts b/packages/cli/src/commands/wiki/count.ts index ca6a7b2..a4f86cd 100644 --- a/packages/cli/src/commands/wiki/count.ts +++ b/packages/cli/src/commands/wiki/count.ts @@ -2,6 +2,7 @@ import type { BacklogWikiCount } from "@repo/api"; import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -12,15 +13,16 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const result = await client("/wikis/count", { - query: { projectIdOrKey: args.project }, + query: { projectIdOrKey: project }, }); consola.log(`${result.count} wiki page(s)`); diff --git a/packages/cli/src/commands/wiki/create.ts b/packages/cli/src/commands/wiki/create.ts index c77db18..c53f249 100644 --- a/packages/cli/src/commands/wiki/create.ts +++ b/packages/cli/src/commands/wiki/create.ts @@ -3,7 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { promptRequired } from "#utils/prompt.ts"; -import { resolveProjectId } from "#utils/resolve.ts"; +import { resolveProjectArg, resolveProjectId } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -14,8 +14,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, name: { type: "string", @@ -33,12 +32,14 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const name = await promptRequired("Page name:", args.name); const body = await promptRequired("Page content:", args.body); - const projectId = await resolveProjectId(client, args.project); + const projectId = await resolveProjectId(client, project); const requestBody: Record = { projectId, diff --git a/packages/cli/src/commands/wiki/list.ts b/packages/cli/src/commands/wiki/list.ts index 55935e3..e71894c 100644 --- a/packages/cli/src/commands/wiki/list.ts +++ b/packages/cli/src/commands/wiki/list.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { formatDate, padEnd } from "#utils/format.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -13,8 +14,7 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, keyword: { type: "string", @@ -43,10 +43,12 @@ export default defineCommand({ }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const query: Record = { - projectIdOrKey: args.project, + projectIdOrKey: project, sort: args.sort, order: args.order, count: Number.parseInt(args.limit, 10), diff --git a/packages/cli/src/commands/wiki/tags.ts b/packages/cli/src/commands/wiki/tags.ts index 4091927..c5e199b 100644 --- a/packages/cli/src/commands/wiki/tags.ts +++ b/packages/cli/src/commands/wiki/tags.ts @@ -3,6 +3,7 @@ import { defineCommand } from "citty"; import consola from "consola"; import { getClient } from "#utils/client.ts"; import { padEnd } from "#utils/format.ts"; +import { resolveProjectArg } from "#utils/resolve.ts"; export default defineCommand({ meta: { @@ -13,15 +14,16 @@ export default defineCommand({ project: { type: "string", alias: "p", - description: "Project key", - required: true, + description: "Project key (env: BACKLOG_PROJECT)", }, }, async run({ args }) { + const project = resolveProjectArg(args.project); + const { client } = await getClient(); const tags = await client("/wikis/tags", { - query: { projectIdOrKey: args.project }, + query: { projectIdOrKey: project }, }); if (tags.length === 0) { diff --git a/packages/cli/src/utils/resolve.test.ts b/packages/cli/src/utils/resolve.test.ts index 8171cd4..f00547d 100644 --- a/packages/cli/src/utils/resolve.test.ts +++ b/packages/cli/src/utils/resolve.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; import type { BacklogClient } from "#utils/client.ts"; import { extractProjectKey, @@ -7,6 +7,7 @@ import { resolveIssueTypeId, resolveOpenStatusId, resolvePriorityId, + resolveProjectArg, resolveProjectId, resolveResolutionId, resolveStatusId, @@ -22,6 +23,47 @@ function createMockClient(responses: Record): BacklogClient { }) as unknown as BacklogClient; } +describe("resolveProjectArg", () => { + const originalEnv = process.env.BACKLOG_PROJECT; + + afterEach(() => { + if (originalEnv === undefined) { + delete process.env.BACKLOG_PROJECT; + } else { + process.env.BACKLOG_PROJECT = originalEnv; + } + }); + + it("引数が指定されていればそのまま返す", () => { + expect(resolveProjectArg("MY_PROJECT")).toBe("MY_PROJECT"); + }); + + it("引数が指定されていれば環境変数より優先する", () => { + process.env.BACKLOG_PROJECT = "ENV_PROJECT"; + expect(resolveProjectArg("ARG_PROJECT")).toBe("ARG_PROJECT"); + }); + + it("引数が未指定の場合は BACKLOG_PROJECT 環境変数を使う", () => { + process.env.BACKLOG_PROJECT = "ENV_PROJECT"; + expect(resolveProjectArg(undefined)).toBe("ENV_PROJECT"); + }); + + it("引数も環境変数も未指定の場合は process.exit(1) を呼ぶ", () => { + delete process.env.BACKLOG_PROJECT; + const exitSpy = vi + .spyOn(process, "exit") + .mockImplementation(() => undefined as never); + resolveProjectArg(undefined); + expect(exitSpy).toHaveBeenCalledWith(1); + exitSpy.mockRestore(); + }); + + it("引数が空文字の場合は環境変数にフォールバックする", () => { + process.env.BACKLOG_PROJECT = "ENV_PROJECT"; + expect(resolveProjectArg("")).toBe("ENV_PROJECT"); + }); +}); + describe("resolveByName", () => { it("名前でアイテムを検索してIDを返す", async () => { const client = createMockClient({ diff --git a/packages/cli/src/utils/resolve.ts b/packages/cli/src/utils/resolve.ts index dac33bf..5effac5 100644 --- a/packages/cli/src/utils/resolve.ts +++ b/packages/cli/src/utils/resolve.ts @@ -6,8 +6,29 @@ import type { BacklogStatus, BacklogUser, } from "@repo/api"; +import consola from "consola"; import type { BacklogClient } from "#utils/client.ts"; +/** + * Resolves the project key from the provided argument or `BACKLOG_PROJECT` environment variable. + * + * @param argValue - The value provided via `--project` flag. + * @returns The resolved project key. + * @throws Exits with code 1 if neither the argument nor the environment variable is set. + */ +export function resolveProjectArg(argValue?: string): string { + const project = argValue || process.env.BACKLOG_PROJECT; + + if (!project) { + consola.error( + "Project key is required. Specify --project (-p) or set BACKLOG_PROJECT environment variable.", + ); + return process.exit(1); + } + + return project; +} + /** * Generic name-to-ID resolver factory. *