Skip to content

Commit

Permalink
fix: get document version from filename to fix backstage cmds
Browse files Browse the repository at this point in the history
Previously we were reading the document version from the top level
x-snyk-api-version extension. This extension does not exist on documents
past the pivot date as versions are done per endpoint. This resulted in
all backstage commands failing. (eg vervet backstage update-catalog)

This information is available in the file path, so instead of poluting
our new documents with this extension we can read it from there instead.
This change is backwards compatable with older specs.
  • Loading branch information
jgresty committed Oct 17, 2024
1 parent 4fd246d commit 0b1ce42
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 3 additions & 5 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,9 @@ func (d *Document) LoadReference(relPath, refPath string, target interface{}) (_

// Version returns the version of the document.
func (d *Document) Version() (Version, error) {
vs, err := ExtensionString(d.Extensions, ExtSnykApiVersion)
if err != nil {
return Version{}, err
}
return ParseVersion(vs)
versionDir := filepath.Dir(d.path)
versionStr := filepath.Base(versionDir)
return ParseVersion(versionStr)
}

// Lifecycle returns the lifecycle of the document.
Expand Down
10 changes: 10 additions & 0 deletions document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ func TestNewDocumentFile(t *testing.T) {
c.Assert(doc.Components.Schemas["HelloWorld"], qt.Not(qt.IsNil))
c.Assert(doc.Validate(context.TODO()), qt.IsNil)
}

func TestDocumentVersion(t *testing.T) {
c := qt.New(t)
doc, err := vervet.NewDocumentFile(testdata.Path("resources/_examples/hello-world/2021-06-01/spec.yaml"))
c.Assert(err, qt.IsNil)

version, err := doc.Version()
c.Assert(err, qt.IsNil)
c.Assert(version, qt.Equals, vervet.MustParseVersion("2021-06-01"))
}

0 comments on commit 0b1ce42

Please sign in to comment.