|
19 | 19 | """ |
20 | 20 |
|
21 | 21 | import sys |
| 22 | +import os |
| 23 | +import json |
| 24 | +import uuid |
22 | 25 | import requests |
23 | 26 |
|
24 | 27 | # need to install via pip |
25 | | -import hjson |
| 28 | +try: |
| 29 | + import hjson |
| 30 | +except ImportError: |
| 31 | + print("Error: hjson module not found. Please install it with: pip install hjson", file=sys.stderr) |
| 32 | + sys.exit(1) |
26 | 33 |
|
27 | 34 | def get_pr_json(pr_num): |
28 | | - return requests.get(f'https://api.github.com/repos/dashpay/dash/pulls/{pr_num}').json() |
| 35 | + # Get repository from environment or default to dashpay/dash |
| 36 | + repo = os.environ.get('GITHUB_REPOSITORY', 'dashpay/dash') |
| 37 | + |
| 38 | + try: |
| 39 | + response = requests.get(f'https://api.github.com/repos/{repo}/pulls/{pr_num}') |
| 40 | + response.raise_for_status() |
| 41 | + pr_data = response.json() |
| 42 | + |
| 43 | + # Check if we got an error response |
| 44 | + if 'message' in pr_data and 'head' not in pr_data: |
| 45 | + print(f"Warning: GitHub API error for PR {pr_num}: {pr_data.get('message', 'Unknown error')}", file=sys.stderr) |
| 46 | + return None |
| 47 | + |
| 48 | + return pr_data |
| 49 | + except requests.RequestException as e: |
| 50 | + print(f"Warning: Error fetching PR {pr_num}: {e}", file=sys.stderr) |
| 51 | + return None |
| 52 | + except json.JSONDecodeError as e: |
| 53 | + print(f"Warning: Error parsing JSON for PR {pr_num}: {e}", file=sys.stderr) |
| 54 | + return None |
| 55 | + |
| 56 | +def set_github_output(name, value): |
| 57 | + """Set GitHub Actions output""" |
| 58 | + if 'GITHUB_OUTPUT' not in os.environ: |
| 59 | + print(f"Warning: GITHUB_OUTPUT not set, skipping output: {name}={value}", file=sys.stderr) |
| 60 | + return |
| 61 | + |
| 62 | + try: |
| 63 | + with open(os.environ['GITHUB_OUTPUT'], 'a', encoding='utf8') as f: |
| 64 | + # For multiline values, use the delimiter syntax |
| 65 | + if '\n' in str(value): |
| 66 | + delimiter = f"EOF_{uuid.uuid4()}" |
| 67 | + f.write(f"{name}<<{delimiter}\n{value}\n{delimiter}\n") |
| 68 | + else: |
| 69 | + f.write(f"{name}={value}\n") |
| 70 | + except IOError as e: |
| 71 | + print(f"Error writing to GITHUB_OUTPUT: {e}", file=sys.stderr) |
29 | 72 |
|
30 | 73 | def main(): |
31 | 74 | if len(sys.argv) != 2: |
32 | 75 | print(f'Usage: {sys.argv[0]} <conflicts>', file=sys.stderr) |
33 | 76 | sys.exit(1) |
34 | 77 |
|
35 | | - input = sys.argv[1] |
36 | | - print(input) |
37 | | - j_input = hjson.loads(input) |
38 | | - print(j_input) |
| 78 | + conflict_input = sys.argv[1] |
39 | 79 |
|
| 80 | + try: |
| 81 | + j_input = hjson.loads(conflict_input) |
| 82 | + except Exception as e: |
| 83 | + print(f"Error parsing input JSON: {e}", file=sys.stderr) |
| 84 | + sys.exit(1) |
| 85 | + |
| 86 | + # Validate required fields |
| 87 | + if 'pull_number' not in j_input: |
| 88 | + print("Error: 'pull_number' field missing from input", file=sys.stderr) |
| 89 | + sys.exit(1) |
| 90 | + if 'conflictPrs' not in j_input: |
| 91 | + print("Error: 'conflictPrs' field missing from input", file=sys.stderr) |
| 92 | + sys.exit(1) |
40 | 93 |
|
41 | 94 | our_pr_num = j_input['pull_number'] |
42 | | - our_pr_label = get_pr_json(our_pr_num)['head']['label'] |
43 | | - conflictPrs = j_input['conflictPrs'] |
| 95 | + our_pr_json = get_pr_json(our_pr_num) |
| 96 | + |
| 97 | + if our_pr_json is None: |
| 98 | + print(f"Error: Failed to fetch PR {our_pr_num}", file=sys.stderr) |
| 99 | + sys.exit(1) |
| 100 | + |
| 101 | + if 'head' not in our_pr_json or 'label' not in our_pr_json['head']: |
| 102 | + print(f"Error: Invalid PR data structure for PR {our_pr_num}", file=sys.stderr) |
| 103 | + sys.exit(1) |
| 104 | + |
| 105 | + our_pr_label = our_pr_json['head']['label'] |
| 106 | + conflict_prs = j_input['conflictPrs'] |
44 | 107 |
|
45 | 108 | good = [] |
46 | 109 | bad = [] |
| 110 | + conflict_details = [] |
| 111 | + |
| 112 | + for conflict in conflict_prs: |
| 113 | + if 'number' not in conflict: |
| 114 | + print("Warning: Skipping conflict entry without 'number' field", file=sys.stderr) |
| 115 | + continue |
47 | 116 |
|
48 | | - for conflict in conflictPrs: |
49 | 117 | conflict_pr_num = conflict['number'] |
50 | | - print(conflict_pr_num) |
51 | 118 |
|
52 | 119 | conflict_pr_json = get_pr_json(conflict_pr_num) |
| 120 | + |
| 121 | + if conflict_pr_json is None: |
| 122 | + print(f"Warning: Failed to fetch PR {conflict_pr_num}, skipping", file=sys.stderr) |
| 123 | + continue |
| 124 | + |
| 125 | + if 'head' not in conflict_pr_json or 'label' not in conflict_pr_json['head']: |
| 126 | + print(f"Warning: Invalid PR data structure for PR {conflict_pr_num}, skipping", file=sys.stderr) |
| 127 | + continue |
| 128 | + |
53 | 129 | conflict_pr_label = conflict_pr_json['head']['label'] |
54 | | - print(conflict_pr_label) |
55 | 130 |
|
56 | | - if conflict_pr_json['mergeable_state'] == "dirty": |
57 | | - print(f'{conflict_pr_num} needs rebase. Skipping conflict check') |
| 131 | + if conflict_pr_json.get('mergeable_state') == "dirty": |
| 132 | + print(f'PR #{conflict_pr_num} needs rebase. Skipping conflict check', file=sys.stderr) |
58 | 133 | continue |
59 | 134 |
|
60 | | - if conflict_pr_json['draft']: |
61 | | - print(f'{conflict_pr_num} is a draft. Skipping conflict check') |
| 135 | + if conflict_pr_json.get('draft', False): |
| 136 | + print(f'PR #{conflict_pr_num} is a draft. Skipping conflict check', file=sys.stderr) |
| 137 | + continue |
| 138 | + |
| 139 | + # Get repository from environment |
| 140 | + repo = os.environ.get('GITHUB_REPOSITORY', 'dashpay/dash') |
| 141 | + merge_check_url = f'https://github.com/{repo}/branches/pre_mergeable/{our_pr_label}...{conflict_pr_label}' |
| 142 | + |
| 143 | + try: |
| 144 | + pre_mergeable = requests.get(merge_check_url) |
| 145 | + pre_mergeable.raise_for_status() |
| 146 | + except requests.RequestException as e: |
| 147 | + print(f"Error checking mergeability for PR {conflict_pr_num}: {e}", file=sys.stderr) |
62 | 148 | continue |
63 | 149 |
|
64 | | - pre_mergeable = requests.get(f'https://github.com/dashpay/dash/branches/pre_mergeable/{our_pr_label}...{conflict_pr_label}') |
65 | 150 | if "These branches can be automatically merged." in pre_mergeable.text: |
66 | 151 | good.append(conflict_pr_num) |
67 | | - elif "Can’t automatically merge" in pre_mergeable.text: |
| 152 | + elif "Can't automatic" in pre_mergeable.text or "octicon octicon-x" in pre_mergeable.text: |
| 153 | + # Check for partial text or the X icon which indicates conflicts |
68 | 154 | bad.append(conflict_pr_num) |
| 155 | + conflict_details.append({ |
| 156 | + 'number': conflict_pr_num, |
| 157 | + 'title': conflict_pr_json.get('title', 'Unknown'), |
| 158 | + 'url': conflict_pr_json.get('html_url', f'https://github.com/{repo}/pull/{conflict_pr_num}') |
| 159 | + }) |
| 160 | + else: |
| 161 | + print(f"Warning: Unexpected response for PR {conflict_pr_num} mergeability check. URL: {pre_mergeable.url}", file=sys.stderr) |
| 162 | + |
| 163 | + print(f"Not conflicting PRs: {good}", file=sys.stderr) |
| 164 | + print(f"Conflicting PRs: {bad}", file=sys.stderr) |
| 165 | + |
| 166 | + # Set GitHub Actions outputs |
| 167 | + if 'GITHUB_OUTPUT' in os.environ: |
| 168 | + set_github_output('has_conflicts', 'true' if len(bad) > 0 else 'false') |
| 169 | + |
| 170 | + # Format conflict details as markdown list |
| 171 | + if conflict_details: |
| 172 | + markdown_list = [] |
| 173 | + for conflict in conflict_details: |
| 174 | + markdown_list.append(f"- #{conflict['number']} - [{conflict['title']}]({conflict['url']})") |
| 175 | + conflict_markdown = '\n'.join(markdown_list) |
| 176 | + set_github_output('conflict_details', conflict_markdown) |
69 | 177 | else: |
70 | | - raise Exception("not mergeable or unmergable!") |
| 178 | + set_github_output('conflict_details', '') |
71 | 179 |
|
72 | | - print("Not conflicting PRs: ", good) |
| 180 | + set_github_output('conflicting_prs', ','.join(map(str, bad))) |
73 | 181 |
|
74 | | - print("Conflicting PRs: ", bad) |
75 | 182 | if len(bad) > 0: |
76 | 183 | sys.exit(1) |
77 | 184 |
|
|
0 commit comments