Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/update-codejson.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

- name: Update code.json
id: generator
uses: DSACMS/automated-codejson-generator@sachin/local-ai-feature
uses: DSACMS/automated-codejson-generator@main
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH: "main"
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"dependencies": {
"@actions/core": "^3.0.0",
"@octokit/action": "^7.0.0",
"codejson-core": "^0.1.1",
"codejson-core": "^0.2.0",
"node-llama-cpp": "^3.21.1",
"octokit-plugin-create-pull-request": "^6.0.1",
"zod": "^4.2.1"
Expand Down
124 changes: 2 additions & 122 deletions src/__tests__/unit/codejson.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
import { describe, it, expect } from "@jest/globals";
import {
CodeJSON,
assembleDraft,
draftBaseline,
droppedFields,
validateCodeJSON,
} from "../../codejson.js";
import { droppedFields, validateCodeJSON } from "../../codejson.js";
import validCodeJSON from "../fixtures/test-code.json";

// The schema itself is owned and exhaustively tested by codejson-core. What follows
// pins the contract this action depends on: that we're bound to the CMS variant, that
// assembly never rejects an incomplete draft, and that the merge rules we handed over
// to core still behave the way this action needs them to.

const FIXED_NOW = new Date("2026-01-01T00:00:00.000Z");
const existing = validCodeJSON as unknown as CodeJSON;

const observed: Partial<CodeJSON> = {
name: "test-repo",
repositoryURL: "https://github.com/test-owner/test-repo",
repositoryVisibility: "public",
};
// pins the contract this action depends on: that we're bound to the CMS variant.

describe("validateCodeJSON", () => {
it("accepts a complete CMS code.json", () => {
Expand Down Expand Up @@ -58,109 +41,6 @@ describe("validateCodeJSON", () => {
});
});

describe("draftBaseline", () => {
it("keeps enum fields present so they survive JSON.stringify", () => {
const serialized = JSON.parse(JSON.stringify(draftBaseline));

for (const field of [
"status",
"repositoryHost",
"repositoryVisibility",
"softwareType",
"maintenance",
"repositoryType",
"fismaLevel",
]) {
expect(serialized).toHaveProperty(field, "");
}
});

it("defaults to CMS as the organization and CC0 as the license", () => {
expect(draftBaseline.organization).toBe(
"Centers for Medicare & Medicaid Services",
);
expect(draftBaseline.permissions?.licenses).toEqual([
{ name: "CC0-1.0", URL: "" },
]);
});

it("carries the CMS-only fields", () => {
expect(draftBaseline).toHaveProperty("longDescription");
expect(draftBaseline).toHaveProperty("maturityModelTier");
expect(draftBaseline).toHaveProperty("subsetInHealthcare");
});
});

describe("assembleDraft", () => {
it("does not throw on an incomplete draft", () => {
const result = assembleDraft(observed, null, { now: () => FIXED_NOW });

expect(result.name).toBe("test-repo");
expect(result.status).toBe("");
expect(validateCodeJSON(result).length).toBeGreaterThan(0);
});

it("derives feedbackMechanism and SBOM from the repository URL", () => {
const result = assembleDraft(observed, null);

expect(result.feedbackMechanism).toBe(
"https://github.com/test-owner/test-repo/issues",
);
expect(result.SBOM).toBe(
"https://github.com/test-owner/test-repo/network/dependencies",
);
});

it("keeps an existing feedbackMechanism and SBOM", () => {
const result = assembleDraft(observed, existing);

expect(result.feedbackMechanism).toBe(existing.feedbackMechanism);
expect(result.SBOM).toBe(existing.SBOM);
});

it("stamps metadataLastUpdated from the injected clock", () => {
const result = assembleDraft(observed, existing, { now: () => FIXED_NOW });

expect(result.date.metadataLastUpdated).toBe("2026-01-01T00:00:00.000Z");
});

it("drops fields that are no longer part of the schema", () => {
const stale = { ...existing, retiredField: "stale" };

const result = assembleDraft(observed, stale as unknown as CodeJSON);

expect(result).not.toHaveProperty("retiredField");
});

it("migrates a legacy string contractNumber to an array", () => {
const legacy = { ...existing, contractNumber: "CONTRACT-001" };

const result = assembleDraft(observed, legacy as unknown as CodeJSON);

expect(result.contractNumber).toEqual(["CONTRACT-001"]);
});

it("preserves clones from the existing file and takes forks from observation", () => {
const result = assembleDraft(
{ ...observed, reuseFrequency: { forks: 42 } },
existing,
);

expect(result.reuseFrequency).toEqual({ forks: 42, clones: 50 });
});

it("marks the repository archived without duplicating the tag", () => {
const archived = { ...existing, tags: ["archived"] };

const result = assembleDraft(observed, archived as unknown as CodeJSON, {
isArchived: true,
});

expect(result.status).toBe("Archival");
expect(result.tags.filter((tag) => tag === "archived")).toHaveLength(1);
});
});

describe("droppedFields", () => {
it("reports keys that are not part of the schema", () => {
expect(droppedFields({ name: "test", retiredField: "stale" })).toEqual([
Expand Down
67 changes: 3 additions & 64 deletions src/codejson.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,7 @@
import { z } from "zod";
import {
CMS_SCHEMA_VERSION,
cmsBaselineCodeJSON,
cmsProfile,
createCodeJSONProfile,
type CMSCodeJSON,
} from "codejson-core";
import { cmsProfile, type CMSCodeJSON } from "codejson-core";

// this action targets the CMS variant of the schema, so everything here binds to cmsProfile.
// we could make this generic / agency agnostic in the future but for now, the CMS variant is the only one that has been implemented in codejson-core.
export type CodeJSON = CMSCodeJSON;

// sort of a hack since codejson-core validates against the CMS schema, but we want to generate a draft that is inherently not valid.
// this allows us to use the same assemble and validate functions without having to create a separate profile for invalid drafts.
const blankEnumValue = "" as never;

export const draftBaseline: Partial<CodeJSON> = {
...cmsBaselineCodeJSON,
status: blankEnumValue,
repositoryHost: blankEnumValue,
repositoryVisibility: blankEnumValue,
softwareType: blankEnumValue,
maintenance: blankEnumValue,
repositoryType: blankEnumValue,
fismaLevel: blankEnumValue,
// core's CMS baseline ships an empty license list; CMS repositories default to CC0
permissions: {
licenses: [
{
name: "CC0-1.0",
URL: "",
},
],
usageType: [],
exemptionText: "",
},
};

// again, a hack to support drafts but we should change this upstream so that it can accept drafts.
// we need this because the CMS schema is strict and does not allow blank enum values, but we want to generate a draft that is inherently invalid.
const draftProfile = createCodeJSONProfile(
z.custom<CodeJSON>(),
draftBaseline,
CMS_SCHEMA_VERSION,
);

// after the refactor, all we need is cmsProfile.assemble but for now we need to wrap it so that we can pass in the draft baseline and not have to worry about the strict CMS schema.
export const assembleDraft: (
observed: Partial<CodeJSON>,
existing: CodeJSON | null,
options?: { isArchived?: boolean; now?: () => Date },
) => CodeJSON = draftProfile.assemble;

// validates against the full CMS schema. an empty array means valid
export const draftCodeJSON = cmsProfile.draft;
export const validateCodeJSON = cmsProfile.validate;

// core drops unknown fields silently, but this action has always reported them
export function droppedFields(
existingCodeJSON: Record<string, unknown> | null,
): string[] {
if (!existingCodeJSON) {
return [];
}

const validKeys = new Set(Object.keys(draftBaseline));
return Object.keys(existingCodeJSON).filter((key) => !validKeys.has(key));
}
export const droppedFields = cmsProfile.droppedFields;
14 changes: 7 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cmsProfile } from "codejson-core";
import {
CodeJSON,
assembleDraft,
draftBaseline,
draftCodeJSON,
droppedFields,
validateCodeJSON,
} from "./codejson.js";
Expand Down Expand Up @@ -46,7 +46,7 @@ async function getMetaData(
const permissions: CodeJSON["permissions"] = {
licenses:
existingCodeJSON?.permissions?.licenses ??
draftBaseline.permissions?.licenses ??
cmsProfile.baseline.permissions?.licenses ??
[],
usageType:
existingUsageType.length > 0
Expand Down Expand Up @@ -111,22 +111,22 @@ export async function runWithDeps(deps: Dependencies): Promise<void> {
}

const metaData = await getMetaData(helpers, currentCodeJSON);
const draftCodeJSON = assembleDraft(metaData, currentCodeJSON, {
const draft = draftCodeJSON(metaData, currentCodeJSON, {
isArchived: deps.isArchived,
});
const finalCodeJSON = deps.enableAI
? await enrichCodeJSON(
draftCodeJSON,
draft,
{
readme: await helpers.readREADME(),
secrets: [deps.githubToken, deps.adminToken],
},
deps.log,
deps.runModel,
)
: draftCodeJSON;
: draft;

const aiGeneratedFields = enrichedFields(draftCodeJSON, finalCodeJSON);
const aiGeneratedFields = enrichedFields(draft, finalCodeJSON);

// a generated code.json is a draft so we must report what fields are missing
const validationErrors = validateCodeJSON(finalCodeJSON);
Expand Down
Loading