def mock_remote_functionality(distgit: Path, upstream: Path): def mocked_create_pr(*args, **kwargs): return PullRequestReadOnly( title="", id=42, status=PRStatus.open, url="", description="", author="", source_branch="", target_branch="", created=datetime.datetime(1969, 11, 11, 11, 11, 11, 11), ) flexmock(GithubService) github_service = GithubService() flexmock( GithubService, get_project=lambda repo, namespace: GithubProject( "also-not", github_service, "set", github_repo=flexmock() ), ) flexmock( PagureProject, get_git_urls=lambda: {"git": DOWNSTREAM_PROJECT_URL}, fork_create=lambda: None, get_fork=lambda: PagureProject("", "", PagureService()), create_pr=mocked_create_pr, ) flexmock( GithubProject, get_git_urls=lambda: {"git": UPSTREAM_PROJECT_URL}, fork_create=lambda: None, ) flexmock(PagureUser, get_username=lambda: "packito") dglp = LocalProject( working_dir=distgit, git_url="https://packit.dev/rpms/beer", git_service=PagureService(), ) flexmock( DistGit, push_to_fork=lambda *args, **kwargs: None, # let's not hammer the production lookaside cache webserver is_archive_in_lookaside_cache=lambda archive_path: False, local_project=dglp, ) flexmock(DistGit).should_receive("existing_pr").and_return(None) def mocked_new_sources(sources=None): if not Path(sources).is_file(): raise RuntimeError("archive does not exist") flexmock(PkgTool, new_sources=mocked_new_sources) flexmock(PackitAPI, init_kerberos_ticket=lambda: None) pc = get_local_package_config(str(upstream)) pc.dist_git_clone_path = str(distgit) pc.upstream_project_url = str(upstream) return upstream, distgit
def test_set_status_github_check( project, commit_sha, pr_id, pr_object, state, title, summary, check_name, url, check_status, check_conclusion, trigger_id, ): project = GithubProject(None, None, None) reporter = StatusReporter.get_instance(project=project, commit_sha=commit_sha, pr_id=pr_id, trigger_id=trigger_id) act_upon = flexmock(GithubProject) act_upon.should_receive("create_check_run").with_args( name=check_name, commit_sha=commit_sha, url=url, external_id=str(trigger_id), status=check_status, conclusion=check_conclusion, output=create_github_check_run_output(title, summary), ).once() reporter.set_status(state, title, check_name, url)
def get_package_config_from_github_release( self, event: dict ) -> Optional[Tuple[JobTriggerType, PackageConfig, GitProject]]: """ look into the provided event and see if it's one for a published github release """ action = nested_get(event, "action") logger.debug(f"action = {action}") release = nested_get(event, "release") if action == "published" and release: repo_namespace = nested_get(event, "repository", "owner", "login") repo_name = nested_get(event, "repository", "name") if not (repo_namespace and repo_name): logger.warning( "We could not figure out the full name of the repository.") return None release_ref = nested_get(event, "release", "tag_name") if not release_ref: logger.warning("Release tag name is not set.") return None logger.info( f"New release event {release_ref} for repo {repo_namespace}/{repo_name}." ) gh_proj = GithubProject(repo=repo_name, namespace=repo_namespace, service=self.github_service) package_config = get_packit_config_from_repo(gh_proj, release_ref) return JobTriggerType.release, package_config, gh_proj return None
def test_check_and_report(whitelist: Whitelist, events: List[Tuple[AbstractGithubEvent, bool]]): """ :param whitelist: fixture :param events: fixture: [(Event, should-be-approved)] """ flexmock( GithubProject, pr_comment=lambda *args, **kwargs: None, set_commit_status=lambda *args, **kwargs: None, issue_comment=lambda *args, **kwargs: None, ) flexmock(PullRequestEvent).should_receive("get_package_config").and_return( flexmock(jobs=[ JobConfig( job=JobType.tests, trigger=JobTriggerType.pull_request, metadata={"targets": ["fedora-rawhide"]}, ) ], )) git_project = GithubProject("", GithubService(), "") for event, is_valid in events: if isinstance(event, PullRequestEvent) and not is_valid: # Report the status flexmock(CoprHelper).should_receive("get_copr_client").and_return( Client( config={ "copr_url": "https://copr.fedorainfracloud.org", "username": "******", })) flexmock(LocalProject).should_receive( "refresh_the_arguments").and_return(None) flexmock(LocalProject).should_receive("checkout_pr").and_return( None) flexmock(Model).should_receive("save").and_return(None) flexmock(StatusReporter).should_receive("report").with_args( description="Account is not whitelisted!", state="error", url=FAQ_URL, check_names=[EXPECTED_TESTING_FARM_CHECK_NAME], ).once() assert (whitelist.check_and_report( event, git_project, config=flexmock(deployment=Deployment.stg, command_handler_work_dir=""), ) is is_valid)
def test_status_instead_check( project, commit_sha, pr_id, pr_object, state, title, summary, check_name, url, check_status, check_conclusion, commit_state_to_set, exception_mock, trigger_id, ): project = GithubProject(None, None, None) reporter = StatusReporter.get_instance(project=project, commit_sha=commit_sha, trigger_id=trigger_id, pr_id=pr_id) act_upon = flexmock(GithubProject) exception, exception_args, exception_kwargs = exception_mock act_upon.should_receive("create_check_run").with_args( name=check_name, commit_sha=commit_sha, url=url, external_id=str(trigger_id), status=check_status, conclusion=check_conclusion, output=create_github_check_run_output(title, summary), ).and_raise(exception, *exception_args, **exception_kwargs).once() act_upon.should_receive("set_commit_status").with_args(commit_sha, commit_state_to_set, url, title, check_name, trim=True).once() reporter.set_status(state, title, check_name, url)
def api_instance_sync_push(sourcegit_and_remote, distgit_and_remote): sourcegit, _ = sourcegit_and_remote distgit, _ = distgit_and_remote repo = flexmock( default_branch="main", get_pulls=lambda state, sort, direction: [], fork="fork" ) service = flexmock(user=flexmock(get_username=lambda: "packit")) up_lp = LocalProject( working_dir=sourcegit, git_project=GithubProject( repo="beer", service=service, namespace="packit.dev", github_repo=repo ), ) api = mock_api_for_source_git(sourcegit, distgit, up_lp) add_source_git_commit_trailer(api) api.dg.specfile.version = "1.2.3" api.dg.commit("dist-git commit to be sync back", "") return api
def test_check_and_report(event, should_pass): w = Whitelist() w.db = { "anakin": { "status": "approved_manually" }, "yoda": { "status": "approved_manually" }, "naboo": { "status": "approved_manually" }, } flexmock( GithubProject, pr_comment=lambda *args, **kwargs: None, set_commit_status=lambda *args, **kwargs: None, issue_comment=lambda *args, **kwargs: None, ) git_project = GithubProject("", GithubService(), "") assert w.check_and_report(event, git_project) == should_pass
def test_check_and_report(whitelist: Whitelist, events: List[Tuple[AbstractGithubEvent, bool]]): """ :param whitelist: fixture :param events: fixture: [(Event, should-be-approved)] """ flexmock( GithubProject, pr_comment=lambda *args, **kwargs: None, set_commit_status=lambda *args, **kwargs: None, issue_comment=lambda *args, **kwargs: None, ) flexmock(PullRequestEvent).should_receive("get_package_config").and_return( flexmock(jobs=[ JobConfig(job=JobType.tests, trigger=JobTriggerType.pull_request, metadata={}) ])) git_project = GithubProject("", GithubService(), "") for event in events: assert (whitelist.check_and_report( event[0], git_project, config=flexmock(deployment=Deployment.stg)) is event[1])
def test_get_service_class_not_found(url, mapping): with pytest.raises(OgrException) as ex: _ = get_service_class(url=url, service_mapping_update=mapping) assert str(ex.value) == "No matching service was found." @pytest.mark.parametrize( "url,mapping,instances,force_custom_instance,result", [ ( "https://github.com/packit-service/ogr", None, None, True, GithubProject( namespace="packit-service", repo="ogr", service=GithubService() ), ), ( "github.com/packit-service/ogr", None, None, True, GithubProject( namespace="packit-service", repo="ogr", service=GithubService() ), ), ( "[email protected]:packit-service/ogr.git", None, None,
) def test_get_service_class_not_found(url, mapping): with pytest.raises(OgrException) as ex: _ = get_service_class(url=url, service_mapping_update=mapping) assert str(ex.value) == "No matching service was found." @pytest.mark.parametrize( "url,mapping,instances,result", [ ( "https://github.com/packit-service/ogr", None, None, GithubProject(namespace="packit-service", repo="ogr", service=GithubService()), ), ( "github.com/packit-service/ogr", None, None, GithubProject(namespace="packit-service", repo="ogr", service=GithubService()), ), ( "[email protected]:packit-service/ogr.git", None, None, GithubProject(namespace="packit-service",
def mock_remote_functionality(distgit, upstream): def mocked_pr_create(*args, **kwargs): return PullRequest( title="", id=42, status=PRStatus.open, url="", description="", author="", source_branch="", target_branch="", created=datetime.datetime(1969, 11, 11, 11, 11, 11, 11), ) flexmock(GithubService) github_service = GithubService() flexmock( GithubService, get_project=lambda repo, namespace: GithubProject( "also-not", github_service, "set", github_repo=flexmock()), ) flexmock( PagureProject, get_git_urls=lambda: {"git": DOWNSTREAM_PROJECT_URL}, fork_create=lambda: None, get_fork=lambda: PagureProject("", "", PagureService()), pr_create=mocked_pr_create, ) flexmock( OurPagure, get_git_urls=lambda: {"git": DOWNSTREAM_PROJECT_URL}, get_fork=lambda: PagureProject("", "", flexmock()), ) flexmock( GithubProject, get_git_urls=lambda: {"git": UPSTREAM_PROJECT_URL}, fork_create=lambda: None, ) def mock_download_remote_sources(): """ mock download of the remote archive and place it into dist-git repo """ tarball_path = distgit / TARBALL_NAME hops_filename = "hops" hops_path = distgit / hops_filename hops_path.write_text("Cascade\n") subprocess.check_call( ["tar", "-cf", str(tarball_path), hops_filename], cwd=distgit) flexmock(SpecFile, download_remote_sources=mock_download_remote_sources) flexmock( DistGit, push_to_fork=lambda *args, **kwargs: None, # let's not hammer the production lookaside cache webserver is_archive_in_lookaside_cache=lambda archive_path: False, build=lambda scratch: None, ) def mocked_new_sources(sources=None): if not Path(sources).is_file(): raise RuntimeError("archive does not exist") flexmock(FedPKG, init_ticket=lambda x=None: None, new_sources=mocked_new_sources) pc = get_local_package_config(str(upstream)) pc.downstream_project_url = str(distgit) pc.upstream_project_url = str(upstream) # https://stackoverflow.com/questions/45580215/using-flexmock-on-python-modules flexmock(sys.modules["packit.bot_api"]).should_receive( "get_packit_config_from_repo").and_return(pc) return upstream, distgit
def _is_private(project: GitProject) -> bool: github_project = GithubProject( repo=project.repo, service=project.service, namespace=project.namespace ) return github_project.github_repo.private
def test_check_and_report(whitelist: Whitelist, events: List[Tuple[AbstractGithubEvent, bool]]): """ :param whitelist: fixture :param events: fixture: [(Event, should-be-approved)] """ flexmock( GithubProject, pr_comment=lambda *args, **kwargs: None, set_commit_status=lambda *args, **kwargs: None, issue_comment=lambda *args, **kwargs: None, ) flexmock(PullRequestGithubEvent).should_receive( "get_package_config").and_return( flexmock(jobs=[ JobConfig( type=JobType.tests, trigger=JobConfigTriggerType.pull_request, metadata=JobMetadataConfig(targets=["fedora-rawhide"]), ) ], )) git_project = GithubProject("", GithubService(), "") for event, is_valid in events: if isinstance(event, PullRequestGithubEvent) and not is_valid: # Report the status flexmock(CoprHelper).should_receive("get_copr_client").and_return( Client( config={ "copr_url": "https://copr.fedorainfracloud.org", "username": "******", })) flexmock(LocalProject).should_receive( "refresh_the_arguments").and_return(None) flexmock(LocalProject).should_receive("checkout_pr").and_return( None) flexmock(StatusReporter).should_receive("report").with_args( description="Account is not whitelisted!", state=CommitStatus.error, url=FAQ_URL, check_names=[EXPECTED_TESTING_FARM_CHECK_NAME], ).once() # get_account returns the whitelist object if it exists # returns nothing if it isn't whitelisted # then inside the whitelist.py file, a function checks if the status is # one of the approved statuses # this exact code is used twice above but mypy has an issue with this one only whitelist_mock = flexmock(DBWhitelist).should_receive("get_account") if not TYPE_CHECKING: if is_valid: whitelist_mock.and_return( DBWhitelist(status="approved_manually")) else: whitelist_mock.and_return(None) assert (whitelist.check_and_report( event, git_project, config=flexmock(deployment=Deployment.stg, command_handler_work_dir=""), ) is is_valid)
def test_check_and_report(allowlist: Allowlist, events: List[Tuple[AbstractGithubEvent, bool]]): """ :param allowlist: fixture :param events: fixture: [(Event, should-be-approved)] """ flexmock( GithubProject, pr_comment=lambda *args, **kwargs: None, set_commit_status=lambda *args, **kwargs: None, issue_comment=lambda *args, **kwargs: None, get_pr=lambda *args, **kwargs: flexmock(source_project=flexmock(), author=None), ) job_configs = [ JobConfig( type=JobType.tests, trigger=JobConfigTriggerType.pull_request, metadata=JobMetadataConfig(targets=["fedora-rawhide"]), ) ] flexmock(PullRequestGithubEvent).should_receive( "get_package_config").and_return(flexmock(jobs=job_configs, )) flexmock(PullRequestModel).should_receive("get_or_create").and_return( flexmock(job_config_trigger_type=JobConfigTriggerType.pull_request)) git_project = GithubProject("", GithubService(), "") for event, is_valid in events: flexmock(GithubProject, can_merge_pr=lambda username: is_valid) flexmock( event, project=git_project).should_receive("get_dict").and_return(None) # needs to be included when running only `test_allowlist` # flexmock(event).should_receive("db_trigger").and_return( # flexmock(job_config_trigger_type=job_configs[0].trigger).mock() # ) flexmock(EventData).should_receive("from_event_dict").and_return( flexmock(commit_sha="0000000", pr_id="0")) if isinstance(event, PullRequestGithubEvent) and not is_valid: # Report the status flexmock(CoprHelper).should_receive("get_copr_client").and_return( Client( config={ "copr_url": "https://copr.fedorainfracloud.org", "username": "******", })) flexmock(LocalProject).should_receive( "refresh_the_arguments").and_return(None) flexmock(LocalProject).should_receive("checkout_pr").and_return( None) flexmock(StatusReporter).should_receive("report").with_args( description="Namespace is not allowed!", state=CommitStatus.error, url=FAQ_URL, check_names=[EXPECTED_TESTING_FARM_CHECK_NAME], ).once() flexmock(packit_service.worker.build.copr_build).should_receive( "get_valid_build_targets").and_return({ "fedora-rawhide-x86_64", }) # get_account returns the allowlist object if it exists # returns nothing if it isn't allowlisted # then inside the allowlist.py file, a function checks if the status is # one of the approved statuses # this exact code is used twice above but mypy has an issue with this one only allowlist_mock = flexmock(DBAllowlist).should_receive("get_account") if not TYPE_CHECKING: if is_valid: allowlist_mock.and_return( DBAllowlist(status="approved_manually")) else: allowlist_mock.and_return(None) assert (allowlist.check_and_report( event, git_project, service_config=flexmock( deployment=Deployment.stg, command_handler_work_dir="", admins=["admin"], ), job_configs=job_configs, ) is is_valid)
def test_check_and_report( allowlist: Allowlist, allowlist_entries, events: Iterable[Tuple[AbstractGithubEvent, bool, Iterable[str]]], ): """ :param allowlist: fixture :param events: fixture: [(Event, should-be-approved)] """ flexmock( GithubProject, create_check_run=lambda *args, **kwargs: None, get_issue=lambda *args, **kwargs: flexmock(comment=lambda *args, ** kwargs: None), get_pr=lambda *args, **kwargs: flexmock(source_project=flexmock(), author=None, comment=lambda *args, **kwargs: None), ) job_configs = [ JobConfig( type=JobType.tests, trigger=JobConfigTriggerType.pull_request, metadata=JobMetadataConfig(_targets=["fedora-rawhide"]), ) ] flexmock(PullRequestGithubEvent).should_receive( "get_package_config").and_return(flexmock(jobs=job_configs, )) flexmock(PullRequestModel).should_receive("get_or_create").and_return( flexmock( job_config_trigger_type=JobConfigTriggerType.pull_request, id=123, job_trigger_model_type=JobTriggerModelType.pull_request, )) flexmock(JobTriggerModel).should_receive("get_or_create").with_args( type=JobTriggerModelType.pull_request, trigger_id=123).and_return( flexmock(id=2, type=JobTriggerModelType.pull_request)) git_project = GithubProject("", GithubService(), "") for event, is_valid, resolved_through in events: flexmock(GithubProject, can_merge_pr=lambda username: is_valid) flexmock( event, project=git_project).should_receive("get_dict").and_return(None) # needs to be included when running only `test_allowlist` # flexmock(event).should_receive("db_trigger").and_return( # flexmock(job_config_trigger_type=job_configs[0].trigger).mock() # ) flexmock(EventData).should_receive("from_event_dict").and_return( flexmock(commit_sha="0000000", pr_id="0")) if isinstance(event, PullRequestGithubEvent) and not is_valid: # Report the status flexmock(CoprHelper).should_receive("get_copr_client").and_return( Client( config={ "copr_url": "https://copr.fedorainfracloud.org", "username": "******", })) flexmock(LocalProject).should_receive( "refresh_the_arguments").and_return(None) flexmock(LocalProject).should_receive("checkout_pr").and_return( None) flexmock(StatusReporter).should_receive("report").with_args( description="Namespace is not allowed!", state=BaseCommitStatus.neutral, url=FAQ_URL, check_names=[EXPECTED_TESTING_FARM_CHECK_NAME], markdown_content=None, ).once() flexmock(packit_service.worker.build.copr_build).should_receive( "get_valid_build_targets").and_return({ "fedora-rawhide-x86_64", }) mock_model(allowlist_entries, resolved_through) assert (allowlist.check_and_report( event, git_project, service_config=flexmock( deployment=Deployment.stg, command_handler_work_dir="", admins=["admin"], ), job_configs=job_configs, ) is is_valid)