Skip to content
Open
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
39 changes: 32 additions & 7 deletions libs/skills/release_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/url"
"strings"
"time"

"golang.org/x/mod/semver"
)

// releaseTimeout bounds one call to the published release, the release lookup
Expand All @@ -30,17 +32,24 @@ func NewReleaseSource() Source {
func (releaseSource) Catalog(ctx context.Context) (*Catalog, error) {
ctx, cancel := context.WithTimeout(ctx, releaseTimeout)
defer cancel()
src, err := openRelease(ctx)
src, version, err := openRelease(ctx)
if err != nil {
return nil, err
}
catalog, err := src.Catalog(ctx)
if err != nil {
return nil, err
}
return src.Catalog(ctx)
// The manifest's version field is the manifest schema version, the same in
// every release, so the release tag names the release instead.
catalog.Version = version
return catalog, nil
}

func (releaseSource) ReadFile(ctx context.Context, skill, file string) ([]byte, error) {
ctx, cancel := context.WithTimeout(ctx, releaseTimeout)
defer cancel()
src, err := openRelease(ctx)
src, _, err := openRelease(ctx)
if err != nil {
return nil, err
}
Expand All @@ -50,11 +59,13 @@ func (releaseSource) ReadFile(ctx context.Context, skill, file string) ([]byte,
// openRelease resolves the release ref with GetSkillsRef and returns a Source
// that reads that release's files over HTTP. It does not use the installer's
// embedded-version fallback: a failed lookup is ErrSourceUnreachable, and the
// caller decides what to serve instead.
func openRelease(ctx context.Context) (Source, error) {
// caller decides what to serve instead. It also returns the release version: the
// resolved ref without its leading "v" when the ref is a semantic version, and
// the ref as given otherwise (a branch or commit).
func openRelease(ctx context.Context) (Source, string, error) {
ref, _, err := GetSkillsRef(ctx)
if err != nil {
return nil, unreachable(err)
return nil, "", unreachable(err)
}
base := GetSkillsBaseURL(ctx) + "/" + ref
return &fsSource{
Expand All @@ -64,7 +75,21 @@ func openRelease(ctx context.Context) (Source, error) {
return fetchRelease(ctx, releaseFileURL(base, name), name == manifestFile)
},
label: base,
}, nil
}, releaseVersion(ref), nil
}

// releaseVersion strips the leading "v" from a full vMAJOR.MINOR.PATCH tag,
// including one with +build metadata. Any other ref, such as a branch named
// vendor-fix or v2, is returned as given. This differs on purpose from the
// installer's display helper, which strips one leading "v" from any ref, so
// vendor-fix would become endor-fix.
func releaseVersion(ref string) string {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[nice to have] installer.DisplaySkillsVersion in libs/aitools/installer/installer.go already turns a skills ref into a display version, but it strips the v from every ref, so the two now disagree. With DATABRICKS_SKILLS_REF=vendor-fix, this catalog reports vendor-fix while aitools install shows endor-fix. installer already imports libs/skills, so could this become the exported helper, with DisplaySkillsVersion calling it? If they're meant to differ, the doc comment should say how.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Right, they disagree for non-tag refs. I left DisplaySkillsVersion alone: it feeds aitools install state, update and downgrade checks, which are not behind the skills preview gate, so changing it would alter existing behaviour in a PR meant to be gated. The releaseVersion comment now says the two differ on purpose. Making DisplaySkillsVersion call this helper (which also fixes vendor-fix showing as endor-fix) is better done as its own change with its own tests.

// semver.Canonical drops build metadata, so compare without it.
base, _, _ := strings.Cut(ref, "+")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[nit] releaseVersion removes the build part before the check, but it does not check that the full ref is valid. For v1.2.3+ (an empty build part, which is not valid semver), it returns 1.2.3+. The doc above says that only a full tag loses its "v". Only a mistyped DATABRICKS_SKILLS_REF gives this input, so the risk is low. Add semver.IsValid(ref) && to the condition.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in bfd4c24: the condition now starts with semver.IsValid(ref), so v1.2.3+ is returned as given. Added it as a test case.

if semver.IsValid(ref) && semver.Canonical(base) == base {
return strings.TrimPrefix(ref, "v")
}
return ref
}

// releaseFileURL returns the URL of the file name in the release at base.
Expand Down
70 changes: 67 additions & 3 deletions libs/skills/release_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ func serveRelease(t *testing.T, h http.Handler) {
// serveReleaseFS serves fsys as the release v1.2.0.
func serveReleaseFS(t *testing.T, fsys fs.FS) {
t.Helper()
serveRelease(t, http.StripPrefix("/v1.2.0/", http.FileServer(http.FS(fsys))))
serveReleases(t, map[string]fs.FS{"v1.2.0": fsys})
t.Setenv("DATABRICKS_SKILLS_REF", "v1.2.0")
}

func TestReleaseSourceCatalog(t *testing.T) {
serveReleaseFS(t, os.DirFS("testdata/release"))
// The fixture manifest says 1.2.0, so a different tag shows the tag is used.
serveReleases(t, map[string]fs.FS{"v3.4.5": os.DirFS("testdata/release")})
t.Setenv("DATABRICKS_SKILLS_REF", "v3.4.5")

catalog, err := NewReleaseSource().Catalog(t.Context())
require.NoError(t, err)
assert.Equal(t, "1.2.0", catalog.Version)
assert.Equal(t, "3.4.5", catalog.Version)
assert.Equal(t, "Use the catalog before you read a skill.\n", lf(catalog.Rules))
require.Len(t, catalog.Skills, 2)
assert.Equal(t, "alpha", catalog.Skills[0].Name)
Expand Down Expand Up @@ -241,3 +244,64 @@ func TestReleaseSourceRejectsManifestPathsOutsideRelease(t *testing.T) {
})
}
}

// serveReleases serves one tree per release tag under the base URL.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[nit] serveReleases repeats the StripPrefix + FileServer setup that serveRelease/serveReleaseFS (lines 22-34) already do for a single tag. Could serveReleaseFS take the tag instead, or could those two helpers be built on serveReleases, so there's one way to serve a test release?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in 59e43af: serveReleaseFS now calls serveReleases with the single tag v1.2.0 and sets the ref, so the StripPrefix + FileServer setup lives in one place. serveRelease stays for the tests that need a custom handler.

func serveReleases(t *testing.T, releases map[string]fs.FS) {
t.Helper()
mux := http.NewServeMux()
for tag, fsys := range releases {
mux.Handle("/"+tag+"/", http.StripPrefix("/"+tag+"/", http.FileServer(http.FS(fsys))))
}
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
t.Setenv("DATABRICKS_SKILLS_BASE_URL", server.URL)
}

func releaseWithSkillVersion(skillVersion string) fs.FS {
return fstest.MapFS{
"manifest.json": {Data: []byte(`{"version": "2", "skills": {"a": {"version": "` + skillVersion + `", "files": ["SKILL.md"]}}}`)},
"skills/a/SKILL.md": {Data: []byte("# A\n")},
}
}

func TestReleaseSourceVersionIsReleaseTagNotManifestVersion(t *testing.T) {
serveReleases(t, map[string]fs.FS{"v1.2.0": releaseWithSkillVersion("1.0.0"), "v1.3.0": releaseWithSkillVersion("1.1.0")})

t.Setenv("DATABRICKS_SKILLS_REF", "v1.2.0")
first, err := NewReleaseSource().Catalog(t.Context())
require.NoError(t, err)

t.Setenv("DATABRICKS_SKILLS_REF", "v1.3.0")
second, err := NewReleaseSource().Catalog(t.Context())
require.NoError(t, err)

assert.Equal(t, "1.2.0", first.Version)
assert.Equal(t, "1.3.0", second.Version)
}

func TestReleaseSourceVersionDropsLeadingV(t *testing.T) {
release := releaseWithSkillVersion("1.0.0")
tests := map[string]string{
"v1.2.0": "1.2.0",
"v1.2.3+build.1": "1.2.3+build.1",
"v1.2.3-rc.1": "1.2.3-rc.1",
"v1.2.3+": "v1.2.3+",
"1.2.0": "1.2.0",
"vendor-fix": "vendor-fix",
"v2": "v2",
}
releases := map[string]fs.FS{}
for ref := range tests {
releases[ref] = release
}
serveReleases(t, releases)

for ref, want := range tests {
t.Run(ref, func(t *testing.T) {
t.Setenv("DATABRICKS_SKILLS_REF", ref)
catalog, err := NewReleaseSource().Catalog(t.Context())
require.NoError(t, err)
assert.Equal(t, want, catalog.Version)
})
}
}
5 changes: 4 additions & 1 deletion libs/skills/skills.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ type Catalog struct {
// paths, which the catalog does not check yet.
Skills []Skill

// Version is the release version.
// Version is the release version: the release tag without its leading "v"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[nice to have] This doc names two cases: the published release and a local folder. The bundled copy is a third case. NewBundledSource in libs/skills/bundled_source.go builds its catalog with NewFSSource, so it also reports the manifest's version field. A reader can think that every source except a local folder reports a release tag. Add the bundled copy to the manifest-version case. For example: "A local folder and the bundled copy report the manifest's version field."

Note: the earlier bot comment said that this tree has no bundled source. That is not correct. libs/skills/bundled_source.go is on main since #8077, and this file still names the bundled copy in the package doc and in the FetchedAt and Stale docs.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in bfd4c24: the Catalog.Version doc now says a local folder and the bundled copy report the manifest's version field.

// for the published release (a pinned DATABRICKS_SKILLS_REF that is a
// branch or commit is reported as given). A local folder and the
// bundled copy have no release tag and report the manifest's version field.
Version string

// Label says where the catalog came from.
Expand Down
Loading