From 1fae8faa7bcda6242ed45846f8e0223601dd400c Mon Sep 17 00:00:00 2001 From: Yash Ponnaganti Date: Wed, 25 Nov 2026 14:19:06 -0500 Subject: [PATCH] T1.1 (5/8): source assets, the ingest_project_sources job and blame run requests - Assets peopleportal_projects, project_repositories, gitea_pull_requests and persist_project_sources. Archived projects and archived or empty repositories are skipped. - Job ingest_project_sources materializes them with the schema. - Job blame_ownership is the existing per-repository graph without them, and blame_run_requests() yields one RunRequest per project repository, keyed org/repo, for the weekly schedule (T7.3). The assets use master's plain asset keys and numbered groups (01_sources, 07_database). --- horizon/pipeline/assets/__init__.py | 14 ++ horizon/pipeline/assets/project_sources.py | 244 ++++++++++++++++++++ horizon/pipeline/definitions.py | 36 ++- horizon/tests/test_pipeline_definitions.py | 37 +++ horizon/tests/test_project_source_assets.py | 12 + 5 files changed, 342 insertions(+), 1 deletion(-) create mode 100644 horizon/pipeline/assets/project_sources.py create mode 100644 horizon/tests/test_project_source_assets.py diff --git a/horizon/pipeline/assets/__init__.py b/horizon/pipeline/assets/__init__.py index 521d4ed..4d1a5cd 100644 --- a/horizon/pipeline/assets/__init__.py +++ b/horizon/pipeline/assets/__init__.py @@ -55,12 +55,26 @@ from .ranking_explanations import member_explanations from .ranking_publish import publish_ranking, publish_ranking_integrity from .ranking_scores import member_scores, member_scores_integrity +from .project_sources import ( + ProjectSourcesConfig, + blame_run_requests, + gitea_pull_requests, + peopleportal_projects, + persist_project_sources, + project_repositories, +) from .source import RepositoryConfig, authentik_users, gitea_repository __all__ = [ "BlameCapture", "RankingRun", "RankingRunConfig", + "ProjectSourcesConfig", + "blame_run_requests", + "gitea_pull_requests", + "peopleportal_projects", + "persist_project_sources", + "project_repositories", "RepositoryConfig", "authentik_users", "blame_capture", diff --git a/horizon/pipeline/assets/project_sources.py b/horizon/pipeline/assets/project_sources.py new file mode 100644 index 0000000..64b3bad --- /dev/null +++ b/horizon/pipeline/assets/project_sources.py @@ -0,0 +1,244 @@ +"""Project roster and Gitea pull request sources (T1.1). + +Four assets form one contract: peopleportal_projects -> project_repositories -> +gitea_pull_requests -> persist_project_sources. They are season-wide (every +project of the semester), unlike the per-repository blame assets. +""" + +# No `from __future__ import annotations`: Dagster resolves asset annotations +# at decoration time. +from collections.abc import Iterator, Sequence +from dataclasses import replace +from datetime import datetime, timezone +from typing import Optional + +from dagster import ( + AssetExecutionContext, + Config, + Failure, + MetadataValue, + RunRequest, + asset, +) + +from ..config import get_pipeline_settings +from ..identity import resolve_gitea_login +from ..project_sources import ( + ProjectRepositoryRow, + ProjectSources, + PullRequestSources, +) +from ..resources import GiteaClient, PeoplePortalClient +from ..storage import PostgresResource + + +class ProjectSourcesConfig(Config): + """Semester to ingest; defaults to HORIZON_SEASON_TYPE / HORIZON_SEASON_YEAR.""" + + season_type: Optional[str] = None + season_year: Optional[int] = None + + +def _season(config: ProjectSourcesConfig) -> tuple[str, int]: + settings = get_pipeline_settings() + season_type = (config.season_type or settings.season_type or "").strip().upper() + season_year = config.season_year or settings.season_year + if season_type not in ("FALL", "SPRING") or not season_year: + raise Failure( + description=( + "Set the semester: season_type FALL or SPRING and season_year, in the " + "run config or as HORIZON_SEASON_TYPE / HORIZON_SEASON_YEAR." + ) + ) + return season_type, int(season_year) + + +@asset( + group_name="01_sources", + kinds={"peopleportal", "python"}, + owners=["yash.ponnaganti@gmail.com"], + tags={"spec": "T1.1"}, + description=( + "T1.1: every semester project and its members with roles from People " + "Portal's T3.1 API; malformed projects or members fail the source run." + ), + metadata={"endpoint": "GET /api/horizon/projects", "tables": "projects, project_members"}, +) +def peopleportal_projects( + context: AssetExecutionContext, + config: ProjectSourcesConfig, + peopleportal: PeoplePortalClient, +) -> ProjectSources: + season_type, season_year = _season(config) + sources = peopleportal.get_projects(season_type, season_year) + context.add_output_metadata( + { + "season": MetadataValue.text(f"{season_type} {season_year}"), + "dagster_run_id": MetadataValue.text(context.run_id), + "projects": MetadataValue.int(len(sources.projects)), + "archived_projects": MetadataValue.int(sum(p.archived for p in sources.projects)), + "members": MetadataValue.int(len(sources.members)), + "role_mapping_version": MetadataValue.text(sources.role_mapping_version), + } + ) + return sources + + +@asset( + group_name="01_sources", + kinds={"gitea", "python"}, + owners=["yash.ponnaganti@gmail.com"], + tags={"spec": "T1.1"}, + description=( + "T1.1: repositories of each non-archived project's Gitea organization; " + "archived and empty repositories are skipped." + ), + metadata={"endpoint": "GET /api/v1/orgs/{org}/repos", "table": "project_repositories"}, +) +def project_repositories( + context: AssetExecutionContext, + peopleportal_projects: ProjectSources, + gitea: GiteaClient, +) -> list[ProjectRepositoryRow]: + rows: list[ProjectRepositoryRow] = [] + listed: dict[str, int] = {} + for project in peopleportal_projects.projects: + if project.archived: + continue + repositories = gitea.list_org_repositories(project.gitea_org) + listed[project.gitea_org] = len(repositories) + for repo in repositories: + if repo.archived or repo.empty or not repo.default_branch: + continue + rows.append( + ProjectRepositoryRow( + project_pk=project.project_pk, + organization=repo.org, + repository=repo.name, + default_branch=repo.default_branch, + ) + ) + context.add_output_metadata( + { + "repositories": MetadataValue.int(len(rows)), + "dagster_run_id": MetadataValue.text(context.run_id), + "listed_per_org": MetadataValue.json(listed), + } + ) + return rows + + +@asset( + group_name="01_sources", + kinds={"gitea", "python"}, + owners=["yash.ponnaganti@gmail.com"], + tags={"spec": "T1.1"}, + description=( + "T1.1: every pull request, review and review request in the project " + "repositories. Logins resolve to Authentik pks only through " + "identity.resolve_gitea_login; unresolved logins stay NULL." + ), + metadata={ + "endpoints": "pulls?state=all, pulls/{n}/reviews, issues/{n}/timeline", + "tables": "gitea_pull_requests, gitea_pr_reviews, gitea_pr_review_requests", + }, +) +def gitea_pull_requests( + context: AssetExecutionContext, + peopleportal_projects: ProjectSources, + project_repositories: list[ProjectRepositoryRow], + gitea: GiteaClient, +) -> PullRequestSources: + roster = peopleportal_projects.members + pulls, reviews, requests, totals = [], [], [], {} + for repo in project_repositories: + fetched = gitea.get_repository_pull_requests(repo.organization, repo.repository) + totals[f"{repo.organization}/{repo.repository}"] = ( + fetched.pull_request_total, + len(fetched.pull_requests), + ) + pulls += [ + replace(row, author_pk=resolve_gitea_login(row.author_login, roster)) + for row in fetched.pull_requests + ] + reviews += [ + replace(row, reviewer_pk=resolve_gitea_login(row.reviewer_login, roster)) + for row in fetched.reviews + ] + requests += [ + replace(row, requested_pk=resolve_gitea_login(row.requested_login, roster)) + for row in fetched.review_requests + ] + unresolved = sorted( + {p.author_login for p in pulls if p.author_pk is None} + | {r.reviewer_login for r in reviews if r.reviewer_pk is None} + | {r.requested_login for r in requests if r.requested_pk is None} + ) + context.add_output_metadata( + { + "pull_requests": MetadataValue.int(len(pulls)), + "reviews": MetadataValue.int(len(reviews)), + "review_requests": MetadataValue.int(len(requests)), + "unresolved_logins": MetadataValue.int(len(unresolved)), + "dagster_run_id": MetadataValue.text(context.run_id), + } + ) + return PullRequestSources( + pull_requests=pulls, + reviews=reviews, + review_requests=requests, + pull_request_totals=totals, + retrieved_at=datetime.now(timezone.utc), + ) + + +@asset( + group_name="07_database", + kinds={"postgres", "python"}, + owners=["yash.ponnaganti@gmail.com"], + tags={"spec": "T1.1"}, + description=( + "T1.1: upserts all six latest-state project and PR source tables atomically." + ), + metadata={"schema": "horizon_gt_features"}, +) +def persist_project_sources( + context: AssetExecutionContext, + feature_store_schema: str, + peopleportal_projects: ProjectSources, + project_repositories: list[ProjectRepositoryRow], + gitea_pull_requests: PullRequestSources, + postgres: PostgresResource, +) -> dict[str, int]: + counts = postgres.write_project_sources( + peopleportal_projects, project_repositories, gitea_pull_requests, + dagster_run_id=context.run_id, + ) + context.add_output_metadata({ + **{name: MetadataValue.int(n) for name, n in counts.items()}, + "dagster_run_id": MetadataValue.text(context.run_id), + "run_status": MetadataValue.text("complete"), + }) + return counts + + +def blame_run_requests( + repositories: Sequence[tuple[str, str]], + job_name: str = "blame_ownership", +) -> Iterator[RunRequest]: + """One RunRequest per project repository for the blame/ownership job, keyed org/repo. + + T7.3 feeds this from PostgresResource.read_project_repositories(season) to + schedule a full-club run. + """ + + for organization, repository in repositories: + slug = f"{organization}/{repository}" + yield RunRequest( + run_key=slug, + job_name=job_name, + run_config={ + "ops": {"gitea_repository": {"config": {"org": organization, "repo": repository}}} + }, + tags={"horizon/repository": slug}, + ) diff --git a/horizon/pipeline/definitions.py b/horizon/pipeline/definitions.py index 22e9e69..35fd28a 100644 --- a/horizon/pipeline/definitions.py +++ b/horizon/pipeline/definitions.py @@ -2,7 +2,7 @@ from __future__ import annotations -from dagster import Definitions +from dagster import AssetSelection, Definitions, define_asset_job from .assets import ( authentik_users, @@ -42,6 +42,10 @@ member_explanations, publish_ranking, publish_ranking_integrity, + gitea_pull_requests, + peopleportal_projects, + persist_project_sources, + project_repositories, ) from .checks import ( capture_is_non_empty, @@ -54,8 +58,38 @@ ) from .resources import build_resources +PROJECT_SOURCE_ASSETS = [ + peopleportal_projects, + project_repositories, + gitea_pull_requests, + persist_project_sources, +] + +# T1.1: season-wide project roster and PR source tables. +ingest_project_sources = define_asset_job( + "ingest_project_sources", + selection=AssetSelection.assets(feature_store_schema, *PROJECT_SOURCE_ASSETS), + description="Projects, members, repositories and every PR/review/review request (T1.1).", +) + +# The per-repository blame/ownership graph; blame_run_requests() targets it. +blame_ownership = define_asset_job( + "blame_ownership", + selection=AssetSelection.groups( + "01_sources", "02_capture", "03_parse", "04_identity", "05_ownership", + "06_pr_metrics", "07_database", + ) + - AssetSelection.assets(*PROJECT_SOURCE_ASSETS), + description="Blame, identity, ownership and PR metrics for one repository (config: org, repo).", +) + defs = Definitions( + jobs=[ingest_project_sources, blame_ownership], assets=[ + peopleportal_projects, + project_repositories, + gitea_pull_requests, + persist_project_sources, gitea_repository, authentik_users, blame_capture, diff --git a/horizon/tests/test_pipeline_definitions.py b/horizon/tests/test_pipeline_definitions.py index 0f41bdb..d8d75d7 100644 --- a/horizon/tests/test_pipeline_definitions.py +++ b/horizon/tests/test_pipeline_definitions.py @@ -48,6 +48,10 @@ AssetKey("member_scores"), AssetKey("member_explanations"), AssetKey("publish_ranking"), + AssetKey("peopleportal_projects"), + AssetKey("project_repositories"), + AssetKey("gitea_pull_requests"), + AssetKey("persist_project_sources"), } @@ -135,6 +139,10 @@ def test_each_layer_is_its_own_group(): "member_scores": "08_ranking", "member_explanations": "08_ranking", "publish_ranking": "08_ranking", + "peopleportal_projects": "01_sources", + "project_repositories": "01_sources", + "gitea_pull_requests": "01_sources", + "persist_project_sources": "07_database", } @@ -262,6 +270,35 @@ def test_definitions_expose_shared_resources(): } +def test_project_source_job_and_blame_job_are_separate(): + repository = defs.get_repository_def() + ingest = repository.get_job("ingest_project_sources") + blame = repository.get_job("blame_ownership") + ingest_keys = {key.to_user_string() for key in ingest.asset_layer.selected_asset_keys} + blame_keys = {key.to_user_string() for key in blame.asset_layer.selected_asset_keys} + assert ingest_keys == { + "feature_store_schema", + "peopleportal_projects", + "project_repositories", + "gitea_pull_requests", + "persist_project_sources", + } + assert "gitea_repository" in blame_keys and "pipeline_run_completed" in blame_keys + assert not blame_keys & (ingest_keys - {"feature_store_schema"}) + + +def test_persist_project_sources_depends_on_all_three_sources(): + assets_by_key = defs.get_repository_def().assets_defs_by_key + key = AssetKey("persist_project_sources") + deps = assets_by_key[key].asset_deps[key] + assert { + AssetKey("feature_store_schema"), + AssetKey("peopleportal_projects"), + AssetKey("project_repositories"), + AssetKey("gitea_pull_requests"), + } <= deps + + def test_resources_are_built_from_settings(): settings = PipelineSettings( HORIZON_GITEA_URL="https://git.example.com", diff --git a/horizon/tests/test_project_source_assets.py b/horizon/tests/test_project_source_assets.py new file mode 100644 index 0000000..3fb64db --- /dev/null +++ b/horizon/tests/test_project_source_assets.py @@ -0,0 +1,12 @@ +"""T1.1: one blame/ownership run request per project repository.""" + +from pipeline.assets.project_sources import blame_run_requests + + +def test_blame_run_requests_one_per_repository(): + requests = list(blame_run_requests([("Atlas", "app"), ("Beacon", "api")])) + assert [r.run_key for r in requests] == ["Atlas/app", "Beacon/api"] + assert requests[0].job_name == "blame_ownership" + assert requests[1].run_config == { + "ops": {"gitea_repository": {"config": {"org": "Beacon", "repo": "api"}}} + }