-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_pull_request_context.py
More file actions
293 lines (229 loc) · 8.97 KB
/
Copy pathtest_pull_request_context.py
File metadata and controls
293 lines (229 loc) · 8.97 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
282
283
284
285
286
287
288
289
290
291
292
293
import pytest
from socketsecurity.core.pull_request import (
parse_pull_request_number,
resolve_pull_request_context,
)
@pytest.mark.parametrize(
"value",
# "false" is what Buildkite puts in BUILDKITE_PULL_REQUEST on a branch build.
# main_code stores this result rather than the raw value, because the string
# is truthy and GithubConfig would read it as a real pull request number.
["false", "0", "", None, "-1", "not-a-number"],
)
def test_non_pull_request_values_canonicalize_to_zero(value):
assert parse_pull_request_number(value) == 0
def test_pull_request_numbers_survive_canonicalization():
assert parse_pull_request_number("42") == 42
assert parse_pull_request_number(42) == 42
def test_explicit_pr_number_wins_over_detected_context():
context = resolve_pull_request_context(
"github",
"42",
"acme/widgets",
configured_explicit=True,
env={
"GITHUB_REF": "refs/pull/99/merge",
"GITHUB_REPOSITORY": "acme/widgets",
},
)
assert context.number == 42
assert context.url == "https://github.com/acme/widgets/pull/42"
def test_explicit_zero_disables_pr_auto_detection():
context = resolve_pull_request_context(
"github",
"0",
"acme/widgets",
configured_explicit=True,
env={"GITHUB_REF": "refs/pull/99/merge"},
)
assert context.number == 0
assert context.url is None
def test_buildkite_non_pr_sentinel_is_treated_as_no_pull_request():
context = resolve_pull_request_context(
"github",
"false",
"acme/widgets",
configured_explicit=True,
env={},
)
assert context.number == 0
assert context.url is None
def test_github_context_is_detected_from_actions_environment():
context = resolve_pull_request_context(
"github",
"0",
None,
env={
"GITHUB_REF": "refs/pull/123/merge",
"GITHUB_REPOSITORY": "acme/widgets",
"GITHUB_SERVER_URL": "https://github.example.com",
},
)
assert context.number == 123
assert context.url == "https://github.example.com/acme/widgets/pull/123"
def test_gitlab_context_is_detected_from_merge_request_environment():
context = resolve_pull_request_context(
"gitlab",
"0",
None,
env={
"CI_MERGE_REQUEST_IID": "81",
"CI_PROJECT_URL": "https://gitlab.example.com/acme/widgets",
},
)
assert context.number == 81
assert context.url == "https://gitlab.example.com/acme/widgets/-/merge_requests/81"
def test_azure_repos_context_uses_pull_request_id():
context = resolve_pull_request_context(
"azure",
"0",
None,
env={
"SYSTEM_PULLREQUEST_PULLREQUESTID": "17",
"BUILD_REPOSITORY_URI": "https://dev.azure.com/acme/platform/_git/widgets",
},
)
assert context.number == 17
assert context.url == "https://dev.azure.com/acme/platform/_git/widgets/pullrequest/17"
def test_azure_fork_context_uses_target_repository_url():
context = resolve_pull_request_context(
"azure",
"0",
None,
env={
"SYSTEM_PULLREQUEST_PULLREQUESTID": "17",
"BUILD_REPOSITORY_URI": "https://dev.azure.com/acme/platform/_git/widgets",
"SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI": (
"https://dev.azure.com/contributor/forks/_git/widgets"
),
},
)
assert context.number == 17
assert context.url == "https://dev.azure.com/acme/platform/_git/widgets/pullrequest/17"
def test_azure_pipeline_with_github_repo_uses_pull_request_number():
context = resolve_pull_request_context(
"azure",
"0",
None,
env={
"SYSTEM_PULLREQUEST_PULLREQUESTNUMBER": "23",
"SYSTEM_PULLREQUEST_PULLREQUESTID": "98765",
"BUILD_REPOSITORY_URI": "https://github.com/acme/widgets.git",
},
)
assert context.number == 23
assert context.url == "https://github.com/acme/widgets/pull/23"
def test_explicit_azure_github_pr_number_still_uses_github_url_shape():
context = resolve_pull_request_context(
"azure",
"23",
None,
configured_explicit=True,
env={"BUILD_REPOSITORY_URI": "https://github.com/acme/widgets.git"},
)
assert context.number == 23
assert context.url == "https://github.com/acme/widgets/pull/23"
def test_non_pr_run_has_no_context():
context = resolve_pull_request_context("azure", "0", "acme/widgets", env={})
assert context.number == 0
assert context.url is None
# ---------------------------------------------------------------------------
# Provider-neutral CI (Buildkite). The provider comes from --integration and the
# PR number from --pr-number; only the repository slug has to be recovered from
# the checkout URL, because config.repo is a bare repository name with no owner.
# ---------------------------------------------------------------------------
def test_buildkite_github_repo_url_is_derived_from_the_checkout_remote():
context = resolve_pull_request_context(
"github",
"42",
"widgets",
configured_explicit=True,
env={"BUILDKITE_REPO": "git@github.com:acme/widgets.git"},
)
assert context.number == 42
assert context.url == "https://github.com/acme/widgets/pull/42"
def test_buildkite_github_enterprise_host_is_taken_from_the_remote():
context = resolve_pull_request_context(
"github",
"42",
"widgets",
configured_explicit=True,
env={"BUILDKITE_REPO": "https://github.example.com/acme/widgets.git"},
)
assert context.url == "https://github.example.com/acme/widgets/pull/42"
@pytest.mark.parametrize(
"server",
["javascript:alert(1)", "notaurl", "ftp://example.com", "https://", ""],
)
def test_unusable_github_server_url_falls_back_to_the_default(server):
"""The result becomes a diff scan's external_href, so validate before composing."""
context = resolve_pull_request_context(
"github",
"42",
"acme/widgets",
configured_explicit=True,
env={"GITHUB_SERVER_URL": server, "GITHUB_REPOSITORY": "acme/widgets"},
)
assert context.url == "https://github.com/acme/widgets/pull/42"
@pytest.mark.parametrize("server", ["javascript:alert(1)", "notaurl", "ftp://example.com"])
def test_unusable_gitlab_server_url_yields_no_link(server):
"""GitLab has no public default host to fall back to, so the link is dropped."""
context = resolve_pull_request_context(
"gitlab",
"42",
"acme/widgets",
configured_explicit=True,
env={"CI_SERVER_URL": server, "CI_PROJECT_PATH": "acme/widgets"},
)
assert context.number == 42
assert context.url is None
def test_self_hosted_server_urls_are_still_honored():
assert resolve_pull_request_context(
"github", "42", None, configured_explicit=True,
env={"GITHUB_SERVER_URL": "https://github.example.com", "GITHUB_REPOSITORY": "acme/widgets"},
).url == "https://github.example.com/acme/widgets/pull/42"
assert resolve_pull_request_context(
"gitlab", "42", None, configured_explicit=True,
env={"CI_SERVER_URL": "http://gitlab.internal", "CI_PROJECT_PATH": "acme/platform/widgets"},
).url == "http://gitlab.internal/acme/platform/widgets/-/merge_requests/42"
def test_github_actions_environment_wins_over_the_checkout_remote():
context = resolve_pull_request_context(
"github",
"42",
"widgets",
configured_explicit=True,
env={
"GITHUB_REPOSITORY": "acme/widgets",
"GITHUB_SERVER_URL": "https://github.example.com",
"BUILDKITE_REPO": "git@github.com:stale/mirror.git",
},
)
assert context.url == "https://github.example.com/acme/widgets/pull/42"
def test_buildkite_gitlab_repo_url_keeps_nested_subgroups():
context = resolve_pull_request_context(
"gitlab",
"81",
"widgets",
configured_explicit=True,
env={"BUILDKITE_REPO": "ssh://git@gitlab.example.com/acme/platform/widgets.git"},
)
assert context.url == "https://gitlab.example.com/acme/platform/widgets/-/merge_requests/81"
def test_gitlab_ci_project_url_wins_over_the_checkout_remote():
context = resolve_pull_request_context(
"gitlab",
"81",
"widgets",
configured_explicit=True,
env={
"CI_PROJECT_URL": "https://gitlab.example.com/acme/widgets",
"BUILDKITE_REPO": "git@gitlab.example.com:stale/mirror.git",
},
)
assert context.url == "https://gitlab.example.com/acme/widgets/-/merge_requests/81"
def test_bare_repository_name_alone_yields_no_url():
"""config.repo has no owner segment, so it cannot stand in for a slug."""
context = resolve_pull_request_context(
"github", "42", "widgets", configured_explicit=True, env={}
)
assert context.number == 42
assert context.url is None