Skip to content

Commit

Permalink
Merge pull request #65 from speakeasy-api/chase/fix-migrate
Browse files Browse the repository at this point in the history
fix: properly add codeSamples registry URLs
  • Loading branch information
chase-crumbaugh authored Nov 7, 2024
2 parents c4e61fc + 48608a5 commit 7c61d42
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 12 deletions.
55 changes: 54 additions & 1 deletion workflow/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/AlekSi/pointer"
"io/fs"
"os"
"path"
"path/filepath"
"slices"
"strings"
Expand Down Expand Up @@ -174,6 +175,9 @@ func (w Workflow) migrate(telemetryDisabled bool) Workflow {
},
Blocking: pointer.ToBool(false),
}
} else {
// Fix the registry location if it needs fixing
target.CodeSamples.Registry.Location = codeSamplesRegistryUpdatedLocation(w, target.CodeSamples)
}

w.Targets[targetID] = target
Expand All @@ -183,6 +187,55 @@ func (w Workflow) migrate(telemetryDisabled bool) Workflow {
return w
}

// For a brief time we were not properly adding -code-samples to the namespace and so we created some duplicated registry locations
// This function checks if the registry location is a duplicate and if so, updates it to include -code-samples
func codeSamplesRegistryUpdatedLocation(wf Workflow, codeSamples *CodeSamples) SourceRegistryLocation {
if codeSamples.Registry == nil {
return ""
}

namespace := getNamespace(codeSamples.Registry.Location)
if namespace == "" {
return ""
}

// Registry namespaces should be unique
var namespaces []string
for _, source := range wf.Sources {
if source.Registry != nil {
namespaces = append(namespaces, getNamespace(source.Registry.Location))
}
}

for _, target := range wf.Targets {
if target.CodeSamples != nil && target.CodeSamples != codeSamples && target.CodeSamples.Registry != nil {
namespaces = append(namespaces, getNamespace(target.CodeSamples.Registry.Location))
}
}

// For a brief time we were not properly adding -code-samples to the namespace and so we created some duplicated registry locations
if slices.Contains(namespaces, namespace) {
namespace += "-code-samples"
}

// Even if the namespace was already unique, we still want to return this because it also trims any tags/revisions,
// which should not be present in an output registry location
return makeRegistryLocation(namespace)
}

func getNamespace(location SourceRegistryLocation) string {
if parsed := ParseSpeakeasyRegistryReference(string(location)); parsed != nil {
return parsed.NamespaceID
}
return ""
}

func codeSamplesRegistryLocation(sourceRegistryURL SourceRegistryLocation) SourceRegistryLocation {
return SourceRegistryLocation(string(sourceRegistryURL) + "-code-samples")
registryDocument := ParseSpeakeasyRegistryReference(string(sourceRegistryURL))
newNamespace := registryDocument.NamespaceID + "-code-samples"
return makeRegistryLocation(newNamespace)
}

func makeRegistryLocation(namespace string) SourceRegistryLocation {
return SourceRegistryLocation(path.Join(baseRegistryURL, namespace))
}
92 changes: 81 additions & 11 deletions workflow/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ func TestWorkflow_Validate(t *testing.T) {
}

func TestMigrate_Success(t *testing.T) {
in := `workflowVersion: 1.0.0
tests := []struct {
in string
expected string
}{{
in: `workflowVersion: 1.0.0
sources:
testSource:
inputs:
Expand All @@ -269,9 +273,72 @@ targets:
typescript:
target: typescript
source: testSource
`

expected := `workflowVersion: 1.0.0
`,
expected: `workflowVersion: 1.0.0
speakeasyVersion: latest
sources:
testSource:
inputs:
- location: ./openapi.yaml
registry:
location: registry.speakeasyapi.dev/org/workspace/testSource
targets:
typescript:
target: typescript
source: testSource
codeSamples:
registry:
location: registry.speakeasyapi.dev/org/workspace/testSource-code-samples
blocking: false
`,
}, {
in: `workflowVersion: 1.0.0
sources:
testSource:
inputs:
- location: ./openapi.yaml
registry:
location: registry.speakeasyapi.dev/org/workspace/testSource:main
targets:
typescript:
target: typescript
source: testSource
`,
expected: `workflowVersion: 1.0.0
speakeasyVersion: latest
sources:
testSource:
inputs:
- location: ./openapi.yaml
registry:
location: registry.speakeasyapi.dev/org/workspace/testSource:main
targets:
typescript:
target: typescript
source: testSource
codeSamples:
registry:
location: registry.speakeasyapi.dev/org/workspace/testSource-code-samples
blocking: false
`,
}, {
in: `workflowVersion: 1.0.0
sources:
testSource:
inputs:
- location: ./openapi.yaml
registry:
location: registry.speakeasyapi.dev/org/workspace/testSource
targets:
typescript:
target: typescript
source: testSource
codeSamples:
registry:
location: registry.speakeasyapi.dev/org/workspace/testSource
blocking: false
`,
expected: `workflowVersion: 1.0.0
speakeasyVersion: latest
sources:
testSource:
Expand All @@ -287,17 +354,20 @@ targets:
registry:
location: registry.speakeasyapi.dev/org/workspace/testSource-code-samples
blocking: false
`
`,
}}

var workflow workflow.Workflow
require.NoError(t, yaml.Unmarshal([]byte(in), &workflow))
for _, tt := range tests {
var workflow workflow.Workflow
require.NoError(t, yaml.Unmarshal([]byte(tt.in), &workflow))

workflow = workflow.Migrate()
workflow = workflow.Migrate()

actual, err := yaml.Marshal(workflow)
require.NoError(t, err)
actual, err := yaml.Marshal(workflow)
require.NoError(t, err)

assert.Equal(t, expected, string(actual))
assert.Equal(t, tt.expected, string(actual))
}
}

func createTempFile(dir string, fileName, contents string) error {
Expand Down

0 comments on commit 7c61d42

Please sign in to comment.