|
10 | 10 | import json |
11 | 11 | import math |
12 | 12 | from pathlib import Path |
| 13 | +import os |
13 | 14 | import re |
| 15 | +import shutil |
14 | 16 | import subprocess |
15 | 17 | import time |
16 | 18 | import harness_blocks |
@@ -121,12 +123,55 @@ def delivery_time(lane, cfg, clock): |
121 | 123 | return newest |
122 | 124 |
|
123 | 125 |
|
| 126 | +# ~/dev/.bin is where the M4 keeps gh (its only copy there); ~/.local/bin is first on the m5, as in launch.sh. |
| 127 | +GH_FALLBACKS = (os.path.expanduser('~/.local/bin/gh'), os.path.expanduser('~/dev/.bin/gh'), |
| 128 | + '/opt/homebrew/bin/gh', '/usr/local/bin/gh') |
| 129 | + |
| 130 | + |
| 131 | +def gh_binary(): |
| 132 | + """The gh to run. A scheduled job has a bare PATH, and `gh` was then a FileNotFoundError warning 144 times |
| 133 | + a day. RH_GH wins; then PATH (the bare name, so subprocess resolves it the same way); then the usual |
| 134 | + install locations. Nothing found returns 'gh', which fails the way it always did.""" |
| 135 | + if os.environ.get('RH_GH'): |
| 136 | + return os.environ['RH_GH'] |
| 137 | + if shutil.which('gh'): |
| 138 | + return 'gh' |
| 139 | + for path in GH_FALLBACKS: |
| 140 | + if os.access(path, os.X_OK) and os.path.isfile(path): |
| 141 | + return path |
| 142 | + return 'gh' |
| 143 | + |
| 144 | + |
| 145 | +def warn_once_a_day(lane, reason): |
| 146 | + """cadence.public-activity warns at most once per lane per day. The state file (day per lane and reason) |
| 147 | + sits next to the harness-blocks ledger. A state file that cannot be read or written never hides a warning.""" |
| 148 | + path = harness_blocks.ledger_path().parent / 'cadence-public-warned.json' |
| 149 | + today = dt.datetime.now(UTC).date().isoformat() |
| 150 | + key = '{}|{}'.format(lane.name, reason) |
| 151 | + try: |
| 152 | + seen = json.loads(path.read_text()) |
| 153 | + if not isinstance(seen, dict): |
| 154 | + seen = {} |
| 155 | + except (OSError, ValueError): |
| 156 | + seen = {} |
| 157 | + if seen.get(key) == today: |
| 158 | + return |
| 159 | + harness_blocks.warn('cadence.public-activity', reason, lane=lane.name) |
| 160 | + try: |
| 161 | + seen = {k: v for k, v in seen.items() if v == today} |
| 162 | + seen[key] = today |
| 163 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 164 | + path.write_text(json.dumps(seen, sort_keys=True)) |
| 165 | + except OSError: |
| 166 | + pass |
| 167 | + |
| 168 | + |
124 | 169 | def github_delivery_time(clock, since=None): |
125 | 170 | """Latest authored comment on our public PRs/issues, or public PR review.""" |
126 | 171 | deadline = time.monotonic() + 15 |
127 | 172 |
|
128 | 173 | def api(*args): |
129 | | - return subprocess.run(['gh', 'api', '--hostname', 'github.com', *args], |
| 174 | + return subprocess.run([gh_binary(), 'api', '--hostname', 'github.com', *args], |
130 | 175 | check=True, capture_output=True, text=True, |
131 | 176 | timeout=max(0, deadline - time.monotonic())).stdout |
132 | 177 |
|
@@ -184,14 +229,12 @@ def public_delivery_time(lane, cfg, clock): |
184 | 229 | when = delivery_time(lane, cfg, clock) |
185 | 230 | except (OSError, ValueError, OverflowError) as exc: |
186 | 231 | complete = False |
187 | | - harness_blocks.warn('cadence.public-activity', |
188 | | - 'could not read the delivery ledger ({})'.format(type(exc).__name__), lane=lane.name) |
| 232 | + warn_once_a_day(lane, 'could not read the delivery ledger ({})'.format(type(exc).__name__)) |
189 | 233 | try: |
190 | 234 | activity = github_delivery_time(clock, since=when) |
191 | 235 | except (OSError, subprocess.SubprocessError, ValueError, TypeError, KeyError, AttributeError) as exc: |
192 | 236 | complete = False |
193 | | - harness_blocks.warn('cadence.public-activity', |
194 | | - 'could not read public GitHub activity ({})'.format(type(exc).__name__), lane=lane.name) |
| 237 | + warn_once_a_day(lane, 'could not read public GitHub activity ({})'.format(type(exc).__name__)) |
195 | 238 | else: |
196 | 239 | if activity is not None: |
197 | 240 | when = activity |
|
0 commit comments