Skip to content

Commit

Permalink
fix(openapi): replace date with product version
Browse files Browse the repository at this point in the history
This allows us to remove the comparison logic and simply re-generate the spec file every time.
  • Loading branch information
dnephin committed Mar 23, 2022
1 parent c75a149 commit 2715f62
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .github/release-please.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
".": {
"release-type": "go",
"extra-files": [
"helm/charts/infra/Chart.yaml"
"helm/charts/infra/Chart.yaml",
"internal/version.go"
]
}
}
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@ openapi-lint: docs/api/openapi3.json
@command -v openapi --version >/dev/null || { echo "openapi missing, try: npm install -g @redocly/openapi-cli" && exit 1; }
openapi lint $<

.PHONY: docs/api/openapi3.json
docs/api/openapi3.json:
go run ./internal/openapigen $@
2 changes: 1 addition & 1 deletion docs/api/openapi3.json
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
},
"title": "Infra API",
"version": "2022-03-21"
"version": "0.6.1"
},
"paths": {
"/v1/access-keys": {
Expand Down
44 changes: 6 additions & 38 deletions internal/server/openapi.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package server

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand All @@ -17,6 +16,7 @@ import (
"github.com/getkin/kin-openapi/openapi3"

"github.com/infrahq/infra/api"
"github.com/infrahq/infra/internal"
)

var (
Expand Down Expand Up @@ -216,11 +216,11 @@ func buildProperty(f reflect.StructField, t, parent reflect.Type, parentSchema *
}
}

func writeOpenAPISpec(spec openapi3.T, version string, out io.Writer) error {
func writeOpenAPISpec(spec openapi3.T, out io.Writer) error {
spec.OpenAPI = "3.0.0"
spec.Info = &openapi3.Info{
Title: "Infra API",
Version: version,
Version: internal.Version,
Description: "Infra API",
License: &openapi3.License{Name: "Apache 2.0", URL: "https://www.apache.org/licenses/LICENSE-2.0.html"},
}
Expand All @@ -237,46 +237,14 @@ func writeOpenAPISpec(spec openapi3.T, version string, out io.Writer) error {
}

func WriteOpenAPISpecToFile(filename string) error {
version := time.Now().Format("2006-01-02")

var buf bytes.Buffer
if err := writeOpenAPISpec(openAPISchema, version, &buf); err != nil {
return err
}

old, err := readOpenAPISpec(filename)
fh, err := os.Create(filename)
if err != nil {
return err
}
old.Info.Version = version

var oldBuf bytes.Buffer
if err := writeOpenAPISpec(old, version, &oldBuf); err != nil {
if err := writeOpenAPISpec(openAPISchema, fh); err != nil {
return err
}

if bytes.Equal(buf.Bytes(), oldBuf.Bytes()) {
// no changes to the schema
return nil
}

// nolint: gosec // 0644 is the right mode
return os.WriteFile(filename, buf.Bytes(), 0o644)
}

func readOpenAPISpec(filename string) (openapi3.T, error) {
spec := openapi3.T{}

fh, err := os.Open(filename)
if err != nil {
return spec, fmt.Errorf("failed to create file: %w", err)
}
defer fh.Close()

if err := json.NewDecoder(fh).Decode(&spec); err != nil {
return spec, fmt.Errorf("failed to parse last openapi schema from %s: %w", filename, err)
}
return spec, nil
return nil
}

func setTagInfo(f reflect.StructField, t, parent reflect.Type, schema, parentSchema *openapi3.Schema) {
Expand Down
10 changes: 6 additions & 4 deletions internal/version.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package internal

var (
Branch = "main"
Version = "0.0.0-development"
Commit = ""
Date = ""
Branch = "main"
// {x-release-please-start-version}
Version = "0.6.1"
// {x-release-please-end}
Commit = ""
Date = ""
)

0 comments on commit 2715f62

Please sign in to comment.