Skip to content

原文(日本語に翻訳)

ステータスラインのJSON入力でGitHubリポジトリとPR情報が検出された場合、それらを含めるようになりました

原文(英語)

Status line JSON input now includes GitHub repo and PR information when detected

概要

Claude Code v2.1.145から、ステータスラインのJSON入力データにGitHubのリポジトリ情報とプルリクエスト(PR)情報が自動的に含まれるようになりました。これにより、tmuxステータスバーや外部ツールなどでGitHubのコンテキスト情報(リポジトリ名、PRタイトル、PR番号など)を活用したカスタムステータス表示が可能になります。

基本的な使い方

ステータスラインのJSON入力にはGitHub情報が自動的に含まれます。カスタムのステータス表示スクリプトでは、新しいフィールドにアクセスできます。

bash
# ステータスラインのJSON出力を確認
# (JSON入力を受け取るスクリプトで以下のようなデータが渡される)
# {
#   "session_id": "...",
#   "working_directory": "/path/to/repo",
#   "github": {
#     "repo": "owner/repository",
#     "pr_number": 123,
#     "pr_title": "Fix: update feature X",
#     "pr_url": "https://github.com/owner/repository/pull/123"
#   }
# }

実践例

tmuxステータスバーにGitHub PR情報を表示

tmuxのステータスバーにカレントPRの情報を表示するスクリプトです。

bash
#!/bin/bash
# ~/.local/bin/claude-status.sh
# Claude Codeのステータス情報からGitHub PR情報を抽出して表示

STATUS_JSON="$1"  # JSON文字列をスクリプトに渡す

if [ -z "$STATUS_JSON" ]; then
  exit 0
fi

# GitHub PR情報を抽出
PR_NUMBER=$(echo "$STATUS_JSON" | jq -r '.github.pr_number // empty')
PR_TITLE=$(echo "$STATUS_JSON" | jq -r '.github.pr_title // empty')
REPO=$(echo "$STATUS_JSON" | jq -r '.github.repo // empty')

if [ -n "$PR_NUMBER" ]; then
  echo "PR #${PR_NUMBER}: ${PR_TITLE:0:30}..."
elif [ -n "$REPO" ]; then
  echo "${REPO}"
fi
bash
# ~/.tmux.conf でスクリプトを使用
set -g status-right '#(~/.local/bin/claude-status.sh "$(claude agents --json | jq -r ".[0]")")'

GitHubコンテキストを活用したカスタム通知

PRが関連付けられているセッションで特定のタスクが完了したときに通知を送る例です。

bash
#!/bin/bash
# notify-pr-completion.sh - PRに関連するタスク完了時に通知

# Claude Codeのステータス情報を取得(Stopフック等から受け取る)
STATUS_JSON=$(cat)

PR_NUMBER=$(echo "$STATUS_JSON" | jq -r '.github.pr_number // empty')
PR_TITLE=$(echo "$STATUS_JSON" | jq -r '.github.pr_title // empty')
REPO=$(echo "$STATUS_JSON" | jq -r '.github.repo // empty')

if [ -n "$PR_NUMBER" ]; then
  MESSAGE="タスク完了: ${REPO} PR #${PR_NUMBER} - ${PR_TITLE}"
  
  # macOSの場合
  osascript -e "display notification \"${MESSAGE}\" with title \"Claude Code\""
  
  # Linuxの場合(notify-send)
  # notify-send "Claude Code" "${MESSAGE}"
fi

Stopフックでのコンテキスト活用

Stopフックと組み合わせてGitHub PRのコンテキストに基づいた処理を行います。

bash
#!/bin/bash
# .claude/hooks/stop.sh - セッション終了時にPRコメントを自動投稿

STATUS_JSON=$(cat)

PR_NUMBER=$(echo "$STATUS_JSON" | jq -r '.github.pr_number // empty')
REPO=$(echo "$STATUS_JSON" | jq -r '.github.repo // empty')

if [ -n "$PR_NUMBER" ] && [ -n "$REPO" ]; then
  # gh CLIでPRにコメントを投稿
  gh pr comment "$PR_NUMBER" \
    --repo "$REPO" \
    --body "Claude Codeによる作業セッションが完了しました。"
fi

ステータスラインデータの構造確認

python
# parse_status.py - ステータスラインのJSONを解析するユーティリティ

import json
import sys

def parse_claude_status(json_str):
    """Claude Codeのステータス情報を解析"""
    try:
        data = json.loads(json_str)
    except json.JSONDecodeError:
        return None
    
    result = {
        'session_id': data.get('session_id'),
        'working_dir': data.get('working_directory'),
    }
    
    # GitHub情報の抽出
    github = data.get('github', {})
    if github:
        result['github'] = {
            'repo': github.get('repo'),
            'pr_number': github.get('pr_number'),
            'pr_title': github.get('pr_title'),
            'pr_url': github.get('pr_url'),
        }
    
    return result

if __name__ == '__main__':
    data = parse_claude_status(sys.stdin.read())
    if data:
        print(json.dumps(data, indent=2, ensure_ascii=False))

注意点

  • GitHub情報はGitHubリポジトリが検出された場合のみJSON入力に含まれます(非GitHubリポジトリでは github フィールドは存在しません)
  • PR情報は、現在のブランチが追跡しているリモートブランチに対応するPRが存在する場合に含まれます
  • gh CLIがインストールされており、GitHubに認証済みであることが必要です
  • PRが見つからない場合は github.pr_number などのPR固有フィールドは含まれず、リポジトリ情報のみが提供されます
  • フィールドの存在チェックを行ってから利用することを推奨します(jq// empty や Python の dict.get() を活用)

関連情報