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
5 changes: 3 additions & 2 deletions plugins/changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type File struct {
TagPrefix string // a release's git tag is TagPrefix + its version: "v" for v0.86.0
Path string // the changelog's path in the repo
Notes bool // the changelog is each GitHub release's notes, not a file
Pins string // the source a release's section says it pins a version of ("stripe/api"; pins.go), or ""
Tag string // pinned: the release's tag: "2.0.0", "v0.86.0"
Release string // pinned: the release as the changelog heads it: "2.0.0"
}
Expand Down Expand Up @@ -317,15 +318,15 @@ func (p Plugin) ListVersions(ctx context.Context, env plugin.Env) ([]plugin.Vers
vs, err = versions.NPMReleases(ctx, env.Get, f.Package)
default:
// No registry patch-it lists from: the repo's release tags.
return versions.GitHubTags(ctx, env.Get, f.Repo, regexp.MustCompile(`^`+regexp.QuoteMeta(f.TagPrefix)+`(\d+(?:\.\d+)+)$`))
vs, err = versions.GitHubTags(ctx, env.Get, f.Repo, regexp.MustCompile(`^`+regexp.QuoteMeta(f.TagPrefix)+`(\d+(?:\.\d+)+)$`))
}
if err != nil {
return nil, err
}
for i := range vs {
vs[i].Ref = f.TagPrefix + vs[i].ID
}
return vs, nil
return f.pinned(ctx, env, vs), nil
}

// ReadAt fetches the changelog at the release's tag.
Expand Down
108 changes: 108 additions & 0 deletions plugins/changelog/pins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package changelog

import (
"context"
"log"
"regexp"
"strings"

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

// What a release pins: a sentence in its own section of the changelog that
// names the version of another source it pins, in the words its SDK uses
// (Stripe's, checked 2026-11-20 in all seven SDKs' CHANGELOG.md):
//
// This release changes the pinned API version to `2026-11-18.endive`.
// This release **doesn't** change the pinned API version; it still uses `2026-05-27.dahlia`.
// This release **does not** change the pinned API version. It's still `2026-06-24.dahlia`.
// This release uses the same pinned API version to `2026-03-25.dahlia` as the last major release.
//
// A release that says nothing pins nothing here, even though it keeps the
// release before's: only what a release states is read. Two different
// versions in one section are none (precision over recall). Older wordings
// ("Major version release for API version 2022-08-01", "Move to API version
// …") don't say "pinned" and aren't read.
var statedPin = []*regexp.Regexp{
regexp.MustCompile("pinned API version to `(" + apiVersion + ")`"),
regexp.MustCompile("change the pinned API version[.;] (?:[Ii]t's still|it still uses) `(" + apiVersion + ")`"),
}

// apiVersion is a dated API version, with the release's name when it has
// one: "2026-11-18.endive", "2022-08-01".
const apiVersion = `\d{4}-\d{2}-\d{2}(?:\.[a-z]+)?`

// pins are the versions each release of a Markdown changelog states it
// pins, by release as its heading names it.
func pins(file []byte) map[string]string {
out, conflict := map[string]string{}, map[string]bool{}
release, fenced := "", false
for _, l := range strings.Split(strings.ReplaceAll(string(file), "\r\n", "\n"), "\n") {
// A code block first: a "# comment" in an example isn't a heading.
// A release's heading closes a block its section left open.
if m := mdRelease.FindStringSubmatch(l); m != nil && strings.HasPrefix(l, "## ") {
release, fenced = m[1], false
continue
}
if strings.HasPrefix(strings.TrimSpace(l), "```") {
fenced = !fenced
continue
}
if fenced {
continue
}
if strings.HasPrefix(l, "## ") || strings.HasPrefix(l, "# ") {

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: the heading check runs before the fence check, so a # comment line inside a fenced Ruby/Python/shell example (common in migration notes) ends the current release: any pin sentence after that code block in the same section is lost. That loses a pin rather than inventing one, so precision is fine. Moving the fence toggle above the heading check fixes it:

if strings.HasPrefix(strings.TrimSpace(l), "```") {
	fenced = !fenced
	continue
}
if fenced {
	continue
}
if strings.HasPrefix(l, "## ") || strings.HasPrefix(l, "# ") { ... }

(A changelog that never closes a fence would then hide every heading after it. If you care about that, also reset fenced on a ## heading.)

release = ""
if m := mdRelease.FindStringSubmatch(l); m != nil {
release = m[1]
}
continue
}
if release == "" {
continue
}
l = strings.ReplaceAll(l, "**", "")
for _, re := range statedPin {
for _, m := range re.FindAllStringSubmatch(l, -1) {
if have, ok := out[release]; ok && have != m[1] {
conflict[release] = true
}
out[release] = m[1]
}
}
}
for r := range conflict {
delete(out, r)
}
return out
}

// pinned sets each version's MapsTo to what its section states it pins (f.Pins),
// read from the changelog at the newest version's tag, which has every
// earlier release's section, or at the one before when that tag can't be read
// (a release on the registry before its tag is pushed). A changelog that
// can't be fetched leaves the versions as listed: what they pin is read on a
// later listing.
func (f File) pinned(ctx context.Context, env plugin.Env, vs []plugin.Version) []plugin.Version {
if f.Pins == "" || f.Notes || len(vs) == 0 {
return vs
}
var page []byte
var err error
for i := len(vs) - 1; i >= 0 && i >= len(vs)-2; i-- {
if page, err = env.Get(ctx, f.At(vs[i].ID).URL()); err == nil {
break
}
}
if err != nil {
log.Printf("%s: what its releases pin isn't read this time: %v", f.API, err)
return vs
}
stated := pins(page)
for i := range vs {
if v, ok := stated[vs[i].ID]; ok {
vs[i].MapsTo = plugin.Pin{API: f.Pins, Version: v}
}
}
return vs
}
70 changes: 70 additions & 0 deletions plugins/changelog/pins_stripe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package changelog

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

"github.com/patch-it-dev/patch-it-plugins/core/kit/kittest"
"github.com/patch-it-dev/patch-it-plugins/core/plugin"
"github.com/patch-it-dev/patch-it-plugins/core/versions"
_ "github.com/patch-it-dev/patch-it-plugins/plugins/openapi" // registers stripe/api
)

// Every pin the seven Stripe SDK sources read from their saved changelogs is
// a version stripe/api lists, from its saved release listing
// (../openapi/adapter/testdata/versions/): the pin and the listing name an API
// version the same way, so a feed query on an SDK's versions finds the API's.
func TestStripePinsAreVersionsStripeAPILists(t *testing.T) {
listing, err := os.ReadFile("../openapi/adapter/testdata/versions/stripe-openapi-releases.json")
if err != nil {
t.Fatal(err)
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/repos/stripe/openapi/releases" || r.URL.Query().Get("page") != "1" {
t.Errorf("unexpected request %s", r.URL)
http.NotFound(w, r)
return
}
w.Write(listing)
}))
defer srv.Close()
old := versions.GitHubAPI
versions.GitHubAPI = srv.URL
defer func() { versions.GitHubAPI = old }()

api, ok := plugin.Lookup("stripe/api")
if !ok {
t.Fatal("stripe/api isn't registered")
}
vs, err := api.(plugin.Versioned).ListVersions(context.Background(), plugin.Env{})
if err != nil {
t.Fatal(err)
}
listed := map[string]bool{}
for _, v := range vs {
listed[v.ID] = true
}

n := 0
for _, c := range []struct {
file File
tag string
}{
{StripePython, "17.1.0"}, {StripeNode, "23.2.0"}, {StripeGo, "87.2.0"}, {StripeJava, "34.1.0"},
{StripeRuby, "21.1.0"}, {StripeDotnet, "53.2.0"}, {StripePHP, "23.1.0"},
} {
for release, pin := range pins(kittest.Read(t, "testdata/pins/"+strings.ReplaceAll(c.file.API, "/", "-")+"-"+c.tag+"-CHANGELOG.md")) {
n++
if c.file.Pins != "stripe/api" || !listed[pin] {
t.Errorf("%s %s pins %s %s, which stripe/api doesn't list", c.file.API, release, c.file.Pins, pin)
}
}
}
if n == 0 {
t.Fatal("no pins read")
}
}
133 changes: 133 additions & 0 deletions plugins/changelog/pins_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package changelog

import (
"context"
"errors"
"maps"
"strings"
"testing"

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

// Each Stripe SDK's changelog at its newest tag, trimmed to the releases
// that state their pinned API version in each wording the SDKs use, and one
// that states none (testdata/README.md): what each release pins, exactly as
// stated, and nothing for the release that doesn't say.
func TestStripeReleasesStateWhatTheyPin(t *testing.T) {
for _, c := range []struct {
file File
tag string
want map[string]string
}{
{StripePython, "17.1.0", map[string]string{"17.1.0": "2026-11-18.endive"}}, // 17.0.0 says nothing
{StripeNode, "23.2.0", map[string]string{"23.2.0": "2026-11-18.endive", "22.0.0": "2026-03-25.dahlia"}},
{StripeGo, "87.2.0", map[string]string{"87.2.0": "2026-11-18.endive", "86.0.0": "2026-05-27.dahlia"}},
{StripeJava, "34.1.0", map[string]string{"34.1.0": "2026-10-28.endive", "33.0.0": "2026-05-27.dahlia"}},
{StripeRuby, "21.1.0", map[string]string{"21.1.0": "2026-11-18.endive"}},
{StripeDotnet, "53.2.0", map[string]string{"53.2.0": "2026-11-18.endive", "52.0.0": "2026-05-27.dahlia"}},
{StripePHP, "23.1.0", map[string]string{"23.1.0": "2026-11-18.endive", "21.0.0": "2026-06-24.dahlia"}},
} {
t.Run(c.file.API, func(t *testing.T) {
if c.file.Pins != "stripe/api" {
t.Errorf("Pins %q, want stripe/api", c.file.Pins)
}
got := pins(kittest.Read(t, "testdata/pins/"+strings.ReplaceAll(c.file.API, "/", "-")+"-"+c.tag+"-CHANGELOG.md"))
if !maps.Equal(got, c.want) {
t.Errorf("got %v, want %v", got, c.want)
}
})
}
}

// Listed, each version carries what its own section states it pins, read
// from the changelog at the newest version's tag; a version whose section
// says nothing pins nothing.
func TestListedVersionsCarryWhatTheyPin(t *testing.T) {
env := kittest.Env(t, kittest.Pages{
"https://api.github.com/repos/stripe/stripe-go/tags?per_page=100&page=1": "testdata/versions/stripe-go-tags-pins.json",
StripeGo.At("87.2.0").URL(): "testdata/pins/stripe-go-87.2.0-CHANGELOG.md",
})
vs, err := Plugin{File: StripeGo}.ListVersions(context.Background(), env)
if err != nil {
t.Fatal(err)
}
var got []string
for _, v := range vs {
got = append(got, v.ID+"="+v.MapsTo.API+"@"+v.MapsTo.Version)
}
want := "86.0.0=stripe/api@2026-05-27.dahlia 86.4.2=@ 87.2.0=stripe/api@2026-11-18.endive"
if strings.Join(got, " ") != want {
t.Errorf("got %s\nwant %s", strings.Join(got, " "), want)
}
}

// A changelog that can't be fetched at the newest two tags leaves the listing
// as it is, pinning nothing; a source without Pins never fetches it.
func TestPinsAreReadOnlyWhenTheyCanBe(t *testing.T) {
tags := kittest.Read(t, "testdata/versions/stripe-go-tags-pins.json")
var fetched []string
env := plugin.Env{Get: func(_ context.Context, url string) ([]byte, error) {
fetched = append(fetched, url)
if strings.Contains(url, "/tags?") {
return tags, nil
}
return nil, errors.New("503")
}}
vs, err := Plugin{File: StripeGo}.ListVersions(context.Background(), env)
if err != nil || len(vs) != 3 {
t.Fatalf("%v, %d versions", err, len(vs))
}
if len(fetched) != 3 { // the tags, then the newest two releases' changelogs
t.Errorf("fetched %v", fetched)
}
for _, v := range vs {
if !v.MapsTo.IsZero() {
t.Errorf("%s pins %v with no changelog read", v.ID, v.MapsTo)
}
}
unpinned := StripeGo
unpinned.Pins = ""
fetched = nil
if _, err := (Plugin{File: unpinned}).ListVersions(context.Background(), env); err != nil || len(fetched) != 1 {
t.Errorf("without Pins: %v, fetched %v", err, fetched)
}
}

// Two different versions in one release's section are none; a statement in
// a code block, or before the first release, is nobody's.
func TestAnUnsurePinIsNone(t *testing.T) {
got := pins([]byte("# Changelog\nThis release changes the pinned API version to `2020-01-01`.\n" +
"## 2.0.0 - 2026-01-02\nThis release changes the pinned API version to `2026-01-01.a`.\nThis release changes the pinned API version to `2026-01-02.b`.\n" +
"## 1.1.0 - 2026-01-01\n```\nThis release changes the pinned API version to `2025-12-01.a`.\n```\n" +
"## 1.0.0 - 2025-12-01\n* This release changes the pinned API version to `2025-11-01.a`.\n"))
if want := map[string]string{"1.0.0": "2025-11-01.a"}; !maps.Equal(got, want) {
t.Errorf("got %v, want %v", got, want)
}
// A "# comment" in a code example doesn't end the release's section.
got = pins([]byte("## 3.0.0 - 2026-02-01\n```sh\n# upgrade\n```\nThis release changes the pinned API version to `2026-02-01.c`.\n"))
if want := map[string]string{"3.0.0": "2026-02-01.c"}; !maps.Equal(got, want) {
t.Errorf("after a code block: got %v, want %v", got, want)
}
}

// When the newest release's tag can't be read yet, the one before's
// changelog says what the earlier releases pin.
func TestPinsFallBackToTheTagBefore(t *testing.T) {
tags := kittest.Read(t, "testdata/versions/stripe-go-tags-pins.json")
file := kittest.Read(t, "testdata/pins/stripe-go-87.2.0-CHANGELOG.md")
env := plugin.Env{Get: func(_ context.Context, url string) ([]byte, error) {
switch {
case strings.Contains(url, "/tags?"):
return tags, nil
case url == StripeGo.At("86.4.2").URL():
return file, nil
}
return nil, errors.New("404")
}}
vs, err := Plugin{File: StripeGo}.ListVersions(context.Background(), env)
if err != nil || len(vs) != 3 || vs[0].MapsTo.Version != "2026-05-27.dahlia" {
t.Errorf("%v %+v", err, vs)
}
}
18 changes: 10 additions & 8 deletions plugins/changelog/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ func init() {

// Stripe's SDKs: one CHANGELOG.md each, "## <a id="X-Y-Z"></a>X.Y.Z - date"
// (older ones "## X.Y.Z - date"), every breaking bullet marked "⚠️"; each
// release tagged vX.Y.Z.
// release tagged vX.Y.Z. A release that changes the API version the SDK
// pins says so in its section, "This release changes the pinned API version
// to `2026-11-18.endive`." (pins.go): its MapsTo is that stripe/api version.
var (
StripePython = File{API: "stripe/python", Package: "stripe", Ecosystem: "pypi", Module: "stripe",
Repo: "stripe/stripe-python", TagPrefix: "v", Path: "CHANGELOG.md"}
Repo: "stripe/stripe-python", TagPrefix: "v", Path: "CHANGELOG.md", Pins: "stripe/api"}
StripeNode = File{API: "stripe/node", Package: "stripe", Ecosystem: "npm",
Repo: "stripe/stripe-node", TagPrefix: "v", Path: "CHANGELOG.md"}
Repo: "stripe/stripe-node", TagPrefix: "v", Path: "CHANGELOG.md", Pins: "stripe/api"}
StripeGo = File{API: "stripe/go", Package: "github.com/stripe/stripe-go", Ecosystem: "go",
Repo: "stripe/stripe-go", TagPrefix: "v", Path: "CHANGELOG.md"}
Repo: "stripe/stripe-go", TagPrefix: "v", Path: "CHANGELOG.md", Pins: "stripe/api"}
StripeJava = File{API: "stripe/java", Package: "com.stripe:stripe-java", Ecosystem: "maven",
Repo: "stripe/stripe-java", TagPrefix: "v", Path: "CHANGELOG.md"}
Repo: "stripe/stripe-java", TagPrefix: "v", Path: "CHANGELOG.md", Pins: "stripe/api"}
StripeRuby = File{API: "stripe/ruby", Package: "stripe", Ecosystem: "rubygems",
Repo: "stripe/stripe-ruby", TagPrefix: "v", Path: "CHANGELOG.md"}
Repo: "stripe/stripe-ruby", TagPrefix: "v", Path: "CHANGELOG.md", Pins: "stripe/api"}
StripeDotnet = File{API: "stripe/dotnet", Package: "Stripe.net", Ecosystem: "nuget",
Repo: "stripe/stripe-dotnet", TagPrefix: "v", Path: "CHANGELOG.md"}
Repo: "stripe/stripe-dotnet", TagPrefix: "v", Path: "CHANGELOG.md", Pins: "stripe/api"}
StripePHP = File{API: "stripe/php", Package: "stripe/stripe-php", Ecosystem: "packagist",
Repo: "stripe/stripe-php", TagPrefix: "v", Path: "CHANGELOG.md"}
Repo: "stripe/stripe-php", TagPrefix: "v", Path: "CHANGELOG.md", Pins: "stripe/api"}
)
16 changes: 15 additions & 1 deletion plugins/changelog/testdata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,25 @@ Read with `Plugin{File: ...}` and `Notes: true` (`sdks.go`): an SDK whose change
| `github-octokit-net-0.5.2-release.json` | https://api.github.com/repos/octokit/octokit.net/releases/tags/v0.5.2 | `4b61105` | 2026-11-20 | MIT (octokit.net); trimmed to `html_url`, `tag_name`, `name`, `published_at` and `body`, re-indented, values unchanged | golden |
| `github-octokit-net-0.5.1-release.json` | https://api.github.com/repos/octokit/octokit.net/releases/tags/v0.5.1 | `8a9e828` | 2026-11-20 | MIT (octokit.net); trimmed to `html_url`, `tag_name`, `name`, `published_at` and `body`, re-indented, values unchanged | drop test |

## What a release pins

Read by `pins.go`; `pins_test.go` checks each release's stated pin. Every file is the SDK's `CHANGELOG.md` at its newest release tag, saved with `curl` on 2026-11-20, **trimmed** to the sections listed, each kept whole and unchanged.

| File | URL | Tag commit | Sections kept | License | Quote |
|---|---|---|---|---|---|
| `pins/stripe-python-17.1.0-CHANGELOG.md` | https://raw.githubusercontent.com/stripe/stripe-python/v17.1.0/CHANGELOG.md | `2bbff43` | 17.1.0 (changes the pin), 17.0.0 (says nothing) | MIT (stripe-python) | "This release changes the pinned API version to `2026-11-18.endive`." |
| `pins/stripe-node-23.2.0-CHANGELOG.md` | https://raw.githubusercontent.com/stripe/stripe-node/v23.2.0/CHANGELOG.md | `dd92429` | 23.2.0, 23.0.1 (says nothing), 22.0.0 ("uses the same pinned API version to") | MIT (stripe-node) | "This release uses the same pinned API version to `2026-03-25.dahlia` as the last major release." |
| `pins/stripe-go-87.2.0-CHANGELOG.md` | https://raw.githubusercontent.com/stripe/stripe-go/v87.2.0/CHANGELOG.md | `c97713a` | 87.2.0, 86.4.2 (says nothing), 86.0.0 ("doesn't change") | MIT (stripe-go) | "This release **doesn't** change the pinned API version; it still uses `2026-05-27.dahlia`." |
| `pins/stripe-java-34.1.0-CHANGELOG.md` | https://raw.githubusercontent.com/stripe/stripe-java/v34.1.0/CHANGELOG.md | `d1dcf1a` | 34.1.0, 33.4.2 (says nothing), 33.0.0 ("doesn't change") | MIT (stripe-java) | "This release changes the pinned API version to `2026-10-28.endive`." |
| `pins/stripe-ruby-21.1.0-CHANGELOG.md` | https://raw.githubusercontent.com/stripe/stripe-ruby/v21.1.0/CHANGELOG.md | `6605e66` | 21.1.0, 21.0.0 (says nothing) | MIT (stripe-ruby) | "This release changes the pinned API version to `2026-11-18.endive`." |
| `pins/stripe-dotnet-53.2.0-CHANGELOG.md` | https://raw.githubusercontent.com/stripe/stripe-dotnet/v53.2.0/CHANGELOG.md | `a32d3eb` | 53.2.0, 52.4.2 (says nothing), 52.0.0 ("doesn't change") | Apache-2.0 (stripe-dotnet) | "This release changes the pinned API version to `2026-11-18.endive`." |
| `pins/stripe-php-23.1.0-CHANGELOG.md` | https://raw.githubusercontent.com/stripe/stripe-php/v23.1.0/CHANGELOG.md | `6694b4d` | 23.1.0, 23.0.0 (says nothing), 21.0.0 ("does not change") | MIT (stripe-php) | "This release **does not** change the pinned API version. It's still `2026-06-24.dahlia`." |

## Version lists

| File | URL | Saved | License | Use |
|---|---|---|---|---|
| `versions/stripe-go-tags.json`, `versions/octokit-rest-releases.json` | https://api.github.com/repos/stripe/stripe-go/tags, https://api.github.com/repos/octokit/rest.js/releases (shape only) | written for the test, 2026-11-20 | not saved copies: a few real tag names and releases in the shape of GitHub's API (a pre-release, a draft and a tag of another series among them) | for the version-listing test only (tags for an ecosystem with no registry lister, releases for release notes); no golden reads them |
| `versions/stripe-go-tags.json`, `versions/stripe-go-tags-pins.json`, `versions/octokit-rest-releases.json` | https://api.github.com/repos/stripe/stripe-go/tags, https://api.github.com/repos/octokit/rest.js/releases (shape only) | written for the test, 2026-11-20 | not saved copies: a few real tag names and releases in the shape of GitHub's API (a pre-release, a draft and a tag of another series among them) | for the version-listing test only (tags for an ecosystem with no registry lister, releases for release notes); no golden reads them |
| `pypi-urllib3-excerpt.json` | https://pypi.org/pypi/urllib3/json (shape only) | written for the test, 2026-11-20 | not a saved copy: four releases in the shape of PyPI's JSON API (`releases` → files with `upload_time_iso_8601`, `yanked`), dates approximate, a pre-release among them | for the version-listing test only (ListVersions, consecutive pairs); no golden reads it |

## JSON changelogs
Expand Down
Loading
Loading