#!/usr/bin/env python3 """ Daily PR Merge Status Sync Fetches open PRs from data-service-infra-ai-configurations and sends summary to Feishu. """ import os import json import re import requests from datetime import datetime # Load environment variables from bashrc try: with open(os.path.expanduser('~/.bashrc')) as f: for line in f: if 'FEISHU_CODE_EXPERT' in line and line.startswith('export'): match = re.search(r'export (\w+)=\"(.*?)\"', line) if match: key, value = match.groups() os.environ.setdefault(key, value) except Exception: # Ignore - bashrc loading is best-effort, missing vars handled downstream pass # Config REPO = "tidbcloud/data-service-infra-ai-configurations" FEISHU_CHAT_ID = "oc_f0e4a980f9ec5e0bcf2111856c7e981f" FEISHU_APP_ID = os.environ.get("FEISHU_CODE_EXPERT_APP_ID", "") FEISHU_APP_SECRET = os.environ.get("FEISHU_CODE_EXPERT_APP_SECRET", "") def get_feishu_token(): """Get Feishu tenant access token.""" resp = requests.post( "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal", json={"app_id": FEISHU_APP_ID, "app_secret": FEISHU_APP_SECRET} ) return resp.json().get("tenant_access_token") def send_feishu_message(token, chat_id, content): """Send message to Feishu chat.""" resp = requests.post( "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=chat_id", json={ "receive_id": chat_id, "msg_type": "text", "content": json.dumps({"text": content}) }, headers={"Authorization": f"Bearer {token}", "Content-Type": "application/json"} ) return resp.json() def get_github_prs(): """Get open PRs from GitHub using gh CLI.""" import subprocess result = subprocess.run( ["gh", "pr", "list", "--repo", REPO, "--state", "open", "--json", "number,title,author,createdAt,url"], capture_output=True, text=True ) if result.returncode != 0: print(f"gh error: {result.stderr}") return [] return json.loads(result.stdout) def main(): print(f"[{datetime.now()}] Starting daily PR merge status sync...") # Get open PRs prs = get_github_prs() if not prs: message = "✅ No open PRs in data-service-infra-ai-configurations" else: lines = [f"📋 Open PRs in {REPO} ({len(prs)} total):\n"] for pr in prs: author = pr.get("author", {}).get("login", "unknown") created = pr.get("createdAt", "")[:10] lines.append(f"• #{pr['number']} {pr['title']}") lines.append(f" Author: {author} | Created: {created}") lines.append(f" {pr['url']}\n") message = "\n".join(lines) print(message) # Send to Feishu if not FEISHU_APP_ID or not FEISHU_APP_SECRET: print("FEISHU_APP_ID or FEISHU_APP_SECRET not set, skipping notification") return token = get_feishu_token() if not token: print("Failed to get Feishu token") return result = send_feishu_message(token, FEISHU_CHAT_ID, message) if result.get("code") == 0: print("✅ Feishu message sent successfully") else: print(f"❌ Feishu error: {result}") if __name__ == "__main__": main()