diff --git a/collectoss/application/schema/alembic/versions/47_populate_default_data_for_forge_.py b/collectoss/application/schema/alembic/versions/47_populate_default_data_for_forge_.py new file mode 100644 index 000000000..28ff8377a --- /dev/null +++ b/collectoss/application/schema/alembic/versions/47_populate_default_data_for_forge_.py @@ -0,0 +1,108 @@ +"""Populate default data for forge_instances table + +Revision ID: 47 +Revises: 46 +Create Date: 2026-11-16 12:19:48.031786 + +""" +from alembic import op +import sqlalchemy as sa + +from sqlalchemy import Column, Integer, String, Boolean, DateTime, func +from sqlalchemy.orm import relationship, DeclarativeBase, Session + +from enum import Enum +from datetime import datetime +from zoneinfo import ZoneInfo + +# revision identifiers, used by Alembic. +revision = '47' +down_revision = '46' +branch_labels = None +depends_on = None + +# Normally we dont allow migrations to create/modify data in a database except as part of structural schema changes +# however, we anticipate a future migration to adjust foreign keys to rely on this table instead, and need to +# effectively guarantee the data exists prior to that migration existing in a way that is automatic for most instances. + + +# We include a copy of the forge_instance schema definition and other relevant types here because it may change in the future and we need to be accurate +# to what the state of a CollectOSS instance is going to be prior to this migration + + +class ForgePlatformType(Enum): + """Identification values for different known forges. + Intended for helping identify the relevant API endpoints and tasks to use for collection""" + UNRESOLVEABLE = 0 + GITHUB = 1 + GITLAB = 2 + +class Base(DeclarativeBase): + pass + +class ForgeInstance(Base): + __tablename__ = "forge_instance" + __table_args__ = { "schema": "operations" } + + id = Column(Integer, primary_key=True, nullable=False, comment="Internal unique identifier for this forge instance") + # platform_type stores an integer that CollectOSS maps/will map to it's internal platform identifier Enum + # (as used in ContributorUUID) for identifying the API endpoints and tasks to use for collection + platform_type = Column(Integer, nullable=False, comment="Type specifier identifying the relevant platform API interface to CollectOSS") + name = Column(String, nullable=False, comment="User-specified name for this forge instance") + # https://stackoverflow.com/a/54800233 + date_added = Column(DateTime(timezone=True), nullable=False, default=func.now()) + domain_name = Column(String, nullable=False, comment="The base domain name (without the scheme) where this instance is hosted") + enabled = Column(Boolean, default=True, nullable=False, comment="denotes whether collection should run for this instance") + privacy_url = Column(String, nullable=False, comment="The link to the privacy policy for this instance") + terms_url = Column(String, nullable=False, comment="The link to the terms of service for this instance") + policy_reviewed_date = Column(DateTime(timezone=True), nullable=False, default=func.now()) + policy_reviewed_by = Column(String, nullable=False, comment="Who conducted the last review of these policies") + policy_review_comments = Column(String, nullable=True, comment="Reviewer comments from the most recent policy review.") + +# these are the default definitions for forge instance objects in the DB provided by CollectOSS maintainers. +# Future instances will likely be added by instance operators, not via migrations +DEFAULT_FORGES = [ + ForgeInstance( + platform_type=ForgePlatformType.GITHUB.value, + name="GitHub (default)", + domain_name="github.com", + privacy_url = "https://docs.github.com/en/site-policy/privacy-policies/github-general-privacy-statement", + terms_url = "https://docs.github.com/en/site-policy/github-terms/github-terms-of-service", + policy_reviewed_date = datetime(2026, 10, 12, tzinfo=ZoneInfo("America/New_York")), + policy_reviewed_by = "CollectOSS Maintainers", + policy_review_comments = "CollectOSS primarily accesses public information from GitHub. Terms state: " + "\"Depending on your account settings, we may share Personal Data with other users of the Services " + " and the public. You control what information is made public.\"", + enabled=True + ), + ForgeInstance( + platform_type=ForgePlatformType.GITLAB.value, + name="GitLab (default)", + domain_name="gitlab.com", + privacy_url = "https://about.gitlab.com/privacy/", + terms_url = "https://about.gitlab.com/terms/", + policy_reviewed_date = datetime(2026, 11, 16, tzinfo=ZoneInfo("America/New_York")), + policy_reviewed_by = "CollectOSS Maintainers", + policy_review_comments = "CollectOSS primarily accesses public information from GitLab. Terms state: " + "\"We may share your Personal Data with other users of the Services and with the public if you " + "choose to make your SaaS Profile public. You have control over what information is public;\"", + enabled=True + ) +] + +def upgrade(): + bind = op.get_bind() + session = Session(bind=bind) + + session.add_all(DEFAULT_FORGES) + session.flush() + + +def downgrade(): + bind = op.get_bind() + session = Session(bind=bind) + + # Delete only the rows you seeded (skip this if downgrade drops the whole table) + forge_ids = [forge.id for forge in DEFAULT_FORGES] + session.execute(sa.delete(ForgeInstance).where(ForgeInstance.id.in_(forge_ids))) + session.flush() diff --git a/collectoss/util/startup.py b/collectoss/util/startup.py index 8fe5b2374..e9f64851e 100644 --- a/collectoss/util/startup.py +++ b/collectoss/util/startup.py @@ -9,6 +9,11 @@ import platform import sys +from datetime import datetime +from zoneinfo import ZoneInfo + +from collectoss.application.db.models.operations import ForgeInstance +from collectoss.util.enums import ForgePlatformType from sqlalchemy.orm.attributes import get_history from collectoss.application.config import SystemConfig from collectoss.application.db.session import DatabaseSession @@ -219,6 +224,57 @@ def merge_config( config.load_config_from_dict(augmented_config) +def initialize_tables(engine, logger): + """Initialize certain tables with data that is required for first run + + This includes: + - populating the forge_instances table with the core forges (github, gitlab) + - ensuring default repos are present + + Args: + engine (_type_): the database engine to use + logger (_type_): the logger instance to use + """ + + # Step 1: ensure theres at least one forge in forge_instances + with DatabaseSession(logger, engine=engine) as session: + + + all_instance_count = session.query(ForgeInstance).count() + + if all_instance_count == 0: + github_instance = ForgeInstance( + platform_type=ForgePlatformType.GITHUB.value, + name="GitHub (default)", + domain_name="github.com", + privacy_url = "https://docs.github.com/en/site-policy/privacy-policies/github-general-privacy-statement", + terms_url = "https://docs.github.com/en/site-policy/github-terms/github-terms-of-service", + policy_reviewed_date = datetime(2026, 10, 12, tzinfo=ZoneInfo("America/New_York")), + policy_reviewed_by = "CollectOSS Maintainers", + policy_review_comments = "CollectOSS primarily accesses public information from GitHub. Terms state: " + "\"Depending on your account settings, we may share Personal Data with other users of the Services " + " and the public. You control what information is made public.\"", + enabled=True + ) + session.add(github_instance) + session.commit() + + gitlab_instance = ForgeInstance( + platform_type=ForgePlatformType.GITLAB.value, + name="GitLab (default)", + domain_name="gitlab.com", + privacy_url = "https://about.gitlab.com/privacy/", + terms_url = "https://about.gitlab.com/terms/", + policy_reviewed_date = datetime(2026, 11, 16, tzinfo=ZoneInfo("America/New_York")), + policy_reviewed_by = "CollectOSS Maintainers", + policy_review_comments = "CollectOSS primarily accesses public information from GitLab. Terms state: " + "\"We may share your Personal Data with other users of the Services and with the public if you " + "choose to make your SaaS Profile public. You have control over what information is public;\"", + enabled=True + ) + session.add(gitlab_instance) + session.commit() + # TODO: ensure default repos are present @deprecated("automatic import is deprecated. This is a function to warn users and help them transition") def warn_import_repos(logger): @@ -241,4 +297,4 @@ def warn_import_repos(logger): def print_platform_information(logger): logger.info(f"PATH: {os.environ.get('PATH')}") logger.info(f"Python executable (current): {sys.executable}") - logger.info(f"Python version: {platform.python_version()}") \ No newline at end of file + logger.info(f"Python version: {platform.python_version()}") diff --git a/docker/backend/preflight.py b/docker/backend/preflight.py index 4207db8b7..3ae7eb3bd 100755 --- a/docker/backend/preflight.py +++ b/docker/backend/preflight.py @@ -1,4 +1,4 @@ -from collectoss.util.startup import collect_env_variables, check_init_schema, check_update_schema, setup_facade_directory, merge_config, warn_import_repos, print_platform_information +from collectoss.util.startup import collect_env_variables, check_init_schema, check_update_schema, setup_facade_directory, merge_config, initialize_tables, warn_import_repos, print_platform_information from collectoss.application.logs import getFormatter from collectoss.application.cli import DatabaseContext import sys @@ -27,6 +27,8 @@ setup_facade_directory(logger) merge_config(DatabaseContext().engine, logger) + + initialize_tables(DatabaseContext().engine, logger) warn_import_repos(logger)