Skip to content
Original file line number Diff line number Diff line change
@@ -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()
58 changes: 57 additions & 1 deletion collectoss/util/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pylint] reported by reviewdog 🐶
W0611: Unused get_history imported from sqlalchemy.orm.attributes (unused-import)

from collectoss.application.config import SystemConfig
from collectoss.application.db.session import DatabaseSession
Expand Down Expand Up @@ -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):
Expand All @@ -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()}")
logger.info(f"Python version: {platform.python_version()}")
4 changes: 3 additions & 1 deletion docker/backend/preflight.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -27,6 +27,8 @@
setup_facade_directory(logger)

merge_config(DatabaseContext().engine, logger)

initialize_tables(DatabaseContext().engine, logger)

warn_import_repos(logger)

Expand Down
Loading