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
92 changes: 92 additions & 0 deletions plugins/openapi/adapter/cloudflare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package adapter

import (
"context"
"encoding/json"
"fmt"

"github.com/patch-it-dev/patch-it-plugins/core/fetch"
"github.com/patch-it-dev/patch-it-plugins/core/github"
"github.com/patch-it-dev/patch-it-plugins/core/plugin"
)

// Variables, not constants, only so tests can point them at a local server.
var (
cloudflareRepo = "cloudflare/api-schemas"
cloudflarePath = "openapi.json"
// cloudflareCommitsURL lists the commits that changed the spec, newest
// first; with per_page=1, the newest.
cloudflareCommitsURL = "https://api.github.com/repos/cloudflare/api-schemas/commits?path=openapi.json&per_page=1"
// cloudflareCommitURL is one commit, for its date.
cloudflareCommitURL = "https://api.github.com/repos/cloudflare/api-schemas/commits/%s"
// cloudflareSpecURLTemplate is the OpenAPI description of the whole
// Cloudflare API (api.cloudflare.com/client/v4) at one commit of
// cloudflare/api-schemas (BSD-3-Clause), the spec Cloudflare's SDKs and
// Terraform provider are generated from. A bot commits it as "Update
// schema: openapi.json" several times a day and the repo has no tags
// (checked 2026-11-21), so the commits are the versions. About 30 MB;
// openapi.yaml beside it is the same spec.
cloudflareSpecURLTemplate = "https://raw.githubusercontent.com/cloudflare/api-schemas/%s/openapi.json"
)

// Cloudflare adapts the Cloudflare API's spec in cloudflare/api-schemas for
// the openapi plugin. A tag here is a commit SHA.
type Cloudflare struct{}

var _ Adapter = Cloudflare{}

func (Cloudflare) API() string { return "cloudflare/api-spec" }

// LatestTag is the SHA of the newest commit that changed the spec.
func (Cloudflare) LatestTag(ctx context.Context) (string, error) {
sha, err := github.LatestCommit(ctx, github.Get, cloudflareCommitsURL)
if err != nil {
return "", fmt.Errorf("looking up cloudflare's latest spec commit: %w", err)
}
return sha, nil
}

// Versions are the days the spec changed, each read at that day's last
// commit (Daily).
func (Cloudflare) Versions(ctx context.Context) ([]plugin.Version, error) {
return commits(ctx, cloudflareRepo, cloudflarePath)
}

// Fetch downloads the spec at commit sha and moves the server's /client/v4
// into the paths: see ServerBasePath. Version is the commit's date
// (YYYY-MM-DD): info.version is a fixed "4.0.0" (checked 2026-11-21).
func (Cloudflare) Fetch(ctx context.Context, sha string) (Spec, error) {
fullSHA, date, err := github.Commit(ctx, github.Get, fmt.Sprintf(cloudflareCommitURL, sha))
if err != nil {
return Spec{}, fmt.Errorf("looking up cloudflare spec commit %s: %w", sha, err)
}
if fullSHA == "" {
return Spec{}, fmt.Errorf("github commit %s has no sha", sha)
}
url := fmt.Sprintf(cloudflareSpecURLTemplate, fullSHA)
body, err := fetch.Get(ctx, url, nil) // raw.githubusercontent.com needs no auth
if err != nil {
return Spec{}, fmt.Errorf("fetching cloudflare spec %s: %w", sha, err)
}
if !json.Valid(body) {
return Spec{}, fmt.Errorf("cloudflare spec %s is not valid json", sha)
}
// The one server is "https://api.cloudflare.com/client/v4" and the
// paths don't repeat it: moved, an operation is named by the path a
// caller sends.
content, err := ServerBasePath(body)
if err != nil {
return Spec{}, fmt.Errorf("cloudflare spec %s: %w", sha, err)
}
return Spec{Content: content, Version: date.UTC().Format("2006-01-02"), SourceURL: url}, nil
}

// SourcePointer: see BasePathPointer.
func (Cloudflare) SourcePointer(content []byte) func(string) string {
return BasePathPointer(content)
}

var (
_ Reshaper = Cloudflare{}
_ Lister = Cloudflare{}
)
78 changes: 78 additions & 0 deletions plugins/openapi/adapter/mongodb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package adapter

import (
"context"
"encoding/json"
"fmt"

"github.com/patch-it-dev/patch-it-plugins/core/fetch"
"github.com/patch-it-dev/patch-it-plugins/core/github"
"github.com/patch-it-dev/patch-it-plugins/core/plugin"
)

// Variables, not constants, only so tests can point them at a local server.
var (
atlasRepo = "mongodb/openapi"
atlasPath = "openapi/v2.json"
// atlasCommitsURL lists the commits that changed the spec, newest first;
// with per_page=1, the newest.
atlasCommitsURL = "https://api.github.com/repos/mongodb/openapi/commits?path=openapi/v2.json&per_page=1"
// atlasCommitURL is one commit, for its date.
atlasCommitURL = "https://api.github.com/repos/mongodb/openapi/commits/%s"
// atlasSpecURLTemplate is the OpenAPI description of the MongoDB Atlas
// Administration API v2 at one commit of mongodb/openapi: every
// operation with every resource version it serves. MongoDB's release
// workflow commits it ("ci(prod): Release OpenAPI Spec") a few times a
// week, 768 commits from 2024-07-25 to 2026-11-19 (checked 2026-11-21);
// the repo's v0.0.N tags mark tooling releases, not the spec.
atlasSpecURLTemplate = "https://raw.githubusercontent.com/mongodb/openapi/%s/openapi/v2.json"
)

// MongoDBAtlas adapts the Atlas Administration API's spec in mongodb/openapi
// for the openapi plugin. A tag here is a commit SHA.
type MongoDBAtlas struct{}

var _ Adapter = MongoDBAtlas{}

func (MongoDBAtlas) API() string { return "mongodb/atlas-admin-spec" }

// LatestTag is the SHA of the newest commit that changed the spec.
func (MongoDBAtlas) LatestTag(ctx context.Context) (string, error) {
sha, err := github.LatestCommit(ctx, github.Get, atlasCommitsURL)
if err != nil {
return "", fmt.Errorf("looking up mongodb atlas's latest spec commit: %w", err)
}
return sha, nil
}

// Versions are the days the spec changed, each read at that day's last
// commit (Daily).
func (MongoDBAtlas) Versions(ctx context.Context) ([]plugin.Version, error) {
return commits(ctx, atlasRepo, atlasPath)
}

// Fetch downloads the spec at commit sha. Version is the commit's date
// (YYYY-MM-DD): info.version is a fixed "2.0", and the resource versions
// (application/vnd.atlas.2025-03-12+json) are per operation, several in one
// spec. The server is https://cloud.mongodb.com with no path: the paths are
// already what a caller sends (/api/atlas/v2/...).
func (MongoDBAtlas) Fetch(ctx context.Context, sha string) (Spec, error) {
fullSHA, date, err := github.Commit(ctx, github.Get, fmt.Sprintf(atlasCommitURL, sha))
if err != nil {
return Spec{}, fmt.Errorf("looking up mongodb atlas spec commit %s: %w", sha, err)
}
if fullSHA == "" {
return Spec{}, fmt.Errorf("github commit %s has no sha", sha)
}
url := fmt.Sprintf(atlasSpecURLTemplate, fullSHA)
body, err := fetch.Get(ctx, url, nil) // raw.githubusercontent.com needs no auth
if err != nil {
return Spec{}, fmt.Errorf("fetching mongodb atlas spec %s: %w", sha, err)
}
if !json.Valid(body) {
return Spec{}, fmt.Errorf("mongodb atlas spec %s is not valid json", sha)
}
return Spec{Content: body, Version: date.UTC().Format("2006-01-02"), SourceURL: url}, nil
}

var _ Lister = MongoDBAtlas{}
102 changes: 102 additions & 0 deletions plugins/openapi/adapter/openai.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package adapter

import (
"context"
"encoding/json"
"fmt"

"github.com/patch-it-dev/patch-it-plugins/core/fetch"
"github.com/patch-it-dev/patch-it-plugins/core/github"
"github.com/patch-it-dev/patch-it-plugins/core/plugin"
"gopkg.in/yaml.v3"
)

// Variables, not constants, only so tests can point them at a local server.
var (
openaiRepo = "openai/openai-openapi"
openaiPath = "openapi.yaml"
// openaiCommitsURL lists the commits that changed the spec, newest
// first; with per_page=1, the newest.
openaiCommitsURL = "https://api.github.com/repos/openai/openai-openapi/commits?path=openapi.yaml&per_page=1"
// openaiCommitURL is one commit, for its date.
openaiCommitURL = "https://api.github.com/repos/openai/openai-openapi/commits/%s"
// openaiSpecURLTemplate is the OpenAPI description of the OpenAI API at
// one commit of openai/openai-openapi (MIT), the spec OpenAI's own SDKs
// are generated from. The repo commits it several times a day (589
// commits that changed openapi.yaml from 2022-01-19 to 2026-11-21,
// checked 2026-11-21) and tags it seldom (1.3.0, 2.0.0), so the commits
// are the versions. openapi.json beside it only exists from 2026-07-16:
// the YAML has the whole history.
openaiSpecURLTemplate = "https://raw.githubusercontent.com/openai/openai-openapi/%s/openapi.yaml"
)

// OpenAI adapts the OpenAI API's spec in openai/openai-openapi for the
// openapi plugin. A tag here is a commit SHA.
type OpenAI struct{}

var _ Adapter = OpenAI{}

func (OpenAI) API() string { return "openai/api" }

// LatestTag is the SHA of the newest commit that changed the spec.
func (OpenAI) LatestTag(ctx context.Context) (string, error) {
sha, err := github.LatestCommit(ctx, github.Get, openaiCommitsURL)
if err != nil {
return "", fmt.Errorf("looking up openai's latest spec commit: %w", err)
}
return sha, nil
}

// Versions are the days the spec changed, each read at that day's last
// commit (Daily): several commits a day is the norm here, and a day is the
// version a caller can name.
func (OpenAI) Versions(ctx context.Context) ([]plugin.Version, error) {
return commits(ctx, openaiRepo, openaiPath)
}

// Fetch downloads the spec at commit sha, converts it from YAML to JSON (what
// diff.Parse reads) and moves the server's /v1 into the paths: see
// ServerBasePath. Version is the commit's date (YYYY-MM-DD): info.version
// ("2.3.0") isn't bumped with each change, and the API has no dated versions
// of its own.
func (OpenAI) Fetch(ctx context.Context, sha string) (Spec, error) {
fullSHA, date, err := github.Commit(ctx, github.Get, fmt.Sprintf(openaiCommitURL, sha))
if err != nil {
return Spec{}, fmt.Errorf("looking up openai spec commit %s: %w", sha, err)
}
if fullSHA == "" {
return Spec{}, fmt.Errorf("github commit %s has no sha", sha)
}
url := fmt.Sprintf(openaiSpecURLTemplate, fullSHA)
body, err := fetch.Get(ctx, url, nil) // raw.githubusercontent.com needs no auth
if err != nil {
return Spec{}, fmt.Errorf("fetching openai spec %s: %w", sha, err)
}
content, err := YAMLToJSON(body)
if err != nil {
return Spec{}, fmt.Errorf("openai spec %s: %w", sha, err)
}
// The server is "https://api.openai.com/v1" and the paths don't repeat
// /v1 (checked 2026-11-21): moved, an operation is named by the path a
// caller sends ("POST /v1/audio/translations").
if content, err = ServerBasePath(content); err != nil {
return Spec{}, fmt.Errorf("openai spec %s: %w", sha, err)
}
return Spec{Content: content, Version: date.UTC().Format("2006-01-02"), SourceURL: url}, nil
}

// SourcePointer: see BasePathPointer.
func (OpenAI) SourcePointer(content []byte) func(string) string { return BasePathPointer(content) }

var _ Reshaper = OpenAI{}

// YAMLToJSON converts a YAML spec to JSON: diff.Parse reads JSON only, and
// normalize quotes the node it cites re-serialized, never the original bytes,
// so nothing quoted changes.
func YAMLToJSON(body []byte) ([]byte, error) {
var doc any
if err := yaml.Unmarshal(body, &doc); err != nil {
return nil, fmt.Errorf("parsing yaml: %w", err)
}
return json.Marshal(doc)
}
33 changes: 28 additions & 5 deletions plugins/openapi/adapter/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ func releases(ctx context.Context, repo string, keep func(string) bool) ([]plugi
return versions.GitHubReleases(ctx, github.Get, repo, keep)
}

// commits are the days a file changed, each read at the day's last commit
// (Daily).
func commits(ctx context.Context, repo, path string) ([]plugin.Version, error) {
return versions.GitHubCommits(ctx, github.Get, repo, path)
vs, err := versions.GitHubCommits(ctx, github.Get, repo, path)
if err != nil {
return nil, err
}
return Daily(vs), nil
}

// Versions are stripe/openapi's releases.
Expand Down Expand Up @@ -233,9 +239,9 @@ func (a Algolia) Versions(ctx context.Context) ([]plugin.Version, error) {
return out, nil
}

// Versions are the commits on appwrite/specs' default branch that changed
// specs/, oldest first: the ID is the commit's date (Fetch's Version), the
// Ref its SHA, read at the newest minor's directory.
// Versions are the days appwrite/specs' default branch changed specs/,
// oldest first, each at the day's last commit (Daily): the ID is the date
// (Fetch's Version), the Ref the SHA, read at the newest minor's directory.
func (a Appwrite) Versions(ctx context.Context) ([]plugin.Version, error) {
cs, err := versions.GitHubPages[struct {
SHA string `json:"sha"`
Expand All @@ -258,7 +264,7 @@ func (a Appwrite) Versions(ctx context.Context) ([]plugin.Version, error) {
out = append(out, plugin.Version{ID: at.Format(time.DateOnly), Released: at, Ref: c.SHA, URL: c.URL})
}
slices.SortStableFunc(out, func(a, b plugin.Version) int { return a.Released.Compare(b.Released) })
return out, nil
return Daily(out), nil
}

var (
Expand All @@ -269,3 +275,20 @@ var (
_ Lister = Algolia{}
_ Lister = Appwrite{}
)

// Daily keeps one version per day of vs (oldest first, IDs dates, as
// versions.GitHubCommits lists them): the day's last commit, so a spec
// committed several times a day has one version per day, each ID once.
func Daily(vs []plugin.Version) []plugin.Version {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: Daily looks right to me. GitHubCommits and github.Commit both use the committer date in UTC, so the listed ID and Fetch's Version agree, and the duplicate IDs are always next to each other because the list is sorted.

One thing to check when this rolls out: Supabase, Neon, Convex, PlanetScale and Appwrite are already running. If patch-it's hosted ingestion service last stored a day with two commits by its ID, that ID now points at the day's last commit. If the stored ID came from an earlier commit that day, the commits after it on that day may never be diffed. This happens at most once per source and can only miss events, never produce wrong ones, so it fits "precision over recall". A sentence about it in the PR description would help whoever deploys this.

Small inconsistency: var _ Lister = OpenAI{} (line 294) sits here, while the Cloudflare and MongoDB assertions are in their own files. It could move to openai.go.

var out []plugin.Version
for _, v := range vs {
if n := len(out); n > 0 && out[n-1].ID == v.ID {
out[n-1] = v
continue
}
out = append(out, v)
}
return out
}

var _ Lister = OpenAI{}
21 changes: 17 additions & 4 deletions plugins/openapi/adapter/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,32 @@ func TestAlgoliaListsEveryReleaseTag(t *testing.T) {
}
}

// Every commit that changed specs/, oldest first, dated.
// Every day specs/ changed, oldest first, at the day's last commit: the 40
// commits saved fall on 16 days, and a version ID (the date) is named once.
func TestAppwriteListsEverySpecCommit(t *testing.T) {
srv := recorded(t, map[string]string{"/repos/appwrite/specs/commits?path=specs&per_page=100&page=1": "appwrite-commits.json"})
vs, err := Appwrite{APIBase: srv.URL}.Versions(context.Background())
if err != nil {
t.Fatal(err)
}
if len(vs) != 40 {
t.Fatalf("%d versions, want 40", len(vs))
if len(vs) != 16 {
t.Fatalf("%d versions, want 16", len(vs))
}
for i := 1; i < len(vs); i++ {
if vs[i].Released.Before(vs[i-1].Released) || len(vs[i].Ref) != 40 {
if !vs[i].Released.After(vs[i-1].Released) || vs[i].ID == vs[i-1].ID || len(vs[i].Ref) != 40 {
t.Errorf("%v after %v", vs[i], vs[i-1])
}
}
}

// Daily keeps each day's last commit, in order.
func TestDailyKeepsEachDaysLastCommit(t *testing.T) {
in := []plugin.Version{{ID: "2026-10-27", Ref: "a"}, {ID: "2026-10-28", Ref: "b"}, {ID: "2026-10-28", Ref: "c"}, {ID: "2026-10-30", Ref: "d"}}
var got []string
for _, v := range Daily(in) {
got = append(got, v.ID+" "+v.Ref)
}
if want := "2026-10-27 a,2026-10-28 c,2026-10-30 d"; strings.Join(got, ",") != want {
t.Errorf("got %v, want %s", got, want)
}
}
Loading
Loading