Exemplo n.º 1
0
    def run(self, labels: list, analysis_id=None):
        """Run Thoth Advising Bot."""
        if self.parsed_payload:
            if self.parsed_payload.get("event") not in _EVENTS_SUPPORTED:
                _LOGGER.info(
                    "ThothAdviseManager doesn't act on %r events.",
                    self.parsed_payload.get("event"),
                )
                return

        if analysis_id is None:
            with cloned_repo(self, depth=1) as repo:
                self.repo = repo
                if not os.path.isfile("Pipfile"):
                    _LOGGER.warning("Pipfile not found in repo... Creating issue")
                    self.sm.open_issue_if_not_exist(
                        "Missing Pipfile",
                        lambda: "Check your repository to make sure Pipfile exists",
                        labels=labels,
                    )
                    return False

                lib.advise_here(
                    nowait=True,
                    origin=(f"{self.service_url}/{self.slug}"),
                    source_type=ThothAdviserIntegrationEnum.KEBECHET,
                    kebechet_metadata=self.metadata,
                )
            return True
        else:
            with cloned_repo(self, depth=1) as repo:
                self.repo = repo
                _LOGGER.info("Using analysis results from %s", analysis_id)
                res = lib.get_analysis_results(analysis_id)
                branch_name = self._construct_branch_name()
                branch = self.repo.git.checkout("-B", branch_name)  # noqa F841
                self._cached_merge_requests = self.sm.repository.get_pr_list()

                if res is None:
                    _LOGGER.error(
                        "Advise failed on server side, contact the maintainer"
                    )
                    return False
                _LOGGER.debug(json.dumps(res))

                if res[1] is False:
                    _LOGGER.info("Advise succeeded")
                    self._write_advise(res)
                    self._open_merge_request(
                        branch_name, labels, ["Pipfile.lock"], res[0].get("metadata")
                    )
                    return True
                else:
                    _LOGGER.warning(
                        "Found error while running adviser... Creating issue"
                    )
                    self._issue_advise_error(res, labels)
                    return False
Exemplo n.º 2
0
    def run(self, labels: list, analysis_id: Optional[str] = None):
        """Run the provenance check bot."""
        if self.parsed_payload:
            if self.parsed_payload.get("event") not in _EVENTS_SUPPORTED:
                _LOGGER.info(
                    "ThothProvenanceManager doesn't act on %r events.",
                    self.parsed_payload.get("event"),
                )
                return

        if not analysis_id:
            with cloned_repo(self, depth=1) as repo:
                self.repo = repo
                if not (os.path.isfile("Pipfile")
                        and os.path.isfile("Pipfile.lock")):
                    _LOGGER.warning(
                        "Pipfile or Pipfile.lock is missing from repo, opening issue"
                    )
                    issue = self.get_issue_by_title("Missing pipenv files")
                    if issue is None:
                        self.project.create_issue(
                            title="Missing pipenv files",
                            body=
                            "Check your repository to make sure Pipfile and Pipfile.lock exist.",
                            labels=labels,
                        )
                    return False
                _LOGGER.info((self.service_url + self.slug))
                lib.provenance_check_here(
                    nowait=True, origin=f"{self.service_url}/{self.slug}")
            return True
        else:
            if not analysis_id.startswith("provenance"):
                _LOGGER.debug(
                    "Analysis id isn't provenance, manager terminating...")
                return False

            with cloned_repo(self, depth=1) as repo:
                res = lib.get_analysis_results(analysis_id)
                if res is None:
                    _LOGGER.error(
                        "Provenance check failed on server side, contact the maintainer"
                    )
                    return False
                if res[1] is False:
                    _LOGGER.info(
                        "Provenance check found problems, creating issue...")
                    self._issue_provenance_error(res, labels)
                    return False
                else:
                    _LOGGER.info(
                        "Provenance check found no problems, carry on coding :)"
                    )
                    return True
Exemplo n.º 3
0
    def run(self, labels: list, analysis_id=None):
        """Run the provenance check bot."""
        if self.parsed_payload:
            if self.parsed_payload.get('event') not in _EVENTS_SUPPORTED:
                _LOGGER.info(
                    "ThothProvenanceManager doesn't act on %r events.",
                    self.parsed_payload.get('event'))
                return

        if not analysis_id:
            with cloned_repo(self.service_url, self.slug, depth=1) as repo:
                self.repo = repo
                if not (os.path.isfile("Pipfile")
                        and os.path.isfile("Pipfile.lock")):
                    _LOGGER.warning(
                        "Pipfile or Pipfile.lock is missing from repo, opening issue"
                    )
                    self.sm.open_issue_if_not_exist(
                        "Missing pipenv files",
                        lambda:
                        "Check your repository to make sure Pipfile and Pipfile.lock exist.",
                        labels=labels)
                    return False
                _LOGGER.info((self.service_url + self.slug))
                lib.provenance_check_here(
                    nowait=True, origin=f"{self.service_url}/{self.slug}")
            return True
        else:
            with cloned_repo(self.service_url, self.slug, depth=1) as repo:
                res = lib.get_analysis_results(analysis_id)
                if res is None:
                    _LOGGER.error(
                        "Provenance check failed on server side, contact the maintainer"
                    )
                    return False
                if res[1] is False:
                    _LOGGER.info(
                        "Provenance check found problems, creating issue...")
                    self._issue_provenance_error(res, labels)
                    return False
                else:
                    _LOGGER.info(
                        "Provenance check found no problems, carry on coding :)"
                    )
                    return True
Exemplo n.º 4
0
    def run(self, labels: list, analysis_id=None):
        """Run Thoth Advising Bot."""
        if analysis_id is None:
            with cloned_repo(self.service_url, self.slug, depth=1) as repo:
                self.repo = repo
                if not os.path.isfile("Pipfile"):
                    _LOGGER.warning(
                        "Pipfile not found in repo... Creating issue")
                    self.sm.open_issue_if_not_exist(
                        "Missing Pipfile",
                        lambda:
                        "Check your repository to make sure Pipfile exists",
                        labels=labels)
                    return False

                lib.advise_here(nowait=True,
                                origin=(f"{self.service_url}/{self.slug}"))
            return True
        else:
            with cloned_repo(self.service_url, self.slug, depth=1) as repo:
                self.repo = repo
                _LOGGER.info("Using analysis results from %s", analysis_id)
                res = lib.get_analysis_results(analysis_id)
                branch_name = self._construct_branch_name()
                branch = self.repo.git.checkout("-B", branch_name)
                self._cached_merge_requests = self.sm.repository.merge_requests

                if res is None:
                    _LOGGER.error(
                        "Advise failed on server side, contact the maintainer")
                    return False
                _LOGGER.debug(json.dumps(res))

                if res[1] is False:
                    _LOGGER.info('Advise succeeded')
                    self._write_advise(res)
                    self._open_merge_request(branch_name, labels,
                                             ["Pipfile.lock"])
                    return True
                else:
                    _LOGGER.warning(
                        'Found error while running adviser... Creating issue')
                    self._issue_advise_error(res, labels)
                    return False
Exemplo n.º 5
0
    def run(self, labels: list, analysis_id=None):
        """Run Thoth Advising Bot."""
        if self.parsed_payload:
            if self.parsed_payload.get("event") not in _EVENTS_SUPPORTED:
                _LOGGER.info(
                    "ThothAdviseManager doesn't act on %r events.",
                    self.parsed_payload.get("event"),
                )
                return

        self._issue_list = self.project.get_issue_list()
        self._close_advise_issues4users_lacking_perms()
        self._tracking_issue = self._close_all_but_oldest_issue()
        runtime_environments: typing.List[typing.Tuple[str, Issue]]

        if analysis_id is None:
            if self._tracking_issue is None:
                _LOGGER.debug("No issue found to start advises for.")
                return
            elif not self._advise_issue_is_fresh(self._tracking_issue):
                _LOGGER.debug(
                    "Issue has already been acted on by Kebechet and is still 'in progress'"
                )
                return

            with cloned_repo(self, self.project.default_branch, depth=1) as repo:
                self.repo = repo

                with open(".thoth.yaml", "r") as f:
                    thoth_config = yaml.safe_load(f)

                if thoth_config.get("overlays_dir"):
                    runtime_environments = [
                        e["name"] for e in thoth_config["runtime_environments"]
                    ]
                    for e in thoth_config["runtime_environments"]:
                        runtime_environments.append(e["name"])
                else:
                    runtime_environments = [
                        thoth_config["runtime_environments"][0]["name"]
                    ]

                for e in runtime_environments:
                    try:
                        analysis_id = lib.advise_here(
                            nowait=True,
                            origin=(f"{self.service_url}/{self.slug}"),
                            source_type=ThothAdviserIntegrationEnum.KEBECHET,
                            kebechet_metadata=self.metadata,
                            runtime_environment_name=e,
                        )
                        self._tracking_issue.comment(
                            STARTED_ADVISE_COMMENT.format(
                                analysis_id=analysis_id,
                                env_name=e,
                                host=thoth_config["host"],
                            )
                        )
                    except (FileLoadError, ThothConfigurationError) as exc:
                        if isinstance(exc, FileLoadError):
                            self._tracking_issue.comment(
                                f"""Result for {e}: Error advising, no requirements found.

                                If this project does not use requirements.txt or Pipfile then remove thoth-advise
                                manager from your .thoth.yaml configuration."""
                            )
                        elif isinstance(exc, ThothConfigurationError):
                            self._tracking_issue.comment(
                                f"""Result for {e}: Error advising, configuration error found in .thoth.yaml. The
                                following exception was caught when submitting:

                                ```
                                {exc}
                                ```"""
                            )
                            # open issue
                            # comment on advise issue with issue id
                        return False
            return True
        else:
            with cloned_repo(self, self.project.default_branch, depth=1) as repo:
                self.repo = repo
                _LOGGER.info("Using analysis results from %s", analysis_id)
                res = lib.get_analysis_results(analysis_id)
                if self._metadata_indicates_internal_trigger(res[0].get("metadata")):
                    self._tracking_issue = None  # internal trigger advise results should not be tracked by issue
                branch_name = self._construct_branch_name(analysis_id)
                branch = self.repo.git.checkout("-B", branch_name)  # noqa F841
                self._cached_merge_requests = self.project.get_pr_list()

                if res is None:
                    _LOGGER.error(
                        "Advise failed on server side, contact the maintainer"
                    )
                    return False
                _LOGGER.debug(json.dumps(res))

                self.runtime_environment = _runtime_env_name_from_advise_response(
                    res[0]
                )
                to_ret = False
                if res[1] is False:
                    _LOGGER.info("Advise succeeded")
                    self._write_advise(res)
                    opened_merge = self._open_merge_request(
                        branch_name, labels, ["Pipfile.lock"], res[0].get("metadata")
                    )
                    if opened_merge and self._tracking_issue:
                        comment = (
                            SUCCESSFUL_ADVISE_COMMENT.format(
                                env=self.runtime_environment
                            )
                            + f"Opened merge request, see: #{opened_merge.id}"
                        )
                        self._tracking_issue.comment(comment)
                    elif self._tracking_issue:
                        comment = (
                            SUCCESSFUL_ADVISE_COMMENT.format(
                                env=self.runtime_environment
                            )
                            + "Dependencies for this runtime environment are already up to date :)."
                        )
                        self._tracking_issue.comment(comment)
                    to_ret = True
                else:
                    _LOGGER.warning(
                        "Found error while running adviser... Creating issue"
                    )
                    self._act_on_advise_error(res)
                if self._tracking_issue:
                    to_open = len(
                        self._tracking_issue.get_comments(
                            filter_regex=STARTED_ADVISE_REGEX,
                            author=APP_NAME,
                        )
                    )
                    finished = len(
                        self._tracking_issue.get_comments(
                            filter_regex=SUCCESSFUL_ADVISE_REGEX,
                            author=APP_NAME,
                        )
                    )
                    errors = len(
                        self._tracking_issue.get_comments(
                            filter_regex=ERROR_ADVISE_REGEX,
                            author=APP_NAME,
                        )
                    )
                    if to_open - finished == 0:
                        if errors > 0:
                            comment = f"""All advises complete, but leaving issue open because {errors} could not be
                            successfully submitted and may require user action."""
                        else:
                            self._tracking_issue.comment(
                                "Finished advising for all environments."
                            )
                            self._tracking_issue.close()
                return to_ret