|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import importlib.util |
| 16 | +import sys |
| 17 | +import types |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +REPO_ROOT = Path(__file__).resolve().parents[3] |
| 23 | +ONCALL_MANAGER_PATH = REPO_ROOT / ".github" / "scripts" / "oncall_manager.py" |
| 24 | + |
| 25 | + |
| 26 | +class FakeResponse: |
| 27 | + def __init__(self, status_code, json_data=None, text=""): |
| 28 | + self.status_code = status_code |
| 29 | + self._json_data = json_data |
| 30 | + self.text = text |
| 31 | + |
| 32 | + def json(self): |
| 33 | + return self._json_data |
| 34 | + |
| 35 | + |
| 36 | +class FakeRequests: |
| 37 | + def __init__(self, pr_data): |
| 38 | + self.pr_data = pr_data |
| 39 | + self.posts = [] |
| 40 | + |
| 41 | + def get(self, url, headers=None): |
| 42 | + return FakeResponse(200, self.pr_data) |
| 43 | + |
| 44 | + def post(self, url, headers=None, json=None): |
| 45 | + self.posts.append({"url": url, "headers": headers, "json": json}) |
| 46 | + return FakeResponse(201) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture |
| 50 | +def oncall_manager(monkeypatch): |
| 51 | + slack_module = types.ModuleType("slack_sdk") |
| 52 | + slack_module.WebClient = object |
| 53 | + |
| 54 | + slack_errors_module = types.ModuleType("slack_sdk.errors") |
| 55 | + slack_errors_module.SlackApiError = Exception |
| 56 | + requests_module = types.ModuleType("requests") |
| 57 | + |
| 58 | + monkeypatch.setitem(sys.modules, "requests", requests_module) |
| 59 | + monkeypatch.setitem(sys.modules, "slack_sdk", slack_module) |
| 60 | + monkeypatch.setitem(sys.modules, "slack_sdk.errors", slack_errors_module) |
| 61 | + monkeypatch.setenv("GITHUB_REPOSITORY", "NVIDIA/Megatron-LM") |
| 62 | + monkeypatch.setenv("GH_TOKEN", "token") |
| 63 | + |
| 64 | + spec = importlib.util.spec_from_file_location("oncall_manager", ONCALL_MANAGER_PATH) |
| 65 | + module = importlib.util.module_from_spec(spec) |
| 66 | + spec.loader.exec_module(module) |
| 67 | + return module |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.parametrize( |
| 71 | + "pr_data", |
| 72 | + [ |
| 73 | + { |
| 74 | + "user": {"login": "maintainer"}, |
| 75 | + "requested_reviewers": [{"login": "alice"}], |
| 76 | + "requested_teams": [], |
| 77 | + }, |
| 78 | + { |
| 79 | + "user": {"login": "maintainer"}, |
| 80 | + "requested_reviewers": [], |
| 81 | + "requested_teams": [{"slug": "mcore"}], |
| 82 | + }, |
| 83 | + { |
| 84 | + "requested_reviewers": [{"login": "alice"}], |
| 85 | + "requested_teams": [{"slug": "mcore-oncall"}], |
| 86 | + "labels": [{"name": "community-request"}], |
| 87 | + }, |
| 88 | + ], |
| 89 | +) |
| 90 | +def test_assign_reviewer_skips_when_oncall_is_not_needed( |
| 91 | + oncall_manager, monkeypatch, capsys, pr_data |
| 92 | +): |
| 93 | + fake_requests = FakeRequests(pr_data) |
| 94 | + monkeypatch.setattr(oncall_manager, "requests", fake_requests) |
| 95 | + |
| 96 | + oncall_manager.assign_reviewer(123) |
| 97 | + |
| 98 | + assert fake_requests.posts == [] |
| 99 | + assert "Skipping reviewer request" in capsys.readouterr().out |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize( |
| 103 | + "pr_data", |
| 104 | + [ |
| 105 | + {"user": {"login": "maintainer"}, "requested_reviewers": [], "requested_teams": []}, |
| 106 | + { |
| 107 | + "requested_reviewers": [{"login": "alice"}], |
| 108 | + "requested_teams": [{"slug": "mcore"}], |
| 109 | + "labels": [{"name": "community-request"}], |
| 110 | + }, |
| 111 | + ], |
| 112 | +) |
| 113 | +def test_assign_reviewer_requests_oncall_when_needed(oncall_manager, monkeypatch, pr_data): |
| 114 | + fake_requests = FakeRequests(pr_data) |
| 115 | + monkeypatch.setattr(oncall_manager, "requests", fake_requests) |
| 116 | + |
| 117 | + oncall_manager.assign_reviewer(123) |
| 118 | + |
| 119 | + assert fake_requests.posts == [ |
| 120 | + { |
| 121 | + "url": "https://api.github.com/repos/NVIDIA/Megatron-LM/pulls/123/requested_reviewers", |
| 122 | + "headers": {"Authorization": "token token", "Accept": "application/vnd.github.v3+json"}, |
| 123 | + "json": {"team_reviewers": ["mcore-oncall"]}, |
| 124 | + } |
| 125 | + ] |
0 commit comments