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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/patch-it-dev/patch-it-plugins.svg)](https://pkg.go.dev/github.com/patch-it-dev/patch-it-plugins)
[![Go version](https://img.shields.io/github/go-mod/go-version/patch-it-dev/patch-it-plugins)](go.mod)
[![License: Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-3f5bf0)](LICENSE)
[![Plugins](https://img.shields.io/badge/plugins-20-3f5bf0)](docs/plugins.md)
[![Plugins](https://img.shields.io/badge/plugins-21-3f5bf0)](docs/plugins.md)
[![Docs](https://img.shields.io/badge/docs-docs.patch--it.dev-3f5bf0)](https://docs.patch-it.dev)
[![Discussions](https://img.shields.io/github/discussions/patch-it-dev/patch-it-plugins)](https://github.com/patch-it-dev/patch-it-plugins/discussions)

Expand Down
131 changes: 131 additions & 0 deletions adapter/elasticsearch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package adapter

import (
"context"
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"

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

// Variables, not constants, only so tests can point them at a local server.
var (
// esReleasesURL is Elasticsearch's own releases, newest first by date (an
// 8.19 patch can come after a 9.5 one), so the newest version is computed,
// not taken from releases/latest.
esReleasesURL = "https://api.github.com/repos/elastic/elasticsearch/releases?per_page=100"
// esBranchesURL lists every branch of elastic/elasticsearch-specification
// in one response (the branches API pages; this doesn't): one per minor
// version, "9.5".
esBranchesURL = "https://api.github.com/repos/elastic/elasticsearch-specification/git/matching-refs/heads/"
// esCommitURL is a branch's head commit.
esCommitURL = "https://api.github.com/repos/elastic/elasticsearch-specification/commits/%s"
// esSpecURLTemplate is the OpenAPI spec generated from the specification
// (the one Elastic's clients and API reference are built from) at one
// commit. The OpenAPI output starts with the 8.14 branch.
esSpecURLTemplate = "https://raw.githubusercontent.com/elastic/elasticsearch-specification/%s/output/openapi/elasticsearch-openapi.json"
)

// Elasticsearch adapts the Elasticsearch REST API spec in
// elastic/elasticsearch-specification for the openapi plugin, minor version
// by minor version. A tag here is a minor version's branch ("9.5").
type Elasticsearch struct{}

var _ Adapter = Elasticsearch{}

func (Elasticsearch) API() string { return "elasticsearch" }

var (
esRelease = regexp.MustCompile(`^v(\d+)\.(\d+)\.(\d+)$`)
esBranch = regexp.MustCompile(`^(\d+)\.(\d+)$`)
)

// LatestTag is the branch of the newest minor version Elastic has released:
// not the newest branch, which is the next, unreleased one (9.6 on
// 2026-11-19, when the newest release was 9.5.6), whose spec can still
// change before it ships.
func (Elasticsearch) LatestTag(ctx context.Context) (string, error) {
body, err := fetch.Get(ctx, esReleasesURL, githubHeaders())
if err != nil {
return "", fmt.Errorf("listing elasticsearch releases: %w", err)
}
var releases []struct {
Tag string `json:"tag_name"`
Prerelease bool `json:"prerelease"`
Draft bool `json:"draft"`
}
if err := json.Unmarshal(body, &releases); err != nil {
return "", fmt.Errorf("parsing github releases: %w", err)
}
newest := [2]int{-1, -1}
for _, r := range releases {
m := esRelease.FindStringSubmatch(r.Tag)
if m == nil || r.Prerelease || r.Draft {
continue
}
v := [2]int{atoi(m[1]), atoi(m[2])}
if v[0] > newest[0] || v[0] == newest[0] && v[1] > newest[1] {
newest = v
}
}
if newest[0] < 0 {
return "", fmt.Errorf("github lists no elasticsearch release")
}

body, err = fetch.Get(ctx, esBranchesURL, githubHeaders())
if err != nil {
return "", fmt.Errorf("listing elasticsearch-specification branches: %w", err)
}
var refs []struct {
Ref string `json:"ref"`
}
if err := json.Unmarshal(body, &refs); err != nil {
return "", fmt.Errorf("parsing github branch refs: %w", err)
}
want := fmt.Sprintf("%d.%d", newest[0], newest[1])
for _, r := range refs {
if strings.TrimPrefix(r.Ref, "refs/heads/") == want {
return want, nil
}
}
return "", fmt.Errorf("elasticsearch %s is released, but elasticsearch-specification has no %s branch", want, want)
}

func atoi(s string) int {
n, _ := strconv.Atoi(s)
return n
}

// Fetch downloads the spec at the head of the branch, pinned to that commit:
// a branch moves (fixes land on it after the release), a commit doesn't.
//
// Version is the minor's first release, "9.5.0": what a repo pins, and the
// elasticsearch clients' versions follow the server's (elasticsearch-py 9.x
// for Elasticsearch 9.x). info.version in the spec is empty.
func (Elasticsearch) Fetch(ctx context.Context, branch string) (Spec, error) {
if !esBranch.MatchString(branch) {
return Spec{}, fmt.Errorf("%q isn't an elasticsearch-specification minor branch", branch)
}
meta, err := fetch.Get(ctx, fmt.Sprintf(esCommitURL, branch), githubHeaders())
if err != nil {
return Spec{}, fmt.Errorf("looking up the head of elasticsearch-specification %s: %w", branch, err)
}
var commit struct {
SHA string `json:"sha"`
}
if err := json.Unmarshal(meta, &commit); err != nil || commit.SHA == "" {
return Spec{}, fmt.Errorf("elasticsearch-specification %s: no head commit", branch)
}
url := fmt.Sprintf(esSpecURLTemplate, commit.SHA)
body, err := fetch.Get(ctx, url, nil) // raw.githubusercontent.com needs no auth
if err != nil {
return Spec{}, fmt.Errorf("fetching the elasticsearch %s spec: %w", branch, err)
}
if !json.Valid(body) {
return Spec{}, fmt.Errorf("the elasticsearch %s spec is not valid json", branch)
}
return Spec{Content: body, Version: branch + ".0", SourceURL: url}, nil
}
86 changes: 86 additions & 0 deletions adapter/elasticsearch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package adapter

import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func fakeESGitHub(t *testing.T, releases, branches string, heads, specs map[string]string) *httptest.Server {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/releases":
w.Write([]byte(releases))
case r.URL.Path == "/branches":
w.Write([]byte(branches))
case strings.HasPrefix(r.URL.Path, "/commits/"):
sha, ok := heads[strings.TrimPrefix(r.URL.Path, "/commits/")]
if !ok {
http.NotFound(w, r)
return
}
w.Write([]byte(`{"sha":"` + sha + `"}`))
default:
body, ok := specs[r.URL.Path]
if !ok {
http.NotFound(w, r)
return
}
w.Write([]byte(body))
}
}))
t.Cleanup(srv.Close)
a, b, c, d := esReleasesURL, esBranchesURL, esCommitURL, esSpecURLTemplate
esReleasesURL, esBranchesURL = srv.URL+"/releases", srv.URL+"/branches"
esCommitURL, esSpecURLTemplate = srv.URL+"/commits/%s", srv.URL+"/%s/spec.json"
t.Cleanup(func() { esReleasesURL, esBranchesURL, esCommitURL, esSpecURLTemplate = a, b, c, d })
return srv
}

const esBranches = `[{"ref":"refs/heads/main"},{"ref":"refs/heads/8.19"},{"ref":"refs/heads/9.5"},{"ref":"refs/heads/9.6"}]`

// The newest released minor, not the newest branch (9.6 isn't released), and
// not the newest release by date (8.19.25 came out after 9.5.6).
func TestElasticsearchLatestTagIsTheNewestReleasedMinor(t *testing.T) {
fakeESGitHub(t, `[{"tag_name":"v8.19.25"},{"tag_name":"v9.5.6"},{"tag_name":"v9.6.0-rc1","prerelease":true},{"tag_name":"v9.4.9"}]`, esBranches, nil, nil)
tag, err := Elasticsearch{}.LatestTag(context.Background())
if err != nil || tag != "9.5" {
t.Errorf("got %q, %v; want 9.5", tag, err)
}
}

func TestElasticsearchLatestTagFailures(t *testing.T) {
fakeESGitHub(t, `[{"tag_name":"v9.6.0-rc1","prerelease":true}]`, esBranches, nil, nil)
if _, err := (Elasticsearch{}).LatestTag(context.Background()); err == nil {
t.Error("no release: want an error")
}
fakeESGitHub(t, `[{"tag_name":"v9.7.0"}]`, esBranches, nil, nil)
if _, err := (Elasticsearch{}).LatestTag(context.Background()); err == nil {
t.Error("a release with no spec branch: want an error, not an older branch")
}
}

// Pinned to the branch head's commit; the version is the minor's .0 release.
func TestElasticsearchFetchPinsTheHead(t *testing.T) {
const sha = "57ecfe5fda6976b94fc8b8830ecb51f4969dccd6"
srv := fakeESGitHub(t, "", "", map[string]string{"9.0": sha}, map[string]string{"/" + sha + "/spec.json": `{"openapi":"3.0.3","info":{"version":""},"paths":{}}`})
spec, err := Elasticsearch{}.Fetch(context.Background(), "9.0")
if err != nil {
t.Fatal(err)
}
if spec.Version != "9.0.0" || spec.SourceURL != srv.URL+"/"+sha+"/spec.json" {
t.Errorf("got %q %s", spec.Version, spec.SourceURL)
}
for _, b := range []string{"main", "v9.0", "9.0.1"} {
if _, err := (Elasticsearch{}).Fetch(context.Background(), b); err == nil {
t.Errorf("%s: want an error", b)
}
}
fakeESGitHub(t, "", "", map[string]string{"9.0": sha}, map[string]string{"/" + sha + "/spec.json": "<html>"})
if _, err := (Elasticsearch{}).Fetch(context.Background(), "9.0"); err == nil {
t.Error("invalid json: want an error")
}
}
12 changes: 11 additions & 1 deletion docs/plugins.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_comment": "Generated from the plugin registry and docs/plugins.md by `make plugins-json` (go run ./cmd/list-plugins). Don't edit by hand.",
"$schema": "./plugins.schema.json",
"count": 20,
"count": 21,
"plugins": [
{
"id": "algolia-ingestion",
Expand Down Expand Up @@ -73,6 +73,16 @@
"status": "live",
"provider_page": "providers/datadog.md"
},
{
"id": "elasticsearch",
"provider": "Elastic",
"layer": "api",
"watches": "Elasticsearch REST API operations deprecated, and deprecated operations removed, between released minor versions of the spec",
"format": "openapi",
"source_url": "https://github.com/elastic/elasticsearch-specification",
"status": "live",
"provider_page": "providers/elasticsearch.md"
},
{
"id": "github",
"provider": "GitHub",
Expand Down
1 change: 1 addition & 0 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The table between the `plugins:start` and `plugins:end` markers is also publishe
| [`algolia-search`](https://github.com/patch-it-dev/patch-it-plugins/blob/main/providers/algolia.md) | Algolia | API | Search API operations deprecated, and deprecated operations removed, between releases of the spec | `openapi` | [algolia/api-clients-automation](https://github.com/algolia/api-clients-automation) (`specs/bundled/search.yml`) | live |
| [`algolia-ingestion`](https://github.com/patch-it-dev/patch-it-plugins/blob/main/providers/algolia.md) | Algolia | API | Ingestion API operations deprecated, and deprecated operations removed, between releases of the spec | `openapi` | [algolia/api-clients-automation](https://github.com/algolia/api-clients-automation) (`specs/bundled/ingestion.yml`) | live |
| [`algolia-recommend`](https://github.com/patch-it-dev/patch-it-plugins/blob/main/providers/algolia.md) | Algolia | API | Recommend API operations deprecated, and deprecated operations removed, between releases of the spec | `openapi` | [algolia/api-clients-automation](https://github.com/algolia/api-clients-automation) (`specs/bundled/recommend.yml`) | live |
| [`elasticsearch`](https://github.com/patch-it-dev/patch-it-plugins/blob/main/providers/elasticsearch.md) | Elastic | API | Elasticsearch REST API operations deprecated, and deprecated operations removed, between released minor versions of the spec | `openapi` | [elastic/elasticsearch-specification](https://github.com/elastic/elasticsearch-specification) (`output/openapi/elasticsearch-openapi.json`) | live |
| [`mongodb-atlas`](https://github.com/patch-it-dev/patch-it-plugins/blob/main/providers/mongodb-atlas.md) | MongoDB | API | Atlas Administration API versions deprecated and removed at their sunset date, per endpoint, from MongoDB's own API changelog | `apichangelog` | [mongodb/openapi `changelog/changelog.json`](https://github.com/mongodb/openapi/blob/main/changelog/changelog.json) | live |
| [`openai-models`](https://github.com/patch-it-dev/patch-it-plugins/blob/main/providers/openai-models.md) | OpenAI | AI models | Model deprecations and shutdown dates (Chat Completions and Responses text models) | `retirements` | [OpenAI deprecations](https://developers.openai.com/api/docs/deprecations) | live |
| [`anthropic-models`](https://github.com/patch-it-dev/patch-it-plugins/blob/main/providers/anthropic-models.md) | Anthropic | AI models | Model deprecations and retirement dates (Messages API models) | `retirements` | [Anthropic model deprecations](https://platform.claude.com/docs/en/about-claude/model-deprecations) | live |
Expand Down
24 changes: 24 additions & 0 deletions plugins/openapi/elasticsearch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package openapi

import "github.com/patch-it-dev/patch-it-plugins/diff"

// elasticsearchDiff reads Elastic's spec like Supabase's: deprecations by the
// `deprecated` flag, quoting the whole operation, and path templates matched.
var elasticsearchDiff = diff.Options{PathTemplates: true, QuoteDeprecatedOperation: true}

// elasticsearchKeep keeps operation deprecations, and removals of operations
// marked deprecated first. Over the minor versions with an OpenAPI output
// (8.14 to 9.6, compared in version order) that's 23 deprecations and 3
// removals, all in 9.0: GET and POST /{index}/_knn_search, and POST
// /{index}/_unfreeze ("Remove unfreeze REST endpoint", Elastic's 9.0.0
// breaking changes). The 31 other removals weren't deprecated first and
// mostly weren't removals from Elasticsearch:
//
// - APIs a later minor of the older major had and the newer major's
// branch, cut earlier, didn't yet: 8.19 to 9.0 drops
// /_data_stream/{name}/_options, which is back in 9.1;
// - APIs Elastic documents as not for direct use ("designed for indirect
// use by Elasticsearch Service, Elastic Cloud Enterprise, and Elastic
// Cloud on Kubernetes. Direct use is not supported."), left out of the
// spec from 9.2: /_autoscaling, /_nodes/{node_id}/shutdown.
var elasticsearchKeep = deprecationsOnly(elasticsearchDiff)
Loading
Loading