Private/Compile/Import-GovernanceSource.ps1

function Import-GovernanceSource {
    <#
        .SYNOPSIS
        Loads and parses a program's authored governance source (hierarchy +
        access + members + taxonomy + systems) from its programs/<program>/ folder,
        returning the raw model plus a source hash for provenance.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)][string]$ProgramPath
    )

    $manifestPath  = Join-Path $ProgramPath 'manifest.yaml'
    $hierarchyPath = Join-Path $ProgramPath 'hierarchy.yaml'
    $accessPath    = Join-Path $ProgramPath 'access.yaml'

    if (-not (Test-Path $manifestPath))  { throw "Manifest file not found: $manifestPath" }
    if (-not (Test-Path $hierarchyPath)) { throw "Hierarchy file not found: $hierarchyPath" }
    if (-not (Test-Path $accessPath))    { throw "Access file not found: $accessPath" }

    $manifestRaw  = Get-Content -Path $manifestPath -Raw
    $hierarchyRaw = Get-Content -Path $hierarchyPath -Raw
    $accessRaw    = Get-Content -Path $accessPath -Raw

    $manifest  = ConvertFrom-Yaml $manifestRaw
    $hierarchy = ConvertFrom-Yaml $hierarchyRaw
    $access    = ConvertFrom-Yaml $accessRaw

    # Membership: programs/<program>/members/<short>.yaml, keyed by file base name.
    $membersDir = Join-Path $ProgramPath 'members'
    $members    = @{}
    $membersRaw = ''
    if (Test-Path $membersDir) {
        foreach ($file in Get-ChildItem -Path $membersDir -Filter '*.yaml' -File) {
            $raw = Get-Content -Path $file.FullName -Raw
            $membersRaw += $raw
            $members[$file.BaseName] = ConvertFrom-Yaml $raw
        }
    }

    # taxonomy.yaml: optional governed vocabularies (tags today; other controlled
    # value lists later). Separate from hierarchy.yaml because it is a flat list
    # of allowed strings, not part of the product/team tree.
    $taxonomyPath = Join-Path $ProgramPath 'taxonomy.yaml'
    $taxonomyRaw  = ''
    $taxonomy     = $null
    if (Test-Path $taxonomyPath) {
        $taxonomyRaw = Get-Content -Path $taxonomyPath -Raw
        $taxonomy    = ConvertFrom-Yaml $taxonomyRaw
    }

    # systems.yaml: optional reusable systems — named sets of governed
    # sub-elements stamped onto teams via `systems:` in hierarchy.yaml.
    $systemsPath = Join-Path $ProgramPath 'systems.yaml'
    $systemsRaw  = ''
    $systems     = $null
    if (Test-Path $systemsPath) {
        $systemsRaw = Get-Content -Path $systemsPath -Raw
        $systems    = ConvertFrom-Yaml $systemsRaw
    }

    # sources.yaml: optional pre-migration source locations per codePath —
    # where each incoming team lives TODAY, consumed only by preflight. The
    # raw text joins the hash last so programs without one keep their hash.
    $sourcesPath  = Join-Path $ProgramPath 'sources.yaml'
    $sourcesRaw   = ''
    $sources      = $null
    $sourceLabels    = $null   # optional `labels:` — engagement fields attached per preflight check id (ADR-008)
    $sourceReporting = $null   # optional `reporting:` — how the rendered fix report is framed (ADR-009)
    $sourceScope     = $null   # optional `scope:` — the migration query narrowing what is validated (ADR-010)
    $sourceReports   = $null   # optional `reports:` — several entries combined into one report
    $sourceRepoMap   = $null   # optional `repoMigration:` — target repo name -> @{ source; move }
    $sourceAccessReview = $false   # optional `accessReview: true` — gather who has access today and the repo review
    if (Test-Path $sourcesPath) {
        $sourcesRaw = Get-Content -Path $sourcesPath -Raw
        $parsed     = ConvertFrom-Yaml $sourcesRaw
        if ($parsed -and $parsed.sources)   { $sources         = $parsed.sources }
        if ($parsed -and $parsed.labels)    { $sourceLabels    = $parsed.labels }
        if ($parsed -and $parsed.reporting) { $sourceReporting = $parsed.reporting }
        if ($parsed -and $parsed.scope)     { $sourceScope     = $parsed.scope }
        if ($parsed -and $parsed.reports)   { $sourceReports   = $parsed.reports }
        if ($parsed -and $parsed.Contains('accessReview')) { $sourceAccessReview = if ([string]$parsed.accessReview -eq 'write') { 'write' } else { [bool]$parsed.accessReview } }
        # repoMigration: the repo migration config (source repo -> target name,
        # Sync). With hierarchy.yaml repos: (which team owns which target repo)
        # it says which source repos are each team's — nothing is guessed.
        if ($parsed -and $parsed.repoMigration) {
            $rmPath = [string]$parsed.repoMigration
            if (-not [System.IO.Path]::IsPathRooted($rmPath)) { $rmPath = [System.IO.Path]::GetFullPath((Join-Path $ProgramPath $rmPath)) }
            if (-not (Test-Path -LiteralPath $rmPath)) { throw "sources.yaml repoMigration not found: $rmPath" }
            $rm = Get-Content -LiteralPath $rmPath -Raw | ConvertFrom-Json -AsHashtable -Depth 20
            $sourceRepoMap = @{}
            foreach ($run in @($rm.Runs)) {
                if (-not $run -or -not $run.TargetRepoName) { continue }
                $sourceRepoMap[[string]$run.TargetRepoName] = [ordered]@{ source = [string]$run.RepoName; move = ([string]$run.Sync -eq 'true') }
            }
        }

        # migrationConfig: the entry's migration query lives in the dataset's
        # own Azure DevOps Migration Tools config, which is the record of truth
        # for what moves. Its query becomes the entry's scope here, so every
        # consumer downstream sees one shape. Both on one entry is ambiguous.
        if ($sources) {
            foreach ($code in @($sources.Keys)) {
                $src = $sources[$code]
                if (-not $src -or -not $src.migrationConfig) { continue }
                if ($src.scope) { throw "sources.yaml '$code' has both 'scope' and 'migrationConfig'. The query belongs in one place: keep migrationConfig and remove scope." }
                $cfgPath = [string]$src.migrationConfig
                if (-not [System.IO.Path]::IsPathRooted($cfgPath)) { $cfgPath = [System.IO.Path]::GetFullPath((Join-Path $ProgramPath $cfgPath)) }
                $src.scope = Get-GovernanceMigrationConfigScope -Path $cfgPath -Code $code
                $src.migrationConfigPath = $cfgPath
            }
        }
    }

    $bytes  = [System.Text.Encoding]::UTF8.GetBytes($manifestRaw + $hierarchyRaw + $accessRaw + $membersRaw + $taxonomyRaw + $systemsRaw + $sourcesRaw)
    $stream = [System.IO.MemoryStream]::new($bytes)
    $hash   = (Get-FileHash -InputStream $stream -Algorithm SHA256).Hash.ToLowerInvariant()

    # cadence.yaml: optional iteration cadence config.
    $cadencePath = Join-Path $ProgramPath 'cadence.yaml'
    $cadence     = $null
    if (Test-Path $cadencePath) {
        $cadence = ConvertFrom-Yaml (Get-Content -Path $cadencePath -Raw)
    }

    # team-ids.yaml: auto-maintained state file mapping codePath -> ADO team GUID.
    # Allows teams to be found by stable codePath even after a rename.
    $teamIdsPath = Join-Path $ProgramPath 'team-ids.yaml'
    $teamIds     = @{}
    if (Test-Path $teamIdsPath) {
        $parsed = ConvertFrom-Yaml (Get-Content -Path $teamIdsPath -Raw)
        if ($parsed -and $parsed.teams) {
            foreach ($k in $parsed.teams.Keys) { $teamIds[$k] = $parsed.teams[$k] }
        }
    }

    return @{
        Manifest     = $manifest
        Hierarchy    = $hierarchy
        Access       = $access
        Members      = $members
        Hash         = $hash
        Cadence      = $cadence
        Taxonomy     = $taxonomy
        Systems      = $systems
        Sources      = $sources
        SourceLabels    = $sourceLabels
        SourceReporting = $sourceReporting
        SourceScope     = $sourceScope
        SourceReports   = $sourceReports
        SourceAccessReview = $sourceAccessReview
        SourceRepoMap   = $sourceRepoMap
        TeamIds      = $teamIds
        TeamIdsPath  = $teamIdsPath
    }
}