Repository navigation
skills: report the release tag as the release source's catalog version #8191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9b905f1
c952ec0
c519cfe
59e43af
ad95dc5
bfd4c24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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 | ||
| } | ||
|
|
@@ -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{ | ||
|
|
@@ -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 { | ||
| // semver.Canonical drops build metadata, so compare without it. | ||
| base, _, _ := strings.Cut(ref, "+") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit]
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
|
@@ -241,3 +244,64 @@ func TestReleaseSourceRejectsManifestPathsOutsideRelease(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| // serveReleases serves one tree per release tag under the base URL. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit]
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 59e43af: |
||
| 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) | ||
| }) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. Note: the earlier bot comment said that this tree has no bundled source. That is not correct.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nice to have]
installer.DisplaySkillsVersioninlibs/aitools/installer/installer.goalready turns a skills ref into a display version, but it strips thevfrom every ref, so the two now disagree. WithDATABRICKS_SKILLS_REF=vendor-fix, this catalog reportsvendor-fixwhileaitools installshowsendor-fix.installeralready importslibs/skills, so could this become the exported helper, withDisplaySkillsVersioncalling it? If they're meant to differ, the doc comment should say how.There was a problem hiding this comment.
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
DisplaySkillsVersionalone: it feedsaitoolsinstall 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. ThereleaseVersioncomment now says the two differ on purpose. MakingDisplaySkillsVersioncall this helper (which also fixesvendor-fixshowing asendor-fix) is better done as its own change with its own tests.