Skip to content

Commit

Permalink
feat: dry run generator if specs are changed
Browse files Browse the repository at this point in the history
- to catch spec parsing faliures early
  • Loading branch information
tinygrasshopper committed Aug 1, 2024
1 parent 58aaddf commit 58c3354
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/test-dry-run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Test API Docs Generation

on:
pull_request:
paths:
- '**.yaml'
- '**.json'

jobs:
test:
name: api-docs-generator-dry-run
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
cache-dependency-path: |
tools/api-docs-generator/go.sum
- name: test
working-directory: ./tools/api-docs-generator
run: make dry-run
5 changes: 4 additions & 1 deletion tools/api-docs-generator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ format: tidy
go run golang.org/x/tools/cmd/goimports -w -local=github.com/snyk/user-docs/tools/api-docs-generator .

tidy:
go mod tidy
go mod tidy

dry-run:
@go run . config.yml ../.. --dry-run
40 changes: 24 additions & 16 deletions tools/api-docs-generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"flag"
"log"
"os"
"path"
Expand All @@ -14,35 +15,42 @@ import (
"github.com/snyk/user-docs/tools/api-docs-generator/generator"
)

var dryRunFlag = flag.Bool("dry-run", false,
`runs the generation without updating the changelog or fetching the latest spec, `+
`useful for testing the validity of the current specifications`)

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

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)
}

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

// replace latest spec
err = fetcher.FetchSpec(ctx, cfg, docsDirectory)
if err != nil {
log.Panic(err)
if !*dryRunFlag {
var syncStateCfg config.SyncStateConfig
syncStateCfg, err = config.LoadSyncState(path.Join(docsDirectory, cfg.Changelog.SyncStateFile))
if err != nil {
log.Panic(err)
}

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

// replace latest spec
err = fetcher.FetchSpec(ctx, cfg, docsDirectory)
if err != nil {
log.Panic(err)
}
}

err = generator.GenerateReferenceDocs(cfg, docsDirectory)
if err != nil {
log.Panic(err)
Expand Down

0 comments on commit 58c3354

Please sign in to comment.