|
| 1 | +"""Which full scans ask the API to become their branch's head scan.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from socketsecurity.core import Core |
| 6 | +from socketsecurity.core.scm.github import Github |
| 7 | +from tests.core.test_strict_blocking_no_manifest_change import cli_runtime # noqa: F401 |
| 8 | + |
| 9 | +PULL_REQUEST_ENVIRONMENT = ( |
| 10 | + "GITHUB_EVENT_NAME", |
| 11 | + "GITHUB_EVENT_PATH", |
| 12 | + "GITHUB_REF", |
| 13 | + "PR_NUMBER", |
| 14 | + "EVENT_ACTION", |
| 15 | + "CI_MERGE_REQUEST_IID", |
| 16 | + "CI_PIPELINE_SOURCE", |
| 17 | + "BITBUCKET_PR_ID", |
| 18 | + "BUILDKITE_PULL_REQUEST", |
| 19 | + "BUILD_REASON", |
| 20 | + "SYSTEM_PULLREQUEST_PULLREQUESTID", |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(autouse=True) |
| 25 | +def branch_push_environment(monkeypatch): |
| 26 | + # The suite itself runs on pull_request events in CI. |
| 27 | + for name in PULL_REQUEST_ENVIRONMENT: |
| 28 | + monkeypatch.delenv(name, raising=False) |
| 29 | + |
| 30 | + |
| 31 | +def _scan_params(runtime): |
| 32 | + runtime.full_scan.assert_called_once() |
| 33 | + return runtime.full_scan.call_args.args[1] |
| 34 | + |
| 35 | + |
| 36 | +def test_branch_scan_asks_to_be_the_branch_head(cli_runtime): # noqa: F811 |
| 37 | + assert cli_runtime.run(["--branch", "release/1.0"]) == 0 |
| 38 | + |
| 39 | + params = _scan_params(cli_runtime) |
| 40 | + assert params.branch == "release/1.0" |
| 41 | + assert params.set_as_pending_head is True |
| 42 | + assert params.make_default_branch is False |
| 43 | + |
| 44 | + |
| 45 | +def test_default_branch_scan_is_the_head_and_the_default_branch(cli_runtime): # noqa: F811 |
| 46 | + cli_runtime.git_repo.is_default_branch = True |
| 47 | + |
| 48 | + assert cli_runtime.run() == 0 |
| 49 | + |
| 50 | + params = _scan_params(cli_runtime) |
| 51 | + assert params.set_as_pending_head is True |
| 52 | + assert params.make_default_branch is True |
| 53 | + |
| 54 | + |
| 55 | +def test_pull_request_scan_of_a_non_default_branch_does_not_request_the_head(cli_runtime): # noqa: F811 |
| 56 | + assert cli_runtime.run(["--pr-number", "42"]) == 0 |
| 57 | + |
| 58 | + params = _scan_params(cli_runtime) |
| 59 | + assert params.set_as_pending_head is False |
| 60 | + assert params.make_default_branch is False |
| 61 | + |
| 62 | + |
| 63 | +def test_pull_request_event_without_a_pr_number_does_not_request_the_head( |
| 64 | + cli_runtime, monkeypatch # noqa: F811 |
| 65 | +): |
| 66 | + monkeypatch.setenv("GITHUB_EVENT_NAME", "pull_request") |
| 67 | + monkeypatch.setenv("GITHUB_REF", "refs/pull/7/merge") |
| 68 | + |
| 69 | + assert cli_runtime.run(["--pr-number", "0"]) == 0 |
| 70 | + |
| 71 | + params = _scan_params(cli_runtime) |
| 72 | + assert params.pull_request == 0 |
| 73 | + assert params.set_as_pending_head is False |
| 74 | + |
| 75 | + |
| 76 | +def test_scm_comparison_without_a_pr_number_does_not_request_the_head( |
| 77 | + cli_runtime, monkeypatch, mocker # noqa: F811 |
| 78 | +): |
| 79 | + monkeypatch.setenv("GITHUB_EVENT_NAME", "pull_request") |
| 80 | + monkeypatch.setenv("EVENT_ACTION", "synchronize") |
| 81 | + monkeypatch.setenv("GITHUB_REPOSITORY", "example/widgets") |
| 82 | + monkeypatch.setenv("GH_API_TOKEN", "test") |
| 83 | + mocker.patch.object(Github, "get_comments_for_pr", return_value={}) |
| 84 | + mocker.patch.object(Github, "add_socket_comments") |
| 85 | + mocker.patch.object(Github, "set_commit_status", create=True) |
| 86 | + # A manifest change keeps the run on the SCM comparison path. |
| 87 | + monkeypatch.setattr(Core, "has_manifest_files", lambda self, files: True) |
| 88 | + |
| 89 | + assert cli_runtime.run(["--scm", "github", "--pr-number", "0"]) == 0 |
| 90 | + |
| 91 | + cli_runtime.full_scan.assert_not_called() |
| 92 | + cli_runtime.compare.assert_called_once() |
| 93 | + assert cli_runtime.compare.call_args.args[1].set_as_pending_head is False |
| 94 | + |
| 95 | + |
| 96 | +def test_explicit_default_branch_on_a_pull_request_still_requests_the_head(cli_runtime): # noqa: F811 |
| 97 | + # Unchanged from earlier releases: --default-branch marks the scan on any run. |
| 98 | + assert cli_runtime.run(["--pr-number", "42", "--default-branch"]) == 0 |
| 99 | + |
| 100 | + params = _scan_params(cli_runtime) |
| 101 | + assert params.set_as_pending_head is True |
| 102 | + assert params.make_default_branch is True |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.parametrize("detected_default", [True, False], ids=["default-branch", "tracked-branch"]) |
| 106 | +def test_no_default_branch_does_not_request_the_head(cli_runtime, detected_default): # noqa: F811 |
| 107 | + cli_runtime.git_repo.is_default_branch = detected_default |
| 108 | + |
| 109 | + assert cli_runtime.run(["--branch", "release/1.0", "--no-default-branch"]) == 0 |
| 110 | + |
| 111 | + params = _scan_params(cli_runtime) |
| 112 | + assert params.set_as_pending_head is False |
| 113 | + assert params.make_default_branch is False |
0 commit comments