-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filter test collection based on files changed in upstream PRs (#14931)
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
GITHUB: | ||
FOREMAN: | ||
ORG: theforeman | ||
REPO: foreman | ||
RULES: | ||
- PATH: | ||
MARKER: | ||
KATELLO: | ||
ORG: Katello | ||
REPO: katello | ||
RULES: | ||
- PATH: | ||
MARKER: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from github import Github | ||
|
||
from robottelo.config import settings | ||
from robottelo.logging import collection_logger as logger | ||
|
||
|
||
def pytest_addoption(parser): | ||
"""Add CLI option to specify upstream GitHub PRs. | ||
Add --upstream-pr option for filtering tests based on the files modified by upstream | ||
PRs. | ||
""" | ||
parser.addoption( | ||
"--upstream-pr", | ||
help=( | ||
"Filter test collection based on files modified by upstream PR." | ||
" Example: `--upstream-pr REPO/PR_ID`" | ||
), | ||
) | ||
|
||
|
||
def pytest_collection_modifyitems(session, items, config): | ||
"""Filter tests based on upstream PRs. | ||
1. Get the list of modified files in the upstream PRs. | ||
2. Map each file to at most one component. | ||
3. Filter the collected tests to include only those with matching components. | ||
""" | ||
upstream_prs = [ | ||
pr_info for pr_info in (config.getoption('upstream_pr') or '').split(',') if pr_info != '' | ||
] | ||
if not upstream_prs: | ||
return | ||
|
||
markers = set() | ||
for pr_info in upstream_prs: | ||
# Get all filenames modified by this PR | ||
repo_key, pr_id = pr_info.split('/') | ||
if not (repo_config := settings.github.get(repo_key)): | ||
raise Exception(f"Key {repo_key} not found in settings file.") | ||
pr = Github().get_repo(f"{repo_config.org}/{repo_config.repo}").get_pull(int(pr_id)) | ||
pr_filenames = {file.filename for commit in pr.get_commits() for file in commit.files} | ||
|
||
# Get a list of markers from all of the matching rules | ||
unprocessed_filenames = pr_filenames.copy() | ||
for rule in repo_config.rules: | ||
if matched_filenames := { | ||
filename for filename in unprocessed_filenames if filename.startswith(rule.path) | ||
}: | ||
markers.add(rule.marker) | ||
unprocessed_filenames.difference_update(matched_filenames) | ||
|
||
# If no markers were found above, deselect all tests. | ||
# Any unprocessed filenames are ignored. | ||
selected = [] | ||
deselected = [] | ||
for item in items: | ||
if markers: | ||
if any(item_marker.name in markers for item_marker in item.iter_markers()): | ||
selected.append(item) | ||
else: | ||
logger.debug(f'Deselected test {item.nodeid} due to PR filter {upstream_prs}') | ||
deselected.append(item) | ||
else: | ||
logger.debug(f'Deselected test {item.nodeid} due to PR filter {upstream_prs}') | ||
deselected.append(item) | ||
|
||
config.hook.pytest_deselected(items=deselected) | ||
items[:] = selected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters