Skip to content

Commit 30a8c6d

Browse files
authored
ci: use mocks for GitHub calls in needs_testrun.py tests (#11692)
Avoid hitting rate limits when testing this script, especially when the data/responses from GitHub API should be static anyways. Avoids: https://gitlab.ddbuild.io/DataDog/apm-reliability/dd-trace-py/-/jobs/736522083 ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
1 parent ae0547e commit 30a8c6d

File tree

8 files changed

+24076
-51
lines changed

8 files changed

+24076
-51
lines changed

.riot/requirements/151a249.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.13
3+
# by the following command:
4+
#
5+
# pip-compile --allow-unsafe --no-annotate .riot/requirements/151a249.in
6+
#
7+
attrs==24.2.0
8+
coverage[toml]==7.6.9
9+
hypothesis==6.45.0
10+
iniconfig==2.0.0
11+
lxml==5.3.0
12+
mock==5.1.0
13+
opentracing==2.4.0
14+
packaging==24.2
15+
pluggy==1.5.0
16+
pytest==8.3.4
17+
pytest-cov==6.0.0
18+
pytest-mock==3.14.0
19+
ruamel-yaml==0.18.6
20+
sortedcontainers==2.4.0

.riot/requirements/196755b.txt

Lines changed: 0 additions & 20 deletions
This file was deleted.

.riot/requirements/4d1fa34.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.13
3+
# by the following command:
4+
#
5+
# pip-compile --allow-unsafe --no-annotate .riot/requirements/4d1fa34.in
6+
#
7+
attrs==24.2.0
8+
coverage[toml]==7.6.9
9+
hypothesis==6.45.0
10+
iniconfig==2.0.0
11+
lxml==5.3.0
12+
mock==5.1.0
13+
opentracing==2.4.0
14+
packaging==24.2
15+
pluggy==1.5.0
16+
pytest==8.3.4
17+
pytest-cov==6.0.0
18+
pytest-mock==3.14.0
19+
ruamel-yaml==0.18.6
20+
sortedcontainers==2.4.0

.riot/requirements/b2ac981.txt

Lines changed: 0 additions & 22 deletions
This file was deleted.

hatch.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,10 @@ _ = [
133133
detached = true
134134
python = "3.10"
135135
extra-dependencies = [
136+
"lxml==5.3.0",
136137
"packaging==23.1",
137138
"ruamel.yaml==0.18.6",
139+
"vcrpy==6.0.2",
138140
]
139141

140142
[envs.scripts.scripts]

riotfile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def select_pys(min_version: str = MIN_PYTHON_VERSION, max_version: str = MAX_PYT
116116
pys=["3"],
117117
pkgs={
118118
"ruamel.yaml": latest,
119+
"lxml": latest,
119120
},
120121
),
121122
Venv(
@@ -124,6 +125,7 @@ def select_pys(min_version: str = MIN_PYTHON_VERSION, max_version: str = MAX_PYT
124125
pys=["3"],
125126
pkgs={
126127
"ruamel.yaml": latest,
128+
"lxml": latest,
127129
},
128130
),
129131
Venv(

scripts/needs_testrun.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
import logging
99
import os
1010
from pathlib import Path
11-
import re
1211
from subprocess import check_output
1312
import sys
1413
import typing as t
1514
from urllib.parse import urlencode
1615
from urllib.request import Request
1716
from urllib.request import urlopen
1817

18+
from lxml import html
19+
1920

2021
sys.path.insert(0, str(Path(__file__).parents[1]))
2122

@@ -26,20 +27,34 @@
2627

2728
LOGGER = logging.getLogger(__name__)
2829

29-
BASE_BRANCH_PATTERN = re.compile(r':<span class="css-truncate-target">([^<]+)')
30-
3130

3231
@cache
3332
def get_base_branch(pr_number: int) -> str:
3433
"""Get the base branch of a PR
3534
36-
>>> get_base_branch(6412)
35+
>>> import vcr
36+
>>> with vcr.use_cassette(
37+
... "scripts/vcr/needs_testrun.yaml",
38+
... filter_headers=["authorization", "user-agent"],
39+
... record_mode="all"):
40+
... get_base_branch(6412)
41+
... get_base_branch(11534)
42+
... get_base_branch(11690)
3743
'1.x'
44+
'2.15'
45+
'main'
3846
"""
3947

4048
pr_page_content = urlopen(f"https://github.com/DataDog/dd-trace-py/pull/{pr_number}").read().decode("utf-8")
4149

42-
return BASE_BRANCH_PATTERN.search(pr_page_content).group(1)
50+
tree = html.fromstring(pr_page_content)
51+
base_ref = tree.find_class("base-ref")
52+
if base_ref:
53+
ref = base_ref[0].text_content().strip()
54+
# We might have `DataDog:1.x` or `DataDog:main` so we need to strip the prefix
55+
_, _, ref = ref.rpartition(":")
56+
return ref.strip()
57+
return "main"
4358

4459

4560
@cache
@@ -116,7 +131,12 @@ def get_changed_files(pr_number: int, sha: t.Optional[str] = None) -> t.Set[str]
116131
or if there is a specific SHA given, use the less accurate method of
117132
diffing against a base commit, either the given SHA or the merge-base.
118133
119-
>>> sorted(get_changed_files(6388)) # doctest: +NORMALIZE_WHITESPACE
134+
>>> import vcr
135+
>>> with vcr.use_cassette(
136+
... "scripts/vcr/needs_testrun.yaml",
137+
... filter_headers=["authorization", "user-agent"],
138+
... record_mode="all"):
139+
... sorted(get_changed_files(6388)) # doctest: +NORMALIZE_WHITESPACE
120140
['ddtrace/debugging/_expressions.py',
121141
'releasenotes/notes/fix-debugger-expressions-none-literal-30f3328d2e386f40.yaml',
122142
'tests/debugging/test_expressions.py']
@@ -141,12 +161,19 @@ def get_changed_files(pr_number: int, sha: t.Optional[str] = None) -> t.Set[str]
141161
def needs_testrun(suite: str, pr_number: int, sha: t.Optional[str] = None) -> bool:
142162
"""Check if a testrun is needed for a suite and PR
143163
144-
>>> needs_testrun("debugger", 6485)
164+
>>> import vcr
165+
>>> with vcr.use_cassette(
166+
... "scripts/vcr/needs_testrun.yaml",
167+
... filter_headers=["authorization", "user-agent"],
168+
... record_mode="all"):
169+
... needs_testrun("debugger", 6485)
170+
... needs_testrun("debugger", 6388)
171+
... needs_testrun("foobar", 6412)
172+
... needs_testrun("profile", 11690)
145173
True
146-
>>> needs_testrun("debugger", 6388)
147174
True
148-
>>> needs_testrun("foobar", 6412)
149175
True
176+
False
150177
"""
151178
if "itr:noskip" in get_latest_commit_message().lower():
152179
return True

0 commit comments

Comments
 (0)