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
72 changes: 72 additions & 0 deletions .github/workflows/integrity-sweep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Weekly Atlas data-integrity sweep.
#
# Calls the token-gated GET /api/ops/integrity (deterministic, certain-only rules in
# atlas/lib/integrity.ts) and reconciles the findings into the per-cause
# `data-integrity` issues (#338-#348, plus one catch-all issue the script creates
# once): new findings are appended as unticked lines, lines the sweep wrote whose
# finding is gone are ticked. Nothing is deleted and human-edited lines are never
# touched. See scripts/integrity-sweep/sweep.mjs.
#
# The DB reads happen on the Worker; this job never holds DB credentials.
#
# SECURITY: no `github.event.*` data is referenced. Secrets and the one typed boolean
# input reach the script via `env:` only, never inlined into a `run:` script.
#
# Required repository secrets:
# INTEGRITY_URL NEW - e.g. https://atlas.buspark.io/api/ops/integrity
# (the route must be deployed to that Worker first; deploys are manual)
# DIGEST_TOKEN already exists for the weekly digest; must match the Worker's
# DIGEST_TOKEN
# GITHUB_TOKEN is the built-in token, scoped below to issues: write.
name: Integrity sweep

on:
schedule:
# 14:00 UTC Monday, an hour after the weekly digest.
- cron: "0 14 * * 1"
workflow_dispatch:
inputs:
dry_run:
description: "Read and print what would change, write nothing"
type: boolean
default: false

# Least privilege: it edits issue bodies (and may create one issue), nothing else.
permissions:
contents: read
issues: write

concurrency:
# Two overlapping runs would both append the same new lines.
group: integrity-sweep
cancel-in-progress: false

jobs:
sweep:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false

- uses: actions/setup-node@v4
with:
node-version: "20"

# No secrets: if the reconciliation logic is broken, fail before touching issues.
- name: Verify the reconcile logic
run: node scripts/integrity-sweep/sweep.mjs --self-check

- name: Sweep and reconcile issues
env:
INTEGRITY_URL: ${{ secrets.INTEGRITY_URL }}
DIGEST_TOKEN: ${{ secrets.DIGEST_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DRY_RUN: ${{ inputs.dry_run && '1' || '0' }}
run: |
if [ "$DRY_RUN" = "1" ]; then
node scripts/integrity-sweep/sweep.mjs --dry-run
else
node scripts/integrity-sweep/sweep.mjs
fi
32 changes: 32 additions & 0 deletions atlas/app/api/ops/integrity/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Certain-only data-integrity findings for the weekly sweep
// (scripts/integrity-sweep/sweep.mjs, run by .github/workflows/integrity-sweep.yml).
//
// GitHub Actions has no DB credentials on purpose, so the sweep reads Atlas through
// this route. Read-only, all orgs. The payload is project ids, field names and
// fixed problem sentences (lib/integrity.ts) — never a stored value — but it is
// still bearer-gated and fails closed, like /api/digest/weekly.
import { timingSafeEqual } from "node:crypto";
import { getAllProjectsAdmin } from "@/lib/db";
import { integrityFindings } from "@/lib/integrity";

function bearerMatches(header: string | null, token: string): boolean {
const a = Buffer.from(header || "");
const b = Buffer.from(`Bearer ${token}`);
return a.length === b.length && timingSafeEqual(a, b);
}

export async function GET(req: Request) {
const token = process.env.DIGEST_TOKEN;
const noStore = { "Cache-Control": "no-store" };
if (!token) return Response.json({ error: "Integrity sweep is not configured (DIGEST_TOKEN unset)." }, { status: 503, headers: noStore });
if (!bearerMatches(req.headers.get("authorization"), token)) {
return Response.json({ error: "Unauthorized" }, { status: 401, headers: noStore });
}
const projects = await getAllProjectsAdmin();
// projectCount lets the sweep refuse an empty read: zero findings from zero
// projects would otherwise tick every open line as "fixed".
return Response.json(
{ projectCount: projects.length, findings: integrityFindings(projects), generatedAt: new Date().toISOString() },
{ headers: noStore }
);
}
109 changes: 109 additions & 0 deletions atlas/lib/integrity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { describe, it, expect } from "vitest";
import { personValueProblems, repoUrlProblems, projectIntegrity } from "./integrity";
import type { Project, Run } from "./types";

// Fictional data only.
const run = (o: Partial<Run> = {}): Run => ({ term: "Fall 2025", course: "DS519", discipline: "SWE", students: [], ...o });
const project = (o: Partial<Project> = {}): Project =>
({ id: "demo-proj", title: "Demo", blurb: "A tool that maps bus stops for a city agency.", partner: "Example Org",
clientType: "Nonprofit", tech: ["Python"], runs: [run()], ...o }) as Project;
const causes = (p: Project) => projectIntegrity(p).map((f) => `${f.cause}:${f.field}`);

describe("personValueProblems", () => {
it("accepts ordinary names, including a preferred-name parenthetical", () => {
for (const v of ["Alex Rivera", "Wei (Amy) Zhang", "Jean-Luc Picard", "", " "]) {
expect(personValueProblems(v), v).toEqual([]);
}
});
it("flags placeholders", () => {
for (const v of ["N/A", "TBD", "no EIR", "None yet", "Teammate 2", "Mentor Mentor"]) {
expect(personValueProblems(v), v).toEqual(["holds a placeholder, not a person"]);
}
});
it("flags role labels glued to a name", () => {
expect(personValueProblems("Class Instructor Alex Rivera")).toContain("has a role label glued to the name");
expect(personValueProblems("Client Sam Lee")).toContain("has a role label glued to the name");
});
it("flags two people in one field", () => {
expect(personValueProblems("Alex Rivera and Sam Lee")).toContain("holds more than one person");
expect(personValueProblems("Alex Rivera // Sam Lee")).toContain("holds more than one person");
});
it("flags GitHub handles", () => {
expect(personValueProblems("Alex Rivera (arivera42)")).toContain("has a GitHub handle glued to the name");
expect(personValueProblems("Alex Rivera (@arivera)")).toContain("has a GitHub handle glued to the name");
expect(personValueProblems("Alex Rivera (GitHub: AR)")).toContain("has a GitHub handle glued to the name");
});
it("flags emails", () => {
expect(personValueProblems("alex@example.com")).toEqual(["holds an email address, not a name"]);
});
});

describe("repoUrlProblems", () => {
it("accepts a repo root and non-GitHub hosts", () => {
expect(repoUrlProblems("https://github.com/example/demo")).toEqual([]);
expect(repoUrlProblems("https://github.com/example")).toEqual([]);
expect(repoUrlProblems("https://gitlab.com/a/b/-/tree/x")).toEqual([]);
});
it("flags branch, issue, .git and non-URLs", () => {
expect(repoUrlProblems("https://github.com/example/demo/tree/dev")[0]).toMatch(/branch/);
expect(repoUrlProblems("https://github.com/example/demo/issues/3")[0]).toMatch(/issue/);
expect(repoUrlProblems("https://github.com/example/demo.git")).toEqual(['ends in ".git"']);
expect(repoUrlProblems("separate org")).toEqual(["is not a valid web address"]);
});
});

describe("projectIntegrity", () => {
it("a clean project has no findings", () => {
expect(projectIntegrity(project({ repoUrl: "https://github.com/example/demo" }))).toEqual([]);
});

it("role findings name the run and field, never the value", () => {
const f = projectIntegrity(project({ runs: [run({ pm: "Alex Rivera and Sam Lee", classInstructors: ["TBD"] })] }));
expect(f.map((x) => x.field)).toEqual(["runs[Fall 2025].pm", "runs[Fall 2025].classInstructors[0]"]);
expect(f.every((x) => x.cause === "roles")).toBe(true);
expect(JSON.stringify(f)).not.toMatch(/Alex|Sam|TBD/);
});

it("blurb and tech-note text rules reuse the PD sync holds", () => {
const p = project({
blurb: "We map bus stops. Contact alex@example.com for access.",
techNote: "List of tools/ tech stack preferred by client e.g. Tableau, PowerBI\nPython",
clientDesc: "db at postgres://admin:hunter2x@db.example.com/prod",
});
const c = causes(p);
expect(c).toContain("contact_in_text:blurb");
expect(c).toContain("tech_note:tech_note");
expect(c).toContain("credentials:client_desc");
expect(JSON.stringify(projectIntegrity(p))).not.toMatch(/alex@|hunter2/);
});

it("tech list rules", () => {
const c = projectIntegrity(project({ tech: ["Power BI", "PowerBI", "Tableau", "TBD"], techNote: "e.g. Tableau, PowerBI" }))
.map((f) => f.problem);
expect(c.some((x) => /more than once/.test(x))).toBe(true);
expect(c.some((x) => /placeholder/.test(x))).toBe(true);
expect(c.some((x) => /example line/.test(x))).toBe(true);
});

it("url rules: Drive folder as PD, non-Drive drive link, repo branch", () => {
const folder = "https://drive.google.com/drive/folders/abc";
const c = causes(project({ pdUrl: folder, runs: [run({ pdUrl: folder })], driveUrl: "https://example.com/x",
repoUrl: "https://github.com/example/demo/tree/main" }));
expect(c).toEqual(["urls:repo_url", "urls:runs[Fall 2025].pd_url", "urls:drive_url"]);
});

it("run rules: malformed term, unknown course, discipline mismatch, duplicate run", () => {
const c = projectIntegrity(project({ runs: [
run({ term: "" }),
run({ course: "Basket Weaving" }),
run({ term: "Spring 2025", discipline: "ML" }),
run({ term: "Spring 2025", discipline: "ML" }),
run({ term: "Summer 2025", course: "Spark! Summer Internship", discipline: "" }),
] })).filter((f) => f.cause === "runs").map((f) => f.problem);
expect(c.some((x) => /no term/.test(x))).toBe(true);
expect(c.some((x) => /not one Atlas recognises/.test(x))).toBe(true);
expect(c.some((x) => /contradicts/.test(x))).toBe(true);
expect(c.some((x) => /share the same term/.test(x))).toBe(true);
expect(c.some((x) => /Summer 2025/.test(x))).toBe(false); // internships are fine
});
});
Loading
Loading