Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

1.6 KB
Raw Blame
import { readFileSync } from "node:fs"
import { fileURLToPath } from "node:url"
import { projectPlan, validateExecutionPlan } from "./changed-scopes.mjs"

export function validatePlanEnvelope({ planJson, planFile, planHash, projectionName, projectionValue }) {
  if (planJson && planFile) throw new Error("choose one execution plan input")
  const body = planFile ? readFileSync(planFile, "utf8") : planJson
  if (!body) throw new Error("CI execution plan is required")
  const plan = validateExecutionPlan(JSON.parse(body))
  if (plan.plan_hash !== planHash) throw new Error("CI execution plan envelope hash mismatch")

  if (projectionName) {
    const projections = projectPlan(plan)
    if (!Object.hasOwn(projections, projectionName)) {
      throw new Error(`unknown CI execution plan projection: ${projectionName}`)
    }
    if (projections[projectionName] !== projectionValue) {
      throw new Error(`CI execution plan projection mismatch: ${projectionName}`)
    }
  }
  return plan
}

export function runCli(env = process.env) {
  const plan = validatePlanEnvelope({
    planJson: env.CI_EXECUTION_PLAN,
    planFile: env.CI_EXECUTION_PLAN_FILE,
    planHash: env.CI_PLAN_HASH,
    projectionName: env.CI_PROJECTION_NAME,
    projectionValue: env.CI_PROJECTION_VALUE,
  })
  process.stdout.write(
    `Validated execution plan ${plan.plan_hash} (schema ${plan.schema_version}, policy ${plan.policy_version}).\n`,
  )
}

export function runIfMain(metaUrl, argvPath = process.argv[1], env = process.env) {
  if (argvPath === fileURLToPath(metaUrl)) runCli(env)
}

runIfMain(import.meta.url)