|
| 1 | +# type: ignore |
| 2 | +# pylint: disable=all |
| 3 | +import json |
| 4 | +import os |
| 5 | +from unittest.mock import mock_open, patch |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from github_actions.action.event import GitHubEvent |
| 10 | +from github_actions.action.run import ( |
| 11 | + MAX_PR_COMMITS, |
| 12 | + PER_PAGE_COMMITS, |
| 13 | + get_pr_commit_messages, |
| 14 | +) |
| 15 | +from tests.fixtures.actions_env import set_github_env_vars |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(scope="module", autouse=True) |
| 19 | +def setup_env(): |
| 20 | + set_github_env_vars() |
| 21 | + |
| 22 | + |
| 23 | +@patch("github_actions.action.run.request_github_api") |
| 24 | +@patch.dict(os.environ, {**os.environ, "GITHUB_EVENT_NAME": "pull_request"}) |
| 25 | +def test__get_pr_commit_messages__single_page( |
| 26 | + mock_request_github_api, |
| 27 | +): |
| 28 | + # mock github api request |
| 29 | + mock_request_github_api.return_value = ( |
| 30 | + 200, |
| 31 | + [{"commit": {"message": "feat: commit message"}}], |
| 32 | + ) |
| 33 | + |
| 34 | + payload = {"number": 10, "pull_request": {"commits": 2}} |
| 35 | + with patch("builtins.open", mock_open(read_data=json.dumps(payload))): |
| 36 | + event = GitHubEvent() |
| 37 | + result = get_pr_commit_messages(event) |
| 38 | + assert result == ["feat: commit message"] |
| 39 | + |
| 40 | + mock_request_github_api.assert_called_once_with( |
| 41 | + method="GET", |
| 42 | + url="/repos/opensource-nepal/commitlint/pulls/10/commits", |
| 43 | + token="token", |
| 44 | + params={"per_page": PER_PAGE_COMMITS, "page": 1}, |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +@patch("github_actions.action.run.request_github_api") |
| 49 | +@patch.dict(os.environ, {**os.environ, "GITHUB_EVENT_NAME": "pull_request"}) |
| 50 | +def test__get_pr_commit_messages__multiple_page( |
| 51 | + mock_request_github_api, |
| 52 | +): |
| 53 | + # mock github api request |
| 54 | + mock_request_github_api.side_effect = [ |
| 55 | + ( |
| 56 | + 200, |
| 57 | + [{"commit": {"message": "feat: commit message1"}}], |
| 58 | + ), |
| 59 | + ( |
| 60 | + 200, |
| 61 | + [{"commit": {"message": "feat: commit message2"}}], |
| 62 | + ), |
| 63 | + ] |
| 64 | + |
| 65 | + payload = {"number": 10, "pull_request": {"commits": 60}} |
| 66 | + with patch("builtins.open", mock_open(read_data=json.dumps(payload))): |
| 67 | + event = GitHubEvent() |
| 68 | + result = get_pr_commit_messages(event) |
| 69 | + assert result == ["feat: commit message1", "feat: commit message2"] |
| 70 | + |
| 71 | + assert mock_request_github_api.call_count == 2 |
| 72 | + mock_request_github_api.assert_any_call( |
| 73 | + method="GET", |
| 74 | + url="/repos/opensource-nepal/commitlint/pulls/10/commits", |
| 75 | + token="token", |
| 76 | + params={"per_page": PER_PAGE_COMMITS, "page": 1}, |
| 77 | + ) |
| 78 | + |
| 79 | + mock_request_github_api.assert_any_call( |
| 80 | + method="GET", |
| 81 | + url="/repos/opensource-nepal/commitlint/pulls/10/commits", |
| 82 | + token="token", |
| 83 | + params={"per_page": PER_PAGE_COMMITS, "page": 2}, |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +@patch("github_actions.action.run.request_github_api") |
| 88 | +@patch.dict(os.environ, {**os.environ, "GITHUB_EVENT_NAME": "pull_request"}) |
| 89 | +def test__get_pr_commit_messages__api_failure( |
| 90 | + mock_request_github_api, |
| 91 | +): |
| 92 | + mock_request_github_api.return_value = (500, None) |
| 93 | + payload = {"number": 10, "pull_request": {"commits": 60}} |
| 94 | + with patch("builtins.open", mock_open(read_data=json.dumps(payload))): |
| 95 | + with pytest.raises(SystemExit): |
| 96 | + event = GitHubEvent() |
| 97 | + get_pr_commit_messages(event) |
| 98 | + |
| 99 | + |
| 100 | +@patch.dict(os.environ, {**os.environ, "GITHUB_EVENT_NAME": "pull_request"}) |
| 101 | +def test__get_pr_commit_messages__exceed_max_commits(): |
| 102 | + payload = {"number": 10, "pull_request": {"commits": MAX_PR_COMMITS + 1}} |
| 103 | + with patch("builtins.open", mock_open(read_data=json.dumps(payload))): |
| 104 | + with pytest.raises(SystemExit): |
| 105 | + event = GitHubEvent() |
| 106 | + get_pr_commit_messages(event) |
0 commit comments