Repository navigation
Expand file tree
/
Copy pathresolve_upload_context.py
More file actions
281 lines (239 loc) · 8.96 KB
/
Copy pathresolve_upload_context.py
File metadata and controls
281 lines (239 loc) · 8.96 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python3
import json
import os
import subprocess
from collections.abc import Mapping
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, Optional
from urllib.parse import urlparse
@dataclass(frozen=True)
class UploadContext:
should_upload: bool
commit_oid: str = ""
ref: str = ""
pr_number: str = ""
annotation_level: str = ""
message: str = ""
@dataclass(frozen=True)
class PullRequestLookup:
number: str = ""
error: str = ""
warning: str = ""
@dataclass(frozen=True)
class PullRequestCandidate:
number: int
base_ref_name: str
def _parse_pull_request_lookup(
output: str,
repository: str,
ref_name: str,
default_branch: str,
) -> PullRequestLookup:
try:
pull_requests = json.loads(output)
except json.JSONDecodeError as error:
return PullRequestLookup(error=f"gh returned invalid JSON: {error.msg}")
if not isinstance(pull_requests, list):
return PullRequestLookup(error="gh returned invalid JSON: expected a list")
matching_pull_requests: list[PullRequestCandidate] = []
for pull_request in pull_requests:
if not isinstance(pull_request, dict):
return PullRequestLookup(
error="gh returned invalid JSON: expected pull request objects"
)
head_repository = pull_request.get("headRepository")
if not isinstance(head_repository, dict):
continue
head_repository_name = head_repository.get("nameWithOwner")
if (
pull_request.get("headRefName") != ref_name
or not isinstance(head_repository_name, str)
or head_repository_name.casefold() != repository.casefold()
):
continue
number = pull_request.get("number")
if not isinstance(number, int):
return PullRequestLookup(
error="gh returned invalid JSON: expected a pull request number"
)
base_ref_name = pull_request.get("baseRefName")
if not isinstance(base_ref_name, str):
return PullRequestLookup(
error="gh returned invalid JSON: expected a pull request base branch"
)
if base_ref_name == default_branch:
return PullRequestLookup(number=str(number))
matching_pull_requests.append(
PullRequestCandidate(number=number, base_ref_name=base_ref_name)
)
if len(matching_pull_requests) == 1:
return PullRequestLookup(number=str(matching_pull_requests[0].number))
if len(matching_pull_requests) > 1:
numbers = ", ".join(
str(candidate.number)
for candidate in sorted(
matching_pull_requests,
key=lambda candidate: candidate.number,
)
)
return PullRequestLookup(
warning=(
f"multiple open pull requests from {repository}:{ref_name} matched: "
f"{numbers}, but none uniquely targets the default branch "
f"{default_branch}"
)
)
return PullRequestLookup()
def _find_open_pull_request(environ: Mapping[str, str]) -> PullRequestLookup:
repository = environ.get("GITHUB_REPOSITORY", "")
ref_name = environ.get("GITHUB_REF_NAME", "")
default_branch = environ.get("COVERAGE_DEFAULT_BRANCH", "")
command_environment = dict(environ)
server_url = environ.get("GITHUB_SERVER_URL", "")
if server_url:
command_environment["GH_HOST"] = urlparse(server_url).netloc
try:
result = subprocess.run(
[
"gh",
"pr",
"list",
"--repo",
repository,
"--head",
ref_name,
"--state",
"open",
"--limit",
"1000",
"--json",
"number,headRefName,headRepository,baseRefName",
],
capture_output=True,
check=False,
env=command_environment,
text=True,
)
except OSError as error:
return PullRequestLookup(error=f"unable to run gh: {error}")
if result.returncode != 0:
return PullRequestLookup(
error=result.stderr.strip() or f"gh exited with status {result.returncode}"
)
return _parse_pull_request_lookup(
result.stdout,
repository,
ref_name,
default_branch,
)
def resolve_upload_context(
environ: Mapping[str, str],
pull_request_lookup: Optional[Callable[[Mapping[str, str]], PullRequestLookup]] = None,
) -> UploadContext:
env = dict(environ)
lookup_pull_request = (
_find_open_pull_request if pull_request_lookup is None else pull_request_lookup
)
event_name = env.get("GITHUB_EVENT_NAME", "")
repository = env.get("GITHUB_REPOSITORY", "")
ref = env.get("GITHUB_REF", "")
if event_name == "merge_group":
return UploadContext(
should_upload=False,
annotation_level="warning",
message=(
"Skipping coverage upload for merge queue. Coverage should be uploaded "
"for pull requests and the default branch instead."
),
)
if event_name in {"pull_request", "pull_request_target"}:
head_repository = env.get("COVERAGE_PR_HEAD_REPOSITORY", "")
if head_repository and head_repository != repository:
return UploadContext(
should_upload=False,
annotation_level="notice",
message=f"Skipping coverage upload for fork PR from {head_repository}.",
)
return UploadContext(
should_upload=True,
commit_oid=env.get("COVERAGE_PR_HEAD_SHA", ""),
pr_number=env.get("COVERAGE_PR_NUMBER", ""),
)
if event_name == "push":
default_branch = env.get("COVERAGE_DEFAULT_BRANCH", "")
default_branch_ref = f"refs/heads/{default_branch}"
is_branch_push = ref.startswith("refs/heads/")
is_non_default_branch = default_branch and is_branch_push and ref != default_branch_ref
if is_non_default_branch:
pull_request = lookup_pull_request(env)
if pull_request.number:
return UploadContext(
should_upload=True,
commit_oid=env.get("GITHUB_SHA", ""),
ref=ref,
pr_number=pull_request.number,
)
if pull_request.error:
return UploadContext(
should_upload=False,
annotation_level="error",
message=(
"Failed to query pull requests using the GitHub CLI: "
f"{pull_request.error}. Ensure gh is installed and the workflow "
"grants pull-requests: read permission."
),
)
if pull_request.warning:
return UploadContext(
should_upload=False,
annotation_level="warning",
message=(
f"Skipping coverage upload because {pull_request.warning}. "
"Use a pull_request workflow trigger for unambiguous per-PR "
"uploads."
),
)
return UploadContext(
should_upload=False,
annotation_level="notice",
message=(
"Skipping coverage upload for non-default branch push because no "
"open pull request was found."
),
)
return UploadContext(
should_upload=True,
commit_oid=env.get("GITHUB_SHA", ""),
ref=ref,
)
def _append_lines(file_path: str, lines: list[str]) -> None:
if not file_path:
return
with Path(file_path).open("a", encoding="utf-8") as stream:
for line in lines:
stream.write(f"{line}\n")
def main(environ: Optional[Mapping[str, str]] = None) -> int:
env = dict(os.environ if environ is None else environ)
context = resolve_upload_context(env)
output_file_path = env.get("GITHUB_OUTPUT", "")
summary_file_path = env.get("GITHUB_STEP_SUMMARY", "")
_append_lines(
output_file_path,
[
f"should_upload={str(context.should_upload).lower()}",
f"commit_oid={context.commit_oid}",
f"ref={context.ref}",
f"pr_number={context.pr_number}",
],
)
if not context.should_upload:
print(f"::{context.annotation_level}::{context.message}")
outcome = "failed" if context.annotation_level == "error" else "skipped"
_append_lines(
summary_file_path,
[f"### Code coverage upload {outcome}", "", context.message],
)
return 1 if context.annotation_level == "error" else 0
if __name__ == "__main__":
raise SystemExit(main())