Skip to content

Commit

Permalink
chore: localize source openapi doc before publish (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
disintegrator committed Apr 17, 2024
1 parent 562495f commit fde1cba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 46 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ require (
github.com/iancoleman/orderedmap v0.3.0
github.com/iancoleman/strcase v0.3.0
github.com/manifoldco/promptui v0.9.0
github.com/pb33f/libopenapi v0.15.14
github.com/pb33f/libopenapi v0.15.15
github.com/pkg/errors v0.9.1
github.com/sethvargo/go-githubactions v1.1.0
github.com/speakeasy-api/huh v0.0.1
github.com/speakeasy-api/openapi-changes v1.0.3
github.com/speakeasy-api/openapi-generation/v2 v2.306.3
github.com/speakeasy-api/openapi-overlay v0.4.0
github.com/speakeasy-api/sdk-gen-config v1.10.0
github.com/speakeasy-api/sdk-gen-config v1.11.0
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.5.5
github.com/speakeasy-api/speakeasy-core v0.4.4
github.com/speakeasy-api/speakeasy-core v0.4.5
github.com/speakeasy-api/speakeasy-proxy v0.0.2
github.com/speakeasy-sdks/openai-go-sdk/v4 v4.2.1
github.com/spf13/cobra v1.8.0
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@ github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQ
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
github.com/pb33f/doctor v0.0.4 h1:+JVwBsVwwMR0Um13mODlQ/h/HpFpyNPKieQXk/sEf2c=
github.com/pb33f/doctor v0.0.4/go.mod h1:yBs5hFHAoo/eeFvKN9sWwmHmgEPJ2SaotYOJc05GdMU=
github.com/pb33f/libopenapi v0.15.14 h1:A0fn45jbthDyFGXfu5bYIZVsWyPI6hJYm3wG143MT8o=
github.com/pb33f/libopenapi v0.15.14/go.mod h1:PEXNwvtT4KNdjrwudp5OYnD1ryqK6uJ68aMNyWvoMuc=
github.com/pb33f/libopenapi v0.15.15 h1:5pD+JaIoR34VWhyK1L0FxLE9I9pVVPFNPfxWAVRq89A=
github.com/pb33f/libopenapi v0.15.15/go.mod h1:PEXNwvtT4KNdjrwudp5OYnD1ryqK6uJ68aMNyWvoMuc=
github.com/pb33f/libopenapi-validator v0.0.42 h1:bfwPWlxUFHtvPNi0PH+EVpQBU2kA3Db9rVdFkfmUVac=
github.com/pb33f/libopenapi-validator v0.0.42/go.mod h1:kU1JYyXIRlpmsWx3NkL+drNNttLADMgdaNzJgXDhec0=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
Expand Down Expand Up @@ -509,12 +509,12 @@ github.com/speakeasy-api/openapi-generation/v2 v2.306.3 h1:20gezu7JpNxOdu4viXSYJ
github.com/speakeasy-api/openapi-generation/v2 v2.306.3/go.mod h1:hVRNUWDpajfgd+ZQHX0aGsSQb1a8dquWyb173dVihj8=
github.com/speakeasy-api/openapi-overlay v0.4.0 h1:FKxopZDhlAIzPS2lKl2X7pfworNvlsxTYjaDwvlY+gw=
github.com/speakeasy-api/openapi-overlay v0.4.0/go.mod h1:f5FloQrHA7MsxYg9djzMD5h6dxrHjVVByWKh7an8TRc=
github.com/speakeasy-api/sdk-gen-config v1.10.0 h1:ceaH8+O74G/UzpBkWMjoFta8NuLuIydVH8h3MbAK/GA=
github.com/speakeasy-api/sdk-gen-config v1.10.0/go.mod h1:7FsPqdsh//6Z0OcO/jQPD66l6/m1YVvK5tmt0+KRpdo=
github.com/speakeasy-api/sdk-gen-config v1.11.0 h1:YGr1sYwe/ANX2k+EA5lNdzCQ4IzECT2+3pHQzWMxk7M=
github.com/speakeasy-api/sdk-gen-config v1.11.0/go.mod h1:7FsPqdsh//6Z0OcO/jQPD66l6/m1YVvK5tmt0+KRpdo=
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.5.5 h1:FRDnEHlfB9trpvdatO+p1WQtMt6SvfS5z3lPhYFvRr8=
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.5.5/go.mod h1:b4fiZ1Wid0JHwwiYqhaPifDwjmC15uiN7A8Cmid+9kw=
github.com/speakeasy-api/speakeasy-core v0.4.4 h1:bnkcCSlFEh3NSgDM/abLoUUC7B6AvPiLe2lRDz8h8ok=
github.com/speakeasy-api/speakeasy-core v0.4.4/go.mod h1:FTrob6LGyDsd9lCPEHAf3NKlmjGwqWjsvWyvPBsPK08=
github.com/speakeasy-api/speakeasy-core v0.4.5 h1:E6aTIaQdq98BP6ECYvbkmt+yyOADq5+UCqstqJUaASw=
github.com/speakeasy-api/speakeasy-core v0.4.5/go.mod h1:aFP0+btaZFuZo/hDdgzhde5AwohrjOh/zvI9DE1QFT0=
github.com/speakeasy-api/speakeasy-go-sdk v1.8.1 h1:atzohw12oQ5ipaLb1q7ntTu4vvAgKDJsrvaUoOu6sw0=
github.com/speakeasy-api/speakeasy-go-sdk v1.8.1/go.mod h1:XbzaM0sMjj8bGooz/uEtNkOh1FQiJK7RFuNG3LPBSAU=
github.com/speakeasy-api/speakeasy-proxy v0.0.2 h1:u4rQ8lXvuYRCSxiLQGb5JxkZRwNIDlyh+pMFYD6OGjA=
Expand Down
65 changes: 28 additions & 37 deletions internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,55 +491,46 @@ func (w *Workflow) runSource(ctx context.Context, parentStep *WorkflowStep, id s

hasSchemaRegistry, _ := auth.HasWorkspaceFeatureFlag(ctx, shared.FeatureFlagsSchemaRegistry)
if hasSchemaRegistry {
pl := bundler.NewPipeline(&bundler.PipelineOptions{})
memfs := fsextras.NewMemFS()

registryStep := rootStep.NewSubstep("Tracking OpenAPI Changes")

registryStep.NewSubstep("Snapshotting OpenAPI Revision")
memfs := fsextras.NewMemFS()
err = memfs.MkdirAll("/openapi/bundle")

_, err := pl.Localize(ctx, memfs, bundler.LocalizeOptions{
DocumentPath: filepath.Join(w.projectDir, outputLocation),
})
if err != nil {
return "", nil, fmt.Errorf("memfs: mkdirall: %w", err)
return "", nil, fmt.Errorf("error localizing openapi document: %w", err)
}

bs, err := os.ReadFile(filepath.Join(w.projectDir, outputLocation))
err = pl.BuildOCIImage(ctx, bundler.NewReadWriteFS(memfs, memfs), &bundler.OCIBuildOptions{
Tags: []string{"latest"},
})
if err != nil {
return "", nil, fmt.Errorf("failed to read output file: %w", err)
return "", nil, fmt.Errorf("error bundling openapi artifact: %w", err)
}

ext := filepath.Ext(outputLocation)
err = memfs.WriteBytes("/openapi/bundle/openapi"+ext, bs, 0o644)
if err != nil {
return "", nil, fmt.Errorf("memfs: writebytes: %w", err)
serverURL := auth.GetServerURL()
insecurePublish := false
if strings.HasPrefix(serverURL, "http://") {
insecurePublish = true
}

pl := bundler.NewPipeline(&bundler.PipelineOptions{})
if err = pl.BuildOCIImage(ctx, bundler.NewReadWriteFS(memfs, memfs), &bundler.OCIBuildOptions{
Tags: []string{"latest"},
}); err != nil {
// If the preflight error is caused by lack of feature flag we continue without error
if !errors.Is(err, bundler.ErrAccessGated) {
return "", nil, fmt.Errorf("failed to create openapi bundle: %w", err)
}
} else {
serverURL := auth.GetServerURL()
insecurePublish := false
if strings.HasPrefix(serverURL, "http://") {
insecurePublish = true
}
reg := strings.TrimPrefix(serverURL, "http://")
reg = strings.TrimPrefix(reg, "https://")

reg := strings.TrimPrefix(serverURL, "http://")
reg = strings.TrimPrefix(reg, "https://")

registryStep.NewSubstep("Storing OpenAPI Revision")
_, err = pl.PushOCIImage(ctx, memfs, &bundler.OCIPushOptions{
Tags: []string{"latest"},
Registry: reg,
Access: bundler.NewRepositoryAccess(config.GetSpeakeasyAPIKey(), id, bundler.RepositoryAccessOptions{
Insecure: insecurePublish,
}),
})
if err != nil {
return "", nil, fmt.Errorf("failed to publish openapi bundle to registry: %w", err)
}
registryStep.NewSubstep("Storing OpenAPI Revision")
_, err = pl.PushOCIImage(ctx, memfs, &bundler.OCIPushOptions{
Tags: []string{"latest"},
Registry: reg,
Access: bundler.NewRepositoryAccess(config.GetSpeakeasyAPIKey(), id, bundler.RepositoryAccessOptions{
Insecure: insecurePublish,
}),
})
if err != nil && !errors.Is(err, bundler.ErrAccessGated) {
return "", nil, fmt.Errorf("error publishing openapi bundle to registry: %w", err)
}
}

Expand Down

0 comments on commit fde1cba

Please sign in to comment.