From 06434c8b6aec1e2760e89323d401fc090684091a Mon Sep 17 00:00:00 2001 From: Thomas Rooney Date: Fri, 16 Aug 2024 13:30:34 +0100 Subject: [PATCH 1/4] chore: tmp --- integration/workflow_registry_test.go | 24 +++++++++++++++++++++--- internal/reports/reports.go | 4 ++-- internal/run/source.go | 15 --------------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/integration/workflow_registry_test.go b/integration/workflow_registry_test.go index 747089522..e60649f42 100644 --- a/integration/workflow_registry_test.go +++ b/integration/workflow_registry_test.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "testing" "github.com/speakeasy-api/sdk-gen-config/workflow" @@ -41,7 +42,7 @@ func TestStability(t *testing.T) { // Run the initial generation var initialChecksums map[string]string initialArgs := []string{"run", "-t", "all", "--force", "--pinned", "--skip-versioning", "--skip-compile"} - cmdErr := execute(t, temp, initialArgs...).Run() + cmdErr := executeI(t, temp, initialArgs...).Run() require.NoError(t, cmdErr) // Calculate checksums of generated files @@ -49,11 +50,18 @@ func TestStability(t *testing.T) { require.NoError(t, err) // Re-run the generation. We should have stable digests. - cmdErr = execute(t, temp, initialArgs...).Run() + cmdErr = executeI(t, temp, initialArgs...).Run() require.NoError(t, cmdErr) + rerunChecksums, err := calculateChecksums(temp) require.NoError(t, err) require.Equal(t, initialChecksums, rerunChecksums, "Generated files should be identical when using --frozen-workflow-lock") + // Once more, just to be sure. This now has a change report so it could in theory change. + cmdErr = executeI(t, temp, initialArgs...).Run() + require.NoError(t, cmdErr) + rerunChecksums, err = calculateChecksums(temp) + require.NoError(t, err) + require.Equal(t, initialChecksums, rerunChecksums, "Generated files should be identical when using --frozen-workflow-lock") // Modify the workflow file to simulate a change // Shouldn't do anything; we'll validate that later. workflowFile.Sources["test-source"].Inputs[0].Location = "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/petstore.yaml" @@ -61,7 +69,7 @@ func TestStability(t *testing.T) { // Run with --frozen-workflow-lock frozenArgs := []string{"run", "-t", "all", "--pinned", "--frozen-workflow-lockfile", "--skip-compile"} - cmdErr = execute(t, temp, frozenArgs...).Run() + cmdErr = executeI(t, temp, frozenArgs...).Run() require.NoError(t, cmdErr) // Calculate checksums after frozen run @@ -123,6 +131,16 @@ func calculateChecksums(dir string) (map[string]string, error) { if err != nil { return err } + // Skip .git + if strings.Contains(path, ".git") { + return nil + } + // skip gen.lock. In particular, this currently varies between runs if the OAS is reformatted which occurs during bundling + // however we validate stability through the workflow lock file + // TODO: once https://github.com/pb33f/libopenapi/issues/321 is closed, use this to power document checksums and drop this if + if strings.Contains(path, "gen.lock") { + return nil + } if !info.IsDir() { data, err := os.ReadFile(path) if err != nil { diff --git a/internal/reports/reports.go b/internal/reports/reports.go index 45ccad00b..d032a6701 100644 --- a/internal/reports/reports.go +++ b/internal/reports/reports.go @@ -7,10 +7,10 @@ import ( "fmt" "github.com/speakeasy-api/speakeasy-client-sdk-go/v3/pkg/models/operations" "github.com/speakeasy-api/speakeasy-client-sdk-go/v3/pkg/models/shared" + "github.com/speakeasy-api/speakeasy-core/auth" "github.com/speakeasy-api/speakeasy-core/events" "github.com/speakeasy-api/speakeasy/internal/links" "github.com/speakeasy-api/speakeasy/internal/log" - "github.com/speakeasy-api/speakeasy/internal/sdk" "github.com/stoewer/go-strcase" "os" "path/filepath" @@ -31,7 +31,7 @@ func UploadReport(ctx context.Context, reportBytes []byte, reportType shared.Typ } digest := hex.EncodeToString(md5Hasher.Sum(nil)) - s, err := sdk.InitSDK() + s, err := auth.GetSDKFromContext(ctx) if err != nil { return writeLocally(digest, reportBytes, reportType) } diff --git a/internal/run/source.go b/internal/run/source.go index af99faddc..e308936b0 100644 --- a/internal/run/source.go +++ b/internal/run/source.go @@ -30,7 +30,6 @@ import ( "github.com/speakeasy-api/speakeasy/internal/config" "github.com/speakeasy-api/speakeasy/internal/defaultcodesamples" "github.com/speakeasy-api/speakeasy/internal/env" - "github.com/speakeasy-api/speakeasy/internal/git" "github.com/speakeasy-api/speakeasy/internal/github" "github.com/speakeasy-api/speakeasy/internal/log" "github.com/speakeasy-api/speakeasy/internal/overlay" @@ -42,7 +41,6 @@ import ( "github.com/speakeasy-api/speakeasy/internal/workflowTracking" "github.com/speakeasy-api/speakeasy/pkg/merge" "github.com/speakeasy-api/speakeasy/registry" - "go.uber.org/zap" ) type SourceResult struct { @@ -423,11 +421,6 @@ func (w *Workflow) snapshotSource(ctx context.Context, parentStep *workflowTrack return fmt.Errorf("error localizing openapi document: %w", err) } - gitRepo, err := git.NewLocalRepository(w.ProjectDir) - if err != nil { - log.From(ctx).Debug("error sniffing git repository", zap.Error(err)) - } - rootDocument, err := memfs.Open(filepath.Join(bundler.BundleRoot.String(), "openapi.yaml")) if errors.Is(err, fs.ErrNotExist) { rootDocument, err = memfs.Open(filepath.Join(bundler.BundleRoot.String(), "openapi.json")) @@ -441,14 +434,6 @@ func (w *Workflow) snapshotSource(ctx context.Context, parentStep *workflowTrack return fmt.Errorf("error extracting annotations from openapi document: %w", err) } - revision := "" - if gitRepo != nil { - revision, err = gitRepo.HeadHash() - if err != nil { - log.From(ctx).Debug("error sniffing head commit hash", zap.Error(err)) - } - } - annotations.Revision = revision annotations.BundleRoot = strings.TrimPrefix(rootDocumentPath, string(os.PathSeparator)) err = pl.BuildOCIImage(ctx, bundler.NewReadWriteFS(memfs, memfs), &bundler.OCIBuildOptions{ From 44664977864ddc5238c1041312528889af446a9d Mon Sep 17 00:00:00 2001 From: Thomas Rooney Date: Fri, 16 Aug 2024 22:28:17 +0100 Subject: [PATCH 2/4] chore: testing --- integration/workflow_registry_test.go | 11 +++++++++-- internal/reports/reports.go | 13 +++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/integration/workflow_registry_test.go b/integration/workflow_registry_test.go index e60649f42..312296b9d 100644 --- a/integration/workflow_registry_test.go +++ b/integration/workflow_registry_test.go @@ -49,6 +49,10 @@ func TestStability(t *testing.T) { initialChecksums, err = calculateChecksums(temp) require.NoError(t, err) + // Let's move our temporary directory around. We should be resilient to moves + newTemp := setupTestDir(t) + require.NoError(t, os.Rename(temp, newTemp)) + // Re-run the generation. We should have stable digests. cmdErr = executeI(t, temp, initialArgs...).Run() require.NoError(t, cmdErr) @@ -121,7 +125,7 @@ func TestRegistryFlow(t *testing.T) { require.NoError(t, workflow.Save(temp, workflowFile)) // Re-run the generation. It should work. - cmdErr = executeI(t, temp, initialArgs...).Run() + cmdErr = execute(t, temp, initialArgs...).Run() require.NoError(t, cmdErr) } @@ -131,16 +135,19 @@ func calculateChecksums(dir string) (map[string]string, error) { if err != nil { return err } + // Skip .git if strings.Contains(path, ".git") { return nil } + // skip gen.lock. In particular, this currently varies between runs if the OAS is reformatted which occurs during bundling // however we validate stability through the workflow lock file - // TODO: once https://github.com/pb33f/libopenapi/issues/321 is closed, use this to power document checksums and drop this if + // TODO: once https://github.com/pb33f/libopenapi/issues/321 is closed, use this to power document checksums and drop this block if strings.Contains(path, "gen.lock") { return nil } + if !info.IsDir() { data, err := os.ReadFile(path) if err != nil { diff --git a/internal/reports/reports.go b/internal/reports/reports.go index d032a6701..8a8a31ac5 100644 --- a/internal/reports/reports.go +++ b/internal/reports/reports.go @@ -49,6 +49,19 @@ func UploadReport(ctx context.Context, reportBytes []byte, reportType shared.Typ log.From(ctx).Warnf("Failed to upload report to Speakeasy %s", err.Error()) return writeLocally(digest, reportBytes, reportType) } + _, err = s.Reports.UploadReport(ctx, operations.UploadReportRequestBody{ + Data: shared.Report{ + Type: reportType.ToPointer(), + }, + File: operations.File{ + Content: reportBytes, + FileName: digest + ".html", + }, + }) + if err != nil { + log.From(ctx).Warnf("Failed to upload report to Speakeasy %s", err.Error()) + return writeLocally(digest, reportBytes, reportType) + } cliEvent := events.GetTelemetryEventFromContext(ctx) if cliEvent != nil { From 59d1f799e3373ed093f3f1bd4e9a2ac139d72e29 Mon Sep 17 00:00:00 2001 From: Thomas Rooney Date: Sun, 18 Aug 2024 22:17:56 +0100 Subject: [PATCH 3/4] chore: stable manifests --- integration/workflow_registry_test.go | 2 ++ internal/run/target.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/integration/workflow_registry_test.go b/integration/workflow_registry_test.go index 312296b9d..000a7af89 100644 --- a/integration/workflow_registry_test.go +++ b/integration/workflow_registry_test.go @@ -51,7 +51,9 @@ func TestStability(t *testing.T) { // Let's move our temporary directory around. We should be resilient to moves newTemp := setupTestDir(t) + require.NoError(t, os.RemoveAll(newTemp)) require.NoError(t, os.Rename(temp, newTemp)) + temp = newTemp // Re-run the generation. We should have stable digests. cmdErr = executeI(t, temp, initialArgs...).Run() diff --git a/internal/run/target.go b/internal/run/target.go index b9625b6a2..e3dfd991d 100644 --- a/internal/run/target.go +++ b/internal/run/target.go @@ -99,7 +99,7 @@ func (w *Workflow) runTarget(ctx context.Context, target string) (*SourceResult, if t.Output != nil { outDir = *t.Output } else { - outDir = w.ProjectDir + outDir = "." } targetLock.OutLocation = outDir From b465d81648ad082acb2ca493820536789342e5b3 Mon Sep 17 00:00:00 2001 From: Thomas Rooney Date: Sun, 18 Aug 2024 22:19:48 +0100 Subject: [PATCH 4/4] chore: adjust tests --- integration/workflow_registry_test.go | 8 ++++---- internal/reports/reports.go | 13 ------------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/integration/workflow_registry_test.go b/integration/workflow_registry_test.go index 000a7af89..2f26baf3c 100644 --- a/integration/workflow_registry_test.go +++ b/integration/workflow_registry_test.go @@ -42,7 +42,7 @@ func TestStability(t *testing.T) { // Run the initial generation var initialChecksums map[string]string initialArgs := []string{"run", "-t", "all", "--force", "--pinned", "--skip-versioning", "--skip-compile"} - cmdErr := executeI(t, temp, initialArgs...).Run() + cmdErr := execute(t, temp, initialArgs...).Run() require.NoError(t, cmdErr) // Calculate checksums of generated files @@ -56,14 +56,14 @@ func TestStability(t *testing.T) { temp = newTemp // Re-run the generation. We should have stable digests. - cmdErr = executeI(t, temp, initialArgs...).Run() + cmdErr = execute(t, temp, initialArgs...).Run() require.NoError(t, cmdErr) rerunChecksums, err := calculateChecksums(temp) require.NoError(t, err) require.Equal(t, initialChecksums, rerunChecksums, "Generated files should be identical when using --frozen-workflow-lock") // Once more, just to be sure. This now has a change report so it could in theory change. - cmdErr = executeI(t, temp, initialArgs...).Run() + cmdErr = execute(t, temp, initialArgs...).Run() require.NoError(t, cmdErr) rerunChecksums, err = calculateChecksums(temp) require.NoError(t, err) @@ -75,7 +75,7 @@ func TestStability(t *testing.T) { // Run with --frozen-workflow-lock frozenArgs := []string{"run", "-t", "all", "--pinned", "--frozen-workflow-lockfile", "--skip-compile"} - cmdErr = executeI(t, temp, frozenArgs...).Run() + cmdErr = execute(t, temp, frozenArgs...).Run() require.NoError(t, cmdErr) // Calculate checksums after frozen run diff --git a/internal/reports/reports.go b/internal/reports/reports.go index 8a8a31ac5..d032a6701 100644 --- a/internal/reports/reports.go +++ b/internal/reports/reports.go @@ -49,19 +49,6 @@ func UploadReport(ctx context.Context, reportBytes []byte, reportType shared.Typ log.From(ctx).Warnf("Failed to upload report to Speakeasy %s", err.Error()) return writeLocally(digest, reportBytes, reportType) } - _, err = s.Reports.UploadReport(ctx, operations.UploadReportRequestBody{ - Data: shared.Report{ - Type: reportType.ToPointer(), - }, - File: operations.File{ - Content: reportBytes, - FileName: digest + ".html", - }, - }) - if err != nil { - log.From(ctx).Warnf("Failed to upload report to Speakeasy %s", err.Error()) - return writeLocally(digest, reportBytes, reportType) - } cliEvent := events.GetTelemetryEventFromContext(ctx) if cliEvent != nil {