From 346681b042b24f6b90c0bbb18cf232ce15565c4a Mon Sep 17 00:00:00 2001 From: Corneliu Petrescu Date: Wed, 29 May 2024 11:43:02 +0300 Subject: [PATCH] feat: integrate in main workflow --- .../api-docs-generator/changelog/changelog.go | 40 ++++++++++--------- .../cmd/historical-change-log/main.go | 2 +- tools/api-docs-generator/config.yml | 1 + tools/api-docs-generator/config/config.go | 1 + .../api-docs-generator/config/config_test.go | 11 ++++- .../config/{syncState.go => sync_state.go} | 0 .../{syncState_test.go => sync_state_test.go} | 0 tools/api-docs-generator/main.go | 23 +++++++++++ 8 files changed, 57 insertions(+), 21 deletions(-) rename tools/api-docs-generator/config/{syncState.go => sync_state.go} (100%) rename tools/api-docs-generator/config/{syncState_test.go => sync_state_test.go} (100%) diff --git a/tools/api-docs-generator/changelog/changelog.go b/tools/api-docs-generator/changelog/changelog.go index ee3f2919e89d..777c0f9606bb 100644 --- a/tools/api-docs-generator/changelog/changelog.go +++ b/tools/api-docs-generator/changelog/changelog.go @@ -5,6 +5,7 @@ import ( "fmt" "net/url" "os" + "path" "slices" "sort" "strings" @@ -30,7 +31,7 @@ type ChangesByEndpoint struct { checker.Changes } -func UpdateChangelog(ctx context.Context, cfg *config.Config, syncStateCfg config.SyncStateConfig, changeLogFileName string) (string, error) { +func UpdateChangelog(ctx context.Context, cfg *config.Config, syncStateCfg config.SyncStateConfig, docsDirectory string) (string, error) { loader := openapi3.NewLoader() loader.IsExternalRefsAllowed = true @@ -41,12 +42,25 @@ func UpdateChangelog(ctx context.Context, cfg *config.Config, syncStateCfg confi latestGAVersion := versions.GetLatestGAVersion(allVersions) - historicalChangelog, err := os.ReadFile(changeLogFileName) // just pass the file name + nextURL := fmt.Sprintf("%s/%s", cfg.Fetcher.Source, latestGAVersion) + baseURL := path.Join(docsDirectory, cfg.Fetcher.Destination) + + groupedChanges, err := getChangeLog(nextURL, baseURL, loader) + if err != nil { + return "", err + } + + if len(groupedChanges) == 0 { + return "", nil + } + + // changes detected + historicalChangelog, err := os.ReadFile(path.Join(docsDirectory, cfg.Changelog.ChangelogFile)) // just pass the file name if err != nil { fmt.Print(err) } - writer, err := os.Create(changeLogFileName) + writer, err := os.Create(path.Join(docsDirectory, cfg.Changelog.ChangelogFile)) if err != nil { return "", err } @@ -54,22 +68,10 @@ func UpdateChangelog(ctx context.Context, cfg *config.Config, syncStateCfg confi defer func(writer *os.File) { writeErr := writer.Close() if writeErr != nil { - fmt.Printf("Error closing writer for %s\n", changeLogFileName) + fmt.Printf("Error closing writer for %s\n", cfg.Changelog.ChangelogFile) } }(writer) - nextURL := fmt.Sprintf("%s/%s", cfg.Fetcher.Source, latestGAVersion) - baseURL := cfg.Fetcher.Destination - - groupedChanges, err := getChangeLog(nextURL, baseURL, loader) - if err != nil { - return "", err - } - - if len(groupedChanges) == 0 { - return "", nil - } - markdown := md.NewMarkdown(writer) err = WriteToChangeLog(markdown, groupedChanges, latestGAVersion, nextURL, syncStateCfg.LastSyncedVersion) @@ -90,7 +92,7 @@ func UpdateChangelog(ctx context.Context, cfg *config.Config, syncStateCfg confi return latestGAVersion, err } -func GenerateHistorical(ctx context.Context, cfg *config.Config, changeLogFileName, endVersion string) error { +func GenerateHistorical(ctx context.Context, cfg *config.Config) error { allVersions, err := versions.GetCurrentVersions(ctx, cfg) if err != nil { return err @@ -100,12 +102,12 @@ func GenerateHistorical(ctx context.Context, cfg *config.Config, changeLogFileNa loader.IsExternalRefsAllowed = true gaVersions := versions.ExtractGAVersions(allVersions) - endVersionPos := sort.SearchStrings(gaVersions, endVersion) + endVersionPos := sort.SearchStrings(gaVersions, cfg.Changelog.HistoricalVersionCutoff) gaVersions = gaVersions[:endVersionPos] slices.Reverse(gaVersions) nextVersion := gaVersions[0] - writer, err := os.Create(changeLogFileName) + writer, err := os.Create(cfg.Changelog.ChangelogFile) if err != nil { return err } diff --git a/tools/api-docs-generator/cmd/historical-change-log/main.go b/tools/api-docs-generator/cmd/historical-change-log/main.go index 99261442134c..2c532b69a564 100644 --- a/tools/api-docs-generator/cmd/historical-change-log/main.go +++ b/tools/api-docs-generator/cmd/historical-change-log/main.go @@ -25,7 +25,7 @@ func main() { log.Panic("Missing historical version cutoff") } - err = changelog.GenerateHistorical(ctx, cfg, "docs/snyk-api/changelog.md", cfg.Changelog.HistoricalVersionCutoff) + err = changelog.GenerateHistorical(ctx, cfg) if err != nil { log.Panic(err) } diff --git a/tools/api-docs-generator/config.yml b/tools/api-docs-generator/config.yml index 36a530b59e0f..a10f274216b8 100644 --- a/tools/api-docs-generator/config.yml +++ b/tools/api-docs-generator/config.yml @@ -12,3 +12,4 @@ output: changelog: historicalVersionCutoff: "2024-05-24" syncStateFile: tools/api-docs-generator/sync-state.yml + changelogFile: docs/snyk-api/changelog.md diff --git a/tools/api-docs-generator/config/config.go b/tools/api-docs-generator/config/config.go index 500ce12379b0..6d4464b24d4c 100644 --- a/tools/api-docs-generator/config/config.go +++ b/tools/api-docs-generator/config/config.go @@ -20,6 +20,7 @@ type Spec struct { type Changelog struct { HistoricalVersionCutoff string `yaml:"historicalVersionCutoff"` SyncStateFile string `yaml:"syncStateFile"` + ChangelogFile string `yaml:"changelogFile"` } type Output struct { diff --git a/tools/api-docs-generator/config/config_test.go b/tools/api-docs-generator/config/config_test.go index f48eb09c0f5f..a05cd72bf041 100644 --- a/tools/api-docs-generator/config/config_test.go +++ b/tools/api-docs-generator/config/config_test.go @@ -27,7 +27,11 @@ specs: docsHint: hint 2 output: - apiReferencePath: snyk-api/reference`) + apiReferencePath: snyk-api/reference +changelog: + historicalVersionCutoff: "2024-05-24" + syncStateFile: tools/api-docs-generator/sync-state.yml + changelogFile: tools/snyk-api/changelog.md`) }, want: Config{ Fetcher: Fetcher{"source", "destination"}, @@ -36,6 +40,11 @@ output: {".gitbook/assets/rest-spec.json", "", "hint 2"}, }, Output: Output{"snyk-api/reference"}, + Changelog: Changelog{ + HistoricalVersionCutoff: "2024-05-24", + SyncStateFile: "tools/api-docs-generator/sync-state.yml", + ChangelogFile: "tools/snyk-api/changelog.md", + }, }, }, { diff --git a/tools/api-docs-generator/config/syncState.go b/tools/api-docs-generator/config/sync_state.go similarity index 100% rename from tools/api-docs-generator/config/syncState.go rename to tools/api-docs-generator/config/sync_state.go diff --git a/tools/api-docs-generator/config/syncState_test.go b/tools/api-docs-generator/config/sync_state_test.go similarity index 100% rename from tools/api-docs-generator/config/syncState_test.go rename to tools/api-docs-generator/config/sync_state_test.go diff --git a/tools/api-docs-generator/main.go b/tools/api-docs-generator/main.go index bef99db012d1..54d7a41397f0 100644 --- a/tools/api-docs-generator/main.go +++ b/tools/api-docs-generator/main.go @@ -4,8 +4,11 @@ import ( "context" "log" "os" + "path" "time" + "github.com/snyk/user-docs/tools/api-docs-generator/changelog" + "github.com/snyk/user-docs/tools/api-docs-generator/config" "github.com/snyk/user-docs/tools/api-docs-generator/fetcher" "github.com/snyk/user-docs/tools/api-docs-generator/generator" @@ -17,12 +20,32 @@ func main() { if len(os.Args) != 3 { log.Panicf("usage: api-docs ") } + cfg, err := config.Parse(os.Args[1]) if err != nil { log.Panic(err) } docsDirectory := os.Args[2] + syncStateCfg, err := config.LoadSyncState(path.Join(docsDirectory, cfg.Changelog.SyncStateFile)) + if err != nil { + log.Panic(err) + } + + updatedToVersion, 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 { log.Panic(err)