Skip to content

Commit

Permalink
fix: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
corneliu-petrescu committed May 29, 2024
1 parent 346681b commit 6e7cc34
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 35 deletions.
3 changes: 3 additions & 0 deletions tools/api-docs-generator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ test: tidy lint
run:
@go run . config.yml ../..

historical-changelog:
@go run ./cmd/historical-change-log config.yml ../..

lint: tidy
go run github.com/golangci/golangci-lint/cmd/golangci-lint run

Expand Down
47 changes: 29 additions & 18 deletions tools/api-docs-generator/changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ type ChangesByEndpoint struct {
checker.Changes
}

func UpdateChangelog(ctx context.Context, cfg *config.Config, syncStateCfg config.SyncStateConfig, docsDirectory string) (string, error) {
func UpdateChangelog(ctx context.Context, cfg *config.Config, syncStateCfg config.SyncStateConfig, docsDirectory string) error {
changeLogFile := path.Join(docsDirectory, cfg.Changelog.ChangelogFile)
syncStateFile := path.Join(docsDirectory, cfg.Changelog.SyncStateFile)

loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true

allVersions, err := versions.GetCurrentVersions(ctx, cfg)
if err != nil {
return "", err
return err
}

latestGAVersion := versions.GetLatestGAVersion(allVersions)
Expand All @@ -47,52 +50,55 @@ func UpdateChangelog(ctx context.Context, cfg *config.Config, syncStateCfg confi

groupedChanges, err := getChangeLog(nextURL, baseURL, loader)
if err != nil {
return "", err
return err
}

if len(groupedChanges) == 0 {
return "", nil
// skip all logic if no actual changes detected
return nil
}

// changes detected
historicalChangelog, err := os.ReadFile(path.Join(docsDirectory, cfg.Changelog.ChangelogFile)) // just pass the file name
historicalChangelog, err := os.ReadFile(changeLogFile) // just pass the file name
if err != nil {
fmt.Print(err)
}

writer, err := os.Create(path.Join(docsDirectory, cfg.Changelog.ChangelogFile))
writer, err := os.Create(changeLogFile)
if err != nil {
return "", err
return err
}

defer func(writer *os.File) {
writeErr := writer.Close()
if writeErr != nil {
fmt.Printf("Error closing writer for %s\n", cfg.Changelog.ChangelogFile)
fmt.Printf("Error closing writer for %s\n", changeLogFile)
}
}(writer)

markdown := md.NewMarkdown(writer)

err = WriteToChangeLog(markdown, groupedChanges, latestGAVersion, nextURL, syncStateCfg.LastSyncedVersion)
if err != nil {
return "", err
return err
}

err = markdown.Build()
if err != nil {
return "", err
return err
}

_, err = writer.Write(historicalChangelog)
if err != nil {
return "", err
return err
}

return latestGAVersion, err
syncStateCfg.LastSyncedVersion = latestGAVersion
err = config.UpdateSyncState(syncStateFile, syncStateCfg)

return err
}

func GenerateHistorical(ctx context.Context, cfg *config.Config) error {
func GenerateHistorical(ctx context.Context, cfg *config.Config, docsDirectory string) error {
allVersions, err := versions.GetCurrentVersions(ctx, cfg)
if err != nil {
return err
Expand All @@ -107,14 +113,15 @@ func GenerateHistorical(ctx context.Context, cfg *config.Config) error {
slices.Reverse(gaVersions)
nextVersion := gaVersions[0]

writer, err := os.Create(cfg.Changelog.ChangelogFile)
writer, err := os.Create(path.Join(docsDirectory, cfg.Changelog.ChangelogFile))
if err != nil {
return err
}

for _, baseVersion := range gaVersions[1:] {
nextURL := fmt.Sprintf("%s/%s", cfg.Fetcher.Source, nextVersion)
baseURL := fmt.Sprintf("%s/%s", cfg.Fetcher.Source, baseVersion)
fmt.Printf("Comparing %s to %s\n", nextVersion, baseVersion)
groupedChanges, err := getChangeLog(nextURL, baseURL, loader)
if err != nil {
return err
Expand Down Expand Up @@ -181,16 +188,16 @@ func WriteToChangeLog(markdown *md.Markdown, groupedChanges []ChangesByEndpoint,
if err != nil {
return err
}
markdown.BulletList(versions.ReplaceWithCodeQuotes(document.Paths.Find(changeGroup.Path).Operations()[changeGroup.Operation].Description))
markdown.BulletList(normalizeQuotes(document.Paths.Find(changeGroup.Path).Operations()[changeGroup.Operation].Description))
continue
}
markdown.H3f("%s - `%s` - Updated", changeGroup.Operation, changeGroup.Path)
for index := range changeGroup.Changes {
if changeGroup.Changes[index].IsBreaking() {
markdown.BulletList(versions.ReplaceWithCodeQuotes(changeGroup.Changes[index].GetUncolorizedText(localizer)))
markdown.BulletList(normalizeQuotes(changeGroup.Changes[index].GetUncolorizedText(localizer)))
markdown.YellowBadge("Breaking")
} else {
markdown.BulletList(fmt.Sprintf("%s\n", versions.ReplaceWithCodeQuotes(changeGroup.Changes[index].GetUncolorizedText(localizer))))
markdown.BulletList(fmt.Sprintf("%s\n", normalizeQuotes(changeGroup.Changes[index].GetUncolorizedText(localizer))))
}
}

Expand Down Expand Up @@ -255,3 +262,7 @@ func getChangeLog(nextVersionURI, baseVersionURI string, loader *openapi3.Loader

return groupedChanges, nil
}

func normalizeQuotes(description string) string {
return strings.ReplaceAll(description, "'", "`")
}
14 changes: 10 additions & 4 deletions tools/api-docs-generator/cmd/historical-change-log/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,36 @@ package main

import (
"context"
"fmt"
"log"
"os"
"time"

"github.com/snyk/user-docs/tools/api-docs-generator/changelog"

"github.com/snyk/user-docs/tools/api-docs-generator/config"
)

const configFile = "tools/api-docs-generator/config.yml"

func main() {
ctx, cancelCtx := context.WithTimeout(context.Background(), 120*time.Second)
defer cancelCtx()
if len(os.Args) != 3 {
log.Panicf("usage: api-docs <config-file> <docs-dir>")
}

fmt.Println(os.Args)

cfg, err := config.Parse(configFile)
cfg, err := config.Parse(os.Args[1])
if err != nil {
log.Panic(err)
}
docsDirectory := os.Args[2]

if cfg.Changelog.HistoricalVersionCutoff == "" {
log.Panic("Missing historical version cutoff")
}

err = changelog.GenerateHistorical(ctx, cfg)
err = changelog.GenerateHistorical(ctx, cfg, docsDirectory)
if err != nil {
log.Panic(err)
}
Expand Down
10 changes: 1 addition & 9 deletions tools/api-docs-generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,11 @@ func main() {
log.Panic(err)
}

updatedToVersion, err := changelog.UpdateChangelog(ctx, cfg, syncStateCfg, docsDirectory)
err = changelog.UpdateChangelog(ctx, cfg, syncStateCfg, docsDirectory)
if err != nil {
log.Panic(err)
}

if updatedToVersion != "" {
syncStateCfg.LastSyncedVersion = updatedToVersion
updateSyncStateErr := config.UpdateSyncState(path.Join(docsDirectory, cfg.Changelog.SyncStateFile), syncStateCfg)
if err != nil {
log.Panic(updateSyncStateErr)
}
}

// replace latest spec
err = fetcher.FetchSpec(ctx, cfg, docsDirectory)
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions tools/api-docs-generator/versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ func ExtractGAVersions(versions []string) []string {
return gaVersions
}

func ReplaceWithCodeQuotes(description string) string {
return strings.ReplaceAll(description, "'", "`")
}

func GetLatestGAVersion(versions []string) string {
gaVersions := []string{}
for _, version := range versions {
Expand Down

0 comments on commit 6e7cc34

Please sign in to comment.