Example #1
0
def test_check_and_report_calls_method(whitelist, event, method, approved):
    gp = GitProject("", GitService(), "")
    mocked_gp = (flexmock(gp).should_receive(method).with_args(
        0, "Neither account bar nor owner foo are on our whitelist!"))
    mocked_gp.never() if approved else mocked_gp.once()
    assert (whitelist.check_and_report(
        event, gp, config=flexmock(deployment=Deployment.stg)) is approved)
Example #2
0
def test_get_packit_config_from_repo(content):
    flexmock(GitProject).should_receive("get_file_content").and_return(content)
    git_project = GitProject(repo="", service=GitService(), namespace="")
    config = get_packit_config_from_repo(sourcegit_project=git_project, ref="")
    assert isinstance(config, PackageConfig)
    assert config.specfile_path == "packit.spec"
    assert set(config.synced_files) == {"packit.spec", ".packit.yaml"}
def build_handler(metadata=None, trigger=None):
    if not metadata:
        metadata = {
            "owner": "nobody",
            "targets": [
                "fedora-29-x86_64",
                "fedora-30-x86_64",
                "fedora-31-x86_64",
                "fedora-rawhide-x86_64",
            ],
        }
    jobs = [
        JobConfig(
            job=JobType.copr_build,
            trigger=trigger or JobTriggerType.pull_request,
            metadata=metadata,
        )
    ]
    pkg_conf = PackageConfig(jobs=jobs, downstream_package_name="dummy")
    event = Parser.parse_pr_event(pull_request())
    handler = CoprBuildHandler(
        config=ServiceConfig(),
        package_config=pkg_conf,
        project=GitProject("", GitService(), ""),
        event=event,
    )
    handler._api = PackitAPI(ServiceConfig, pkg_conf)
    return handler
Example #4
0
def test_get_package_config_from_repo_alternative_config_name():
    gp = flexmock(GitProject)
    gp.should_receive("full_repo_name").and_return("a/b")
    gp.should_receive("get_file_content").with_args(path=".packit.yaml",
                                                    ref=None).and_raise(
                                                        FileNotFoundError,
                                                        "not found")
    gp.should_receive("get_file_content").with_args(
        path=".packit.yml",
        ref=None).and_return("---\nspecfile_path: packit.spec\n"
                             "synced_files:\n"
                             "  - packit.spec\n"
                             "  - src: .packit.yaml\n"
                             "    dest: .packit2.yaml")
    config = PackageConfigGetter.get_package_config_from_repo(
        project=GitProject(repo="", service=GitService(), namespace=""),
        reference=None,
        spec_file_path="packit.spec",
    )
    assert isinstance(config, PackageConfig)
    assert config.specfile_path == "packit.spec"
    assert config.synced_files == SyncFilesConfig(files_to_sync=[
        SyncFilesItem(src="packit.spec", dest="packit.spec"),
        SyncFilesItem(src=".packit.yaml", dest=".packit2.yaml"),
    ])
    assert config.create_pr
Example #5
0
def test_check_and_report_pr_comment_approve(whitelist):
    event = PullRequestCommentEvent(PullRequestAction["opened"], 0, "", "", "",
                                    "", "", "lojzo", "")
    gp = GitProject("", GitService(), "")
    flexmock(gp).should_receive("pr_comment").with_args(
        0, "Account is not whitelisted!").never()
    assert whitelist.check_and_report(event, gp)
def build_helper(
    event: Union[PullRequestEvent, PullRequestCommentEvent, CoprBuildEvent,
                 PushGitHubEvent, ReleaseEvent, ],
    metadata=None,
    trigger=None,
    jobs=None,
):
    if not metadata:
        metadata = {
            "owner":
            "nobody",
            "targets": [
                "fedora-29-x86_64",
                "fedora-30-x86_64",
                "fedora-31-x86_64",
                "fedora-rawhide-x86_64",
            ],
        }
    jobs = jobs or []
    jobs.append(
        JobConfig(
            type=JobType.copr_build,
            trigger=trigger or JobConfigTriggerType.pull_request,
            metadata=metadata,
        ))
    pkg_conf = PackageConfig(jobs=jobs, downstream_package_name="dummy")
    handler = CoprBuildJobHelper(
        config=ServiceConfig(),
        package_config=pkg_conf,
        project=GitProject("", GitService(), ""),
        event=event,
    )
    handler._api = PackitAPI(ServiceConfig(), pkg_conf)
    return handler
Example #7
0
def test_check_and_report_pr_comment_reject(whitelist):
    event = PullRequestCommentEvent(PullRequestAction["opened"], 0, "brcalnik",
                                    "", "", "", "", "rakosnicek", "")
    gp = GitProject("", GitService(), "")
    flexmock(gp).should_receive("pr_comment").with_args(
        0,
        "Neither account rakosnicek nor owner brcalnik are on our whitelist!"
    ).once()
    assert not whitelist.check_and_report(event, gp)
Example #8
0
def test_get_package_config_from_repo_spec_file_not_defined(content):
    gp = flexmock(GitProject)
    gp.should_receive("full_repo_name").and_return("a/b")
    gp.should_receive("get_file_content").and_return(content)
    gp.should_receive("get_files").and_return(["packit.spec"])
    git_project = GitProject(repo="", service=GitService(), namespace="")
    config = get_package_config_from_repo(project=git_project, ref="")
    assert isinstance(config, PackageConfig)
    assert Path(config.specfile_path).name == "packit.spec"
    assert config.create_pr
Example #9
0
def test_get_packit_config_from_repo(content):
    flexmock(GitProject).should_receive("get_file_content").and_return(content)
    git_project = GitProject(repo="", service=GitService(), namespace="")
    config = get_packit_config_from_repo(sourcegit_project=git_project, ref="")
    assert isinstance(config, PackageConfig)
    assert Path(config.specfile_path).name == "packit.spec"
    assert config.synced_files == SyncFilesConfig(files_to_sync=[
        SyncFilesItem(src="packit.spec", dest="packit.spec"),
        SyncFilesItem(src=".packit.yaml", dest=".packit2.yaml"),
    ])
Example #10
0
def test_get_package_config_from_repo_not_found():
    gp = flexmock(GitProject)
    gp.should_receive("full_repo_name").and_return("a/b")
    gp.should_receive("get_file_content").and_raise(FileNotFoundError,
                                                    "not found")
    assert (PackageConfigGetter.get_package_config_from_repo(
        project=GitProject(repo="", service=GitService(), namespace=""),
        reference=None,
        pr_id=2,
        fail_when_missing=False,
    ) is None)
Example #11
0
def test_get_package_config_from_repo_not_found_exception_pr():
    gp = flexmock(GitProject)
    gp.should_receive("full_repo_name").and_return("a/b")
    gp.should_receive("get_file_content").and_raise(FileNotFoundError, "not found")
    gp.should_receive("pr_comment").and_return(None).once()
    with pytest.raises(PackitConfigException):
        PackageConfigGetter.get_package_config_from_repo(
            project=GitProject(repo="", service=GitService(), namespace=""),
            reference=None,
            pr_id=2,
        )
Example #12
0
def test_check_and_report_calls_method(whitelist, event, method, approved):
    gp = GitProject("", GitService(), "")
    mocked_gp = (flexmock(gp).should_receive(method).with_args(
        0, "Neither account bar nor owner foo are on our whitelist!"))
    mocked_gp.never() if approved else mocked_gp.once()
    whitelist_mock = flexmock(DBWhitelist).should_receive("get_account")
    if approved:
        whitelist_mock.and_return(DBWhitelist(status="approved_manually"))
    else:
        whitelist_mock.and_return(None)
    assert (whitelist.check_and_report(
        event, gp, config=flexmock(deployment=Deployment.stg)) is approved)
Example #13
0
def test_get_package_config_from_repo_not_found_exception_existing_issue():
    gp = flexmock(GitProject)
    gp.should_receive("full_repo_name").and_return("a/b")
    gp.should_receive("get_file_content").and_raise(FileNotFoundError,
                                                    "not found")
    gp.should_receive("get_issue_list").and_return(
        [flexmock(title="Invalid packit config")]).once()
    with pytest.raises(PackitConfigException):
        PackageConfigGetter.get_package_config_from_repo(
            project=GitProject(repo="", service=GitService(), namespace=""),
            reference=None,
        )
Example #14
0
def test_get_package_config_from_repo_spec_file_not_defined(content):
    specfile_path = "packit.spec"
    gp = flexmock(GitProject)
    gp.should_receive("full_repo_name").and_return("a/b")
    gp.should_receive("get_file_content").and_return(content)
    gp.should_receive("get_files").and_return([specfile_path])
    git_project = GitProject(repo="", service=GitService(), namespace="")
    config = get_package_config_from_repo(project=git_project)
    assert isinstance(config, PackageConfig)
    assert Path(config.specfile_path).name == specfile_path
    assert config.create_pr
    for j in config.jobs:
        assert j.specfile_path == specfile_path
        assert j.downstream_package_name == config.downstream_package_name
        assert j.upstream_package_name == config.upstream_package_name
Example #15
0
def test_get_package_config_from_repo_not_found_exception_nonexisting_issue():
    flexmock(GitService).should_receive("user").and_return(
        flexmock().should_receive("get_username").and_return("packit").mock())
    gp = flexmock(GitProject)
    gp.should_receive("full_repo_name").and_return("a/b")
    gp.should_receive("get_file_content").and_raise(FileNotFoundError,
                                                    "not found")
    gp.should_receive("get_issue_list").and_return(
        [flexmock(title="issue 1"),
         flexmock(title="issue 2")]).once()
    gp.should_receive("create_issue").and_return(
        flexmock(url="the url")).once()
    with pytest.raises(PackitConfigException):
        PackageConfigGetter.get_package_config_from_repo(
            project=GitProject(repo="", service=GitService(), namespace=""),
            reference=None,
        )
Example #16
0
def test_check_and_report_calls_method(allowlist, event, method, approved):
    gp = GitProject("", GitService(), "")
    mocked_gp = (flexmock(gp).should_receive(method).with_args(
        0, "Namespace foo is not on our allowlist!"))
    mocked_gp.never() if approved else mocked_gp.once()
    flexmock(gp).should_receive("can_merge_pr").with_args(
        event.user_login).and_return(approved)
    flexmock(gp).should_receive("get_pr").and_return(flexmock(author=None))
    allowlist_mock = flexmock(DBAllowlist).should_receive("get_account")
    if approved:
        allowlist_mock.and_return(DBAllowlist(status="approved_manually"))
    else:
        allowlist_mock.and_return(None)
    assert (allowlist.check_and_report(
        event,
        gp,
        service_config=flexmock(deployment=Deployment.stg, admins=["admin"]),
        job_configs=[],
    ) is approved)
Example #17
0
def test_check_and_report_calls_method(allowlist, event, mocked_model,
                                       approved):
    gp = GitProject("", GitService(), "")

    flexmock(gp).should_receive("can_merge_pr").with_args(
        event.actor).and_return(approved)
    mocked_pr_or_issue = flexmock(author=None)
    if isinstance(event, IssueCommentEvent):
        flexmock(gp).should_receive("get_issue").and_return(mocked_pr_or_issue)
    else:
        flexmock(gp).should_receive("get_pr").and_return(mocked_pr_or_issue)
    expectation = mocked_pr_or_issue.should_receive("comment").with_args(
        "Project github.com/foo/bar.git is not on our allowlist!")
    expectation.never() if approved else expectation.once()

    assert (allowlist.check_and_report(
        event,
        gp,
        service_config=flexmock(deployment=Deployment.stg, admins=["admin"]),
        job_configs=[],
    ) == approved)
Example #18
0
    assert sc.testing_farm_secret is not None
    assert sc.github_requests_log_path is not None
    assert sc.webhook_secret is not None
    assert sc.validate_webhooks is not None


@pytest.mark.parametrize(
    "content,project,mock_spec_search,spec_path_option,spec_path,reference",
    [
        (
            "---\nspecfile_path: packit.spec\n"
            "synced_files:\n"
            "  - packit.spec\n"
            "  - src: .packit.yaml\n"
            "    dest: .packit2.yaml",
            GitProject(repo="", service=GitService(), namespace=""),
            True,
            None,
            "packit.spec",
            None,
        ),
        (
            "---\nspecfile_path: packit.spec\n"
            "synced_files:\n"
            "  - packit.spec\n"
            "  - src: .packit.yaml\n"
            "    dest: .packit2.yaml",
            GitProject(repo="", service=GitService(), namespace=""),
            True,
            None,
            "packit.spec",
Example #19
0
def test_get_specfile_path_from_repo(files, expected):
    gp = flexmock(GitProject)
    gp.should_receive("full_repo_name").and_return("a/b")
    gp.should_receive("get_files").and_return(files)
    git_project = GitProject(repo="", service=GitService(), namespace="")
    assert get_specfile_path_from_repo(project=git_project) == expected