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.

9.7 KB
Raw Blame
/**
 * migration-state-schema.js
 *
 * Source of truth for migration-state.json. Three responsibilities:
 *   1. `buildInitialState()` — the skeleton the skill writes at startup, before any
 *      sub-step has run. Defaults to the Authoring Track (more common). Matches stage0-initialized.html.
 *   2. Constants for status / kind / track values used by phases, sub-steps, the
 *      approval gate, and the augmented-prompt cards.
 *   3. Canonical phase blueprints per track. Phase 1 and Phase 4 are shared across
 *      tracks; Phase 2 and Phase 3 differ.
 *
 * Authoring Track (CLI value `A`) — mode is `configurationData` or `all`
 *   For Dev / Test / UAT / Single env. Migrates metadata locally, allows a
 *   customization-remediation pass, then activates EDM.
 *
 * Downstream Track (CLI value `B`) — mode is `configurationDataReferences`
 *   For Prod (ALM assumed). Verifies metadata is already in EDM, then migrates
 *   transactional references and activates.
 *
 * Kept deliberately small: no I/O, no rendering, no CLI concerns. Pure data.
 */

const SCHEMA_VERSION = 1;

const PHASE_STATUS = Object.freeze({
  PENDING: 'pending',
  PENDING_APPROVAL: 'pending-approval',
  IN_PROGRESS: 'in-progress',
  COMPLETED: 'completed',
  BLOCKED: 'blocked',
});

const SUB_STEP_STATUS = Object.freeze({
  PENDING: 'pending',
  IN_PROGRESS: 'in-progress',
  COMPLETED: 'completed',
  BLOCKED: 'blocked',
});

const APPROVAL_KIND = Object.freeze({
  PHASE_START: 'phase-start',
  IN_PHASE: 'in-phase',
});

const PROMPT_STATUS = Object.freeze({
  PENDING: 'pending',
  READY: 'ready',
});

const TRACK = Object.freeze({
  A: 'A',
  B: 'B',
});

// The Authoring Track defaults to Dev/Test/UAT/Single-env (the most common). Until step 1.7
// runs --set-track, the live report renders against this default.
const DEFAULT_TRACK = TRACK.A;

// ── Phase blueprints ───────────────────────────────────────────────────────
// Phase 1 and Phase 4 are shared across tracks.

const PHASE_1 = {
  id: 1,
  title: 'Site Discovery & Pre-checks',
  subSteps: [
    { id: '1.1', label: 'Establish CLI Context' },
    { id: '1.2', label: 'Identify Site Context' },
    { id: '1.3', label: 'Site Discovery and Validate Data Model' },
    { id: '1.4', label: 'Check Existing Migration Status' },
    { id: '1.5', label: 'Validate Required Dependencies' },
    { id: '1.6', label: 'Validate Site Template and V2 Package' },
    { id: '1.7', label: 'Determine Environment Type and Migration Mode' },
  ],
};

const PHASE_4 = {
  id: 4,
  title: 'Post-Migration Validation',
  subSteps: [
    { id: '4.1', label: 'Runtime Smoke Test Recommendation (/test-site)' },
    { id: '4.2', label: 'Final Status, Optional Rollback, Summary' },
  ],
};

const PHASE_2_TRACK_A = {
  id: 2,
  title: 'Configuration Migration & Customization Remediation',
  subSteps: [
    { id: '2.1', label: 'Capture SDM Snapshot' },
    { id: '2.2', label: 'Migrate Metadata' },
    { id: '2.3', label: 'Locate Customization Report' },
    { id: '2.4', label: 'Remediate Customizations' },
  ],
};

const PHASE_2_TRACK_B = {
  id: 2,
  title: 'Setting Up Metadata',
  subSteps: [
    { id: '2.1', label: 'Verify Site in Target Environment' },
    { id: '2.2', label: 'Import Metadata if Missing' },
    { id: '2.3', label: 'Confirm Metadata Ready' },
  ],
};

// Phase 3 differs by track. The Authoring Track already cleaned up customizations in Phase 2.3,
// so Phase 3 only needs verify metadata → migrate refs → activate → restart. The Downstream Track
// never ran the customization remediation in Phase 2, so Phase 3 includes locate + remediate
// after refs migrate.
const PHASE_3_TRACK_A = {
  id: 3,
  title: 'Migration Execution',
  subSteps: [
    { id: '3.1', label: 'Data Diff Validation (SDM ↔ EDM Metadata)' },
    // When mode=`all`, SKILL.md instructs the agent to mark 3.2 completed with
    // output "Skipped — refs already migrated in Phase 2.1 (mode=all)".
    { id: '3.2', label: 'Migrate Transactional References' },
    { id: '3.3', label: 'Activate EDM (Update Data Model Version)' },
    { id: '3.4', label: 'Restart Site' },
  ],
};

const PHASE_3_TRACK_B = {
  id: 3,
  title: 'Migration Execution',
  subSteps: [
    { id: '3.1', label: 'Data Diff Validation (SDM ↔ EDM Metadata)' },
    { id: '3.2', label: 'Migrate Transactional References' },
    { id: '3.3', label: 'Locate Customization Report' },
    { id: '3.4', label: 'Remediate Customizations' },
    { id: '3.5', label: 'Activate EDM (Update Data Model Version)' },
    { id: '3.6', label: 'Restart Site' },
  ],
};

const PHASE_BLUEPRINTS_BY_TRACK = Object.freeze({
  A: [PHASE_1, PHASE_2_TRACK_A, PHASE_3_TRACK_A, PHASE_4],
  B: [PHASE_1, PHASE_2_TRACK_B, PHASE_3_TRACK_B, PHASE_4],
});

function makePhasesFromBlueprint(blueprints) {
  return blueprints.map((p) => ({
    id: p.id,
    title: p.title,
    status: PHASE_STATUS.PENDING,
    startedAt: null,
    completedAt: null,
    subSteps: p.subSteps.map((s) => ({
      id: s.id,
      label: s.label,
      status: SUB_STEP_STATUS.PENDING,
      output: null,
    })),
  }));
}

function normalizePendingInput(value) {
  if (value == null) return null;
  if (typeof value !== 'object' || Array.isArray(value) ||
      typeof value.requiredAction !== 'string' || !value.requiredAction.trim()) {
    throw new Error('Pending input must be an object with a non-empty "requiredAction" string.');
  }
  if (value.responseLocation !== undefined &&
      (typeof value.responseLocation !== 'string' || !value.responseLocation.trim())) {
    throw new Error('Pending input "responseLocation" must be a non-empty string when supplied.');
  }
  return {
    requiredAction: value.requiredAction.trim(),
    responseLocation: value.responseLocation?.trim() || 'Return to the migration conversation and provide your response.',
  };
}

/**
 * Build a fresh migration-state.json skeleton. Defaults to the Authoring Track; the skill
 * calls `--set-track A|B` at the end of step 1.7 once env type and migration
 * mode are known (`A` = Authoring Track, `B` = Downstream Track).
 */
function buildInitialState({ webSiteId, outputDir, track = DEFAULT_TRACK, startedAt = new Date().toISOString() } = {}) {
  if (!webSiteId) throw new Error('buildInitialState: webSiteId is required');
  if (!outputDir) throw new Error('buildInitialState: outputDir is required');
  if (!PHASE_BLUEPRINTS_BY_TRACK[track]) throw new Error(`buildInitialState: unknown track '${track}'`);

  return {
    schemaVersion: SCHEMA_VERSION,
    skillStartedAt: startedAt,
    lastUpdatedAt: startedAt,
    track,
    site: {
      name: null,
      webSiteId,
      portalId: null,
      portalUrl: null,
      slug: null,
      currentDataModel: null,
      template: null,
      environment: null,
      environmentName: null,
      environmentUrl: null,
      migrationMode: null,
      siteRoot: null,
      outputDir,
    },
    phases: makePhasesFromBlueprint(PHASE_BLUEPRINTS_BY_TRACK[track]),
    approvalGate: {
      phaseId: 1,
      kind: APPROVAL_KIND.PHASE_START,
    },
    // Informational pauses must not replace or approve a migration gate.
    pendingInput: null,
    augmentedPrompts: {
      plugin: null,
      dme: null,
    },
    customizationReport: null,
    refsMigration: null,
    // Append-only log of the major migration CHOICES the user makes (env type, mode,
    // track, package install/skip, remediation, reset, rollback, restart). Rendered in
    // the Migration & Review tab for later review. Entries upsert by `id` so re-running
    // a decision point updates in place instead of duplicating. Permission / file
    // create-update-read prompts are intentionally NOT recorded here.
    decisions: [],
    // Structured "now running" pointer for long-running steps (package installs,
    // metadata/refs migration polling). null when idle. When set:
    //   { label, detail, startedAt, expectedDurationSec, poll: { attempt, max } }
    // The report renders this as an always-visible banner whose elapsed clock and
    // estimate bar are advanced client-side every second — so progress stays visible
    // even while a blocking `pac` call prevents the agent from writing new state.
    currentOperation: null,
  };
}

/**
 * Switch the state's track in place. Preserves Phase 1 and Phase 4 (which are
 * shared between tracks and may contain completed sub-steps). Replaces Phase 2
 * and Phase 3 with the new track's blueprints (pristine).
 *
 * Called by update-state.js --set-track A|B (`A` = Authoring Track, `B` = Downstream Track),
 * typically at the end of step 1.7 when the user confirms env type + migration mode.
 */
function rebuildPhasesForTrack(state, newTrack) {
  if (!PHASE_BLUEPRINTS_BY_TRACK[newTrack]) {
    throw new Error(`rebuildPhasesForTrack: unknown track '${newTrack}'`);
  }

  const blueprints = PHASE_BLUEPRINTS_BY_TRACK[newTrack];
  state.phases = blueprints.map((blueprint) => {
    // Phase 1 and Phase 4 are shared across tracks — preserve any existing
    // sub-step status / output. Phase 2 and Phase 3 are track-specific —
    // rebuild from blueprint (pristine).
    if (blueprint.id === 1 || blueprint.id === 4) {
      const existing = state.phases?.find((p) => p.id === blueprint.id);
      if (existing) return existing;
    }
    return makePhasesFromBlueprint([blueprint])[0];
  });
  state.track = newTrack;
}

module.exports = {
  SCHEMA_VERSION,
  PHASE_STATUS,
  SUB_STEP_STATUS,
  APPROVAL_KIND,
  PROMPT_STATUS,
  TRACK,
  normalizePendingInput,
  DEFAULT_TRACK,
  PHASE_BLUEPRINTS_BY_TRACK,
  // Back-compat exports — `PHASE_BLUEPRINT` was the pre-track flat list. We
  // alias it to the Authoring Track (CLI value `A`) so any external caller that imported it keeps working.
  PHASE_BLUEPRINT: PHASE_BLUEPRINTS_BY_TRACK.A,
  buildInitialState,
  rebuildPhasesForTrack,
};