示例#1
0
文件: upstream.py 项目: jlebon/packit
 def set_local_project(self):
     """ update self.local_project """
     # TODO: ogr should have a method, something like this:
     #       get_github_service(token, app_id, inst_id, cert_path) -> GithubService
     #       the logic below should be the function
     #       I want to leave this code here up the end of this sprint
     # TODO: in order to support any git forge here, ogr should also have a method like this:
     #       get_github_service_from_url(url, **kwargs):
     #       ogr should guess the forge based on the url; kwargs should be passed to the
     #       constructor in order to support the above
     if (self.config.github_app_id and self.config.github_app_cert_path
             and self.config.github_app_installation_id):
         logger.info("Authenticating with Github using a Githab app.")
         private_key = Path(self.config.github_app_cert_path).read_text()
         integration = github.GithubIntegration(self.config.github_app_id,
                                                private_key)
         token = integration.get_access_token(
             self.config.github_app_installation_id)
         gh_service = GithubService(token=token)
     else:
         logger.debug("Authenticating with Github using a token.")
         gh_service = GithubService(token=self.config.github_token)
     self.local_project.git_service = gh_service
     if not self.local_project.repo_name:
         # will this ever happen?
         self.local_project.repo_name = self.package_name
示例#2
0
    def setUp(self):
        self.token = os.environ.get("GITHUB_TOKEN")
        self.user = os.environ.get("GITHUB_USER")
        test_name = self.id() or "all"

        persistent_data_file = os.path.join(
            PERSISTENT_DATA_PREFIX, f"test_github_data_{test_name}.yaml")
        persistent_object_storage = PersistentObjectStorage(
            persistent_data_file)

        if persistent_object_storage.is_write_mode and (not self.user
                                                        or not self.token):
            raise EnvironmentError(
                "please set GITHUB_TOKEN GITHUB_USER env variables")

        self.service = GithubService(
            token=self.token, persistent_storage=persistent_object_storage)
        self.colin_project = self.service.get_project(namespace="user-cont",
                                                      repo="colin")
        self.colin_fork = self.service.get_project(namespace="user-cont",
                                                   repo="colin",
                                                   is_fork=True)

        self.not_forked_project = self.service.get_project(
            namespace="fedora-modularity", repo="fed-to-brew")
示例#3
0
def main():
    """Create issues warning users of data purge and how to continue injesting knowledge from Thoth."""
    # this takes care of accidentally passing a filter on empty strings to the database which will usually result in no
    # entries being found
    os_name = os.getenv("PURGE_OS_NAME") if os.getenv(
        "PURGE_OS_NAME") else None
    os_version = os.getenv("PURGE_OS_VERSION") if os.getenv(
        "PURGE_OS_VERSION") else None
    python_version = os.getenv("PURGE_PYTHON_VERSION") if os.getenv(
        "PURGE_PYTHON_VERSION") else None

    all_installations = GRAPH.get_kebechet_github_installation_info_with_software_environment_all(
        os_name=os_name,
        os_version=os_version,
        python_version=python_version,
    )
    available_software_runtimes = GRAPH.get_solved_python_package_versions_software_environment_all(
    )
    gh = GithubService(
        token=os.getenv("GITHUB_KEBECHET_TOKEN"),
        github_app_id=os.getenv("GITHUB_APP_ID"),
        github_private_key_path=os.getenv("GITHUB_PRIVATE_KEY_PATH"),
    )

    number_issues_total = len(all_installations)
    number_issues_created = 0

    for i in all_installations:
        try:
            p = gh.get_project(namespace=i["slug"].split("/")[0],
                               repo=i["repo_name"])
            # We shouldn't have to check if the issue exists because the purge job is run for each env only once
            p.create_issue(
                title=
                f"{os_name}:{os_version}py{python_version} being purged from Thoth DB",
                body=_ISSUE_BODY.format(
                    os_name=os_name,
                    os_version=os_version,
                    python_version=python_version,
                    available_runtimes=json.dumps(available_software_runtimes,
                                                  indent=4),
                ),
                private=i["private"],
                labels=["bot"],
            )

            number_issues_created += 1

        except Exception as e:
            _LOGGER.error(
                f"Could not create issue for {i['slug']} because: {e!r}")

    set_schema_metrics()
    number_purge_issues_created.labels(
        component="workflow-helpers",
        env=Configuration.THOTH_DEPLOYMENT_NAME).set(number_issues_created)
    number_purge_issues_total.labels(
        component="workflow-helpers",
        env=Configuration.THOTH_DEPLOYMENT_NAME).set(number_issues_total)
    send_metrics()
示例#4
0
def create_ogr_service(
    service_type: str,
    service_url: Optional[str] = None,
    token: Optional[str] = None,
    github_app_id: Optional[str] = None,
    github_private_key_path: Optional[str] = None,
):
    """Create a new OGR service for interacting with remote GitForges."""
    service_type = service_type.upper()
    if service_type == "GITHUB":
        ogr_service = GithubService(
            token=token,
            github_app_id=os.getenv("GITHUB_APP_ID"),
            github_private_key_path=os.getenv("GITHUB_PRIVATE_KEY_PATH"),
        )
    elif service_type == "GITLAB":
        ogr_service = GitlabService(token=token, instance_url=service_url)
    elif service_type == "PAGURE":
        ogr_service = PagureService(
            token=token,
            instance_url=service_url,
        )
    else:
        raise NotImplementedError(f"Kebechet cannot act on {service_type}")
    return ogr_service
示例#5
0
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
示例#6
0
def run(project_url: str, fork_namespace: str):
    """Run single repo thoth-advise demo."""
    namespace, repo, service_url = _parse_url_4_args(project_url)
    service = GithubService(token=_TOKEN)
    original_project = service.get_project(namespace=namespace, repo=repo)
    if original_project.is_fork:
        original_project.create_issue(
            title="Thoth Demo failed due to forked repository",
            body=templates.run_on_fork_issue,
        )
        raise NotImplementedError(
            "This workflow does not work on projects which are already forks of other projects."
        )

    original_project.github_repo.create_fork(organization=fork_namespace)
    fork_project = service.get_project(namespace=fork_namespace, repo=repo)
    base_thoth_config = pkg_resources.read_text(resources,
                                                "example.thoth.yaml")
    with cloned_repo(fork_project) as repo:
        _write_to_file(".thoth.yaml", base_thoth_config)

        result, error = thamos.lib.advise_here()  # type: ignore

        if error:
            _LOGGER.warning(
                "Thoth adviser failed to resolve a stack... Quitting")
            original_project.create_issue(
                title="Thoth cannot issue advice at this time.",
                body=templates.failed_issue_body)
            exit(1)

        _write_result2files(result)
        repo.git.checkout("-b", "thoth-demo")
        repo.index.add([".thoth.yaml", "Pipfile", "Pipfile.lock"])
        repo.index.commit(
            "Lock down dependencies using Thoth resolution and add basic thoth configuration."
        )
        repo.remotes.origin.push("thoth-demo")
        original_project.create_pr(
            title="Demonstration of Thoth dependency management",
            body=templates.thoth_demo_body,
            target_branch=original_project.default_branch,
            source_branch="thoth-demo",
            fork_username=fork_namespace,
        )
示例#7
0
 def mock_config(self):
     service_config = ServiceConfig()
     service_config.services = {
         GithubService(token="token"),
         GitlabService(token="token"),
     }
     service_config.dry_run = False
     service_config.github_requests_log_path = "/path"
     ServiceConfig.service_config = service_config
示例#8
0
 def setUp(self):
     self.token = os.environ.get("GITHUB_TOKEN")
     self.user = os.environ.get("GITHUB_USER")
     test_name = self.id() or "all"
     self.is_write_mode = bool(os.environ.get("FORCE_WRITE"))
     if self.is_write_mode and (not self.user or not self.token):
         raise EnvironmentError(
             "please set GITHUB_TOKEN GITHUB_USER env variables")
     persistent_data_file = os.path.join(
         PERSISTENT_DATA_PREFIX, f"test_github_data_{test_name}.yaml")
     self.service = GithubService(
         token=self.token,
         persistent_storage=PersistentObjectStorage(persistent_data_file,
                                                    self.is_write_mode),
         read_only=True,
     )
     self.colin_project = self.service.get_project(namespace="user-cont",
                                                   repo="colin")
示例#9
0
 def setup_class(cls):
     service_config = ServiceConfig()
     service_config.services = {
         GithubService(token="12345"),
         PagureService(instance_url="https://git.stg.centos.org",
                       token="6789"),
     }
     service_config.dry_run = False
     service_config.github_requests_log_path = "/path"
     ServiceConfig.service_config = service_config
示例#10
0
 def _init_helper(self):
     """Handle the initialization or reinitialization of OGR object."""
     if self.service_type == ServiceType.GITHUB:
         if self.service_url:
             self.service = GithubService(self.token,
                                          instance_url=self.service_url)
         else:
             self.service = GithubService(self.token)
         self.repository = self.service.get_project(
             repo=self.repo, namespace=self.namespace)
     elif self.service_type == ServiceType.GITLAB:
         if self.service_url:
             self.service = GitlabService(self.token,
                                          instance_url=self.service_url)
         else:
             self.service = GitlabService(self.token)
         self.repository = self.service.get_project(
             repo=self.repo, namespace=self.namespace)
     else:
         raise NotImplementedError
示例#11
0
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)
示例#12
0
 def ogr_service(cls) -> BaseGitService:
     if cls.SERVICE_NAME == "GITHUB":
         return GithubService(
             token=cls.AUTH_TOKEN,
             github_app_id=cls.GITHUB_APP_ID,
             github_app_private_key_path=cls.GITHUB_APP_PRIVATE_KEY_PATH,
         )
     elif cls.SERVICE_NAME == "GITLAB":
         return GitlabService(token=cls.AUTH_TOKEN,
                              instance_url=cls.SERVICE_URL)
     elif cls.SERVICE_NAME == "PAGURE":
         return PagureService(token=cls.AUTH_TOKEN,
                              instance_url=cls.SERVICE_URL)
     else:
         raise NotImplementedError(
             f"Varangian cannot run on {cls.SERVICE_NAME} git services.")
示例#13
0
def _ogr_service_from_dict(service_dict: Dict[str, str]) -> BaseGitService:
    if service_dict["service_name"] == "GITHUB":
        return GithubService(
            token=service_dict.get("auth_token"),
            github_app_id=service_dict.get("github_app_id"),
            github_app_private_key_path=service_dict.get(
                "github_app_private_key_path"),
        )
    elif service_dict["service_name"] == "GITLAB":
        return GitlabService(token=service_dict.get("auth_token"),
                             instance_url=service_dict.get("service_url"))
    elif service_dict["service_name"] == "PAGURE":
        return PagureService(token=service_dict.get("auth_token"),
                             instance_url=service_dict.get("service_url"))
    else:
        raise NotImplementedError(
            f"Varangian cannot run on {service_dict['service_name']} git services."
        )
示例#14
0
class ReadOnly(unittest.TestCase):
    def setUp(self):
        self.token = os.environ.get("GITHUB_TOKEN")
        self.user = os.environ.get("GITHUB_USER")
        test_name = self.id() or "all"
        self.is_write_mode = bool(os.environ.get("FORCE_WRITE"))
        if self.is_write_mode and (not self.user or not self.token):
            raise EnvironmentError(
                "please set GITHUB_TOKEN GITHUB_USER env variables")
        persistent_data_file = os.path.join(
            PERSISTENT_DATA_PREFIX, f"test_github_data_{test_name}.yaml")
        self.service = GithubService(
            token=self.token,
            persistent_storage=PersistentObjectStorage(persistent_data_file,
                                                       self.is_write_mode),
            read_only=True,
        )
        self.colin_project = self.service.get_project(namespace="user-cont",
                                                      repo="colin")

    def tearDown(self):
        self.service.persistent_storage.dump()

    def test_pr_comments(self):
        pr_comments = self.colin_project.get_pr_comments(7)
        assert pr_comments
        assert len(pr_comments) == 2
        assert pr_comments[0].comment.endswith(
            "I've just integrated your thoughts.")
        assert pr_comments[1].comment.startswith("Thank you!")

    def test_create_pr(self):
        pr = self.colin_project.pr_create("title", "text", "master",
                                          "souce_branch")
        assert pr.title == "title"

    def test_create_fork(self):
        fork = self.colin_project.fork_create()
        assert not fork.is_fork
示例#15
0
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
示例#16
0
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])
示例#17
0
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
示例#18
0
def github_service(github_token):
    return GithubService(token=github_token, read_only=True)
示例#19
0
文件: bot_api.py 项目: jlebon/packit
 def _github_service(self):
     return GithubService(token=self.config.github_token)
示例#20
0
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)
示例#21
0
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)
示例#22
0
def github_service(github_token):
    return GithubService(token=github_token)
示例#23
0
 def service(self):
     if not self._service:
         self._service = GithubService(token=self.token, read_only=True)
     return self._service
示例#24
0
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",
                          repo="ogr",
示例#25
0
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 update_keb_installation():
    """Load files and pass them to storages update function."""
    if _SLUG is None:
        _LOGGER.info("No slug present, continuing to next step in task.")
        return

    service = GithubService(
        github_app_id=os.getenv("GITHUB_APP_ID"),
        github_app_private_key_path=os.getenv("GITHUB_PRIVATE_KEY_PATH"),
    )  # TODO: extend to use other services

    project = service.get_project(namespace=_SLUG.split("/")[0], repo=_SLUG.split("/")[1])

    raw_thoth_config = project.get_file_content(".thoth.yaml")

    with TemporaryDirectory() as repo_path, cwd(repo_path):
        thoth_config.load_config_from_string(raw_thoth_config)
        requirements_format = thoth_config.content["requirements_format"]
        overlays_dir = thoth_config.content.get("overlays_dir")
        to_update: List[RuntimeEnvironment]
        if overlays_dir is not None:
            to_update = [RuntimeEnvironment.from_dict(r) for r in thoth_config.list_runtime_environments()]
        else:
            to_update = [RuntimeEnvironment.from_dict(thoth_config.get_runtime_environment())]

        for runtime_environment in to_update:
            if overlays_dir:
                prefix = f"{overlays_dir}/{runtime_environment.name}/"
            else:
                prefix = ""

            if requirements_format == "pipenv":
                pipfile_r = project.get_file_content(f"{prefix}Pipfile")
                with open("Pipfile", "wb") as f:
                    f.write(pipfile_r)

                try:
                    piplock_r = project.get_file_content(f"{prefix}Pipfile.lock")
                    with open("Pipfile.lock", "wb") as f:
                        f.write(piplock_r)
                    project = Project.from_files(pipfile_path="Pipfile", pipfile_lock_path="Pipfile.lock")
                except Exception:
                    _LOGGER.debug("No Pipfile.lock found")
                    project = Project.from_files(
                        pipfile_path="Pipfile",
                        without_pipfile_lock=True,
                        runtime_environment=runtime_environment,
                    )

            elif requirements_format in ["pip", "pip-tools", "pip-compile"]:
                try:
                    requirements_r = project.get_file_content(f"{prefix}requirements.txt")
                    with open("requirements.txt", "wb") as f:
                        f.write(requirements_r)
                    project = Project.from_pip_compile_files(
                        requirements_path="requirements.txt",
                        allow_without_lock=True,
                        runtime_environment=runtime_environment,
                    )
                except Exception:
                    _LOGGER.debug("No requirements.txt found, trying to download requirements.in")
                    requirements_r = project.get_file_content(f"{prefix}requirements.in")
                    with open("requirements.in", "wb") as f:
                        f.write(requirements_r.content)
                    project = Project.from_pip_compile_files(
                        requirements_path="requirements.in",
                        allow_without_lock=True,
                        runtime_environment=runtime_environment,
                    )

                project = Project.from_pip_compile_files(allow_without_lock=True)
            else:
                raise NotImplementedError(f"{requirements_format} requirements format not supported.")

            db.update_kebechet_installation_using_files(
                slug=_SLUG,
                runtime_environment_name=runtime_environment.name,
                installation_id=str(project.github_repo.id),
                requirements=project.pipfile.to_dict(),
                requirements_lock=project.pipfile_lock.to_dict(),
                thoth_config=thoth_config,
            )

        present_installations = db.get_kebechet_github_app_installations_all(slug=_SLUG)
        cur_env_names = {r.name for r in to_update}
        all_env_names = {installation["runtime_environment_name"] for installation in present_installations}
        to_delete = all_env_names - cur_env_names
        for name in to_delete:
            db.delete_kebechet_github_app_installations(slug=_SLUG, runtime_environment=name)
示例#27
0
文件: sync.py 项目: rpitonak/packit
 def sourcegit_service(self) -> GitService:
     return GithubService(token=self.github_token)
# SPDX-License-Identifier: MIT

import enum
import sentry_sdk
import time

from copr.v3 import Client
from datetime import datetime, timedelta, date
from os import getenv

from github import InputGitAuthor
from ogr.services.github import GithubService
from ogr.abstract import PullRequest

copr = Client.create_from_config_file()
service = GithubService(token=getenv("GITHUB_TOKEN"))
project = service.get_project(repo="hello-world", namespace="packit")
user = InputGitAuthor(name="Release Bot",
                      email="*****@*****.**")


class Trigger(str, enum.Enum):
    comment = "comment"
    pr_opened = "pr_opened"
    push = "push"


class Testcase:
    def __init__(self,
                 pr: PullRequest = None,
                 trigger: Trigger = Trigger.pr_opened):
示例#29
0
文件: jobs.py 项目: jlebon/packit
 def github_service(self):
     if self._github_service is None:
         self._github_service = GithubService(
             token=self.config.github_token)
     return self._github_service