diff --git a/.github/workflows/openapi-sync.yml b/.github/workflows/openapi-sync.yml new file mode 100644 index 0000000..c5e37f3 --- /dev/null +++ b/.github/workflows/openapi-sync.yml @@ -0,0 +1,158 @@ +name: OpenAPI sync + +# Pulls the live HackMD v1 OpenAPI spec, regenerates the raw client and opens +# (or updates) a PR against develop when anything changed. +# +# Opening the PR needs a GitHub App so CI runs on it (PRs opened with +# GITHUB_TOKEN do not trigger other workflows). Configure: +# - variable OPENAPI_SYNC_APP_ID +# - secret OPENAPI_SYNC_APP_PRIVATE_KEY +# Without them the workflow runs in detect-only mode: it fails when the spec has +# drifted, and a maintainer runs `pnpm spec:pull && pnpm codegen` by hand. + +on: + schedule: + - cron: '0 1 * * *' + workflow_dispatch: + repository_dispatch: + types: [openapi-updated] + +permissions: + contents: read + +concurrency: + group: openapi-sync + cancel-in-progress: false + +env: + OASDIFF_VERSION: 1.34.0 + OASDIFF_SHA256: 7c88e09fd500b231af2819eb21f6fdfc7bcdfe2741d2c1937c59e7b72c8a575a + +jobs: + sync: + runs-on: ubuntu-latest + defaults: + run: + working-directory: nodejs + steps: + - uses: actions/checkout@v4 + with: + ref: develop + + - uses: pnpm/action-setup@v6 + with: + version: 10.33.2 + + - uses: actions/setup-node@v4 + with: + node-version: 24 + cache: pnpm + cache-dependency-path: nodejs/pnpm-lock.yaml + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Pull live spec + id: pull + run: | + cp spec/hackmd-openapi.json "$RUNNER_TEMP/old.json" + pnpm spec:pull + if git diff --quiet -- spec; then + echo "changed=false" >> "$GITHUB_OUTPUT" + echo "Spec is up to date." + else + echo "changed=true" >> "$GITHUB_OUTPUT" + fi + + - name: Regenerate client + if: steps.pull.outputs.changed == 'true' + run: | + pnpm codegen + pnpm typecheck + pnpm build + + - name: Install oasdiff + if: steps.pull.outputs.changed == 'true' + working-directory: ${{ runner.temp }} + run: | + archive="oasdiff_${OASDIFF_VERSION}_linux_amd64.tar.gz" + curl -fsSLO "https://github.com/oasdiff/oasdiff/releases/download/v${OASDIFF_VERSION}/${archive}" + echo "${OASDIFF_SHA256} ${archive}" | sha256sum -c - + tar -xzf "${archive}" oasdiff + echo "$RUNNER_TEMP" >> "$GITHUB_PATH" + + - name: Summarize changes + if: steps.pull.outputs.changed == 'true' + id: summary + run: | + ops='[.paths[] | to_entries[] | select(.key | IN("get","post","put","patch","delete")) | .value.operationId] | sort | .[]' + jq -r "$ops" "$RUNNER_TEMP/old.json" > "$RUNNER_TEMP/old-ops.txt" + jq -r "$ops" spec/hackmd-openapi.json > "$RUNNER_TEMP/new-ops.txt" + added=$(comm -13 "$RUNNER_TEMP/old-ops.txt" "$RUNNER_TEMP/new-ops.txt" | sed 's/^/- `/; s/$/`/') + removed=$(comm -23 "$RUNNER_TEMP/old-ops.txt" "$RUNNER_TEMP/new-ops.txt" | sed 's/^/- `/; s/$/`/') + breaking_count=$(oasdiff breaking "$RUNNER_TEMP/old.json" spec/hackmd-openapi.json --format json | jq 'length') + breaking="" + if [ "$breaking_count" -gt 0 ]; then + breaking=$(oasdiff breaking "$RUNNER_TEMP/old.json" spec/hackmd-openapi.json) + fi + + if [ -n "$breaking" ]; then + echo "labels=openapi-sync,breaking-api" >> "$GITHUB_OUTPUT" + else + echo "labels=openapi-sync" >> "$GITHUB_OUTPUT" + fi + + { + echo "Automated sync of \`nodejs/spec/hackmd-openapi.json\` with https://api.hackmd.io/v1/docs/swagger.json, and the regenerated raw client." + echo + echo "### Added operations" + echo "${added:-None}" + echo + echo "### Removed operations" + echo "${removed:-None}" + echo + echo "### Breaking changes (oasdiff)" + if [ -n "$breaking" ]; then + echo '```' + echo "$breaking" + echo '```' + else + echo "None detected." + fi + echo + echo "### Before merging" + echo "- If \`tests/coverage.spec.ts\` fails, wrap each new operation in \`src/index.ts\` or add it to \`RAW_ONLY\`." + echo "- Update the README if the high-level \`API\` changed." + echo "- Added operations mean a minor release; \`breaking-api\` means a major release." + } > "$RUNNER_TEMP/pr-body.md" + cat "$RUNNER_TEMP/pr-body.md" >> "$GITHUB_STEP_SUMMARY" + + - name: Create GitHub App token + if: steps.pull.outputs.changed == 'true' && vars.OPENAPI_SYNC_APP_ID != '' + id: app-token + uses: actions/create-github-app-token@v2 + with: + app-id: ${{ vars.OPENAPI_SYNC_APP_ID }} + private-key: ${{ secrets.OPENAPI_SYNC_APP_PRIVATE_KEY }} + + - name: Open or update sync PR + if: steps.app-token.outcome == 'success' + uses: peter-evans/create-pull-request@v7 + with: + token: ${{ steps.app-token.outputs.token }} + base: develop + branch: chore/openapi-sync + delete-branch: true + add-paths: | + nodejs/spec/hackmd-openapi.json + nodejs/src/generated + commit-message: 'chore: sync OpenAPI spec' + title: 'chore: sync OpenAPI spec' + body-path: ${{ runner.temp }}/pr-body.md + labels: ${{ steps.summary.outputs.labels }} + + - name: Fail on drift (detect-only mode) + if: steps.pull.outputs.changed == 'true' && vars.OPENAPI_SYNC_APP_ID == '' + run: | + echo "::error::The live OpenAPI spec differs from nodejs/spec/hackmd-openapi.json. Run 'pnpm spec:pull && pnpm codegen' and open a PR, or configure OPENAPI_SYNC_APP_ID to let this workflow open it." + exit 1 diff --git a/nodejs/AGENTS.md b/nodejs/AGENTS.md index a72e208..5935eea 100644 --- a/nodejs/AGENTS.md +++ b/nodejs/AGENTS.md @@ -27,3 +27,17 @@ Steps (shown for a minor release, `X.Y.0`): 7. Ask the maintainer to write release notes for the draft GitHub release and publish it. The workflow leaves it empty. For a patch or major release, use the same steps with the matching version number. + +## OpenAPI spec sync + +`.github/workflows/openapi-sync.yml` pulls `https://api.hackmd.io/v1/docs/swagger.json` daily (also on `workflow_dispatch` and `repository_dispatch` type `openapi-updated`). If the spec changed, it regenerates `src/generated`, runs typecheck and build, and opens or updates a `chore: sync OpenAPI spec` PR on branch `chore/openapi-sync` against `develop`. The PR body lists added and removed operations and the `oasdiff breaking` report; the `breaking-api` label marks breaking changes. + +Opening the PR uses a GitHub App (variable `OPENAPI_SYNC_APP_ID`, secret `OPENAPI_SYNC_APP_PRIVATE_KEY`) so CI runs on it. Without them the workflow only fails on drift, and the refresh is done by hand with `pnpm spec:pull && pnpm codegen`. + +Reviewing a sync PR: + +1. `tests/coverage.spec.ts` fails for every new operation until it is wrapped in `src/index.ts` (imported as `generated`) or added to `RAW_ONLY` in that test. Push that change to the sync branch. +2. Update the README if the high-level `API` changed. +3. Merge into `develop` (this publishes a beta). For the stable release, added operations are a minor bump and `breaking-api` is a major bump. + +Merging a stable release also updates `hackmd-cli`: its Dependabot opens a PR to bump `@hackmd/api`, and its `api` commands read operations from `operationRegistry`. diff --git a/nodejs/README.md b/nodejs/README.md index b381884..79c762c 100644 --- a/nodejs/README.md +++ b/nodejs/README.md @@ -252,6 +252,10 @@ The OpenAPI document is committed at `spec/hackmd-openapi.json`, and generated sources are committed under `src/generated` so package builds remain offline and deterministic. +The `OpenAPI sync` workflow (`.github/workflows/openapi-sync.yml`) runs these +steps daily and opens a `chore: sync OpenAPI spec` PR against `develop` when the +live spec changes. See `AGENTS.md` for how to review it. + ## E2E tests (live API) Integration tests call a real HackMD API (staging or production). They are **not** run by `pnpm test` or the default CI job. diff --git a/nodejs/tests/coverage.spec.ts b/nodejs/tests/coverage.spec.ts new file mode 100644 index 0000000..61d5bf2 --- /dev/null +++ b/nodejs/tests/coverage.spec.ts @@ -0,0 +1,33 @@ +import { readFileSync } from 'node:fs' +import { join } from 'node:path' + +import { operationRegistry } from '../src/raw' + +/** + * Operations intentionally exposed only through `@hackmd/api/raw`, without a + * hand-written `API` method in src/index.ts. + * + * When a spec sync adds an operation, this test fails until it is either + * wrapped in src/index.ts or listed here. + */ +const RAW_ONLY: readonly string[] = [] + +function wrappedOperations (): string[] { + const source = readFileSync(join(__dirname, '../src/index.ts'), 'utf8') + return [...source.matchAll(/\b\w+ as generated(\w+)\b/g)].map(match => match[1]) +} + +describe('operation coverage', () => { + const operations = Object.keys(operationRegistry).sort() + const wrapped = new Set(wrappedOperations()) + + test('every spec operation is wrapped by API or marked raw-only', () => { + const uncovered = operations.filter(id => !wrapped.has(id) && !RAW_ONLY.includes(id)) + expect(uncovered).toEqual([]) + }) + + test('raw-only list has no stale or wrapped entries', () => { + expect(RAW_ONLY.filter(id => !operations.includes(id))).toEqual([]) + expect(RAW_ONLY.filter(id => wrapped.has(id))).toEqual([]) + }) +})