|
| 1 | +name: Extract Typesense API |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + schedule: |
| 6 | + - cron: "17 4 * * *" |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - master |
| 10 | + paths: |
| 11 | + - "src/**" |
| 12 | + - "config.json" |
| 13 | + - "package.json" |
| 14 | + - "package-lock.json" |
| 15 | + - "tsconfig.json" |
| 16 | + - ".github/workflows/extract-api.yml" |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: write |
| 20 | + issues: write |
| 21 | + |
| 22 | +jobs: |
| 23 | + extract: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + |
| 26 | + steps: |
| 27 | + - name: Checkout repository |
| 28 | + uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + fetch-depth: 0 |
| 31 | + |
| 32 | + - name: Set up Node.js |
| 33 | + uses: actions/setup-node@v4 |
| 34 | + with: |
| 35 | + node-version: 22 |
| 36 | + cache: npm |
| 37 | + |
| 38 | + - name: Install dependencies |
| 39 | + run: npm ci --legacy-peer-deps |
| 40 | + |
| 41 | + - name: Build CLI |
| 42 | + run: npm run build |
| 43 | + |
| 44 | + - name: Save previous extracted spec |
| 45 | + run: | |
| 46 | + if git cat-file -e HEAD:output/typesense_api_spec.json 2>/dev/null; then |
| 47 | + git show HEAD:output/typesense_api_spec.json > "$RUNNER_TEMP/previous-typesense-api-spec.json" |
| 48 | + else |
| 49 | + printf '%s\n' '{"rootDir":"","routeFile":"","routes":[],"diagnostics":[]}' > "$RUNNER_TEMP/previous-typesense-api-spec.json" |
| 50 | + fi |
| 51 | +
|
| 52 | + - name: Run extractor |
| 53 | + id: extract |
| 54 | + continue-on-error: true |
| 55 | + run: node dist/cli.js extract --config ./config.json --fail-on-diagnostics |
| 56 | + |
| 57 | + - name: Upload extractor artifacts for debugging |
| 58 | + if: steps.extract.outcome != 'success' |
| 59 | + uses: actions/upload-artifact@v4 |
| 60 | + with: |
| 61 | + name: extractor-debug-artifacts |
| 62 | + if-no-files-found: warn |
| 63 | + path: | |
| 64 | + output/typesense_api_spec.json |
| 65 | + output/typesense_api_diagnostics.json |
| 66 | +
|
| 67 | + - name: Stop on extractor diagnostics |
| 68 | + if: steps.extract.outcome != 'success' |
| 69 | + run: exit 1 |
| 70 | + |
| 71 | + - name: Check for extracted output changes |
| 72 | + id: changes |
| 73 | + run: | |
| 74 | + if git diff --quiet -- output/typesense_api_spec.json output/typesense_api_diagnostics.json; then |
| 75 | + echo "changed=false" >> "$GITHUB_OUTPUT" |
| 76 | + else |
| 77 | + echo "changed=true" >> "$GITHUB_OUTPUT" |
| 78 | + fi |
| 79 | +
|
| 80 | + - name: Build semantic API diff |
| 81 | + if: steps.changes.outputs.changed == 'true' |
| 82 | + run: | |
| 83 | + node dist/cli.js semantic-diff \ |
| 84 | + --previous "$RUNNER_TEMP/previous-typesense-api-spec.json" \ |
| 85 | + --next output/typesense_api_spec.json \ |
| 86 | + --json-output "$RUNNER_TEMP/typesense-api-diff.json" \ |
| 87 | + --markdown-output "$RUNNER_TEMP/typesense-api-diff.md" |
| 88 | +
|
| 89 | + - name: Read semantic API diff result |
| 90 | + if: steps.changes.outputs.changed == 'true' |
| 91 | + id: semantic_diff |
| 92 | + run: | |
| 93 | + jq -r '"changed=\(.changed)"' "$RUNNER_TEMP/typesense-api-diff.json" >> "$GITHUB_OUTPUT" |
| 94 | +
|
| 95 | + - name: Commit extracted artifacts |
| 96 | + if: steps.changes.outputs.changed == 'true' |
| 97 | + id: commit_artifacts |
| 98 | + run: | |
| 99 | + git config user.name "github-actions[bot]" |
| 100 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 101 | + git add output/typesense_api_spec.json output/typesense_api_diagnostics.json |
| 102 | + git commit -m "chore: update extracted Typesense API artifacts" |
| 103 | + git push |
| 104 | + echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" |
| 105 | +
|
| 106 | + - name: Open OpenAPI update issue |
| 107 | + if: steps.changes.outputs.changed == 'true' && steps.semantic_diff.outputs.changed == 'true' |
| 108 | + uses: actions/github-script@v7 |
| 109 | + with: |
| 110 | + script: | |
| 111 | + const fs = require("node:fs"); |
| 112 | + const owner = context.repo.owner; |
| 113 | + const repo = context.repo.repo; |
| 114 | + const commitSha = "${{ steps.commit_artifacts.outputs.sha }}"; |
| 115 | + const shortSha = commitSha.slice(0, 12); |
| 116 | + const commitUrl = `https://github.com/${owner}/${repo}/commit/${commitSha}`; |
| 117 | + const title = `Update openapi.yml for extracted Typesense API changes [${shortSha}]`; |
| 118 | + const existing = await github.paginate(github.rest.issues.listForRepo, { |
| 119 | + owner, |
| 120 | + repo, |
| 121 | + state: "open", |
| 122 | + per_page: 100, |
| 123 | + }); |
| 124 | + const alreadyOpen = existing.some((issue) => issue.title === title); |
| 125 | + if (alreadyOpen) { |
| 126 | + core.info(`Open issue already exists: ${title}`); |
| 127 | + return; |
| 128 | + } |
| 129 | +
|
| 130 | + const bodyPath = `${process.env.RUNNER_TEMP}/typesense-api-diff.md`; |
| 131 | + const diffBody = fs.readFileSync(bodyPath, "utf8"); |
| 132 | + const body = [ |
| 133 | + "<!-- typesense-api-sync-issue -->", |
| 134 | + `Commit: ${commitUrl}`, |
| 135 | + "", |
| 136 | + diffBody, |
| 137 | + ].join("\n"); |
| 138 | + await github.rest.issues.create({ |
| 139 | + owner, |
| 140 | + repo, |
| 141 | + title, |
| 142 | + body, |
| 143 | + }); |
0 commit comments