diff --git a/.cursor/skills/roadmap/SKILL.md b/.cursor/skills/roadmap/SKILL.md new file mode 100644 index 00000000..8f24a2f5 --- /dev/null +++ b/.cursor/skills/roadmap/SKILL.md @@ -0,0 +1,48 @@ +--- +name: roadmap +description: Files a GitHub issue for something Athletic Standard should build. Use when someone asks for a feature, or when you find a public request for Athletic Standard. The issue is labeled roadmap and stays in Backlog. +--- + +# Roadmap + +File one issue on `mwhitham/ath` and stop. The issue uses the label `roadmap`. It starts in Backlog. Do not set a project column. Do not move anything to Up next. + +## Before you open an issue + +Search open issues with the `roadmap` label. + +```sh +gh issue list --repo mwhitham/ath --label roadmap --state open --limit 100 +``` + +If one already asks for the same thing, comment on that issue. Do not open a second issue. + +```sh +gh issue comment NUMBER --repo mwhitham/ath --body "BODY" +``` + +## What to write + +Use a few plain sentences under these headings: + +```markdown +## What to build + +## Why + +## Done when +``` + +Add a picture or a simple diagram only when the words need it. + +Leave out a person's real health numbers, dates, and places. Say what was asked. Link the public post. + +## Open the issue + +```sh +gh issue create --repo mwhitham/ath --label roadmap --title "TITLE" --body "BODY" +``` + +If the command fails, report the error and stop. + +Reply with the issue link. Stop. diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 00000000..0086358d --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1 @@ +blank_issues_enabled: true diff --git a/.github/ISSUE_TEMPLATE/roadmap.yml b/.github/ISSUE_TEMPLATE/roadmap.yml new file mode 100644 index 00000000..125b233c --- /dev/null +++ b/.github/ISSUE_TEMPLATE/roadmap.yml @@ -0,0 +1,37 @@ +name: Roadmap idea +description: Something Athletic Standard should build. It starts in Backlog. +labels: + - roadmap +body: + - type: markdown + attributes: + value: | + Write a few plain sentences. Leave out real health numbers, dates, and places. + - type: textarea + id: what + attributes: + label: What to build + description: A few plain sentences. + validations: + required: true + - type: textarea + id: why + attributes: + label: Why + description: Why this is worth building. + validations: + required: true + - type: textarea + id: done + attributes: + label: Done when + description: How we will know this is finished. + validations: + required: true + - type: textarea + id: example + attributes: + label: Example + description: Optional. A picture or a simple diagram, only if the words need it. + validations: + required: false diff --git a/.github/workflows/roadmap.yml b/.github/workflows/roadmap.yml new file mode 100644 index 00000000..5106a890 --- /dev/null +++ b/.github/workflows/roadmap.yml @@ -0,0 +1,45 @@ +# Keeps the public Roadmap project in step with issues labeled roadmap. +# GitHub's workflow token cannot create a project on a personal account. +# Add the repository owner's personal access token as ROADMAP_PROJECT_TOKEN, +# then run this workflow from the Actions tab. + +name: Roadmap + +on: + workflow_dispatch: + issues: + types: [opened, labeled, closed, reopened] + +permissions: + contents: read + +jobs: + ensure-board: + if: github.event_name == 'workflow_dispatch' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + - uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Create the label, project, and columns + env: + ROADMAP_PROJECT_TOKEN: ${{ secrets.ROADMAP_PROJECT_TOKEN }} + ROADMAP_PROJECT_NUMBER: "3" + REPO: ${{ github.repository }} + run: node --experimental-strip-types scripts/roadmap-board.ts ensure + + sync-item: + if: github.event_name == 'issues' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + - uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Move the card + env: + ROADMAP_PROJECT_TOKEN: ${{ secrets.ROADMAP_PROJECT_TOKEN }} + ROADMAP_PROJECT_NUMBER: "3" + REPO: ${{ github.repository }} + run: node --experimental-strip-types scripts/roadmap-board.ts sync diff --git a/AGENTS.md b/AGENTS.md index 19a9a859..defc71e8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -79,6 +79,14 @@ file. New coaching skills follow `docs/coaching-skill-standard.md`. Never publis npm or list on skills.sh without the maintainer; installation tests run locally with `DISABLE_TELEMETRY=1` and `DO_NOT_TRACK=1` (D248). +## Roadmap + +Roadmap ideas are GitHub issues labeled `roadmap` on `mwhitham/ath`. File one by following [`.cursor/skills/roadmap/SKILL.md`](.cursor/skills/roadmap/SKILL.md). Stop when the issue is open. Do not move an issue to Up next. + +The Roadmap workflow adds a new roadmap issue to Backlog and moves a closed one to Done. It does not set Up next or Now. + +Build from Up next on the public project titled Roadmap: . Take one item. Set it to Now. Open a pull request that closes the issue. Closing the issue moves it to Done. Do not start from Backlog. + ## Verification These must pass before any commit: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 63cf3f2f..b550ba46 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,6 +2,14 @@ Use a feature branch and open a pull request against `main`. Keep each commit to one logical change. +## Roadmap + +Use the Roadmap idea issue form for something to build. The issue is labeled `roadmap` and starts in Backlog. The Roadmap workflow places it on the board, and moves it to Done when the issue closes. The repository owner moves work to Up next. Build from Up next. + +The public board is the project titled Roadmap: . + +Do not put real health numbers, dates, or places in an issue. Describe the request. A public issue keeps whatever you write. + ## Protect health data Never commit a real health export or athlete file. This rule applies to public and private repositories because clones, logs, and old commits retain deleted data. diff --git a/README.md b/README.md index c1f8d6aa..aac87856 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,15 @@ Every skill runs `ath`. A skill installed another way, such as with the skills C needs `ath` installed too (see [Install](#install)). Each skill tells the agent how to check for `ath`, and to ask before installing it. +## Roadmap + +What is being built is on the public project titled Roadmap: +. + +A new idea starts in Backlog. It is not chosen yet. The repository owner +moves an idea to Up next when it should be built. Open a roadmap idea with +the issue form. + ## Security The database contains sensitive health and training data. Athletic Standard creates diff --git a/scripts/roadmap-board.ts b/scripts/roadmap-board.ts new file mode 100644 index 00000000..d22f01e6 --- /dev/null +++ b/scripts/roadmap-board.ts @@ -0,0 +1,410 @@ +/** + * Keeps the public Roadmap project in step with issues labeled roadmap. + * + * GitHub's workflow token cannot create a project on a personal account, and + * the built-in project buttons have no API. This script uses a personal token + * from ROADMAP_PROJECT_TOKEN. It never sets Up next or Now. + */ +import { appendFileSync, readFileSync } from "node:fs"; + +const STATUS = [ + { name: "Backlog", color: "GRAY", description: "Not chosen yet.", renameFrom: ["Todo"] }, + { name: "Up next", color: "YELLOW", description: "Chosen to build.", renameFrom: [] }, + { name: "Now", color: "GREEN", description: "Being built.", renameFrom: ["In Progress"] }, + { name: "Done", color: "PURPLE", description: "Finished.", renameFrom: [] }, +] as const; + +const LABEL = { + name: "roadmap", + color: "1D76DB", + description: "A request for something to build. Starts in Backlog.", +}; + +const TOKEN_HELP = [ + "ROADMAP_PROJECT_TOKEN is not set.", + "Add a personal access token as a repository secret named ROADMAP_PROJECT_TOKEN.", + "A classic token needs the project and public_repo scopes.", + "A fine-grained token needs account Projects read and write, and Issues read and write on this repository.", + "The token must belong to the repository owner.", + "Then run the Roadmap workflow from the Actions tab.", +].join(" "); + +export type IssueEvent = { + action?: string; + label?: { name?: string }; + issue?: { + node_id?: string; + pull_request?: unknown; + labels?: { name?: string }[]; + }; +}; + +export type CardAction = { kind: "ignore" } | { kind: "add" | "set"; status: "Backlog" | "Done" }; + +type ExistingColumn = { id: string; name: string; color: string; description: string }; +type Column = { id?: string; name: string; color: string; description: string }; + +/** + * What to do with one issue event. + * `add` sets Backlog only when the card is new. `set` always sets the status. + */ +export function cardAction(event: IssueEvent): CardAction { + if (event.issue?.pull_request) return { kind: "ignore" }; + const names = (event.issue?.labels ?? []).flatMap((label) => (label.name ? [label.name] : [])); + if (!names.includes(LABEL.name)) return { kind: "ignore" }; + if (event.action === "closed") return { kind: "set", status: "Done" }; + if (event.action === "reopened") return { kind: "set", status: "Backlog" }; + if (event.action === "opened") return { kind: "add", status: "Backlog" }; + if (event.action === "labeled" && event.label?.name === LABEL.name) return { kind: "add", status: "Backlog" }; + return { kind: "ignore" }; +} + +/** + * Four status names, reusing an existing option id when one is already there. + * Any other column is kept. Done keeps its id so it stays the finished column. + */ +export function statusColumns(existing: ExistingColumn[]): Column[] { + const unused = existing.map((option) => ({ ...option })); + const take = (names: readonly string[]): ExistingColumn | null => { + const index = unused.findIndex((option) => names.includes(option.name)); + if (index === -1) return null; + const [taken] = unused.splice(index, 1); + return taken ?? null; + }; + const columns: Column[] = STATUS.map((column) => { + const current = take([column.name]) ?? take(column.renameFrom); + if (!current) return { name: column.name, color: column.color, description: column.description }; + return { id: current.id, name: column.name, color: column.color, description: column.description }; + }); + for (const option of unused) columns.push(option); + return columns; +} + +type RestBody = { message?: string; errors?: { code?: string }[] }; + +async function github(token: string, method: string, url: string, payload?: unknown): Promise<{ status: number; body: unknown }> { + const init: RequestInit = { + method, + headers: { + Authorization: `Bearer ${token}`, + Accept: "application/vnd.github+json", + "Content-Type": "application/json", + "User-Agent": "athleticstandard-roadmap", + "X-GitHub-Api-Version": "2022-11-28", + }, + }; + if (payload !== undefined) init.body = JSON.stringify(payload); + const response = await fetch(url, init); + const text = await response.text(); + return { status: response.status, body: text ? JSON.parse(text) as unknown : {} }; +} + +async function graphql(token: string, query: string, variables: Record): Promise { + const { status, body } = await github(token, "POST", "https://api.github.com/graphql", { query, variables }); + const parsed = body as { data?: T; errors?: { message: string }[]; message?: string }; + if (status !== 200 || parsed.errors) { + const message = parsed.errors?.map((error) => error.message).join("\n") || parsed.message; + throw new Error(message || `GitHub API returned ${status}.`); + } + if (parsed.data === undefined) throw new Error("GitHub API returned no data."); + return parsed.data; +} + +async function ensureLabel(token: string, repo: string): Promise { + const url = `https://api.github.com/repos/${repo}/labels`; + const created = await github(token, "POST", url, LABEL); + if (created.status === 201) return; + const body = created.body as RestBody; + const exists = created.status === 422 && body.errors?.some((error) => error.code === "already_exists"); + if (!exists) throw new Error(body.message || `Could not create the roadmap label (${created.status}).`); + const updated = await github(token, "PATCH", `${url}/${LABEL.name}`, LABEL); + if (updated.status !== 200) { + const updateBody = updated.body as RestBody; + throw new Error(updateBody.message || `Could not update the roadmap label (${updated.status}).`); + } +} + +type ProjectNode = { + id: string; + number: number; + title: string; + url: string; + repositories: { nodes: { nameWithOwner: string }[] }; +}; + +type OwnerContext = { + viewer: { id: string; login: string }; + repository: { id: string } | null; + user: { projectsV2: { nodes: ProjectNode[] } } | null; +}; + +async function loadOwner(token: string, owner: string, name: string): Promise { + const data = await graphql( + token, + `query($owner: String!, $name: String!) { + viewer { id login } + repository(owner: $owner, name: $name) { id } + user(login: $owner) { + projectsV2(first: 50, query: "Roadmap") { + nodes { + id + number + title + url + repositories(first: 20) { nodes { nameWithOwner } } + } + } + } + }`, + { owner, name }, + ); + if (data.viewer.login !== owner) throw new Error("The token must belong to the repository owner."); + if (!data.repository || !data.user) throw new Error("The repository was not found."); + return data; +} + +/** The live board. Override with ROADMAP_PROJECT_NUMBER. */ +export function projectNumber(raw: string | undefined): number { + const value = raw === undefined || raw === "" ? "3" : raw; + if (!/^[1-9]\d*$/.test(value)) throw new Error("ROADMAP_PROJECT_NUMBER must be a positive integer."); + return Number(value); +} + +async function loadProjectByNumber(token: string, owner: string, number: number): Promise { + try { + const data = await graphql<{ user: { projectV2: ProjectNode | null } | null }>( + token, + `query($owner: String!, $number: Int!) { + user(login: $owner) { + projectV2(number: $number) { + id + number + title + url + repositories(first: 20) { nodes { nameWithOwner } } + } + } + }`, + { owner, number }, + ); + const project = data.user?.projectV2; + if (!project) throw new Error(`Project ${number} was not found.`); + return project; + } catch (error) { + if (error instanceof Error && /Could not resolve|not found/i.test(error.message)) { + throw new Error(`Project ${number} was not found for ${owner}. The token must belong to the repository owner.`); + } + throw error; + } +} + +async function publishProject(token: string, projectId: string): Promise { + await graphql( + token, + `mutation($projectId: ID!) { + updateProjectV2(input: { + projectId: $projectId + title: "Roadmap" + public: true + shortDescription: "What is being built for Athletic Standard." + }) { + projectV2 { url } + } + }`, + { projectId }, + ); +} + +function alreadyThere(error: unknown): boolean { + return error instanceof Error && /already/i.test(error.message); +} + +async function linkRepository(token: string, projectId: string, repositoryId: string, alreadyLinked: boolean): Promise { + if (alreadyLinked) return; + try { + await graphql( + token, + `mutation($projectId: ID!, $repositoryId: ID!) { + linkProjectV2ToRepository(input: { projectId: $projectId, repositoryId: $repositoryId }) { + repository { id } + } + }`, + { projectId, repositoryId }, + ); + } catch (error) { + if (!alreadyThere(error)) throw error; + } +} + +type StatusField = { id: string; name: string; options: ExistingColumn[] }; + +function isStatusField(node: { name?: string; id?: string; options?: ExistingColumn[] } | null): node is StatusField { + return node?.name === "Status" && typeof node.id === "string" && Array.isArray(node.options); +} + +async function loadStatus(token: string, projectId: string): Promise<{ url: string; field: StatusField }> { + const data = await graphql<{ + node: { url: string; fields: { nodes: ({ name?: string; id?: string; options?: ExistingColumn[] } | null)[] } }; + }>( + token, + `query($id: ID!) { + node(id: $id) { + ... on ProjectV2 { + url + fields(first: 30) { + nodes { + ... on ProjectV2SingleSelectField { + id + name + options { id name color description } + } + } + } + } + } + }`, + { id: projectId }, + ); + const field = data.node.fields.nodes.find(isStatusField); + if (!field) throw new Error("The project has no Status field."); + return { url: data.node.url, field }; +} + +async function writeStatus(token: string, field: StatusField): Promise { + await graphql( + token, + `mutation($fieldId: ID!, $options: [ProjectV2SingleSelectFieldOptionInput!]!) { + updateProjectV2Field(input: { fieldId: $fieldId, singleSelectOptions: $options }) { + projectV2Field { ... on ProjectV2SingleSelectField { id } } + } + }`, + { fieldId: field.id, options: statusColumns(field.options) }, + ); +} + +function optionId(options: ExistingColumn[], name: string): string { + const option = options.find((item) => item.name === name); + if (!option) throw new Error(`The Status field has no ${name} column. Run the Roadmap workflow from the Actions tab.`); + return option.id; +} + +async function itemOnProject(token: string, issueId: string, projectId: string): Promise<{ id: string } | null> { + const data = await graphql<{ node: { projectItems: { nodes: { id: string; project: { id: string } }[] } } }>( + token, + `query($id: ID!) { + node(id: $id) { + ... on Issue { + projectItems(first: 20) { nodes { id project { id } } } + } + } + }`, + { id: issueId }, + ); + return data.node.projectItems.nodes.find((item) => item.project.id === projectId) ?? null; +} + +async function addItem(token: string, projectId: string, issueId: string): Promise { + try { + const data = await graphql<{ addProjectV2ItemById: { item: { id: string } } }>( + token, + `mutation($projectId: ID!, $contentId: ID!) { + addProjectV2ItemById(input: { projectId: $projectId, contentId: $contentId }) { + item { id } + } + }`, + { projectId, contentId: issueId }, + ); + return data.addProjectV2ItemById.item.id; + } catch (error) { + if (!alreadyThere(error)) throw error; + const existing = await itemOnProject(token, issueId, projectId); + if (!existing) throw error; + return existing.id; + } +} + +async function setStatus(token: string, projectId: string, itemId: string, fieldId: string, option: string): Promise { + await graphql( + token, + `mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) { + updateProjectV2ItemFieldValue(input: { + projectId: $projectId + itemId: $itemId + fieldId: $fieldId + value: { singleSelectOptionId: $optionId } + }) { + projectV2Item { id } + } + }`, + { projectId, itemId, fieldId, optionId: option }, + ); +} + +function repositoryParts(): { owner: string; name: string; repo: string } { + const repo = process.env.REPO; + const parts = repo?.split("/") ?? []; + const owner = parts[0]; + const name = parts[1]; + if (!repo || parts.length !== 2 || !owner || !name) throw new Error("REPO must be owner/name."); + return { owner, name, repo }; +} + +function tokenOrNull(): string | null { + const token = process.env.ROADMAP_PROJECT_TOKEN; + return token ? token : null; +} + +async function ensure(): Promise { + const token = tokenOrNull(); + if (!token) throw new Error(TOKEN_HELP); + const { owner, name, repo } = repositoryParts(); + await ensureLabel(token, repo); + const context = await loadOwner(token, owner, name); + const project = await loadProjectByNumber(token, owner, projectNumber(process.env.ROADMAP_PROJECT_NUMBER)); + await publishProject(token, project.id); + if (!context.repository) throw new Error("The repository was not found."); + const linked = project.repositories.nodes.some((repository) => repository.nameWithOwner === repo); + await linkRepository(token, project.id, context.repository.id, linked); + const status = await loadStatus(token, project.id); + await writeStatus(token, status.field); + console.log(status.url); + if (process.env.GITHUB_STEP_SUMMARY) appendFileSync(process.env.GITHUB_STEP_SUMMARY, `Roadmap board: ${status.url}\n`); +} + +async function sync(): Promise { + const token = tokenOrNull(); + if (!token) { + console.error(TOKEN_HELP); + return; + } + const path = process.env.GITHUB_EVENT_PATH; + if (!path) throw new Error("GITHUB_EVENT_PATH is not set."); + const event = JSON.parse(readFileSync(path, "utf8")) as IssueEvent; + const action = cardAction(event); + if (action.kind === "ignore") return; + const issueId = event.issue?.node_id; + if (!issueId) throw new Error("The issue event has no node id."); + const { owner, name, repo } = repositoryParts(); + const context = await loadOwner(token, owner, name); + const project = await loadProjectByNumber(token, owner, projectNumber(process.env.ROADMAP_PROJECT_NUMBER)); + const status = await loadStatus(token, project.id); + let item = await itemOnProject(token, issueId, project.id); + const created = !item; + if (!item) item = { id: await addItem(token, project.id, issueId) }; + if (action.kind === "add" && !created) return; + await setStatus(token, project.id, item.id, status.field.id, optionId(status.field.options, action.status)); +} + +async function main(): Promise { + const command = process.argv[2]; + if (command === "ensure") await ensure(); + else if (command === "sync") await sync(); + else throw new Error("Use ensure or sync."); +} + +if (process.argv[1]?.endsWith("roadmap-board.ts")) { + main().catch((error: unknown) => { + console.error(error instanceof Error ? error.message : error); + process.exitCode = 1; + }); +} diff --git a/tests/roadmap-board.test.ts b/tests/roadmap-board.test.ts new file mode 100644 index 00000000..81d85ed8 --- /dev/null +++ b/tests/roadmap-board.test.ts @@ -0,0 +1,74 @@ +import { describe, expect, it } from "vitest"; +import { cardAction, projectNumber, statusColumns } from "../scripts/roadmap-board.js"; + +const roadmap = { name: "roadmap" }; + +describe("roadmap card moves", () => { + it("puts a new roadmap issue in Backlog and a closed one in Done", () => { + expect(cardAction({ action: "opened", issue: { labels: [roadmap] } })).toEqual({ + kind: "add", + status: "Backlog", + }); + expect(cardAction({ action: "labeled", label: roadmap, issue: { labels: [roadmap] } })).toEqual({ + kind: "add", + status: "Backlog", + }); + expect(cardAction({ action: "closed", issue: { labels: [roadmap] } })).toEqual({ + kind: "set", + status: "Done", + }); + expect(cardAction({ action: "reopened", issue: { labels: [roadmap] } })).toEqual({ + kind: "set", + status: "Backlog", + }); + }); + + it("leaves other issues alone and never chooses Up next or Now", () => { + expect(cardAction({ action: "opened", issue: { labels: [{ name: "bug" }] } })).toEqual({ kind: "ignore" }); + expect(cardAction({ action: "labeled", label: { name: "bug" }, issue: { labels: [roadmap] } })).toEqual({ + kind: "ignore", + }); + expect(cardAction({ action: "closed", issue: { labels: [], pull_request: {} } })).toEqual({ kind: "ignore" }); + + for (const action of ["opened", "labeled", "closed", "reopened"]) { + const plan = cardAction({ action, label: roadmap, issue: { labels: [roadmap] } }); + expect(plan.kind === "ignore" || plan.status === "Backlog" || plan.status === "Done").toBe(true); + } + }); +}); + +describe("roadmap project number", () => { + it("uses project 3 unless another number is set", () => { + expect(projectNumber(undefined)).toBe(3); + expect(projectNumber("")).toBe(3); + expect(projectNumber("3")).toBe(3); + expect(() => projectNumber("0")).toThrow(/positive integer/); + }); +}); + +describe("roadmap columns", () => { + it("renames the default columns and keeps Done", () => { + const columns = statusColumns([ + { id: "todo", name: "Todo", color: "GRAY", description: "" }, + { id: "progress", name: "In Progress", color: "YELLOW", description: "" }, + { id: "done", name: "Done", color: "PURPLE", description: "" }, + ]); + expect(columns.map((column) => column.name)).toEqual(["Backlog", "Up next", "Now", "Done"]); + expect(columns.find((column) => column.name === "Backlog")?.id).toBe("todo"); + expect(columns.find((column) => column.name === "Now")?.id).toBe("progress"); + expect(columns.find((column) => column.name === "Done")?.id).toBe("done"); + expect(columns.find((column) => column.name === "Up next")?.id).toBeUndefined(); + }); + + it("keeps a column the owner added", () => { + const columns = statusColumns([ + { id: "backlog", name: "Backlog", color: "GRAY", description: "Not chosen yet." }, + { id: "next", name: "Up next", color: "YELLOW", description: "Chosen to build." }, + { id: "now", name: "Now", color: "GREEN", description: "Being built." }, + { id: "done", name: "Done", color: "PURPLE", description: "Finished." }, + { id: "extra", name: "Later", color: "BLUE", description: "Waiting." }, + ]); + expect(columns.map((column) => column.name)).toEqual(["Backlog", "Up next", "Now", "Done", "Later"]); + expect(columns.find((column) => column.name === "Later")?.id).toBe("extra"); + }); +});