警告
この記事はAIに書かせており、記載の内容は人間が検証してません
サブモジュール監視・自動化ガイド
1. 監視システムの概要
サブモジュールの更新を検知し、自動または半自動で処理するシステム。GitHub Actions、ローカル環境、Claude APIを組み合わせた包括的なソリューション。
技術的制約
- Gitサブモジュールは独立したリポジトリ
- 親リポジトリは特定のコミットハッシュのみを追跡
- Claude Codeは対話型ツール(定期実行不可)
2. GitHub Actions による自動化
PR監視システム
# .github/workflows/submodule-monitor.yml
name: Submodule Manager
on:
schedule:
- cron: '0 */6 * * *' # 6時間ごと
repository_dispatch:
types: [submodule-updated]
workflow_dispatch:
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Check for updates
run: |
git submodule foreach '
cd $toplevel/$sm_path
git fetch origin
CURRENT=$(git rev-parse HEAD)
LATEST=$(git rev-parse origin/main)
if [ "$CURRENT" != "$LATEST" ]; then
echo "::warning::Update available for $name"
echo "UPDATE_NEEDED=true" >> $GITHUB_ENV
fi
'
- name: Create PR if updates needed
if: env.UPDATE_NEEDED == 'true'
uses: peter-evans/create-pull-request@v5
with:
title: "🔄 Submodule updates available"
body: "Automated submodule update check found new versions"
サブモジュール側からの通知
# サブモジュール側: .github/workflows/notify-parent.yml
name: Notify Parent Repository
on:
pull_request:
types: [closed]
push:
branches: [main]
jobs:
notify:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- name: Notify parent repositories
uses: actions/github-script@v7
with:
github-token: ${{ secrets.PARENT_REPO_TOKEN }}
script: |
const parentRepos = [
{ owner: 'org', repo: 'parent-repo-1' }
];
for (const parent of parentRepos) {
await github.rest.issues.create({
owner: parent.owner,
repo: parent.repo,
title: `[Submodule Update] ${context.repo.repo}`,
body: `更新があります: ${context.sha}`,
labels: ['submodule-update']
});
}
3. ローカル環境での監視
macOS launchd 設定
<!-- ~/Library/LaunchAgents/com.user.submodule-monitor.plist -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.submodule-monitor</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>/path/to/monitor-submodules.sh</string>
</array>
<key>StartInterval</key>
<integer>3600</integer>
</dict>
</plist>
監視スクリプト
#!/bin/bash
# scripts/monitor-submodules.sh
PROJECT_DIR="/path/to/project"
cd "$PROJECT_DIR"
# 更新チェック
updates=""
git submodule foreach '
git fetch origin -q
LOCAL=$(git rev-parse HEAD)
REMOTE=$(git rev-parse origin/main)
if [ "$LOCAL" != "$REMOTE" ]; then
updates="$updates\n- $name: 更新あり"
fi
'
if [ -n "$updates" ]; then
# macOS通知
osascript -e "display notification \"サブモジュールの更新があります\" with title \"Submodule Monitor\""
# ファイルに記録
echo -e "$(date)\n$updates" > .submodule-updates
fi
シェル統合
# ~/.zshrc または ~/.bashrc に追加
# エイリアス
alias cs='check-submodules'
alias csf='claude "サブモジュールの更新を確認してください"'
# プロンプト統合
check_submodules_on_prompt() {
if [ -f ".submodule-updates" ]; then
echo "⚠️ サブモジュール更新あり"
fi
}
PROMPT='$(check_submodules_on_prompt)$ '
# 関数
check-submodules() {
echo "サブモジュールの更新を確認中..."
git submodule foreach 'git fetch origin -q && git status -sb'
if [ "$1" = "--full" ]; then
claude "サブモジュールの更新を確認し、必要に応じて更新してください"
fi
}
4. Claude API 統合
Claude API vs Claude Code
| Claude Code (CLI) | Claude API |
|---|---|
| ローカル専用 | クラウド実行可能 |
| ファイル直接操作 | テキスト処理のみ |
| インタラクティブ | ステートレス |
| Claude Pro料金 | API従量課金 |
GitHub Actions での Claude API 実装
# scripts/claude_analyzer.py
import os
import anthropic
from github import Github
def analyze_submodule_updates(updates_text):
"""Claude APIでサブモジュール更新を分析"""
client = anthropic.Anthropic(
api_key=os.environ['ANTHROPIC_API_KEY']
)
response = client.messages.create(
model="claude-3-sonnet-20240229",
max_tokens=1000,
messages=[{
"role": "user",
"content": f"""
以下の更新を分析してください:
{updates_text}
1. 破壊的変更の可能性
2. 更新の緊急度
3. 推奨アクション
"""
}]
)
return response.content[0].text
def create_github_issue(analysis):
"""分析結果を基にIssue作成"""
g = Github(os.environ['GITHUB_TOKEN'])
repo = g.get_repo(os.environ['GITHUB_REPOSITORY'])
issue = repo.create_issue(
title="[Claude AI] サブモジュール更新分析",
body=f"分析結果:\n{analysis}",
labels=['submodule-update', 'ai-analysis']
)
return issue.number
# .github/workflows/claude-analysis.yml
- name: Analyze with Claude API
if: steps.check.outputs.updates != ''
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
python scripts/claude_analyzer.py "${{ steps.check.outputs.updates }}"
5. Git Hooks による検知
#!/bin/bash
# .git/hooks/post-merge
# .git/hooks/post-checkout
check_submodule_updates() {
echo "🔍 サブモジュールの更新を確認中..."
updates=$(git submodule status | grep '^+' | wc -l)
if [ "$updates" -gt 0 ]; then
echo "⚠️ $updates 個のサブモジュールに更新があります"
echo "実行: claude 'サブモジュールの更新を確認してください'"
# 更新情報を保存
git submodule status > .submodule-status
fi
}
check_submodule_updates
6. 統合ワークフロー
7. 実装優先度
🥇 簡単・即効性あり
-
シェルエイリアス設定
alias cs='claude "サブモジュール更新確認"' -
Git Hooks設定
- pull/merge時に自動チェック
🥈 中程度の実装
-
GitHub Actions定期チェック
- 6-12時間ごとの自動確認
-
ローカル通知システム
- launchd/cronでデスクトップ通知
🥉 高度な実装
-
Claude API統合
- 自動分析と提案
-
Webhook連携
- リアルタイム通知
8. コスト最適化
API使用量管理
class ClaudeAPIManager:
def __init__(self, monthly_budget=50):
self.budget = monthly_budget
def should_use_claude(self, priority='normal'):
priorities = {
'critical': 0.1, # 予算の10%まで
'high': 0.3,
'normal': 0.5,
'low': 0.8
}
threshold = self.budget * priorities[priority]
return self.get_usage() < threshold
キャッシュ戦略
- uses: actions/cache@v3
with:
path: .claude-cache
key: claude-analysis-${{ hashFiles('**/*.json') }}
9. トラブルシューティング
ローカル監視が動かない
- launchd plist の権限確認
- スクリプトの実行権限 (
chmod +x)
GitHub Actions が失敗
- Secrets設定確認 (ANTHROPIC_API_KEY)
- 権限設定確認 (permissions)
通知が多すぎる
- 通知頻度の調整
- 重要度でフィルタリング
10. まとめ
推奨構成
小規模プロジェクト
- Git Hooks + シェルエイリアス
中規模プロジェクト
- GitHub Actions定期チェック + ローカル通知
大規模プロジェクト
- Claude API統合 + Webhook連携 + ダッシュボード
完全自動化は困難だが、これらの組み合わせで実用的な半自動システムが構築可能。