Skip to content

Commit

Permalink
fix: Use minor version for version dropdown href (#673)
Browse files Browse the repository at this point in the history
* docs: Add catalog-v2.json endpoint to site

This endpoint provides a new schema for the catalog.json file.
The motivation for this change is to provide access to the latest
patch version for each major/minor version for each function.
A new endpoint is added in order to avoid breaking backwards
compatibility with existing consumers of the catalog.json
endpoint, such as older versions of kpt already in the wild.

Issues: kptdev/kpt#2609

* docs: Display patch versions in site catalog

Consumes the new catalog-v2.json endpoint which provides the
latest patch versions. The latest patch version is now presented
in the version dropdown for the function.

Issues: kptdev/kpt#2609
  • Loading branch information
sdowell authored Dec 10, 2021
1 parent 508ea75 commit 5c26848
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 5 deletions.
122 changes: 122 additions & 0 deletions scripts/generate_catalog/generate_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,23 @@ func main() {
os.Exit(1)
}

err = writeExampleIndexV2(curatedFns, source, dest)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}

err = writeExampleIndex(contribFns, source, filepath.Join(dest, "contrib"))
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}

err = writeExampleIndexV2(contribFns, source, filepath.Join(dest, "contrib"))
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}

type function struct {
Expand Down Expand Up @@ -109,10 +121,18 @@ type metadata struct {
Hidden bool
}

type catalogEntry struct {
// LatestPatchVersion is the latest Patch semver for a MajorMinor semver
LatestPatchVersion string
// Examples maps exampleName->example
Examples map[string]example
}

var (
// Match start of a version such as v1.9.1
branchSemverPrefix = regexp.MustCompile(`[-\w]*\/(v\d*\.\d*)`)
functionDirPrefix = regexp.MustCompile(`.+/functions/`)
patchTagPattern = regexp.MustCompile(`.*(go|ts)\/[-\w]*\/(v\d*\.\d*\.\d*)`)
)

func getBranches() ([]string, error) {
Expand Down Expand Up @@ -145,6 +165,68 @@ func getBranches() ([]string, error) {
return verBranches, err
}

type functionRelease struct {
FunctionName string
MajorMinor string
}

type patchVersionReader struct {
latestPatchVersions map[functionRelease]string
}

// NewPatchVersionReader constructs a new patchVersionReader
func NewPatchVersionReader() (*patchVersionReader, error) {
pvr := &patchVersionReader{}
err := pvr.Init()
return pvr, err
}

// Init initializes patchVersionReader
func (pvr *patchVersionReader) Init() error {
pvr.latestPatchVersions = make(map[functionRelease]string)

var buf bytes.Buffer
cmd := exec.Command("git", "tag")
cmd.Stdout = &buf
err := cmd.Run()
if err != nil {
return err
}
for _, tag := range strings.Split(buf.String(), "\n") {
segments := strings.Split(tag, "/")
// e.g. functions/go/some-fn/v1.2.3, go/some-fn/v1.2.3
if len(segments) < 3 || !patchTagPattern.MatchString(tag) {
continue
}

patchVersion := segments[len(segments)-1]
fr := functionRelease{
FunctionName: segments[len(segments)-2],
MajorMinor: semver.MajorMinor(patchVersion),
}
ver, ok := pvr.latestPatchVersions[fr]
if !ok {
pvr.latestPatchVersions[fr] = patchVersion
} else if semver.Compare(patchVersion, ver) == 1 {
pvr.latestPatchVersions[fr] = patchVersion
}
}
return nil
}

// LatestPatchVersion for a given major/minor version of a function
func (pvr *patchVersionReader) LatestPatchVersion(funcName, majorMinor string) (string, error) {
val, ok := pvr.latestPatchVersions[functionRelease{
FunctionName: funcName,
MajorMinor: majorMinor,
}]
if !ok {
return "", fmt.Errorf("could not find patch version for %s %s\n",
funcName, majorMinor)
}
return val, nil
}

func getFunctions(branches []string, source string, dest string) []function {
functions := make(map[string]function)
for _, b := range branches {
Expand Down Expand Up @@ -436,3 +518,43 @@ func writeExampleIndex(functions []function, source string, dest string) error {
err = ioutil.WriteFile(filepath.Join(dest, "catalog.json"), funcJson, 0600)
return err
}

// writeExampleIndexV2 forms and writes the output for catalog-v2.json
func writeExampleIndexV2(functions []function, source string, dest string) error {
// Map functionName->majorMinor->catalogEntry
functionVersionMap := make(map[string]map[string]catalogEntry)
pvr, err := NewPatchVersionReader()
if err != nil {
return err
}
for _, f := range functions {
// majorMinor->CatalogEntry
catalogEntryMap := make(map[string]catalogEntry)
for majorMinor, examples := range f.VersionToExamples {
// exampleName->example
exampleMap := make(map[string]example)
for exName, ex := range examples {
e := ex
e.LocalExamplePath = strings.Replace(ex.LocalExamplePath, filepath.Join(source, "site"), "", 1)
exampleMap[exName] = e
}
patch, err := pvr.LatestPatchVersion(f.FunctionName, majorMinor)
if err != nil {
return err
}
catalogEntryMap[majorMinor] = catalogEntry{
LatestPatchVersion: patch,
Examples: exampleMap,
}
}
functionVersionMap[f.FunctionName] = catalogEntryMap
}

funcJson, err := json.Marshal(functionVersionMap)
if err != nil {
return err
}

err = ioutil.WriteFile(filepath.Join(dest, "catalog-v2.json"), funcJson, 0600)
return err
}
11 changes: 6 additions & 5 deletions site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@

let catalogPath;
if (isContribFn) {
catalogPath = "/contrib/catalog.json";
catalogPath = "/contrib/catalog-v2.json";
} else {
catalogPath = "/catalog.json";
catalogPath = "/catalog-v2.json";
}
window.Docsify.get(catalogPath).then(function (
catalogResponse
Expand All @@ -135,7 +135,8 @@
versionName = pathElements[2];
}

const currentVersion = versionName.replaceAll("v", "");
const patchSemver = versions[versionName]['LatestPatchVersion']
const currentVersion = patchSemver.replaceAll("v", "");
const sortedSemvers = Object.keys(versions)
.sort((a, b) => compareVersions(a, b))
.reverse();
Expand All @@ -146,7 +147,7 @@
<ol class="dropdown-menu">`;
sortedSemvers.forEach(
(ver, ix) =>
(versionDropdown += `<li><a href="/${functionName}/${ver}/">${ver.replace(
(versionDropdown += `<li><a href="/${functionName}/${ver}/">${versions[ver]['LatestPatchVersion'].replace(
"v",
""
)}${ix ? "" : " (latest)"}</a></li>`)
Expand All @@ -155,7 +156,7 @@
</ol>
</div>`;

const examples = catalog[functionName][versionName];
const examples = catalog[functionName][versionName]['Examples'];
const ghElement = document
.getElementsByClassName("github-corner")
.item(0);
Expand Down

0 comments on commit 5c26848

Please sign in to comment.