Skip to content

A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.

4.8 KB
Raw Blame
#!/usr/bin/env node
/**
 * validate-plan-alm.js — PostToolUse(Skill) hook validator for the plan-alm skill.
 *
 * Checks that docs/alm-plan.html was written and is non-empty/non-malformed.
 * Gracefully approves if the file does not exist (not a plan-alm session).
 * Blocks only if the file exists but is empty or clearly malformed (< 500 bytes).
 */

const path = require('path');
const fs = require('fs');
const { runValidation, findProjectRoot, block, approve, readDeferralMarker } = require('../../../scripts/lib/validation-helpers');
const { planDataPath } = require('../../../scripts/lib/alm-paths');

runValidation((cwd) => {
  if (readDeferralMarker(findProjectRoot(cwd) || cwd)) return approve();  // ALM deferred — silent-approve.
  const projectRoot = findProjectRoot(cwd);

  // If we can't find the project root, approve gracefully — not a plan-alm session
  if (!projectRoot) {
    approve();
    return;
  }

  const htmlPath = path.join(projectRoot, 'docs', 'alm-plan.html');

  // If the file does not exist at all, approve gracefully (skill may not have run)
  if (!fs.existsSync(htmlPath)) {
    approve();
    return;
  }

  // File exists — check that it is non-empty and large enough to be valid HTML
  let stat;
  try {
    stat = fs.statSync(htmlPath);
  } catch (e) {
    block(`validate-plan-alm: Could not stat ${htmlPath}: ${e.message}`);
    return;
  }

  if (stat.size < 500) {
    block(
      `validate-plan-alm: docs/alm-plan.html exists but is too small (${stat.size} bytes). ` +
      `Expected at least 500 bytes. The file may be empty or truncated. ` +
      `Re-run the render-alm-plan.js script to regenerate the plan.`
    );
    return;
  }

  // Quick content check — must contain the plan-status marker
  let content;
  try {
    content = fs.readFileSync(htmlPath, 'utf8');
  } catch (e) {
    block(`validate-plan-alm: Could not read ${htmlPath}: ${e.message}`);
    return;
  }

  if (!content.includes('plan-status')) {
    block(
      `validate-plan-alm: docs/alm-plan.html does not contain the expected "plan-status" marker. ` +
      `The file may be corrupt or was not generated by render-alm-plan.js.`
    );
    return;
  }

  // Consistency guard for the creation-time status fields. The badge + approver
  // in the HTML are derived from docs/.alm-plan-data.json, so the JSON is the
  // source of truth — catch the two half-written states that the old hand-Edit
  // Phase 4 could produce (and that set-plan-status.js now prevents). Read-only;
  // graceful-approve when there is no plan-data (not every session has one).
  const dataPath = planDataPath(projectRoot);
  if (fs.existsSync(dataPath)) {
    let planData;
    try {
      planData = JSON.parse(fs.readFileSync(dataPath, 'utf8'));
    } catch {
      // A malformed plan-data file is the render path's concern, not this guard's
      // — don't block the plan-alm session over it here.
      planData = null;
    }
    if (planData) {
      const status = planData.PLAN_STATUS || null;
      // Coerce before trimming: APPROVED_BY is normally a string, but a hand-edited
      // plan-data could set it to a truthy non-string (number/object), and calling
      // .trim() on that throws — which would escape runValidation and silently
      // approve, bypassing this guard. String(...) keeps the guard robust to any
      // malformed-but-parseable JSON.
      const approver = String(planData.APPROVED_BY || '').trim();
      // Draft must NOT carry an approver — "approver set + Draft" is the stuck
      // state where the plan shows as approved but never advances (check-alm-plan
      // only promotes from "Approved").
      if (status === 'Draft' && approver) {
        block(
          `validate-plan-alm: docs/.alm-plan-data.json is inconsistent — APPROVED_BY is "${approver}" ` +
          `but PLAN_STATUS is "Draft". An approver was recorded but the plan was never moved to ` +
          `"Approved", so downstream skills will treat it as unapproved. Re-run the approve step so ` +
          `the status matches:\n` +
          `  node "\${PLUGIN_ROOT}/scripts/lib/set-plan-status.js" --projectRoot "${projectRoot}" --status Approved --approver "${approver}" --render`
        );
        return;
      }
      // Approved must HAVE an approver — an empty audit trail on an approved plan.
      if (status === 'Approved' && !approver) {
        block(
          `validate-plan-alm: docs/.alm-plan-data.json is inconsistent — PLAN_STATUS is "Approved" ` +
          `but APPROVED_BY is empty. An approved plan must record who approved it. Re-run the approve ` +
          `step with the approver's name:\n` +
          `  node "\${PLUGIN_ROOT}/scripts/lib/set-plan-status.js" --projectRoot "${projectRoot}" --status Approved --approver "<name>" --render`
        );
        return;
      }
    }
  }

  approve();
});