Skip to content

Commit

Permalink
feat: integrate in main workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
corneliu-petrescu committed May 29, 2024
1 parent 52eb788 commit 346681b
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 21 deletions.
40 changes: 21 additions & 19 deletions tools/api-docs-generator/changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/url"
"os"
"path"
"slices"
"sort"
"strings"
Expand All @@ -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

Expand All @@ -41,35 +42,36 @@ 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
}

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)
Expand All @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion tools/api-docs-generator/cmd/historical-change-log/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions tools/api-docs-generator/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ output:
changelog:
historicalVersionCutoff: "2024-05-24"
syncStateFile: tools/api-docs-generator/sync-state.yml
changelogFile: docs/snyk-api/changelog.md
1 change: 1 addition & 0 deletions tools/api-docs-generator/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion tools/api-docs-generator/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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",
},
},
},
{
Expand Down
23 changes: 23 additions & 0 deletions tools/api-docs-generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,12 +20,32 @@ func main() {
if len(os.Args) != 3 {
log.Panicf("usage: api-docs <config-file> <docs-dir>")
}

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)
Expand Down

0 comments on commit 346681b

Please sign in to comment.