summaryrefslogtreecommitdiff
path: root/tools/ci/scheduled-ci-trigger/wait_for_workflows.py
blob: f840a7166bbea50e7f3f88e3c565cdeca2ce390e (plain)
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
#!/usr/bin/env python3
import os
import json
import requests
import time
import sys
from datetime import datetime, timezone

def wait_for_workflows_to_appear(github_token, repo, workflow_names, start_time, max_wait=300):
    """等待工作流出现在API中"""
    headers = {
        "Authorization": f"token {github_token}",
        "Accept": "application/vnd.github.v3+json"
    }
    
    print(f"Waiting for {len(workflow_names)} workflows to appear...")
    print(f"Start time: {start_time}")
    print(f"Max wait time: {max_wait} seconds")
    
    found_workflows = set()
    start_timestamp = time.time()
    
    while time.time() - start_timestamp < max_wait:
        all_found = True
        
        for workflow_name in workflow_names:
            if workflow_name in found_workflows:
                continue
                
            workflow_id = get_workflow_id(github_token, repo, workflow_name)
            if not workflow_id:
                print(f"Workflow {workflow_name} not found, skipping")
                found_workflows.add(workflow_name)
                continue
            
            # 检查是否有新的运行
            runs = get_recent_runs(github_token, repo, workflow_id, start_time)
            if runs:
                print(f"✓ Found new run for {workflow_name}: {runs[0]['id']}")
                found_workflows.add(workflow_name)
            else:
                print(f"⏳ Waiting for {workflow_name}...")
                all_found = False
        
        if all_found:
            print("✓ All workflows have started!")
            return True
        
        time.sleep(10)  # 每10秒检查一次
    
    print("⚠️ Timeout waiting for workflows to appear")
    print(f"Found {len(found_workflows)} out of {len(workflow_names)} workflows")
    return False

def get_workflow_id(github_token, repo, workflow_name):
    """获取工作流ID"""
    headers = {
        "Authorization": f"token {github_token}",
        "Accept": "application/vnd.github.v3+json"
    }
    
    url = f"https://api.github.com/repos/{repo}/actions/workflows"
    response = requests.get(url, headers=headers)
    
    if response.status_code == 200:
        workflows = response.json()["workflows"]
        for workflow in workflows:
            if workflow["name"] == workflow_name:
                return workflow["id"]
    return None

def get_recent_runs(github_token, repo, workflow_id, start_time):
    """获取开始时间后的运行"""
    headers = {
        "Authorization": f"token {github_token}",
        "Accept": "application/vnd.github.v3+json"
    }
    
    url = f"https://api.github.com/repos/{repo}/actions/workflows/{workflow_id}/runs"
    params = {"per_page": 5}
    
    response = requests.get(url, headers=headers, params=params)
    if response.status_code != 200:
        return []
    
    runs = response.json()["workflow_runs"]
    start_time_dt = datetime.fromisoformat(start_time.replace('Z', '+00:00'))
    
    recent_runs = []
    for run in runs:
        run_time = datetime.fromisoformat(run["created_at"].replace('Z', '+00:00'))
        if run_time >= start_time_dt:
            recent_runs.append(run)
    
    return recent_runs

def main():
    github_token = os.getenv("GITHUB_TOKEN")
    repo = os.getenv("GITHUB_REPOSITORY")
    workflows_json = os.getenv("TARGET_WORKFLOWS")
    start_time = sys.argv[1] if len(sys.argv) > 1 else datetime.now(timezone.utc).isoformat()
    
    if not all([github_token, repo, workflows_json]):
        raise ValueError("Missing required environment variables")
    
    workflows = json.loads(workflows_json)
    success = wait_for_workflows_to_appear(github_token, repo, workflows, start_time)
    
    if not success:
        print("Proceeding anyway, some workflows may not be detected...")

if __name__ == "__main__":
    main()