Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prescriptions metadata in handlers #155

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions thoth/prescriptions_refresh/handlers/cve_warning.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@

import logging
import os
from typing import TYPE_CHECKING

import thoth.prescriptions_refresh
from thoth.storages import GraphDatabase
from thoth.prescriptions_refresh.prescriptions import Prescriptions

if TYPE_CHECKING:
from thoth.prescriptions_refresh.prescriptions import Prescriptions

_LOGGER = logging.getLogger(__name__)
_PRESCRIPTIONS_DEFAULT_REPO = Prescriptions.DEFAULT_PRESCRIPTIONS_REPO
_PRESCRIPTIONS_VERSION = thoth.prescriptions_refresh.__version__

_CVE_WARNING_COUNT = int(os.getenv("THOTH_PRESCRIPTIONS_REFRESH_CVE_WARNING_COUNT", 3))
_CVE_WARNING_PRESCRIPTION_NAME = "cve_warning.yaml"
_CVE_WARNING_PRESCRIPTION_CONTENT = """\
Expand All @@ -47,6 +49,11 @@
Package '{package_name}' is known to have at least {cve_warning_count} vulnerabilities reported in releases
link: cve_warning
package_name: {package_name}
metadata:
- prescriptions_repository: {default_prescriptions_repository}
prescriptions_version: {prescriptions_version}
last_cve_database_update: {last_cve_database_update}

"""


Expand All @@ -55,6 +62,8 @@ def cve_warning(prescriptions: "Prescriptions") -> None:
graph = GraphDatabase()
graph.connect()

cve_timestamp = graph.get_cve_timestamp()

for project_name in prescriptions.iter_projects():
if len(graph.get_python_cve_records_all(project_name)) >= _CVE_WARNING_COUNT:
prescription_name = prescriptions.get_prescription_name("CVEWarningWrap", project_name)
Expand All @@ -63,7 +72,12 @@ def cve_warning(prescriptions: "Prescriptions") -> None:
project_name=project_name,
prescription_name=_CVE_WARNING_PRESCRIPTION_NAME,
content=_CVE_WARNING_PRESCRIPTION_CONTENT.format(
package_name=project_name, prescription_name=prescription_name, cve_warning_count=_CVE_WARNING_COUNT
package_name=project_name,
prescription_name=prescription_name,
cve_warning_count=_CVE_WARNING_COUNT,
default_prescriptions_repository=_PRESCRIPTIONS_DEFAULT_REPO,
prescriptions_version=_PRESCRIPTIONS_VERSION,
last_cve_database_update=cve_timestamp,
),
commit_message=f"Project {project_name!r} has at least {_CVE_WARNING_COUNT} vulnerabilities reported",
)
Expand Down
14 changes: 10 additions & 4 deletions thoth/prescriptions_refresh/handlers/gh_archived.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@
import logging
import requests
import sys
from typing import TYPE_CHECKING

import thoth.prescriptions_refresh
from thoth.prescriptions_refresh.prescriptions import Prescriptions
from .gh_link import iter_gh_info


if TYPE_CHECKING:
from thoth.prescriptions_refresh.prescriptions import Prescriptions

_LOGGER = logging.getLogger(__name__)
_PRESCRIPTIONS_DEFAULT_REPO = Prescriptions.DEFAULT_PRESCRIPTIONS_REPO
_PRESCRIPTIONS_VERSION = thoth.prescriptions_refresh.__version__

_GH_LINK_PRESCRIPTION_NAME = "gh_archived.yaml"
_GH_LINK_PRESCRIPTION_CONTENT = """\
units:
Expand All @@ -47,6 +48,9 @@
message: Package '{package_name}' is marked as archived on GitHub
link: {gh_link}
package_name: {package_name}
metadata:
- prescriptions_repository: {default_prescriptions_repository}
prescriptions_version: {prescriptions_version}
"""


Expand Down Expand Up @@ -96,6 +100,8 @@ def gh_archived(prescriptions: "Prescriptions") -> None:
package_name=project_name,
prescription_name=prescription_name,
gh_link=gh_link,
default_prescriptions_repository=_PRESCRIPTIONS_DEFAULT_REPO,
prescriptions_version=_PRESCRIPTIONS_VERSION,
),
commit_message=f"Repository for {project_name!r} is marked as archived on GitHub",
)
Expand Down
13 changes: 9 additions & 4 deletions thoth/prescriptions_refresh/handlers/gh_contributors.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
import os
import requests
import sys
from typing import TYPE_CHECKING

import thoth.prescriptions_refresh
from thoth.prescriptions_refresh.prescriptions import Prescriptions
from .gh_link import iter_gh_info


if TYPE_CHECKING:
from thoth.prescriptions_refresh.prescriptions import Prescriptions

_LOGGER = logging.getLogger(__name__)
_PRESCRIPTIONS_DEFAULT_REPO = Prescriptions.DEFAULT_PRESCRIPTIONS_REPO
_PRESCRIPTIONS_VERSION = thoth.prescriptions_refresh.__version__

_CONTRIBUTORS_COUNT = int(os.getenv("THOTH_PRESCRIPTIONS_REFRESH_GH_CONTRIBUTORS_COUNT", 5))
_GH_LINK_PRESCRIPTION_NAME = "gh_contributors.yaml"
Expand All @@ -50,6 +50,9 @@
message: Package '{package_name}' has less than {contributors} contributors on GitHub
link: {gh_link}
package_name: {package_name}
metadata:
- prescriptions_repository: {default_prescriptions_repository}
prescriptions_version: {prescriptions_version}
"""


Expand Down Expand Up @@ -101,6 +104,8 @@ def gh_contributors(prescriptions: "Prescriptions") -> None:
prescription_name=prescription_name,
gh_link=gh_link,
contributors=_CONTRIBUTORS_COUNT,
default_prescriptions_repository=_PRESCRIPTIONS_DEFAULT_REPO,
prescriptions_version=_PRESCRIPTIONS_VERSION,
),
commit_message=f"Project {project_name!r} has less than {_CONTRIBUTORS_COUNT} contributors on GitHub",
)
Expand Down
14 changes: 10 additions & 4 deletions thoth/prescriptions_refresh/handlers/gh_forked.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@
import logging
import requests
import sys
from typing import TYPE_CHECKING

import thoth.prescriptions_refresh
from thoth.prescriptions_refresh.prescriptions import Prescriptions
from .gh_link import iter_gh_info


if TYPE_CHECKING:
from thoth.prescriptions_refresh.prescriptions import Prescriptions

_LOGGER = logging.getLogger(__name__)
_PRESCRIPTIONS_DEFAULT_REPO = Prescriptions.DEFAULT_PRESCRIPTIONS_REPO
_PRESCRIPTIONS_VERSION = thoth.prescriptions_refresh.__version__

_GH_LINK_PRESCRIPTION_NAME = "gh_forked.yaml"
_GH_LINK_PRESCRIPTION_CONTENT = """\
units:
Expand All @@ -47,6 +48,9 @@
message: Package '{package_name}' is a GitHub fork
link: {gh_link}
package_name: {package_name}
metadata:
- prescriptions_repository: {default_prescriptions_repository}
prescriptions_version: {prescriptions_version}
"""


Expand Down Expand Up @@ -96,6 +100,8 @@ def gh_forked(prescriptions: "Prescriptions") -> None:
package_name=project_name,
prescription_name=prescription_name,
gh_link=gh_link,
default_prescriptions_repository=_PRESCRIPTIONS_DEFAULT_REPO,
prescriptions_version=_PRESCRIPTIONS_VERSION,
),
commit_message=f"Project {project_name!r} is a fork on GitHub",
)
12 changes: 11 additions & 1 deletion thoth/prescriptions_refresh/handlers/gh_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@
from typing import Tuple
from typing import TYPE_CHECKING

import thoth.prescriptions_refresh
from thoth.prescriptions_refresh.prescriptions import Prescriptions

if TYPE_CHECKING:
from thoth.prescriptions_refresh.knowledge import Knowledge
from thoth.prescriptions_refresh.prescriptions import Prescriptions

_LOGGER = logging.getLogger(__name__)
_PRESCRIPTIONS_DEFAULT_REPO = Prescriptions.DEFAULT_PRESCRIPTIONS_REPO
_PRESCRIPTIONS_VERSION = thoth.prescriptions_refresh.__version__

_GH_LINK_PRESCRIPTION_NAME = "gh_link.yaml"
_GH_LINK_PRESCRIPTION_CONTENT = """\
units:
Expand All @@ -53,6 +58,9 @@
message: Package '{package_name}' is hosted on GitHub
link: {gh_url}
package_name: {package_name}
metadata:
- prescriptions_repository: {default_prescriptions_repository}
prescriptions_version: {prescriptions_version}
"""


Expand Down Expand Up @@ -190,6 +198,8 @@ def gh_link(knowledge: "Knowledge") -> None:
package_name=project_name,
gh_url=gh_url,
prescription_name=prescription_name,
default_prescriptions_repository=_PRESCRIPTIONS_DEFAULT_REPO,
prescriptions_version=_PRESCRIPTIONS_VERSION,
)

knowledge.prescriptions.create_prescription(
Expand Down
14 changes: 10 additions & 4 deletions thoth/prescriptions_refresh/handlers/gh_popularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
from typing import Any
from typing import Tuple
from typing import Dict
from typing import TYPE_CHECKING

import thoth.prescriptions_refresh
from thoth.prescriptions_refresh.prescriptions import Prescriptions
from .gh_link import iter_gh_info


if TYPE_CHECKING:
from thoth.prescriptions_refresh.prescriptions import Prescriptions

_LOGGER = logging.getLogger(__name__)
_PRESCRIPTIONS_DEFAULT_REPO = Prescriptions.DEFAULT_PRESCRIPTIONS_REPO
_PRESCRIPTIONS_VERSION = thoth.prescriptions_refresh.__version__

_GH_POPULARITY_LOW = int(os.getenv("THOTH_PRESCRIPTIONS_REFRESH_GH_POPULARITY_LOW", 20))
_GH_POPULARITY_MODERATE = int(os.getenv("THOTH_PRESCRIPTIONS_REFRESH_GH_POPULARITY_MODERATE", 100))
_GH_POPULARITY_HIGH = int(os.getenv("THOTH_PRESCRIPTIONS_REFRESH_GH_POPULARITY_HIGH", 1000))
Expand All @@ -54,6 +55,9 @@
message: Project '{package_name}' has {popularity_score} popularity on GitHub
link: {gh_link}
package_name: {package_name}
metadata:
- prescriptions_repository: {default_prescriptions_repository}
prescriptions_version: {prescriptions_version}
"""


Expand Down Expand Up @@ -123,6 +127,8 @@ def gh_popularity(prescriptions: "Prescriptions") -> None:
popularity_score=popularity_score,
message_type=message_type,
prescription_name=prescription_name,
default_prescriptions_repository=_PRESCRIPTIONS_DEFAULT_REPO,
prescriptions_version=_PRESCRIPTIONS_VERSION,
),
commit_message=f"Update of GitHub popularity statistics for project {project_name!r}",
)
15 changes: 12 additions & 3 deletions thoth/prescriptions_refresh/handlers/gh_release_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
import logging
import requests
import sys
from typing import TYPE_CHECKING

import thoth.prescriptions_refresh
from thoth.prescriptions_refresh.prescriptions import Prescriptions
from .gh_link import iter_gh_info

if TYPE_CHECKING:
from thoth.prescriptions_refresh.prescriptions import Prescriptions

_LOGGER = logging.getLogger(__name__)
_PRESCRIPTIONS_DEFAULT_REPO = Prescriptions.DEFAULT_PRESCRIPTIONS_REPO
_PRESCRIPTIONS_VERSION = thoth.prescriptions_refresh.__version__

_GH_LINK_PRESCRIPTION_NAME = "gh_release_notes.yaml"
_GH_LINK_PRESCRIPTION_CONTENT = """\
units:
Expand All @@ -44,6 +46,9 @@
release_notes:
organization: {organization}
repository: {repository}
metadata:
- prescriptions_repository: {default_prescriptions_repository}
prescriptions_version: {prescriptions_version}
"""


Expand Down Expand Up @@ -88,6 +93,8 @@ def gh_release_notes(prescriptions: "Prescriptions") -> None:
package_name=project_name,
organization=organization,
repository=repository,
default_prescriptions_repository=_PRESCRIPTIONS_DEFAULT_REPO,
prescriptions_version=_PRESCRIPTIONS_VERSION,
),
commit_message=f"Project {project_name!r} hosts release notes on GitHub",
)
Expand All @@ -112,6 +119,8 @@ def gh_release_notes(prescriptions: "Prescriptions") -> None:
package_name=project_name,
organization=organization,
repository=repository,
default_prescriptions_repository=_PRESCRIPTIONS_DEFAULT_REPO,
prescriptions_version=_PRESCRIPTIONS_VERSION,
),
commit_message=f"Project {project_name!r} hosts release notes on GitHub",
)
Expand Down
14 changes: 10 additions & 4 deletions thoth/prescriptions_refresh/handlers/gh_updated.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
import os
import requests
import sys
from typing import TYPE_CHECKING

import thoth.prescriptions_refresh
from thoth.prescriptions_refresh.prescriptions import Prescriptions
from .gh_link import iter_gh_info


if TYPE_CHECKING:
from thoth.prescriptions_refresh.prescriptions import Prescriptions

_LOGGER = logging.getLogger(__name__)
_PRESCRIPTIONS_DEFAULT_REPO = Prescriptions.DEFAULT_PRESCRIPTIONS_REPO
_PRESCRIPTIONS_VERSION = thoth.prescriptions_refresh.__version__

_MARK_DAYS = int(os.getenv("THOTH_PRESCRIPTIONS_REFRESH_GH_UPDATED_DAYS", 365))
_GH_LINK_PRESCRIPTION_NAME = "gh_updated.yaml"
_GH_LINK_PRESCRIPTION_CONTENT = """\
Expand All @@ -50,6 +51,9 @@
message: Package '{package_name}' was last updated at {updated_at}
link: {gh_link}
package_name: {package_name}
metadata:
- prescriptions_repository: {default_prescriptions_repository}
prescriptions_version: {prescriptions_version}
"""


Expand Down Expand Up @@ -115,6 +119,8 @@ def gh_updated(prescriptions: "Prescriptions") -> None:
prescription_name=prescription_name,
gh_link=gh_link,
updated_at=str(commit_datetime),
default_prescriptions_repository=_PRESCRIPTIONS_DEFAULT_REPO,
prescriptions_version=_PRESCRIPTIONS_VERSION,
),
commit_message=f"Project {project_name!r} was not updated for more than {_MARK_DAYS} days",
)
7 changes: 6 additions & 1 deletion thoth/prescriptions_refresh/handlers/image_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from itertools import chain
from typing import Dict, Any, Optional, Tuple

import thoth.prescriptions_refresh
from thoth.python import Pipfile, PipfileLock

from thoth.prescriptions_refresh.prescriptions import Prescriptions
from .quay.common import get_ps_s2i_image_names
from .quay.common import get_image_containers
Expand Down Expand Up @@ -59,6 +59,9 @@
message: >-
Found predictive stack image that can be used with these dependencies
link: {link}
metadata:
- prescriptions_repository: {default_prescriptions_repository}
prescriptions_version: {prescriptions_version}
advised_manifest_changes:
- file: .thoth.yaml
patch:
Expand Down Expand Up @@ -219,6 +222,8 @@ def thoth_image_analysis(prescriptions: "Prescriptions") -> None:
image=image_url,
resolved_dependencies=resolved_dependencies,
link=image_url,
default_prescriptions_repository=Prescriptions.DEFAULT_PRESCRIPTIONS_REPO,
prescriptions_version=thoth.prescriptions_refresh.__version__,
),
commit_message=f"Created prescriptions from predictable stack image: {image_url}",
)
Expand Down
Loading