-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_watcher.sh
More file actions
executable file
·173 lines (149 loc) · 5.74 KB
/
Copy pathgithub_watcher.sh
File metadata and controls
executable file
·173 lines (149 loc) · 5.74 KB
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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
# github_watcher.sh — Auto-Discovery: задачи из GitHub Issues
#
# Purpose:
# Каждые 5 минут (cron) через `gh api` находит открытые issues с label 'agentforge'.
# Для каждого *нового* (не seen ранее) создаёт задачу в AgentForge
# через POST /tasks с тегами ["github", "automation", "discovery"].
# Использует state-файл чтобы не дублировать задачи.
#
# Integration:
# - Работает с gateway (Rust Axum :9090, replaces task_queue.py :8080)
# - Похож по стилю на watchdog.sh (inline python + bash logging)
# - Создаваемые задачи сразу попадают в dispatcher / resolve_agent
#
# Usage (manual):
# GH_REPO=owner/repo ./github_watcher.sh
#
# Crontab (автоматически добавляется скриптом установки):
# */5 * * * * bash /home/eveselove/agentforge/github_watcher.sh >> /home/eveselove/agentforge/logs/github_watcher.log 2>&1
#
# Config (env):
# GH_REPO — репозиторий для мониторинга (по умолчанию agx/planlytasksko)
# API — база API (по умолчанию http://localhost:9090)
#
# Tags: github,automation,discovery
set -euo pipefail
API="${API:-http://localhost:9090}"
LOG_DIR="/home/eveselove/agentforge/logs"
DATA_DIR="/home/eveselove/agentforge/data"
SEEN_FILE="$DATA_DIR/seen_agentforge_issues.txt"
# Определяем репозиторий (можно переопределить GH_REPO=... в cron или окружении)
GH_REPO="${GH_REPO:-}"
if [[ -z "$GH_REPO" ]]; then
# Пытаемся автоопределить через gh (если cwd — git-репо с remote)
GH_REPO=$(gh repo view --json nameWithOwner -q .nameWithOwner 2>/dev/null || true)
fi
if [[ -z "$GH_REPO" ]]; then
GH_REPO="eveselove/planlytasksko"
fi
mkdir -p "$LOG_DIR" "$DATA_DIR"
touch "$SEEN_FILE"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_DIR/github_watcher.log" >&2
}
log "=== github_watcher START (repo=${GH_REPO}) ==="
if ! command -v gh >/dev/null 2>&1; then
log "ERROR: gh CLI не найден. Установи GitHub CLI и выполни gh auth login"
exit 1
fi
# Получаем открытые issues с лейблом agentforge (до 50 последних)
# gh api возвращает JSON; фильтруем только нужные поля
issues_json=$(gh api \
"repos/${GH_REPO}/issues?labels=agentforge&state=open&per_page=50&sort=created&direction=desc" \
--jq '[.[] | {number, title, body, html_url, created_at, user: .user.login}]' 2>/dev/null || echo '[]')
count=$(echo "$issues_json" | python3 -c 'import sys,json; print(len(json.load(sys.stdin)))' 2>/dev/null || echo 0)
if [[ "$count" -eq 0 ]]; then
log "Нет открытых issues с label 'agentforge' в ${GH_REPO}"
log "=== github_watcher END (nothing to do) ==="
exit 0
fi
log "Найдено $count открытых agentforge issues. Проверяем новые..."
# Основная логика на Python (удобно работать с JSON + HTTP + state)
echo "$issues_json" | python3 -c '
import sys, json, urllib.request, os, time
API = "'"$API"'"
SEEN_FILE = "'"$SEEN_FILE"'"
GH_REPO = "'"$GH_REPO"'"
# Загружаем seen (один ключ на строку: owner/repo#123)
seen = set()
try:
with open(SEEN_FILE, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line:
seen.add(line)
except FileNotFoundError:
pass
issues = json.load(sys.stdin)
created_count = 0
errors = []
for issue in issues:
num = int(issue.get("number", 0))
if not num:
continue
key = f"{GH_REPO}#{num}"
if key in seen:
continue
title = (issue.get("title") or f"GitHub Issue #{num}").strip()[:200]
body = (issue.get("body") or "").strip()
url = issue.get("html_url") or f"https://github.com/{GH_REPO}/issues/{num}"
user = issue.get("user") or "unknown"
created_at = issue.get("created_at") or ""
description = f"""{body}
---
**Источник:** {url}
**Автор:** @{user}
**Создано в GitHub:** {created_at}
Авто-дискавери через github_watcher.sh (label=agentforge)
"""
payload = {
"title": title,
"description": description,
"priority": "medium",
"complexity": "medium",
"preferred_agent": "auto",
"tags": ["github", "automation", "discovery"]
}
data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
try:
req = urllib.request.Request(
f"{API}/tasks",
data=data,
headers={"Content-Type": "application/json"},
method="POST"
)
with urllib.request.urlopen(req, timeout=20) as resp:
status = resp.status
if status in (200, 201):
with open(SEEN_FILE, "a", encoding="utf-8") as sf:
sf.write(key + "\n")
print(f"CREATED|{key}|{title}")
created_count += 1
else:
body_err = resp.read().decode("utf-8", errors="replace")[:200]
print(f"FAIL_HTTP|{key}|HTTP {status}: {body_err}")
errors.append(key)
except Exception as e:
print(f"FAIL|{key}|{str(e)[:120]}")
errors.append(key)
# Выводим итог для парсинга в bash
print(f"SUMMARY|created={created_count}|errors={len(errors)}|total_seen={len(seen)+created_count}")
' | while IFS='|' read -r ACTION REST; do
case "$ACTION" in
CREATED)
IFS='|' read -r key title <<< "$REST"
log "✅ Создана задача из ${key}: ${title}"
;;
FAIL*)
log "❌ Ошибка создания ${REST}"
;;
SUMMARY)
log "ИТОГ: ${REST}"
;;
*)
# игнорируем прочее
;;
esac
done
log "=== github_watcher END ==="