Skip to content
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

[release-15.0] update docgen to embed commit ID in autogenerated doc frontmatter (#14056) #14072

Merged
merged 1 commit into from
Sep 22, 2023
Merged
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
38 changes: 29 additions & 9 deletions go/cmd/internal/docgen/docgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"

Expand All @@ -57,6 +58,10 @@ import (
// written to `dir`. The root command is also renamed to _index.md to remain
// compatible with the vitessio/website content structure expectations.
func GenerateMarkdownTree(cmd *cobra.Command, dir string) error {
sha, err := getCommitID("HEAD")
if err != nil {
return fmt.Errorf("failed to get commit id for HEAD: %w", err)
}
switch fi, err := os.Stat(dir); {
case errors.Is(err, fs.ErrNotExist):
if err := os.MkdirAll(dir, 0755); err != nil {
Expand All @@ -69,7 +74,7 @@ func GenerateMarkdownTree(cmd *cobra.Command, dir string) error {
}

recursivelyDisableAutoGenTags(cmd)
if err := doc.GenMarkdownTreeCustom(cmd, dir, frontmatterFilePrepender, linkHandler); err != nil {
if err := doc.GenMarkdownTreeCustom(cmd, dir, frontmatterFilePrepender(sha), linkHandler); err != nil {
return err
}

Expand All @@ -91,22 +96,37 @@ func recursivelyDisableAutoGenTags(root *cobra.Command) {
}
}

func getCommitID(ref string) (string, error) {
gitShow := exec.Command("git", "show", "--pretty=format:%H", "--no-patch", ref)
out, err := gitShow.Output()
if err != nil {
return "", err
}

return string(out), nil
}

const frontmatter = `---
title: %s
series: %s
commit: %s
---
`

func frontmatterFilePrepender(filename string) string {
name := filepath.Base(filename)
base := strings.TrimSuffix(name, filepath.Ext(name))
func frontmatterFilePrepender(sha string) func(filename string) string {
return func(filename string) string {
name := filepath.Base(filename)
base := strings.TrimSuffix(name, filepath.Ext(name))

root, cmdName, ok := strings.Cut(base, "_")
if !ok { // no `_`, so not a subcommand
cmdName = root
}
root, cmdName, ok := strings.Cut(base, "_")
if !ok { // no `_`, so not a subcommand
cmdName = root
}

return fmt.Sprintf(frontmatter, cmdName, root)
cmdName = strings.ReplaceAll(cmdName, "_", " ")

return fmt.Sprintf(frontmatter, cmdName, root, sha)
}
}

func linkHandler(filename string) string {
Expand Down
Loading