|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | + |
| 6 | +import json |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import responses |
| 10 | +from conftest import FIXTURES_DIR |
| 11 | + |
| 12 | +from code_review_bot.report.github import GithubReporter |
| 13 | +from code_review_bot.revisions import GithubRevision, Revision |
| 14 | +from code_review_bot.tasks.clang_tidy import ClangTidyIssue, ClangTidyTask |
| 15 | +from code_review_bot.tasks.coverage import CoverageIssue, ZeroCoverageTask |
| 16 | + |
| 17 | + |
| 18 | +def test_github_review( |
| 19 | + monkeypatch, |
| 20 | + mock_github, |
| 21 | + mock_config, |
| 22 | + phab, |
| 23 | + mock_github_try_task, |
| 24 | + mock_decision_task, |
| 25 | + mock_task, |
| 26 | + mock_backend_secret, |
| 27 | +): |
| 28 | + """ |
| 29 | + Report 2 cland tidy issues by pushing a review to a Github pull request |
| 30 | + """ |
| 31 | + revision = Revision.from_try_task(mock_github_try_task, mock_decision_task, None) |
| 32 | + assert isinstance(revision, GithubRevision) |
| 33 | + revision.lines = { |
| 34 | + # Add dummy lines diff |
| 35 | + "test.txt": [0], |
| 36 | + "path/to/test.cpp": [0], |
| 37 | + "another_test.cpp": [41, 42, 43], |
| 38 | + } |
| 39 | + revision.files = ["test.txt", "test.cpp", "another_test.cpp"] |
| 40 | + revision.id = 52 |
| 41 | + monkeypatch.setattr(revision, "load_file", lambda x: "some_content") |
| 42 | + |
| 43 | + reporter = GithubReporter( |
| 44 | + { |
| 45 | + "client_id": "client_id", |
| 46 | + "private_key_pem": (Path(FIXTURES_DIR) / "private_key.pem").read_text(), |
| 47 | + "installation_id": 123456789, |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + issue_clang_tidy = ClangTidyIssue( |
| 52 | + mock_task(ClangTidyTask, "source-test-clang-tidy"), |
| 53 | + revision, |
| 54 | + "another_test.cpp", |
| 55 | + "42", |
| 56 | + "51", |
| 57 | + "modernize-use-nullptr", |
| 58 | + "dummy message", |
| 59 | + ) |
| 60 | + assert issue_clang_tidy.is_publishable() |
| 61 | + |
| 62 | + issue_coverage = CoverageIssue( |
| 63 | + mock_task(ZeroCoverageTask, "coverage"), |
| 64 | + "path/to/test.cpp", |
| 65 | + "1", |
| 66 | + "This file is uncovered", |
| 67 | + revision, |
| 68 | + ) |
| 69 | + assert issue_coverage.is_publishable() |
| 70 | + |
| 71 | + responses.add( |
| 72 | + responses.POST, |
| 73 | + "https://api.github.com:443/repos/owner/repo-name/pulls/1/reviews", |
| 74 | + json={}, |
| 75 | + ) |
| 76 | + |
| 77 | + reporter.publish([issue_clang_tidy, issue_coverage], revision, [], [], []) |
| 78 | + assert [(call.request.method, call.request.url) for call in responses.calls] == [ |
| 79 | + ("GET", "https://github.tests.com/owner/repo-name/pull/1.diff"), |
| 80 | + ("GET", "https://api.github.com:443/app/installations"), |
| 81 | + ( |
| 82 | + "POST", |
| 83 | + "https://api.github.com:443/app/installations/123456789/access_tokens", |
| 84 | + ), |
| 85 | + ("GET", "https://api.github.com:443/repos/owner/repo-name"), |
| 86 | + ("GET", "https://api.github.com:443/repos/owner/repo-name/pulls/1"), |
| 87 | + ( |
| 88 | + "GET", |
| 89 | + "https://api.github.com:443/repos/owner/repo-name/commits/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 90 | + ), |
| 91 | + ("POST", "https://api.github.com:443/repos/owner/repo-name/pulls/1/reviews"), |
| 92 | + ] |
| 93 | + review_creation = responses.calls[-1] |
| 94 | + assert json.loads(review_creation.request.body) == { |
| 95 | + "body": "2 issues have been found in this revision", |
| 96 | + "comments": [ |
| 97 | + { |
| 98 | + "body": "dummy message", |
| 99 | + "path": "another_test.cpp", |
| 100 | + "line": 42, |
| 101 | + }, |
| 102 | + { |
| 103 | + "body": "This file is uncovered", |
| 104 | + "path": "path/to/test.cpp", |
| 105 | + "line": 1, |
| 106 | + }, |
| 107 | + ], |
| 108 | + "commit_id": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 109 | + "event": "REQUEST_CHANGES", |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | +def test_github_review_approve( |
| 114 | + monkeypatch, |
| 115 | + mock_github, |
| 116 | + mock_config, |
| 117 | + phab, |
| 118 | + mock_github_try_task, |
| 119 | + mock_decision_task, |
| 120 | + mock_task, |
| 121 | + mock_backend_secret, |
| 122 | +): |
| 123 | + """In case no issue is found, the pull request is approved""" |
| 124 | + revision = Revision.from_try_task(mock_github_try_task, mock_decision_task, None) |
| 125 | + revision.lines = {} |
| 126 | + revision.files = ["test.txt", "test.cpp", "another_test.cpp"] |
| 127 | + revision.id = 52 |
| 128 | + reporter = GithubReporter( |
| 129 | + { |
| 130 | + "client_id": "client_id", |
| 131 | + "private_key_pem": (Path(FIXTURES_DIR) / "private_key.pem").read_text(), |
| 132 | + "installation_id": 123456789, |
| 133 | + } |
| 134 | + ) |
| 135 | + |
| 136 | + responses.add( |
| 137 | + responses.POST, |
| 138 | + "https://api.github.com:443/repos/owner/repo-name/pulls/1/reviews", |
| 139 | + json={}, |
| 140 | + ) |
| 141 | + |
| 142 | + reporter.publish([], revision, [], [], []) |
| 143 | + assert [(call.request.method, call.request.url) for call in responses.calls] == [ |
| 144 | + ("GET", "https://github.tests.com/owner/repo-name/pull/1.diff"), |
| 145 | + ("GET", "https://api.github.com:443/app/installations"), |
| 146 | + ( |
| 147 | + "POST", |
| 148 | + "https://api.github.com:443/app/installations/123456789/access_tokens", |
| 149 | + ), |
| 150 | + ("GET", "https://api.github.com:443/repos/owner/repo-name"), |
| 151 | + ("GET", "https://api.github.com:443/repos/owner/repo-name/pulls/1"), |
| 152 | + ( |
| 153 | + "GET", |
| 154 | + "https://api.github.com:443/repos/owner/repo-name/commits/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 155 | + ), |
| 156 | + ("POST", "https://api.github.com:443/repos/owner/repo-name/pulls/1/reviews"), |
| 157 | + ] |
| 158 | + review_creation = responses.calls[-1] |
| 159 | + assert json.loads(review_creation.request.body) == { |
| 160 | + "comments": [], |
| 161 | + "commit_id": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 162 | + "event": "APPROVE", |
| 163 | + } |
0 commit comments